-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple-nested.cpp
113 lines (92 loc) · 3.51 KB
/
simple-nested.cpp
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
#include "msfsm.hpp"
#include <iostream>
using namespace msfsm;
using namespace std;
class SimpleNested : public msfsm::Fsm<SimpleNested>
{
public:
SimpleNested() { transition(outerState1); }
// Events
struct ToggleOuter {};
struct ToggleInner {};
private:
friend Fsm;
// Base class for outer states
class State : public Fsm::State {
friend Fsm;
using Fsm::State::State;
virtual void event(ToggleOuter) = 0;
virtual void event(ToggleInner) = 0;
};
// This is both a state of SimpleNested FSM and a nested FSM with two inner states
class OuterState1 : public State, public Fsm<OuterState1> {
friend Fsm<SimpleNested>;
friend Fsm<OuterState1>;
public:
using SimpleNested::State::State;
private:
void entry() { cout << "OuterState1" << endl; Fsm<OuterState1>::transition(innerState11); }
void event(ToggleOuter) override { fsm.transition(fsm.outerState2); }
void event(ToggleInner e) override { handle(e); }
void exit() override { destroy(); } // destroy inner FSM
// Base class of inner states
class State : public Fsm<OuterState1>::State {
friend Fsm;
using Fsm::State::State;
virtual void event(ToggleInner e) = 0;
};
class InnerState11 : public State {
friend Fsm;
using State::State;
void entry() { cout << "InnerState11" << endl; };
void event(ToggleInner e) override { transition(fsm.innerState12); }
} innerState11 {this};
class InnerState12 : public State {
friend Fsm;
using State::State;
void entry() { cout << "InnerState12" << endl; }
void event(ToggleInner e) override { transition(fsm.innerState11); }
} innerState12{this};
} outerState1 {this};
class OuterState2 : public State, public Fsm<OuterState2> {
friend Fsm<SimpleNested>;
friend Fsm<OuterState2>;
public:
using SimpleNested::State::State;
private:
void entry() { cout << "OuterState2" << endl; Fsm<OuterState2>::transition(innerState22); }
void event(ToggleOuter) override { fsm.transition(fsm.outerState1); }
void event(ToggleInner e) override { this->handle(e); }
void exit() override { destroy(); } // destroy inner FSM
// Base class of inner states
class State : public Fsm<OuterState2>::State {
friend Fsm;
using Fsm::State::State;
virtual void event(ToggleInner e) = 0;
};
class InnerState21 : public State {
friend Fsm;
using State::State;
void entry() { cout << "InnerState21" << endl; }
void event(ToggleInner e) override { transition(fsm.innerState22); }
} innerState21 {this};
class InnerState22 : public State {
friend Fsm;
using State::State;
void entry() { cout << "InnerState22" << endl; }
void event(ToggleInner e) override { transition(fsm.innerState21); }
} innerState22{this};
} outerState2 {this};
};
int main(int argc, char *argv[])
{
SimpleNested fsm;
fsm.handle(SimpleNested::ToggleInner());
fsm.handle(SimpleNested::ToggleInner());
fsm.handle(SimpleNested::ToggleInner());
fsm.handle(SimpleNested::ToggleOuter());
fsm.handle(SimpleNested::ToggleInner());
fsm.handle(SimpleNested::ToggleInner());
fsm.handle(SimpleNested::ToggleOuter());
return 0;
}