-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathconstexpr_fsm.hpp
More file actions
687 lines (562 loc) · 21.5 KB
/
constexpr_fsm.hpp
File metadata and controls
687 lines (562 loc) · 21.5 KB
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
/*
BSD 3-Clause License
Copyright (c) 2020, Philippe Groarke
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <algorithm>
#include <array>
#include <cassert>
#include <cstdint>
#include <functional>
#include <limits>
#include <tuple>
#if !defined(FEA_FSM_NOTHROW)
#include <stdexcept>
#endif
/*
A compile-time executable very simple fsm.
https://philippegroarke.com/posts/2020/constexpr_fsm/
Features :
- OnEnter, OnUpdate, OnExit.
- OnEnterFrom, OnExitTo.
Overrides event behavior when coming from/going to specified states.
- Supports user arguments in the callbacks, as the fsm stores your lambda
directly.
- static_asserts wherever possible.
- Does NOT provide a "get_current_state" function.
Checking the current state of an fsm is a major smell and usually points
to either a misuse, misunderstanding or incomplete implementation of the
fsm. Do not do that, rethink your states and transitions instead.
Callbacks :
- The first argument of your callback is always a ref to the fsm itself.
This is useful for retriggering and when you store fsms in containers.
You can use auto& to simplify your callback signature.
[](auto& mymachine){}
- Pass your own types at the end of the fsm and fsm_state template.
These will be passed on to your callbacks when you call update or
trigger.
For example : fsm<mytransitions, mystates, int, bool&, const void*>;
Callback signature is:
[](auto& machine, int, bool&, const void*){}
Notes :
- Uses tuple of Func types.
- You *must* return the results of trigger.
- You *must* capture the trigger results, this is your new state.
TODO :
- If this turns out to be useful, adding the following seems reasonable.
- Transition guards.
- Yield transitions (aka history state).
*/
#define FEA_TOKENPASTE(x, y) x##y
#define FEA_TOKENPASTE2(x, y) FEA_TOKENPASTE(x, y)
#define fea_event(name, f) \
struct FEA_TOKENPASTE2( \
FEA_TOKENPASTE2(fea_event_builder_, name), __LINE__) { \
using is_event_builder [[maybe_unused]] = int; \
static constexpr auto unpack() { \
return f; \
} \
} name
namespace fea {
namespace cexpr {
namespace detail {
template <class T>
struct event_ {
using type = void;
};
template <class, class = void>
struct is_event_builder : public std::false_type {};
template <class T>
struct is_event_builder<T, typename event_<typename T::is_event_builder>::type>
: public std::true_type {};
template <class T, class Tuple>
struct tuple_idx {
static_assert(!std::is_same_v<Tuple, std::tuple<>>,
"could not find T in given Tuple");
// static constexpr size_t value = std::numeric_limits<size_t>::max();
};
template <class T, class... Types>
struct tuple_idx<T, std::tuple<T, Types...>> {
static constexpr size_t value = 0;
};
template <class T, class U, class... Types>
struct tuple_idx<T, std::tuple<U, Types...>> {
static constexpr size_t value
= 1 + tuple_idx<T, std::tuple<Types...>>::value;
};
template <class T, class Tuple>
inline constexpr size_t tuple_idx_v = tuple_idx<T, Tuple>::value;
template <class T, class Tuple>
struct tuple_contains;
template <class T>
struct tuple_contains<T, std::tuple<>> : std::false_type {};
template <class T, class U, class... Ts>
struct tuple_contains<T, std::tuple<U, Ts...>>
: tuple_contains<T, std::tuple<Ts...>> {};
template <class T, class... Ts>
struct tuple_contains<T, std::tuple<T, Ts...>> : std::true_type {};
template <class T, class Tuple>
inline constexpr bool tuple_contains_v = tuple_contains<T, Tuple>::value;
template <class Func, size_t... I>
constexpr auto tuple_expander5000_impl(Func func, std::index_sequence<I...>) {
return func(std::integral_constant<size_t, I>{}...);
}
template <size_t N, class Func>
constexpr auto tuple_expander5000(Func func) {
return tuple_expander5000_impl(func, std::make_index_sequence<N>());
}
template <class Func, class Tuple, size_t N = 0>
inline auto runtime_get(Func func, Tuple& tup, size_t idx) {
if (N == idx) {
return func(std::get<N>(tup));
}
if constexpr (N + 1 < std::tuple_size_v<Tuple>) {
return runtime_get<Func, Tuple, N + 1>(func, tup, idx);
}
}
template <class Func, size_t... I>
constexpr auto static_for(Func func, std::index_sequence<I...>) {
return (func(std::integral_constant<size_t, I>{}), ...);
}
template <size_t N, class Func>
constexpr void static_for(Func func) {
return static_for(func, std::make_index_sequence<N>());
}
// Pass in your tuple_map_key type.
// Build with multiple tuple<tuple_map_key<Key>, T>...
template <class KeysBuilder, class ValuesBuilder>
struct tuple_map {
// Search by non-type template.
template <class Key>
static constexpr const auto& find() {
constexpr size_t idx = tuple_idx_v<Key, keys_tup_t>;
return std::get<idx>(_values);
}
// template <class Key>
// static constexpr auto& find() {
// constexpr size_t idx = tuple_idx_v<Key, KeysTuple>;
// return std::get<idx>(_values);
//}
template <class Key>
static constexpr bool contains() {
// Just to make sure we are in constexpr land.
constexpr bool ret = tuple_contains_v<Key, keys_tup_t>;
return ret;
}
private:
// tuple<tuple_map_key<my, type, keys>>
// Used to find the index of the value inside the other tuple.
static constexpr auto _keys = KeysBuilder::unpack();
// tuple<T>
// Your values.
static constexpr auto _values = ValuesBuilder::unpack();
using keys_tup_t = std::decay_t<decltype(_keys)>;
using values_tup_t = std::decay_t<decltype(_values)>;
};
template <class Builder>
constexpr auto make_tuple_map() {
// At compile time, take the tuple of tuple coming from the
// builder, iterate through it, grab the first elements of
// nested tuples (the key) and put that in a new tuple, grab
// the second elements and put that in another tuple.
// Basically, go from tuple<tuple<key, value>...> to
// tuple<tuple<key...>, tuple<values...>>
// which is our map basically.
struct keys_tup {
static constexpr auto unpack() {
constexpr size_t tup_size
= std::tuple_size_v<decltype(Builder::unpack())>;
return detail::tuple_expander5000<tup_size>([](
auto... Idxes) constexpr {
constexpr auto tup_of_tups = Builder::unpack();
// Gets all the keys.
return std::make_tuple(std::get<0>(
std::get<decltype(Idxes)::value>(tup_of_tups))...);
});
}
};
struct vals_tup {
static constexpr auto unpack() {
constexpr size_t tup_size
= std::tuple_size_v<decltype(Builder::unpack())>;
return detail::tuple_expander5000<tup_size>([](
auto... Idxes) constexpr {
constexpr auto tup_of_tups = Builder::unpack();
// Gets all the values.
return std::make_tuple(std::get<1>(
std::get<decltype(Idxes)::value>(tup_of_tups))...);
});
}
};
return tuple_map<keys_tup, vals_tup>{};
}
} // namespace detail
enum class fsm_event : uint8_t {
on_enter_from,
on_enter,
on_update,
on_exit,
on_exit_to,
count,
};
// Fsm keys are used to lookup your transitions and your states.
// Transition Key will query tuple map with Transition to get the target
// state.
template <class TransitionEnum, TransitionEnum FromTransition>
struct fsm_transition_key {};
// Event key will query the tuple map with Event and possibly a To/From
// state to execute your event.
template <fsm_event Event, class StateEnum, StateEnum FromToState>
struct fsm_event_key {};
// State key is used to find states in the state machine itself.
template <class StateEnum, StateEnum State>
struct fsm_state_key {};
template <class TransitionEnum, class StateEnum, class TransitionMap,
class EventMap, StateEnum State>
struct fsm_state {
static constexpr StateEnum state_e = State;
template <TransitionEnum Transition>
static constexpr StateEnum transition_target() {
return _transitions.template find<
fsm_transition_key<TransitionEnum, Transition>>();
}
template <fsm_event Event, StateEnum FromToState = StateEnum::count,
class Machine, class... FuncArgs>
static constexpr auto execute_event([[maybe_unused]] Machine& machine,
[[maybe_unused]] FuncArgs&&... func_args) {
static_assert(Event != fsm_event::on_enter_from,
"state : do not execute on_enter_from, use on_enter instead "
"and provide to_from_state");
static_assert(Event != fsm_event::on_exit_to,
"state : do not execute on_exit_to, use on_exit instead and "
"provide to_from_state");
static_assert(Event != fsm_event::count, "fsm_state : invalid event");
// Check the event, call the appropriate user functions if it is
// stored.
// on_enter and on_enter_from
if constexpr (Event == fsm_event::on_enter) {
// Build our lookup key for the on_enter_from event.
using enter_from_key_t = fsm_event_key<fsm_event::on_enter_from,
StateEnum, FromToState>;
// Build our lookup key for the on_enter event.
using enter_key_t = fsm_event_key<fsm_event::on_enter, StateEnum,
StateEnum::count>;
// Encourage the compiler, he needs some positive reinforcement
// after all this.
constexpr bool has_enter_from
= _events.template contains<enter_from_key_t>();
[[maybe_unused]] constexpr bool has_enter
= _events.template contains<enter_key_t>();
if constexpr (FromToState != StateEnum::count && has_enter_from) {
// Invoke with machine as last argument to support calling
// member functions on object.
constexpr auto& f = _events.template find<enter_from_key_t>();
// std::invoke is not constexpr.
return f(machine, std::forward<FuncArgs>(func_args)...);
} else if constexpr (has_enter) {
constexpr auto& f = _events.template find<enter_key_t>();
return f(machine, std::forward<FuncArgs>(func_args)...);
}
// on_update
} else if constexpr (Event == fsm_event::on_update) {
// Lookup for on_update
using update_key_t = fsm_event_key<fsm_event::on_update, StateEnum,
StateEnum::count>;
constexpr bool has_update
= _events.template contains<update_key_t>();
if constexpr (has_update) {
constexpr auto& f = _events.template find<update_key_t>();
return f(machine, std::forward<FuncArgs>(func_args)...);
}
// on_exit and on_exit_to
} else if constexpr (Event == fsm_event::on_exit) {
// Lookup for on_exit_to
using exit_to_key_t = fsm_event_key<fsm_event::on_exit_to,
StateEnum, FromToState>;
// Lookup for on_exit
using exit_key_t = fsm_event_key<fsm_event::on_exit, StateEnum,
StateEnum::count>;
constexpr bool has_exit_to
= _events.template contains<exit_to_key_t>();
[[maybe_unused]] constexpr bool has_exit
= _events.template contains<exit_key_t>();
if constexpr (FromToState != StateEnum::count && has_exit_to) {
constexpr auto& f = _events.template find<exit_to_key_t>();
return f(machine, std::forward<FuncArgs>(func_args)...);
} else if constexpr (has_exit) {
constexpr auto& f = _events.template find<exit_key_t>();
return f(machine, std::forward<FuncArgs>(func_args)...);
}
}
}
private:
static constexpr auto _transitions = TransitionMap::unpack();
static constexpr auto _events = EventMap::unpack();
};
template <class TransitionEnum, class StateEnum, StateEnum CurrentState,
StateEnum StartState, bool InOnExit, class StateMap>
struct fsm {
using fsm_t = fsm<TransitionEnum, StateEnum, CurrentState, StartState,
InOnExit, StateMap>;
using fsm_inexit_t = fsm<TransitionEnum, StateEnum, CurrentState,
StartState, true, StateMap>;
using fsm_notinexit_t = fsm<TransitionEnum, StateEnum, CurrentState,
StartState, false, StateMap>;
template <class... FuncArgs>
[[nodiscard]] static constexpr auto init(FuncArgs&&... func_args) {
static_assert(CurrentState == StateEnum::count,
"CurrentState is valid, did you already call init?");
// needs init
constexpr auto& s
= _states.template find<fsm_state_key<StateEnum, StartState>>();
using new_fsm_t = fsm<TransitionEnum, StateEnum, StartState, StartState,
InOnExit, StateMap>;
constexpr auto passed_in = new_fsm_t{};
using func_ret_t
= decltype(s.template execute_event<fsm_event::on_enter>(
passed_in, std::forward<FuncArgs>(func_args)...));
// When triggering inside on_enter, the user must return the new fsm.
// If the event returns void, no trigger happened.
if constexpr (!std::is_same_v<func_ret_t, void>) {
return s.template execute_event<fsm_event::on_enter>(
passed_in, std::forward<FuncArgs>(func_args)...);
} else {
s.template execute_event<fsm_event::on_enter>(
passed_in, std::forward<FuncArgs>(func_args)...);
return passed_in;
}
}
template <TransitionEnum Transition, class... FuncArgs>
[[nodiscard]] static constexpr auto trigger(FuncArgs&&... func_args) {
static_assert(CurrentState != StateEnum::count,
"CurrentState is invalid, did you forget to call init?");
static_assert(
InOnExit == false, "cannot trigger transition in on_exit");
// do normal trigger...
// if anything can be considered normal at this point...
// Here is when I started questioning my life decisions...
constexpr StateEnum from_state = CurrentState;
constexpr StateEnum to_state
= _states.template find<fsm_state_key<StateEnum, from_state>>()
.template transition_target<Transition>();
// Call on_exit
constexpr auto& from_s
= _states.template find<fsm_state_key<StateEnum, from_state>>();
constexpr auto passed_in_exit = fsm_inexit_t{};
from_s.template execute_event<fsm_event::on_exit, to_state>(
passed_in_exit, std::forward<FuncArgs>(func_args)...);
// Call on_enter and update current state.
using new_fsm_t = fsm<TransitionEnum, StateEnum, to_state, StartState,
false, StateMap>;
constexpr auto passed_in_enter = new_fsm_t{};
constexpr auto& to_s
= _states.template find<fsm_state_key<StateEnum, to_state>>();
using func_ret_t = decltype(
to_s.template execute_event<fsm_event::on_enter, from_state>(
passed_in_enter, std::forward<FuncArgs>(func_args)...));
// When triggering inside on_enter, the user must return the new fsm.
// If the event returns void, no trigger happened, state stays the same.
if constexpr (!std::is_same_v<func_ret_t, void>) {
// static_assert(detail::is_same_templated<func_ret_t, new_fsm_t>(),
// "You cannot return custom values in on_enter, on_exit. "
// "Only from update.");
// Return new state.
return to_s.template execute_event<fsm_event::on_enter, from_state>(
passed_in_enter, std::forward<FuncArgs>(func_args)...);
} else {
to_s.template execute_event<fsm_event::on_enter, from_state>(
passed_in_enter, std::forward<FuncArgs>(func_args)...);
// Return 'current' state.
return passed_in_enter;
}
}
template <class... FuncArgs>
static constexpr auto update(FuncArgs&&... func_args) {
static_assert(CurrentState != StateEnum::count,
"CurrentState is invalid, did you forget to call init?");
constexpr auto passed_in = fsm_t{};
constexpr auto& s = _states.template find<
fsm_state_key<StateEnum, CurrentState>>();
return s.template execute_event<fsm_event::on_update>(
passed_in, std::forward<FuncArgs>(func_args)...);
}
private:
static constexpr auto _states = StateMap::unpack();
};
template <class TransitionEnum, class StateEnum, TransitionEnum FromT,
StateEnum ToS, class Parent>
struct fsm_transition_builder {
template <TransitionEnum NewFromT, StateEnum NewToS>
static constexpr auto make_transition() {
using parent_t = fsm_transition_builder<TransitionEnum, StateEnum,
FromT, ToS, Parent>;
return fsm_transition_builder<TransitionEnum, StateEnum, NewFromT,
NewToS, parent_t>{};
}
static constexpr auto unpack() {
if constexpr (!std::is_same_v<Parent, void>) {
return std::tuple_cat(
std::make_tuple(std::tuple{
fsm_transition_key<TransitionEnum, FromT>{}, ToS }),
Parent::unpack());
} else {
return std::make_tuple(std::tuple{
fsm_transition_key<TransitionEnum, FromT>{}, ToS });
}
}
};
template <fsm_event Event, class StateEnum, StateEnum FromToState, class Parent,
class Func>
struct fsm_event_builder {
template <fsm_event NewEvent, StateEnum NewFromToState = StateEnum::count,
class NewFunc>
static constexpr auto make_event([[maybe_unused]] NewFunc newfunc) {
#if defined(_MSC_VER) && defined(FEA_FSM_NO_EVENT_WRAPPER)
// Only works on MSVC.
// There is apparently a proposal to remove the restrictions on static
// constexpr variables in static constexpr functions.
// Store the func here temporarily until we unpack this stuff.
// Since the function is templated on NewFunc type, we shouldn't get
// collisions with other make_event calls.
// aka famous last words
static constexpr auto f = newfunc;
struct func_wrapper {
static constexpr auto unpack() {
return f;
}
};
using parent_t = fsm_event_builder<Event, StateEnum, FromToState,
Parent, Func>;
return fsm_event_builder<NewEvent, StateEnum, NewFromToState, parent_t,
func_wrapper>{};
#else
// For now, use macro :(
static_assert(detail::is_event_builder<Func>::value,
"use 'fea_event' macro to create events. ex : "
"fea_event(event_name, [](auto&){});");
using parent_t = fsm_event_builder<Event, StateEnum, FromToState,
Parent, Func>;
return fsm_event_builder<NewEvent, StateEnum, NewFromToState, parent_t,
NewFunc>{};
#endif
}
static constexpr auto unpack() {
if constexpr (!std::is_same_v<Parent, void>) {
return std::tuple_cat(
std::make_tuple(std::tuple{
fsm_event_key<Event, StateEnum, FromToState>{},
Func::unpack() }),
Parent::unpack());
} else {
return std::make_tuple(
std::tuple{ fsm_event_key<Event, StateEnum, FromToState>{},
Func::unpack() });
}
}
}; // namespace cexpr
template <class TransitionEnum, class StateEnum>
struct fsm_builder {
static constexpr auto empty_t() {
return fsm_transition_builder<TransitionEnum, StateEnum,
TransitionEnum::count, StateEnum::count, void>{};
}
static constexpr auto empty_e() {
struct func_wrapper {
static constexpr auto unpack() {
return [](auto&) {};
}
};
return fsm_event_builder<fsm_event::count, StateEnum, StateEnum::count,
void, func_wrapper>{};
}
template <TransitionEnum FromTransition, StateEnum ToState>
static constexpr auto make_transition() {
static_assert(
FromTransition != TransitionEnum::count, "invalid transition");
static_assert(ToState != StateEnum::count, "invalid state");
return fsm_transition_builder<TransitionEnum, StateEnum, FromTransition,
ToState, void>{};
}
template <fsm_event Event, StateEnum FromToState = StateEnum::count,
class Func>
static constexpr auto make_event([[maybe_unused]] Func func) {
static_assert(Event != fsm_event::count, "invalid event");
#if defined(_MSC_VER) && defined(FEA_FSM_NO_EVENT_WRAPPER)
// This only works on MSVC.
// There is a proposal to remove the restrictions on static
// cosntexpr variables in static constexpr functions.
static constexpr auto f = func;
struct func_wrapper {
static constexpr auto unpack() {
return f;
}
};
return fsm_event_builder<Event, StateEnum, FromToState, void,
func_wrapper>{};
#else
// For now, use macro :(
static_assert(detail::is_event_builder<Func>::value,
"use 'fea_event' macro to create events. ex : "
"fea_event(event_name, [](auto&){});");
return fsm_event_builder<Event, StateEnum, FromToState, void, Func>{};
#endif
}
template <StateEnum State, class TransitionBuilder, class EventBuilder>
static constexpr auto make_state(TransitionBuilder, EventBuilder) {
struct t_map {
static constexpr auto unpack() {
return detail::make_tuple_map<TransitionBuilder>();
}
};
struct e_map {
static constexpr auto unpack() {
return detail::make_tuple_map<EventBuilder>();
}
};
return fsm_state<TransitionEnum, StateEnum, t_map, e_map, State>{};
}
template <class State1, class... States>
static constexpr auto make_machine(State1, States...) {
struct s_map {
struct StateBuilder {
static constexpr auto unpack() {
return std::make_tuple(
std::make_tuple(
fsm_state_key<StateEnum, State1::state_e>{},
State1{}),
std::make_tuple(
fsm_state_key<StateEnum, States::state_e>{},
States{})...);
}
};
static constexpr auto unpack() {
return detail::make_tuple_map<StateBuilder>();
}
};
return fsm<TransitionEnum, StateEnum, StateEnum::count, State1::state_e,
false, s_map>{};
}
};
} // namespace cexpr
} // namespace fea