aboutsummaryrefslogtreecommitdiff
path: root/shared/FSM.hpp
blob: c86ed80f2539c23449388fd503e6b54f352e7506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#ifndef _FSM_HPP_
#define _FSM_HPP_

#include "FSM.h"

#include <iostream>

/// <summary>
/// This is a generic update method, unknown classes are not supported.
/// </summary>
/// <typeparam name="TState">IBehaviouralState is the only accepted
/// class.</typeparam>
template <class TState> void FSM<TState>::act() {
  // No implementation for unknown class.
  std::cout << "No implementation for unknown class" << std::endl;
}
//
///// <summary>
///// Calls the IBehaviouralState act method.
///// </summary>
///// <typeparam name="TState">IBehaviouralState is the only accepted
/// class.</typeparam>
template <> inline void FSM<IBehaviouralState>::act() { _currentState->act(); }

/// <summary>
/// This is a generic return of the type.
/// </summary>
/// <typeparam name="TState">Any class type works with this return.</typeparam>
/// <returns>The current IBehaviouralState map.</returns>
template <class TState>
std::map<int, std::shared_ptr<TState>> FSM<TState>::get_states() {
  return _states;
}

///// <summary>
///// State transitioning from current state to newState.
///// Calls Exit on the current state and Enter on the new state.
///// </summary>
///// <typeparam name="TState">IBehaviouralState is the only accepted
/// class.</typeparam>
///// <param name="newState">New state to transition into.</param>
template <>
inline void
FSM<IBehaviouralState>::set_state(std::shared_ptr<IBehaviouralState> newState) {
  // We can guarantee all statemachines are based on IBehaviouralState
  if (_currentState != nullptr)
    _currentState->exit();

  _currentState = nullptr;
  _currentState = newState;
  _currentState->enter();
}

/// <summary>
/// State transitioning from current state to newState.
/// Calls Exit on the current state and Enter on the new state.
/// </summary>
/// <typeparam name="TState">IBehaviouralState is the only accepted
/// class.</typeparam> <param name="newState">New state to transition
/// into.</param>
template <class TState>
void FSM<TState>::set_state(std::shared_ptr<TState> newState) {
  // We can guarantee all statemachines are based on IBehaviouralState
  // No implementation for an unknown state change class.
  std::cout << "No implementation for unknown state change class" << std::endl;
}

#endif // _FSM_HPP_