-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlsm.h
464 lines (372 loc) · 13.2 KB
/
lsm.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
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
// Copyright 2019 Ken Avolic <[email protected]>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <stdexcept>
#include <type_traits>
#include <variant>
#include <functional>
///
/// Light state machine (lsm) is a simple template
/// to build a state machine engine customized for your
/// needs. Base implementation is taken from mvm that follows
/// the same structure as boost msm.
///
//--------------------------------------------------------
// Internal utilities
//--------------------------------------------------------
namespace lsm::utilities {
// fallback type
struct nonsuch {
nonsuch() = delete;
~nonsuch() = delete;
nonsuch(const nonsuch &) = delete;
void operator=(const nonsuch &) = delete;
};
} // namespace lsm::utilities
//--------------------------------------------------------
// List utilies
//--------------------------------------------------------
namespace lsm::list {
// generic type list algorithms
template <typename List>
struct is_empty;
template <template <typename...> typename List, typename... Items>
struct is_empty<List<Items...>> : std::false_type {};
template <template <typename...> typename List>
struct is_empty<List<>> : std::true_type {};
template <typename List>
using is_empty_t = typename is_empty<List>::type;
template <typename List>
constexpr bool is_empty_v = is_empty_t<List>::value;
template <typename List>
struct size;
template <template <typename...> typename List, typename... Items>
struct size<List<Items...>> {
static constexpr std::size_t value = sizeof...(Items);
};
template <template <typename...> typename List>
struct size<List<>> {
static constexpr std::size_t value = 0;
};
template <typename List>
constexpr auto size_v = size<List>::value;
template <typename List>
struct front;
template <template <typename...> typename List, typename Head, typename... Tail>
struct front<List<Head, Tail...>> {
using type = Head;
};
template <typename List>
using front_t = typename front<List>::type;
template <typename List>
struct robust_front {
using type = front_t<List>;
};
template <template <typename...> typename List>
struct robust_front<List<>> {
using type = utilities::nonsuch;
};
template <typename List>
using robust_front_t = typename robust_front<List>::type;
template <typename List>
struct pop_front;
template <template <typename...> typename List, typename Head, typename... Tail>
struct pop_front<List<Head, Tail...>> {
using type = List<Tail...>;
};
template <template <typename...> typename List>
struct pop_front<List<>> {
using type = List<>;
};
template <typename List>
using pop_front_t = typename pop_front<List>::type;
template <typename Item, typename List>
struct push_front;
template <typename Item, template <typename...> typename List,
typename... Items>
struct push_front<Item, List<Items...>> {
using type = List<Item, Items...>;
};
template <typename Item, typename List>
using push_front_t = typename push_front<Item, List>::type;
template <typename List>
struct pop_back;
template <template <typename...> typename List, typename Head, typename... Tail>
struct pop_back<List<Head, Tail...>> {
using type = push_front_t<Head, typename pop_back<List<Tail...>>::type>;
};
template <template <typename...> typename List, typename Head>
struct pop_back<List<Head>> {
using type = List<>;
};
template <typename List>
using pop_back_t = typename pop_back<List>::type;
namespace details {
template <typename Item, typename List, bool = is_empty_v<List>>
struct push_back_impl;
template <typename Item, typename List>
struct push_back_impl<Item, List, false> {
using type =
push_front_t<front_t<List>,
typename push_back_impl<Item, pop_front_t<List>>::type>;
};
template <typename Item, typename List>
struct push_back_impl<Item, List, true> {
using type = push_front_t<Item, List>;
};
} // namespace details
template <typename Item, typename List>
struct push_back : details::push_back_impl<Item, List> {};
template <typename Item, typename List>
using push_back_t = typename push_back<Item, List>::type;
template <typename Item, typename List, bool = is_empty_v<List>>
struct has {
static constexpr bool value =
!is_empty_v<List> && (std::is_same<Item, robust_front_t<List>>::value ||
has<Item, pop_front_t<List>>::value);
};
template <typename Item, typename List>
struct has<Item, List, false> {
static constexpr bool value = std::is_same_v<Item, front_t<List>> ||
has<Item, pop_front_t<List>>::value;
};
template <typename Item, typename List>
struct has<Item, List, true> {
static constexpr bool value = false;
};
template <typename Item, typename List>
constexpr bool has_v = has<Item, List>::value;
template <typename List, bool = (size_v<List>> 1)>
struct remove_dup;
template <typename List>
struct remove_dup<List, true> {
using Head = front_t<List>;
using Tail = pop_front_t<List>;
using type =
std::conditional_t<has_v<Head, Tail>, typename remove_dup<Tail>::type,
push_front_t<Head, typename remove_dup<Tail>::type>>;
};
template <typename List>
struct remove_dup<List, false> {
using type = List;
};
template <typename List>
using remove_dup_t = typename remove_dup<List>::type;
template <typename List1, typename List2, bool = is_empty_v<List2>>
struct concat;
template <typename List1, typename List2>
struct concat<List1, List2, false> {
using type = push_front_t<front_t<List2>,
typename concat<List1, pop_front_t<List2>>::type>;
};
template <typename List1, typename List2>
struct concat<List1, List2, true> {
using type = List1;
};
template <typename List1, typename List2>
using concat_t = typename concat<List1, List2>::type;
template <typename List1, typename List2>
using merge_t = remove_dup_t<concat_t<List1, List2>>;
template <typename... Lists>
struct concat_all;
template <typename L1, typename L2, typename... Ls>
struct concat_all<L1, L2, Ls...> {
using type = concat_t<L1, typename concat_all<L2, Ls...>::type>;
};
template <typename L>
struct concat_all<L> {
using type = L;
};
template <typename... Lists>
using concat_all_t = typename concat_all<Lists...>::type;
template <template <typename...> typename TList, typename SList>
struct rebind;
template <template <typename...> typename TList,
template <typename...> typename SList, typename... Args>
struct rebind<TList, SList<Args...>> {
using type = TList<Args...>;
};
template <template <typename...> typename TList, typename SList>
using rebind_t = typename rebind<TList, SList>::type;
template <typename... Ts>
struct mplist {};
} // namespace lsm::list
//--------------------------------------------------------
// Internal details
//--------------------------------------------------------
namespace lsm::details {
// state type aggregator
template <typename List, bool = lsm::list::is_empty_v<List>>
struct set_state_types_aggregator;
template <typename List>
struct set_state_types_aggregator<List, false> {
using head = lsm::list::front_t<List>;
using type =
lsm::list::merge_t<lsm::list::mplist<typename head::source_state_type,
typename head::target_state_type>,
typename set_state_types_aggregator<
lsm::list::pop_front_t<List>>::type>;
};
template <typename List>
struct set_state_types_aggregator<List, true> {
using type = lsm::list::mplist<>;
};
template <typename List>
using set_state_types_aggregator_t =
typename set_state_types_aggregator<List>::type;
// transition finder
template <typename State, typename Input, typename List,
bool = lsm::list::is_empty_v<List>>
struct tx_finder;
template <typename State, typename Input, typename List>
struct tx_finder<State, Input, List, false> {
using current_tx = lsm::list::front_t<List>;
using type = std::conditional_t<
std::is_same_v<State, typename current_tx::source_state_type> &&
std::is_same_v<Input, typename current_tx::input_type>,
current_tx,
typename tx_finder<State, Input, lsm::list::pop_front_t<List>>::type>;
};
template <typename State, typename Input, typename List>
struct tx_finder<State, Input, List, true> {
using type = utilities::nonsuch;
};
template <typename State, typename Input, typename List>
using tx_finder_t = typename tx_finder<State, Input, List>::type;
} // namespace lsm::details
//--------------------------------------------------------
// State machine traits
//--------------------------------------------------------
namespace lsm::traits {
template <typename T>
struct state_machine_traits {
using transition_table_type = typename T::transition_table;
using state_list_type = list::rebind_t<
std::variant,
details::set_state_types_aggregator_t<transition_table_type>>;
};
} // namespace lsm::traits
//--------------------------------------------------------
// State machine engine implementation
//--------------------------------------------------------
namespace lsm {
///
///@brief Type used to describe transitions
///
template <typename... Ts>
using transition_table_type = list::mplist<Ts...>;
///
/// @brief Base structure used for state definition
///
struct base_state {
virtual void on_enter() {
// no op
}
virtual void on_exit() {
// no op
}
};
///
/// @brief Base state machine descriptor
///
template <typename Base>
class state_machine_desc {
private:
template <typename SourceState, typename Input, typename TargetState>
struct base_transition {
using source_state_type = SourceState;
using input_type = Input;
using target_state_type = TargetState;
};
public:
state_machine_desc() = default;
state_machine_desc(const state_machine_desc &) = delete;
state_machine_desc &operator=(const state_machine_desc &) = delete;
template <typename SourceState, typename Input, typename TargetState,
auto Func>
struct transition_cb : base_transition<SourceState, Input, TargetState> {
template <typename SM, typename... Args>
static auto apply(SM &sm, Args &&... args) {
return (sm.*Func)(std::forward<Args>(args)...);
}
};
template <typename SourceState, typename Input, typename TargetState>
struct transition : base_transition<SourceState, Input, TargetState> {
template <typename VM, typename Arg>
static void apply([[maybe_unused]] VM &vm, [[maybe_unused]] Arg arg) {
// sink
}
};
// To be continued... Create new transition type here suited to your needs
};
///
/// @brief Base state machine frontend
///
template <typename T>
class state_machine_front {
public:
using traits_type = traits::state_machine_traits<T>;
using table_type = typename traits_type::transition_table_type;
using state_list_type = typename traits_type::state_list_type;
using error_handler_type = std::function<void(const std::string&)>;
void set_error_handler(const error_handler_type& h) {
m_error_handler = h;
}
private:
struct default_handler {
void operator()(const std::string& msg) {
throw std::runtime_error(msg.c_str());
}
};
T &m_sm;
state_list_type m_current_state;
error_handler_type m_error_handler = default_handler();
template <typename State, typename Input>
void apply_transition(const Input &input) {
using tx = details::tx_finder_t<State, Input, table_type>;
if constexpr (!std::is_same_v<tx, utilities::nonsuch>) {
using next_state = typename tx::target_state_type;
if (!std::is_same_v<next_state, State>) {
// exit state
std::get<State>(m_current_state).on_exit();
// enter new state
m_current_state = next_state();
std::get<next_state>(m_current_state).on_enter();
}
// apply transition
tx::apply(m_sm, input);
} else {
// For your needs, you could add custom error handler to this class
// easily
m_error_handler("bad transition");
}
}
public:
state_machine_front(T &sm) : m_sm{sm} {}
template <typename State>
void init() {
m_current_state = State();
std::get<State>(m_current_state).on_enter();
}
template <typename Input>
void transit(const Input &input) {
std::visit(
[this, &input](auto &&arg) {
using S = std::decay_t<decltype(arg)>;
this->apply_transition<S, Input>(input);
},
m_current_state);
}
};
} // namespace lsm