This repository has been archived by the owner on Sep 26, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmain.cpp
191 lines (152 loc) · 4.07 KB
/
main.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// HFSM (hierarchical state machine for games and interactive applications)
// Created by Andrew Gresyk
//
// Attachable logger example
// Full output:
//
// --- ctor: ---
//
// Top::enter()
// Top::From::enter()
//
// -- update: --
//
// Top::update()
// Top::transition()
// Top::From::update()
// Top::From::transition()
//
// -- react: ---
//
// Top::react()
// Top::From::react()
// Top::To::substitute()
// Top::From::leave()
// Top::To::enter()
//
// -- detach: --
//
//
// --- dtor: ---
//
// Top::To::leave()
// Top::leave()
//
// --- done! ---
// enable logger functionality
#define HFSM_ENABLE_LOG_INTERFACE
#include <hfsm/machine_single.hpp>
#include <iostream>
//------------------------------------------------------------------------------
// data shared between FSM states and outside code
struct Context {};
// convenience typedef
using M = hfsm::Machine<Context>;
////////////////////////////////////////////////////////////////////////////////
struct Logger
: hfsm::LoggerInterface
{
// hfsm::LoggerInterface
void record(const std::type_index& /*state*/,
const char* const stateName,
const Method /*method*/,
const char* const methodName) override
{
std::cout << stateName << "::" << methodName << "()\n";
}
};
////////////////////////////////////////////////////////////////////////////////
// top-level state in the hierarchy
struct Top
: M::Base
{
// all state methods:
void substitute(Control&, Context&) {} // not going to be called in this example
void enter(Context&) {}
void update(Context&) {}
void transition(Control&, Context&) {}
template <typename TEvent>
void react(const TEvent&, Control&, Context&) {}
void leave(Context&) {}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// forward declared for Red::transition()
struct To;
// initial state
struct From
: M::Base // necessary boilerplate!
{
// all state methods:
void substitute(Control&, Context&) {} // not going to be called in this example
void enter(Context&) {}
void update(Context&) {}
void transition(Control&, Context&) {}
template <typename TEvent>
void react(const TEvent&, Control& control, Context&) { control.changeTo<To>(); }
void leave(Context&) {}
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// transition target state
struct To
: M::Base
{
// all state methods:
void substitute(Control&, Context&) {}
void enter(Context&) {}
void update(Context&) {}
void transition(Control&, Context&) {}
template <typename TEvent>
void react(const TEvent&, Control&, Context&) {} // not going to be called in this example
void leave(Context&) {}
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
};
////////////////////////////////////////////////////////////////////////////////
int main() {
using FSM = M::Root<Top,
Top::From,
Top::To
>;
{
// shared data storage instance
Context context;
// logger
Logger logger;
std::cout << "--- ctor: ---\n\n";
// state machine instance - all initial states are activated
FSM machine(context, &logger);
// output:
// Top::enter()
// Top::From::enter()
std::cout << "\n-- update: --\n\n";
// first update
machine.update();
// output:
// Top::update()
// Top::transition()
// Top::From::update()
// Top::From::transition()
std::cout << "\n-- react: ---\n\n";
machine.react(1);
// output:
// Top::react()
// Top::From::react()
// Top::To::substitute()
// Top::From::leave()
// Top::To::enter()
std::cout << "\n-- detach: --\n\n";
// detach logger and update again
machine.attachLogger(nullptr);
machine.update();
// no output, since logger is detached
std::cout << "\n--- dtor: ---\n\n";
// re-attach logger for destruction log
machine.attachLogger(&logger);
// state machine instance gets destroyed
}
// output:
// Top::To::leave()
// Top::leave()
std::cout << "\n--- done! ---\n\n";
return 0;
}
////////////////////////////////////////////////////////////////////////////////