-
Notifications
You must be signed in to change notification settings - Fork 2
/
constexpr_fsm.cpp
355 lines (273 loc) · 10.6 KB
/
constexpr_fsm.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
#include <cstdio>
#include <fea_state_machines/constexpr_fsm.hpp>
#include <gtest/gtest.h>
namespace fea {
using namespace cexpr;
}
namespace {
TEST(constexpr_fsm, example) {
struct test_data {
size_t num_onenterfrom_calls = 0;
size_t num_onenter_calls = 0;
size_t num_onupdate_calls = 0;
size_t num_onexit_calls = 0;
size_t num_onexitto_calls = 0;
};
test_data mtest_data;
// Create your enums. They MUST end with 'count'.
enum class state { walk, run, jump, count };
enum class transition { do_walk, do_run, do_jump, count };
// Used for callbacks
// Create your states.
fea::fsm_builder<transition, state> builder;
// Walk
auto walk_transitions
= builder.make_transition<transition::do_run, state::run>()
.make_transition<transition::do_jump, state::jump>()
.make_transition<transition::do_walk, state::walk>();
fea_event(walk_onenter, [](auto&, test_data& t) { ++t.num_onenter_calls; });
fea_event(
walk_onupdate, [](auto&, test_data& t) { ++t.num_onupdate_calls; });
fea_event(
walk_onexitto, [](auto&, test_data& t) { ++t.num_onexitto_calls; });
auto walk_events
= builder.make_event<fea::fsm_event::on_enter>(walk_onenter)
.make_event<fea::fsm_event::on_update>(walk_onupdate)
.make_event<fea::fsm_event::on_exit_to, state::jump>(
walk_onexitto);
auto walk_state
= builder.make_state<state::walk>(walk_transitions, walk_events);
// Jump
auto jump_transitions
= builder.make_transition<transition::do_run, state::run>()
.make_transition<transition::do_walk, state::walk>();
fea_event(jump_onenter, [](auto&, test_data& t) { ++t.num_onenter_calls; });
fea_event(
jump_onexitto, [](auto&, test_data& t) { ++t.num_onexitto_calls; });
auto jump_events
= builder.make_event<fea::fsm_event::on_enter>(jump_onenter)
.make_event<fea::fsm_event::on_exit_to, state::run>(
jump_onexitto);
auto jump_state
= builder.make_state<state::jump>(jump_transitions, jump_events);
// Run
auto run_transitions
= builder.make_transition<transition::do_jump, state::jump>()
.make_transition<transition::do_walk, state::walk>();
fea_event(run_onenterfrom,
[](auto&, test_data& t) { ++t.num_onenterfrom_calls; });
fea_event(
run_onupdate, [](auto&, test_data& t) { ++t.num_onupdate_calls; });
fea_event(run_onexit, [](auto&, test_data& t) { ++t.num_onexit_calls; });
auto run_events
= builder.make_event<fea::fsm_event::on_enter_from, state::jump>(
run_onenterfrom)
.make_event<fea::fsm_event::on_update>(run_onupdate)
.make_event<fea::fsm_event::on_exit>(run_onexit);
auto run_state
= builder.make_state<state::run>(run_transitions, run_events);
// Create your state machine.
constexpr auto to_init
= builder.make_machine(walk_state, jump_state, run_state);
auto machine = to_init.init(mtest_data);
EXPECT_EQ(mtest_data.num_onenterfrom_calls, 0u);
EXPECT_EQ(mtest_data.num_onenter_calls, 1u);
EXPECT_EQ(mtest_data.num_onupdate_calls, 0u);
EXPECT_EQ(mtest_data.num_onexit_calls, 0u);
EXPECT_EQ(mtest_data.num_onexitto_calls, 0u);
machine.update(mtest_data);
EXPECT_EQ(mtest_data.num_onenterfrom_calls, 0u);
EXPECT_EQ(mtest_data.num_onenter_calls, 1u);
EXPECT_EQ(mtest_data.num_onupdate_calls, 1u);
EXPECT_EQ(mtest_data.num_onexit_calls, 0u);
EXPECT_EQ(mtest_data.num_onexitto_calls, 0u);
// Capture the trigger output.
auto m1 = machine.template trigger<transition::do_jump>(mtest_data);
EXPECT_EQ(mtest_data.num_onenterfrom_calls, 0u);
EXPECT_EQ(mtest_data.num_onenter_calls, 2u);
EXPECT_EQ(mtest_data.num_onupdate_calls, 1u);
EXPECT_EQ(mtest_data.num_onexit_calls, 0u);
EXPECT_EQ(mtest_data.num_onexitto_calls, 1u);
m1.update(mtest_data);
EXPECT_EQ(mtest_data.num_onenterfrom_calls, 0u);
EXPECT_EQ(mtest_data.num_onenter_calls, 2u);
EXPECT_EQ(mtest_data.num_onupdate_calls, 1u);
EXPECT_EQ(mtest_data.num_onexit_calls, 0u);
EXPECT_EQ(mtest_data.num_onexitto_calls, 1u);
auto m2 = m1.template trigger<transition::do_run>(mtest_data);
EXPECT_EQ(mtest_data.num_onenterfrom_calls, 1u);
EXPECT_EQ(mtest_data.num_onenter_calls, 2u);
EXPECT_EQ(mtest_data.num_onupdate_calls, 1u);
EXPECT_EQ(mtest_data.num_onexit_calls, 0u);
EXPECT_EQ(mtest_data.num_onexitto_calls, 2u);
m2.update(mtest_data);
EXPECT_EQ(mtest_data.num_onenterfrom_calls, 1u);
EXPECT_EQ(mtest_data.num_onenter_calls, 2u);
EXPECT_EQ(mtest_data.num_onupdate_calls, 2u);
EXPECT_EQ(mtest_data.num_onexit_calls, 0u);
EXPECT_EQ(mtest_data.num_onexitto_calls, 2u);
}
TEST(constexpr_fsm, compiler_letter) {
#if defined(NDEBUG)
static constexpr bool debug_build = false;
#else
static constexpr bool debug_build = true;
#endif
static constexpr bool assert_val = true;
enum class transition {
do_intro,
do_debug,
do_release,
do_paragraph,
do_outro,
count
};
enum class state { intro, debug, release, paragraph, outro, count };
fea::fsm_builder<transition, state> builder;
// intro
auto intro_transitions
= builder.make_transition<transition::do_debug, state::debug>()
.make_transition<transition::do_release,
state::release>();
fea_event(intro_onenter, [](auto& machine) {
static_assert(assert_val, "Dear");
if constexpr (debug_build) {
return machine.template trigger<transition::do_debug>();
} else {
return machine.template trigger<transition::do_release>();
}
});
fea_event(intro_onupdate, [](auto&) { return 0; });
fea_event(intro_onexitto_debug, [](auto&) {
static_assert(assert_val, "slow Visual Studio Compiler,");
});
fea_event(intro_onexitto_release, [](auto&) {
static_assert(assert_val, "relatively fast Visual Studio Compiler ;)");
});
auto intro_events
= builder.make_event<fea::fsm_event::on_enter>(intro_onenter)
.make_event<fea::fsm_event::on_update>(intro_onupdate)
.make_event<fea::fsm_event::on_exit_to, state::debug>(
intro_onexitto_debug)
.make_event<fea::fsm_event::on_exit_to, state::release>(
intro_onexitto_release);
auto intro_state
= builder.make_state<state::intro>(intro_transitions, intro_events);
// debug
auto debug_transitions = builder.make_transition<transition::do_paragraph,
state::paragraph>();
fea_event(debug_onenter, [](auto& m) {
static_assert(assert_val, "In debug mode,");
return m.template trigger<transition::do_paragraph>();
});
fea_event(debug_onupdate, [](auto&) { return 1; });
auto debug_events
= builder.make_event<fea::fsm_event::on_enter>(debug_onenter)
.make_event<fea::fsm_event::on_update>(debug_onupdate);
auto debug_state
= builder.make_state<state::debug>(debug_transitions, debug_events);
// release
auto release_transitions = builder.make_transition<transition::do_paragraph,
state::paragraph>();
fea_event(release_onenter, [](auto& m) {
static_assert(assert_val, "In release mode,");
return m.template trigger<transition::do_paragraph>();
});
fea_event(release_onupdate, [](auto&) { return 2; });
auto release_events
= builder.make_event<fea::fsm_event::on_enter>(release_onenter)
.make_event<fea::fsm_event::on_update>(release_onupdate);
auto release_state = builder.make_state<state::release>(
release_transitions, release_events);
// paragraph
auto par_transitions
= builder.make_transition<transition::do_outro, state::outro>();
fea_event(par_onenter, [](auto& m) {
static_assert(assert_val,
"We've been very critical of you in the "
"past.");
return m.template trigger<transition::do_outro>();
});
fea_event(par_onupdate, [](auto&) { return 3; });
fea_event(par_onexit,
[](auto&) { static_assert(assert_val, "And we still are."); });
auto par_events
= builder.make_event<fea::fsm_event::on_enter>(par_onenter)
.make_event<fea::fsm_event::on_update>(par_onupdate)
.make_event<fea::fsm_event::on_exit>(par_onexit);
auto par_state
= builder.make_state<state::paragraph>(par_transitions, par_events);
// outro
fea_event(outro_onenter, [](auto&) {
static_assert(assert_val, "But look how you've grown!");
});
fea_event(outro_onupdate, [](auto&) { return 42; });
auto outro_events
= builder.make_event<fea::fsm_event::on_enter>(outro_onenter)
.make_event<fea::fsm_event::on_update>(outro_onupdate);
auto outro_state
= builder.make_state<state::outro>(builder.empty_t(), outro_events);
// Tada!
auto machine = builder.make_machine(intro_state, debug_state, release_state,
par_state, outro_state)
.init();
// And we can get constexpr values from update, generated conditionally at
// compile time.
constexpr auto result = machine.update();
static_assert(result == 42, "Wrong answer to life.");
}
template <class Machine>
struct my_test1 {
static constexpr auto my_val = Machine::update();
};
template <class Machine>
struct my_test2 {
static constexpr auto my_val = Machine::update();
};
TEST(constexpr_fsm, generate_tuple) {
enum class transition { flip_flop, count };
enum class state { gen_string, gen_int, count };
fea::fsm_builder<transition, state> builder;
auto string_transitions
= builder.make_transition<transition::flip_flop, state::gen_int>();
fea_event(string_enter, [](auto& m, auto val) {
using tup_t = std::decay_t<decltype(val())>;
if constexpr (std::tuple_size_v<tup_t> >= 10) {
return val();
} else {
return m.template trigger<transition::flip_flop>([=]() {
return std::tuple_cat(val(), std::make_tuple("a string"));
});
}
});
auto string_events
= builder.make_event<fea::fsm_event::on_enter>(string_enter);
auto string_state = builder.make_state<state::gen_string>(
string_transitions, string_events);
auto int_transitions = builder.make_transition<transition::flip_flop,
state::gen_string>();
fea_event(int_enter, [](auto& m, auto val) {
return m.template trigger<transition::flip_flop>(
[=]() { return std::tuple_cat(val(), std::make_tuple(42)); });
});
auto int_events = builder.make_event<fea::fsm_event::on_enter>(int_enter);
auto int_state
= builder.make_state<state::gen_int>(int_transitions, int_events);
constexpr auto tup
= builder.make_machine(string_state, int_state).init([]() {
return std::make_tuple("prime");
});
// Print the tuple
// std::apply([](auto&... vals) { ((std::cout << vals << std::endl), ...);
// }, tup);
constexpr auto test_tup = std::make_tuple("prime", "a string", 42,
"a string", 42, "a string", 42, "a string", 42, "a string", 42);
using machine_tup_t = std::decay_t<decltype(tup)>;
using expected_tup_t = std::decay_t<decltype(test_tup)>;
static_assert(std::tuple_size_v<
machine_tup_t> == std::tuple_size_v<expected_tup_t>,
"unit test failed : tuples are different sizes");
static_assert(std::is_same_v<machine_tup_t, expected_tup_t>,
"unit test failed : tuples are different types");
}
} // namespace