-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathfsm.h
155 lines (128 loc) · 4.75 KB
/
fsm.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
Copyright (c) 2007 Gerhard Reitmayr <[email protected]>
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef _FSM_H
#define _FSM_H
/// list of ints as recursive type
template <int HEAD, class TAIL> struct IntList {
enum { head = HEAD };
typedef TAIL tail;
};
/// end of list marker type
struct IntListEnd {};
/// switch statement implementation through iterating over the type list and
/// comparing it to a given state
template <class STATELIST>
struct SwitchTemplate {
template <class CONTEXT>
static typename CONTEXT::ReturnType work( int state, CONTEXT & context){
return ((STATELIST::head == state) ?
context.template operator()<STATELIST::head>() :
SwitchTemplate<typename STATELIST::tail>::work( state, context ));
}
};
/// end of list marker specialization
template <>
struct SwitchTemplate<IntListEnd> {
template <class CONTEXT>
static typename CONTEXT::ReturnType work( int state, CONTEXT & context){
return typename CONTEXT::ReturnType();
}
};
/// The actual state machine implementation
/// sub types are functors passed to the Worker class to execute the
/// right template specialization of the underlying member function of the
/// context object.
template <class CONTEXT, class STATELIST = typename CONTEXT::StateList>
struct StateMachine {
CONTEXT & context;
int state;
template <class RET, class DATA> struct CallEvent {
typedef RET ReturnType;
CONTEXT & context;
DATA & data;
template <int STATE> RET operator()(){
return context.template event<STATE>(data);
}
};
template <class RET, class DATA> struct CallEventConst {
typedef RET ReturnType;
CONTEXT & context;
const DATA & data;
template <int STATE> RET operator()(){
return context.template event<STATE>(data);
}
};
template <class RET> struct CallEventNoData {
typedef RET ReturnType;
CONTEXT & context;
template <int STATE> RET operator()(){
return context.template event<STATE>();
}
};
struct CallEnter {
typedef void ReturnType;
CONTEXT & context;
template <int STATE> ReturnType operator()(){
return context.template enter<STATE>();
}
};
struct CallExit {
typedef void ReturnType;
CONTEXT & context;
template <int STATE> ReturnType operator()(){
return context.template exit<STATE>();
}
};
StateMachine( CONTEXT & c ) : context(c), state(STATELIST::head) {
CallEnter cee = {context};
SwitchTemplate<STATELIST>::work(state, cee);
}
void changeState(const int newstate){
CallExit cl = {context};
SwitchTemplate<STATELIST>::work(state, cl);
state = newstate;
CallEnter cee = {context};
SwitchTemplate<STATELIST>::work(state, cee);
}
void work(){
CallEventNoData<int> ce = {context};
int newstate = SwitchTemplate<STATELIST>::work(state, ce);
if(newstate != state)
changeState(newstate);
}
template <class EVENT>
void work( const EVENT & ev ){
CallEventConst<int, EVENT> ce = {context, ev};
int newstate = SwitchTemplate<STATELIST>::work(state, ce);
if(newstate != state)
changeState(newstate);
}
template <class EVENT>
void work( EVENT & ev ){
CallEvent<int, EVENT> ce = {context, ev};
int newstate = SwitchTemplate<STATELIST>::work(state, ce);
if(newstate != state)
changeState(newstate);
}
};
/// macros for simple state list definition
#define LIST1(a) IntList<a,IntListEnd>
#define LIST2(a,b) IntList<a,LIST1(b) >
#define LIST3(a,b,c) IntList<a,LIST2(b,c) >
#define LIST4(a,b,c,d) IntList<a,LIST3(b,c,d) >
#define LIST5(a,b,c,d,e) IntList<a,LIST4(b,c,d,e) >
#define LIST6(a,b,c,d,e,f) IntList<a,LIST5(b,c,d,e,f) >
#define LIST7(a,b,c,d,e,f,g) IntList<a,LIST6(b,c,d,e,f,g) >
#define LIST8(a,b,c,d,e,f,g,h) IntList<a,LIST7(b,c,d,e,f,g,h) >
#endif // _FSM_H