-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsml
822 lines (704 loc) · 25.8 KB
/
sml
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
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
// <!--
// The MIT License (MIT)
//
// Copyright (c) 2024 Kris Jusiak <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#if 0
// -->
[Overview](#Overview) / [Examples](#Examples) / [API](#API) / [FAQ](#FAQ)
## SML: UML-2.5 State Machine Language
[![MIT Licence](http://img.shields.io/badge/license-MIT-blue.svg)](https://opensource.org/license/mit)
[![Version](https://img.shields.io/github/v/release/qlibs/sml)](https://github.com/qlibs/sml/releases)
[![Build](https://img.shields.io/badge/build-green.svg)](https://godbolt.org/z/Gcfncoo6r)
[![Try it online](https://img.shields.io/badge/try%20it-online-blue.svg)](https://godbolt.org/z/s9a6EW5j9)
> https://en.wikipedia.org/wiki/Finite-state_machine
> https://www.omg.org/spec/UML/2.5.1
### Features
- Single header (https://raw.githubusercontent.com/qlibs/sml/main/sml - for integration see [FAQ](#faq))
- Verifies itself upon include (can be disabled with `-DNTEST` - see [FAQ](#faq))
- Optimized run-time execution, binary size, compilation-times (see [performance](https://godbolt.org/z/W9rP94cYK))
- Minimal [API](#api)
### Requirements
- C++20 ([Clang-15+, GCC-12+](https://en.cppreference.com/w/cpp/compiler_support))
- No dependencies (no `#include/#import`)
- No `virtual` used (`-fno-rtti`)
- No `exceptions` required (`-fno-exceptions`)
---
### Overview
> State Machine (https://godbolt.org/z/s9a6EW5j9)
<p align="center"><img src="https://www.planttext.com/api/plantuml/png/RP313e9034Jl_OfwDK7l7Wo9_WKXPc4RQB8KmXQ-twAoIcHlpRoPQJUFwaQTke1rBqArSY-dGHeuQ4iTuSpLw4H1MGFXBJ40YCMnnFIox8ftZfyKygR_ZcZowfPcCLpMHZmZsHPLuDYQQqDzNHRnTYNsrR5HT-XXoIcGusDsWJsMrZPI9FtpxYoet54_xQARsmprQGR8IRpzA3m1" /></p>
```cpp
// events
struct connect {};
struct established {};
struct ping { bool valid{}; };
struct disconnect {};
struct timeout {};
int main() {
// guards/actions
auto establish = [] { std::puts("establish"); };
auto close = [] { std::puts("close"); };
auto reset = [] { std::puts("reset"); };
// states
struct Disconnected {};
struct Connecting {};
struct Connected {};
// transitions
sml::sm connection = sml::overload{
[](Disconnected, connect) -> Connecting { establish(); },
[](Connecting, established) -> Connected { },
[](Connected, ping event) { if (event.valid) { reset(); } },
[](Connected, timeout) -> Connecting { establish(); },
[](Connected, disconnect) -> Disconnected { close(); },
};
static_assert(sizeof(connection) == 1u);
assert(connection.visit(is<Disconnected>));
assert(connection.process_event(connect{}));
assert(connection.visit(is<Connecting>));
assert(connection.process_event(established{}));
assert(connection.visit(is<Connected>));
assert(connection.process_event(ping{.valid = true}));
assert(connection.visit(is<Connected>));
assert(connection.process_event(disconnect{}));
assert(connection.visit(is<Disconnected>));
}
```
```cpp
main: // $CXX -O3 -fno-exceptions -fno-rtti
push rax
lea rdi, [rip + .L.str.8]
call puts@PLT
lea rdi, [rip + .L.str.9]
call puts@PLT
lea rdi, [rip + .L.str.10]
call puts@PLT
xor eax, eax
pop rcx
ret
.L.str.8: .asciz "establish"
.L.str.9: .asciz "reset"
.L.str.10: .asciz "close"
```
---
### Examples
--
### API
```cpp
template<class T>
requires (requires (T t) { t(); })
struct sm {
constexpr sm(T&&);
template<class TEvent, auto dispatch = if_else>
requires dispatchable<TEvent>
constexpr auto process_event(const TEvent& event) -> bool ;
constexpr auto visit(auto&& fn) const;
};
```
```cpp
template<class... Ts> struct overload;
inline constexpr auto if_else; // if_else dispatch policy
inline constexpr auto jmp_table; // jmp_table dispatch policy
struct X {}; // terminate state
```
---
### FAQ
- How to integrate with [CMake.FetchContent](https://cmake.org/cmake/help/latest/module/FetchContent.html)?
```
include(FetchContent)
FetchContent_Declare(
qlibs.sml
GIT_REPOSITORY https://github.com/qlibs/sml
GIT_TAG v3.0.0
)
FetchContent_MakeAvailable(qlibs.sml)
```
```
target_link_libraries(${PROJECT_NAME} PUBLIC qlibs.sml);
```
- Acknowledgments
> https://www.youtube.com/watch?v=Zb6xcd2as6o
- Similar projects?
> [boost.msm](https://github.com/boostorg/msm), [boost.statechart](https://github.com/boostorg/statechart), [boost-ext.sml](https://github.com/boost-ext/sml)
<!--
#endif
#pragma once
#pragma GCC system_header
namespace sml::inline v3_0_0 {
using size_t = decltype(sizeof(int));
namespace type_traits {
struct none {};
template<class...> inline constexpr bool is_same_v = false;
template<class T> inline constexpr bool is_same_v<T, T> = true;
template<class T> struct remove_reference { using type = T; };
template<class T> struct remove_reference<T&> { using type = T; };
template<class T> struct remove_reference<T&&> { using type = T; };
template<class T> using remove_reference_t = typename remove_reference<T>::type;
template<class T> struct remove_cv { using type = T; };
template<class T> struct remove_cv<const T> { using type = T; };
template<class T> struct remove_cv<volatile T> { using type = T; };
template<class T> struct remove_cv<const volatile T> { using type = T; };
template<class T> using remove_cv_t = typename remove_cv<T>::type;
template<class T> using remove_cvref_t = remove_cv_t<remove_reference_t<T>>;
template<class> struct transition_traits;
template<class T> requires requires { &T::operator(); }
struct transition_traits<T> : transition_traits<decltype(&T::operator())> { };
template<class T> requires requires { &T::template operator()<none>; }
struct transition_traits<T> : transition_traits<decltype(&T::template operator()<none>)> { };
template<class T> requires requires { &T::template operator()<none, none>; }
struct transition_traits<T> : transition_traits<decltype(&T::template operator()<none, none>)> { };
template<class T, class TSrc, class TEvent, class TDst> struct transition_traits<auto (T::*)(TSrc, TEvent) const -> TDst> {
using src = remove_cvref_t<TSrc>;
using event = remove_cvref_t<TEvent>;
using dst = remove_cvref_t<TDst>;
};
template<class T, class TSrc, class TEvent, class TDst> struct transition_traits<auto (T::*)(TSrc, TEvent) -> TDst> {
using src = remove_cvref_t<TSrc>;
using event = remove_cvref_t<TEvent>;
using dst = remove_cvref_t<TDst>;
};
} // namespace type_traits
namespace utility {
template<class T> auto declval() -> T&&;
template<class T, T...> struct integer_sequence { };
template<size_t... Ns> using index_sequence = integer_sequence<size_t, Ns...>;
template<size_t N> using make_index_sequence =
#if defined(__clang__) || defined(_MSC_VER)
__make_integer_seq<integer_sequence, size_t, N>;
#else
index_sequence<__integer_pack(N)...>;
#endif
template<class T> struct wrapper {
[[no_unique_address]] T t;
constexpr const auto& operator()() const { return t; }
};
} // namespace utility
namespace mp {
template<class...> struct type_list {};
using info = const size_t*;
template<info> struct meta_type { constexpr auto friend get(meta_type); };
template<class T> struct meta_info {
using value_type = T;
static constexpr size_t id{};
constexpr auto friend get(meta_type<&id>) { return meta_info{}; }
};
template<class T> inline constexpr auto meta = &meta_info<T>::id;
template<info meta> using type_of = typename decltype(get(meta_type<meta>{}))::value_type;
template<template<class...> class T, const auto& v>
constexpr auto apply() {
return []<size_t... Ns>(utility::index_sequence<Ns...>) {
return T<type_of<v[Ns]>...>{};
}(utility::make_index_sequence<v.size()>{});
}
template<template<class...> class T, const auto& v>
using apply_t = decltype(apply<T, v>());
} // namespace mp
template<class T, size_t N>
struct static_vector {
constexpr static_vector() = default;
constexpr auto push_back(const T& value) { values_[size_++] = value; }
[[nodiscard]] constexpr const auto& operator[](auto i) const { return values_[i]; }
[[nodiscard]] constexpr auto begin() const { return &values_[0]; }
[[nodiscard]] constexpr auto end() const { return &values_[0] + size_; }
[[nodiscard]] constexpr auto size() const { return size_; }
[[nodiscard]] constexpr auto empty() const { return not size_; }
[[nodiscard]] constexpr auto capacity() const { return N; }
T values_[N]{};
size_t size_{};
};
template<class... Ts>
requires (__is_empty(Ts) and ...) and (sizeof...(Ts) < (1u << __CHAR_BIT__))
struct variant {
template<class T> static constexpr auto id = [] {
const bool match[]{type_traits::is_same_v<Ts, T>...};
for (auto i = 0; i < sizeof...(Ts); ++i) if (match[i]) return i;
return -1;
}();
constexpr variant() noexcept = default;
template<class T> constexpr variant(const T& t) noexcept : index{id<T>} { }
template<class T> constexpr auto reset(const T&) noexcept requires (type_traits::is_same_v<T, Ts> or ...) {
index = id<T>;
return true;
}
template<class... TArgs> requires ([]<class T> { return (type_traits::is_same_v<T, TArgs> or ...); }.template operator()<Ts>() or ...)
constexpr auto reset(const variant<TArgs...>& other) noexcept {
if (other.index != -1) {
index = other.index;
return true;
}
return false;
}
char index{-1};
};
inline constexpr auto if_else = []<class Fn, template<class...> class T, class... Ts>(Fn&& fn, const T<Ts...>& v) {
return [&]<size_t... Ns>(utility::index_sequence<Ns...>) {
return ([&] {
if (v.index == Ns) return fn(Ts{});
return false;
}() or ...);
}(utility::make_index_sequence<sizeof...(Ts)>{});
};
inline constexpr auto jmp_table = []<class Fn, template<class...> class T, class... Ts>(Fn&& fn, const T<Ts...>& v) {
static constexpr bool (*dispatch[])(Fn){[](Fn) { return false; }, [](Fn fn) { return fn(Ts{}); }...};
return dispatch[v.index](fn);
};
template<class... Ts> struct overload : Ts... {
using value_type = mp::type_list<Ts...>;
using Ts::operator()...;
};
template<class... Ts> overload(Ts...) -> overload<Ts...>;
template<class T>
requires (requires (T t) { t(); })
class sm {
template<class TState>
static constexpr auto add_state(auto& states, mp::type_list<TState>) {
const auto new_state = mp::meta<TState>;
if (new_state == mp::meta<void> or new_state == mp::meta<type_traits::none>) return;
for (const auto& state : states) if (state == new_state) return;
states.push_back(new_state);
}
template<template<class...> class TList, class... Ts>
static constexpr auto add_state(auto& states, mp::type_list<TList<Ts...>>) {
(add_state(states, mp::type_list<Ts>{}), ...);
}
static constexpr auto states = []<class... Ts>(mp::type_list<Ts...>) {
static_vector<mp::info, 2u * sizeof...(Ts)> states{};
(add_state(states, mp::type_list<typename type_traits::transition_traits<Ts>::src>{}), ...);
(add_state(states, mp::type_list<typename type_traits::transition_traits<Ts>::dst>{}), ...);
return states;
}(typename type_traits::remove_cvref_t<decltype(utility::declval<T>()())>::value_type{});
template<class TEvent>
static constexpr auto dispatchable =
[]<class... TStates>(mp::type_list<TStates...>) {
return (requires(T t, TStates state, TEvent event) { t()(state, event); } or ...);
}(mp::apply_t<mp::type_list, states>{});
public:
constexpr sm(const auto& t) : t_{t} { }
template<class TEvent, auto dispatch = if_else> requires dispatchable<TEvent>
constexpr auto process_event(const TEvent& event) -> bool {
return dispatch([&](const auto& state) {
if constexpr (requires { states_.reset(t_()(state, event)); }) {
return states_.reset(t_()(state, event));
} else if constexpr (requires { t_()(state, event); }) {
t_()(state, event);
return true;
} else {
return false;
}
}, states_);
}
template<auto dispatch = if_else> constexpr auto visit(auto&& fn) const {
return dispatch(fn, states_);
};
private:
[[no_unique_address]] T t_{};
[[no_unique_address]] mp::apply_t<variant, states> states_ = mp::type_of<states[0u]>{};
};
template<class T> requires requires (T t) { t(); } sm(T) -> sm<T>;
template<class T> sm(T) -> sm<utility::wrapper<T>>;
struct X {}; // terminate state
} // namespace sml
#ifndef NTEST
static_assert(([] {
constexpr auto expect = [](bool cond) { if (not cond) { void failed(); failed(); } };
constexpr auto is = []<class... TStates>(const auto& sm, sml::mp::type_list<TStates...>) {
return sm.visit([&]<class TState>(const TState&) {
return (sml::type_traits::is_same_v<TStates, TState> and ...);
});
};
// states
struct idle {};
struct s1 {};
struct s2 {};
struct s3 {};
struct s4 {};
// events
struct e1 {};
struct e2 {};
struct e3 {};
struct e { int value{}; };
struct unexpected {};
// sml::sm::process_event
{
sml::sm sm = sml::overload{
[](s1, e1) -> s2 { return {}; }
};
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s2>{}));
}
#if 0
// start[on_entry]
{
int on_entry_calls{};
sm sm = [&] {
using namespace dsl;
constexpr auto on_entry = dsl::on_entry;
return transition_table{
*"idle"_s + on_entry / [&] { ++on_entry_calls; }
};
};
expect(is(sm, sml::mp::type_list<idle>{}));
expect(on_entry_calls == 1);
}
// process_event[on_entry/on_exit]
{
int on_entry_calls{};
int on_exit_calls{};
sm sm = [&] {
using namespace dsl;
constexpr auto on_entry = dsl::on_entry;
constexpr auto on_exit = dsl::on_exit;
return transition_table{
*"s1"_s + event<e1> = "s2"_s,
"s2"_s + on_entry / [&] { ++on_entry_calls; },
"s2"_s + on_exit / [&] { ++on_exit_calls; },
"s2"_s + event<e2> = "s3"_s,
};
};
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s2>{}));
expect(on_entry_calls == 1);
expect(on_exit_calls == 0);
expect(sm.process_event(e2{}));
expect(on_entry_calls == 1);
expect(on_exit_calls == 1);
expect(is(sm, sml::mp::type_list<s3>{}));
}
// process_event[on_entry/on_exit] with internal transitions
{
int on_entry_calls{};
int on_exit_calls{};
sm sm = [&] {
using namespace dsl;
constexpr auto on_entry = dsl::on_entry;
constexpr auto on_exit = dsl::on_exit;
return transition_table{
*"s1"_s + event<e1> = "s2"_s,
"s2"_s + event<e2>,
"s2"_s + on_entry / [&] { ++on_entry_calls; },
"s2"_s + on_exit / [&] { ++on_exit_calls; },
};
};
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s2>{}));
expect(on_entry_calls == 1);
expect(on_exit_calls == 0);
expect(sm.process_event(e2{}));
expect(on_entry_calls == 1);
expect(on_exit_calls == 0);
expect(is(sm, sml::mp::type_list<s2>{}));
}
#endif
// process_event[same event multiple times]
{
sml::sm sm = sml::overload{
[](s1, e1) -> s2 { return {}; },
[](s2, e1) -> s3 { return {}; },
};
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s2>{}));
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s3>{}));
}
// process_event[different events in any state]
{
sml::sm sm = sml::overload{
[](s1, e1) -> s2 { return {}; },
[](s2, e2) -> s3 { return {}; },
[](s3, e2) -> sml::X { return {}; },
[](auto, e) -> s1 { return {}; },
};
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s2>{}));
expect(sm.process_event(e{{}}));
expect(is(sm, sml::mp::type_list<s1>{}));
expect(not sm.process_event(e2{})); // ignored
expect(is(sm, sml::mp::type_list<s1>{}));
expect(sm.process_event(e1{})); // s1 -> s2
expect(is(sm, sml::mp::type_list<s2>{}));
expect(sm.process_event(e2{})); // s2 -> s3
expect(is(sm, sml::mp::type_list<s3>{}));
expect(sm.process_event(e{})); // _ -> s1
expect(is(sm, sml::mp::type_list<s1>{}));
}
// proces_event[unexpected event]
{
sml::sm sm = sml::overload{
[](s1, e1) -> s2 { return {}; },
};
constexpr auto process_event = [](const auto& event) {
return requires { sm.process_event(event); };
};
static_assert(process_event(e1{}));
static_assert(not process_event(unexpected{}));
}
// events[multiple events]
{
sml::sm test = sml::overload{
[](s1, auto) -> s2 { return {}; },
};
{
sml::sm sm{test};
expect(is(sm, sml::mp::type_list<s1>{}));
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s2>{}));
}
{
sml::sm sm{test};
expect(is(sm, sml::mp::type_list<s1>{}));
expect(sm.process_event(e2{}));
expect(is(sm, sml::mp::type_list<s2>{}));
}
}
// events[same event transitions]
{
sml::sm sm = sml::overload{
[](idle, e1) -> s1 { return {}; },
[](s1, e1) -> s2 { return {}; },
[](s2, e1) -> idle { return {}; },
};
expect(is(sm, sml::mp::type_list<idle>{}));
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s1>{}));
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s2>{}));
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<idle>{}));
}
// transition_table[multiple transitions]
{
sml::sm sm = sml::overload{
[](s1, e1) -> s2 { return {}; },
[](s2, e2) -> s1 { return {}; },
};
expect(is(sm, sml::mp::type_list<s1>{}));
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s2>{}));
expect(not sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s2>{}));
expect(sm.process_event(e2{}));
expect(is(sm, sml::mp::type_list<s1>{}));
}
// transition_table[terminated state]
{
sml::sm sm = sml::overload{
[](s1, e1) -> sml::X { return {}; },
};
expect(is(sm, sml::mp::type_list<s1>{}));
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<sml::X>{}));
expect(not sm.process_event(e1{}));
}
// transition_table[guards/actions]
{
unsigned calls{};
sml::sm sm = sml::overload{
[&](s1, const e& event) -> sml::variant<s1, s2> {
if (event.value) {
++calls;
return s2{};
}
return {};
},
};
expect(not sm.process_event(e{false}));
expect(is(sm, sml::mp::type_list<s1>{}));
expect(sm.process_event(e{true}));
expect(is(sm, sml::mp::type_list<s2>{}));
}
#if 0
// transition_table[process]
{
sm sm = [] {
using namespace dsl;
return transition_table{
*"s1"_s + event<e1> / process(e2{}) = "s2"_s,
"s2"_s + event<e2> = "s3"_s,
};
};
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s3>{}));
}
// transition_table[orthogonal regions]
{
sml::sm sm = sml::overload{
sml::overload{
[](s1, e1) -> s2 { return {}; },
},
sml::overload{
[](s3, e2) -> s4 { return {}; },
}
};
expect(is(sm, sml::mp::type_list<s1, s3>{}));
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s2, s3>{}));
expect(sm.process_event(e2{}));
expect(is(sm, sml::mp::type_list<s2, s4>{}));
}
// transition_table[orthogonal regions]
{
using sml::X;
sml::sm sm = sml::overload{
sml::overload{
[](s1, e1) -> s2 { return {}; },
[](s2, e2) -> X { return {}; },
},
sml::overload{
[](s3, e3) -> X { return {}; },
}
};
expect(is(sm, sml::mp::type_list<s1, s3>{}));
expect(sm.process_event(e1{}));
expect(is(sm, sml::mp::type_list<s2, s3>{}));
expect(sm.process_event(e2{}));
expect(is(sm, sml::mp::type_list<X, s3>{}));
expect(sm.process_event(e3{}));
expect(is(sm, sml::mp::type_list<X, X>{}));
}
#endif
// dependencies
{
struct s {
bool& in;
int& out;
constexpr auto guard() { return in; };
constexpr auto operator()() {
return sml::overload{
[this](s1, const e& event) { if (guard()) out = event.value; },
};
}
};
{
bool in{false};
int out{};
s _{in, out};
sml::sm sm{_};
expect(sm.process_event(e{.value = 42}));
expect(0 == out);
}
{
bool in{true};
int out{};
s _{in, out};
sml::sm sm{_};
expect(sm.process_event(e{.value = 42}));
expect(42 == out);
}
}
// example.connection
{
struct connect {};
struct established {};
struct ping { bool valid{}; };
struct disconnect {};
struct timeout {};
{
struct sm {
struct Disconnected {};
struct Connecting {};
struct Connected {};
constexpr void establish(){}
constexpr void close(){}
constexpr void reset_timeout(){ }
constexpr auto operator()() {
return sml::overload{
[this](Disconnected, connect) -> Connecting { establish(); return {}; },
[ ](Connecting, established) -> Connected { return {}; },
[this](Connected, const ping& event) { if (event.valid) { reset_timeout(); } },
[this](Connected, timeout) -> Connecting { establish(); return {}; },
[this](Connected, disconnect) -> Disconnected { close(); return {}; },
};
}
};
sml::sm connection{sm{}};
struct empty{};
static_assert(sizeof(connection) == sizeof(empty));
expect(is(connection, sml::mp::type_list<sm::Disconnected>{}));
expect(connection.process_event(connect{}));
expect(is(connection, sml::mp::type_list<sm::Connecting>{}));
expect(connection.process_event(established{}));
expect(is(connection, sml::mp::type_list<sm::Connected>{}));
expect(connection.process_event(ping{.valid = true}));
expect(is(connection, sml::mp::type_list<sm::Connected>{}));
expect(connection.process_event(disconnect{}));
expect(is(connection, sml::mp::type_list<sm::Disconnected>{}));
}
#if __cpp_constexpr >= 202211L
{
struct sm {
struct Disconnected {};
struct Connecting {};
struct Connected {};
static constexpr void establish(){}
static constexpr void close(){}
static constexpr void reset_timeout(){ }
constexpr auto operator()() const {
return sml::overload{
[](Disconnected, connect) -> Connecting { establish(); return {}; },
[](Connecting, established) -> Connected { return {}; },
[](Connected, const ping& event) { if (event.valid) { reset_timeout(); } },
[](Connected, timeout) -> Connecting { establish(); return {}; },
[](Connected, disconnect) -> Disconnected { close(); return {}; },
};
}
};
sml::sm connection{sm{}};
struct empty{};
static_assert(sizeof(connection) == sizeof(empty));
expect(is(connection, sml::mp::type_list<sm::Disconnected>{}));
expect(connection.process_event(connect{}));
expect(is(connection, sml::mp::type_list<sm::Connecting>{}));
expect(connection.process_event(established{}));
expect(is(connection, sml::mp::type_list<sm::Connected>{}));
expect(connection.process_event(ping{.valid = true}));
expect(is(connection, sml::mp::type_list<sm::Connected>{}));
expect(connection.process_event(disconnect{}));
expect(is(connection, sml::mp::type_list<sm::Disconnected>{}));
}
{
struct Disconnected {};
struct Connecting {};
struct Connected {};
static constexpr auto establish = []{};
static constexpr auto close = []{ };
static constexpr auto reset_timeout = []{ };
sml::sm connection = sml::overload{
[](Disconnected, connect) -> Connecting { establish(); return {}; },
[](Connecting, established) -> Connected { return {}; },
[](Connected, const ping& event) { if (event.valid) { reset_timeout(); } },
[](Connected, timeout) -> Connecting { establish(); return {}; },
[](Connected, disconnect) -> Disconnected { close(); return {}; },
};
struct empty{};
static_assert(sizeof(connection) == sizeof(empty));
expect(is(connection, sml::mp::type_list<Disconnected>{}));
expect(connection.process_event(connect{}));
expect(is(connection, sml::mp::type_list<Connecting>{}));
expect(connection.process_event(established{}));
expect(is(connection, sml::mp::type_list<Connected>{}));
expect(connection.process_event(ping{.valid = true}));
expect(is(connection, sml::mp::type_list<Connected>{}));
expect(connection.process_event(disconnect{}));
expect(is(connection, sml::mp::type_list<Disconnected>{}));
}
#endif
}
}(), true));
#endif // NTEST