-
Notifications
You must be signed in to change notification settings - Fork 1
/
State.h
59 lines (47 loc) · 1.02 KB
/
State.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
typedef void (*Function0) ();
typedef struct {
Function0 entryFn;
Function0 loopFn;
} State;
State* state;
Function0 stateFn;
void enterState() {
state -> entryFn();
stateFn = state -> loopFn;
}
void changeState(const State* nextState) {
state = nextState;
stateFn = enterState;
}
#define STATE_DEC(stateName) namespace stateName { extern const State* state; }
#define STATE_DEF(stateName, entryBody, loopBody) \
namespace stateName { \
void entry() entryBody \
void loop() loopBody \
const State _state { entry, loop }; \
extern const State* state = &_state; \
}
/* Usage
STATE_DEC(stateName1)
STATE_DEC(stateName2)
STATE_DEF(stateName1,
{
//entry function body, called once before state starts looping
},
{
//loop function body, called repeatedly while this is the current state
//some condition
changeState(stateName2::state);
}
)
STATE_DEF(name2,
{
//entry function body
},
{
//loop function body
//some condition
changeState(stateName1::state);
}
)
*/