-
Notifications
You must be signed in to change notification settings - Fork 0
/
smtest.cpp
135 lines (112 loc) · 3.22 KB
/
smtest.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
/// This file is meant to test Boost.SML aginst Miro Samek's non-trivial
/// HSM that he uses to regression-test QP.
#include <boost/sml.hpp>
#include <iostream>
#include <assert.h>
namespace sml = boost::sml;
// Define events
//
struct eA {};
static auto evtA = sml::event<eA>;
struct eB {};
static auto evtB = sml::event<eB>;
struct eC {};
static auto evtC = sml::event<eC>;
struct eD {};
static auto evtD = sml::event<eD>;
struct eE {};
static auto evtE = sml::event<eE>;
struct eF {};
static auto evtF = sml::event<eF>;
struct eG {};
static auto evtG = sml::event<eG>;
struct eH {};
static auto evtH = sml::event<eH>;
struct eI {};
static auto evtI = sml::event<eI>;
// Define states
//
static auto s11 = sml::state<class s11>;
static auto s211 = sml::state<class s211>;
class smachine;
class s1machine;
class s2machine;
class s21machine;
// Define actions
auto outer_initialAction = []{ std::cout << "outer initial transition" << std::endl; };
auto s_initialAction = []{ std::cout << "s initial transition" << std::endl; };
auto s1_initialAction = []{ std::cout << "s1 initial transition" << std::endl; };
auto s2_initialAction = []{ std::cout << "s2 initial transition" << std::endl; };
auto s21_initialAction = []{ std::cout << "s21 initial transition" << std::endl; };
class smachine
{
public:
auto operator()() const noexcept
{
using namespace sml;
return sml::make_transition_table(
*"initial"_s / s_initialAction = s11,
state<s1machine> + evtC = state<s2machine>,
state<s2machine> + evtC = state<s1machine>,
state<s2machine> + evtF = s11
);
}
};
class s1machine
{
public:
auto operator()() const noexcept
{
using namespace sml;
return sml::make_transition_table(
*"initial"_s / s1_initialAction = s11
);
}
};
class s2machine
{
public:
auto operator()() const noexcept
{
using namespace sml;
return sml::make_transition_table(
*"initial"_s / s2_initialAction = s211
);
}
};
class s21machine
{
public:
auto operator()() const noexcept
{
using namespace sml;
return sml::make_transition_table(
*"initial"_s / s21_initialAction = s211
);
}
};
class basemachine
{
public:
auto operator()() const noexcept
{
using namespace sml;
return sml::make_transition_table(
*"initial"_s / outer_initialAction = state<s2machine>,
state<smachine> + evtE = s11,
state<s1machine> + evtD = state<smachine>
);
}
};
int main()
{
sml::sm<basemachine> basestatemachine;
using namespace sml;
basestatemachine.visit_current_states([](auto state) { std::cout << state.c_str() << std::endl;});
// assert(basestatemachine.is(s211));
basestatemachine.process_event(eC{});
basestatemachine.visit_current_states([](auto state) { std::cout << state.c_str() << std::endl;});
// assert(basestatemachine.is(state<s2machine>));
// basestatemachine.process_event(eC{});
// assert(basestatemachine.is(state<s1machine>));
}