-
Notifications
You must be signed in to change notification settings - Fork 0
/
FiniteStateMachine.h
130 lines (103 loc) · 3.1 KB
/
FiniteStateMachine.h
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#ifndef FINITESTATEMACHINE_H
#define FINITESTATEMACHINE_H
#include "State.h"
#include <map>
#include <memory>
#include <typeinfo>
#include <stdexcept>
template <typename StateBaseType>
class FiniteStateMachine
{
public:
class StateBase;
explicit FiniteStateMachine() = default;
FiniteStateMachine(FiniteStateMachine const&) = delete;
FiniteStateMachine(FiniteStateMachine&&) = delete;
FiniteStateMachine& operator=(FiniteStateMachine const&) = delete;
FiniteStateMachine& operator=(FiniteStateMachine&&) = delete;
virtual ~FiniteStateMachine() = default;
StateBaseType& currentState();
void changeState(State state);
protected:
template <typename StateType>
void registerState(State state);
template <typename StateType, typename... Data>
void registerState(State state, Data&&... data);
using StateMap = std::map<State, std::unique_ptr<StateBaseType>>;
State currentState_;
StateMap states_;
private:
bool started{false};
void throwIfStateNotRegistered(State state) const;
};
template <typename StateBaseType>
class FiniteStateMachine<StateBaseType>::StateBase
{
public:
explicit StateBase(FiniteStateMachine& fsm);
StateBase(StateBase const&) = delete;
StateBase(StateBase&&) = delete;
StateBase& operator=(StateBase const&) = delete;
StateBase& operator=(StateBase&&) = delete;
virtual ~StateBase() = default;
virtual void onEntry() = 0;
virtual void onExit() = 0;
protected:
FiniteStateMachine& fsm_;
};
template <typename StateBaseType>
FiniteStateMachine<StateBaseType>::StateBase::StateBase(FiniteStateMachine& fsm)
: fsm_{fsm}
{
}
template <typename StateBaseType>
StateBaseType&
FiniteStateMachine<StateBaseType>::currentState()
{
auto currentStatePair = states_.find(currentState_);
return *(currentStatePair->second);
}
template <typename StateBaseType>
void
FiniteStateMachine<StateBaseType>::changeState(State state)
{
throwIfStateNotRegistered(state);
if (currentState_ == state && !started)
{
currentState().onEntry();
started = true;
return;
}
currentState().onExit();
auto newType = states_.find(state);
if (newType != states_.end())
{
currentState_ = newType->first;
newType->second->onEntry();
}
}
template <typename StateBaseType>
template <typename StateType>
void
FiniteStateMachine<StateBaseType>::registerState(State state)
{
states_[state] = std::make_unique<StateType>(*this);
}
template <typename StateBaseType>
template <typename StateType, typename... Data>
void
FiniteStateMachine<StateBaseType>::registerState(State state, Data&&... data)
{
states_[state] = std::make_unique<StateType>(*this, std::forward<Data>(data)...);
}
template <typename StateBaseType>
void
FiniteStateMachine<StateBaseType>::throwIfStateNotRegistered(State state) const
{
auto potentialStatePair = states_.find(state);
if (potentialStatePair == states_.end())
{
throw std::out_of_range{ "State not registered." };
}
}
#endif