-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
main.cpp
170 lines (134 loc) · 3.71 KB
/
main.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
// HFSM2 (hierarchical state machine for games and interactive applications)
// Created by Andrew Gresyk
//
// An HFSM2 port of https://gist.github.com/martinmoene/797b1923f9c6c1ae355bb2d6870be25e
// by Martin Moene (see https://twitter.com/MartinMoene/status/1118453128834232320)
#include <iostream>
#include <string>
#include <assert.h>
#define HFSM2_ENABLE_LOG_INTERFACE
#define HFSM2_ENABLE_STRUCTURE_REPORT
#include <hfsm2/machine.hpp>
//------------------------------------------------------------------------------
// events
struct Play { std::string title; };
struct Pause {};
struct Resume {};
struct Stop {};
// shared data stored externally to the fsm
using Context = std::string;
using M = hfsm2::MachineT<hfsm2::Config::ContextT<Context>>;
#if 0
// states need to be forward declared to be used in FSM struct declaration
struct Idle;
struct Playing;
struct Paused;
using FSM = M::PeerRoot<
Idle,
Playing,
Paused
>;
#else
// alternatively, some macro magic can be invoked to simplify FSM structure declaration
#define S(s) struct s
using FSM = M::PeerRoot<
S(Idle),
S(Playing),
S(Paused)
>;
#undef S
#endif
//------------------------------------------------------------------------------
// double-check state ids for Logger::stateName()
static_assert(FSM::stateId<Idle>() == 1, "");
static_assert(FSM::stateId<Playing>() == 2, "");
static_assert(FSM::stateId<Paused>() == 3, "");
// custom logger for recording all transitions
struct Logger
: M::LoggerInterface
{
static const char* stateName(const StateID stateId) {
switch (stateId) {
case 1:
return "Idle";
case 2:
return "Playing";
case 3:
return "Paused";
default:
assert(false);
return "";
}
}
void recordTransition(const Context& /*context*/,
const StateID origin,
const TransitionType /*transition*/,
const StateID target) override
{
std::cout << stateName(origin) << " -> " << stateName(target) << "\n";
}
};
//------------------------------------------------------------------------------
// state helper for error reporting
struct Base
: FSM::State
{
template <typename Event>
void react(const Event&, FullControl&) {
std::cout << "[unsupported transition]\n";
}
};
// states
struct Idle
: Base
{
using Base::react;
void react(const Play& event, FullControl& control) {
control.context() = event.title;
control.changeTo<Playing>();
}
};
struct Playing
: Base
{
using Base::react;
void react(const Pause&, FullControl& control) {
control.changeTo<Paused>();
}
void react(const Stop&, FullControl& control) {
control.changeTo<Idle>();
}
};
struct Paused
: Base
{
using Base::react;
void react(const Resume&, FullControl& control) {
control.changeTo<Playing>();
}
void react(const Stop&, FullControl& control) {
control.changeTo<Idle>();
}
};
//------------------------------------------------------------------------------
int main() {
// construct everything
Context title;
Logger logger;
FSM::Instance machine(title, &logger);
// do the work :)
machine.react(Play{"any"}); // Idle -> Playing
machine.react(Stop{}); // Playing -> Idle
machine.react(Play{"optional"}); // Idle -> Playing
machine.react(Pause{}); // Playing -> Paused
machine.react(Stop{}); // Paused -> Idle
machine.react(Play{"variant"}); // Idle -> Playing
machine.react(Pause{}); //-V760 // Playing -> Paused
machine.react(Resume{}); // Paused -> Playing
machine.react(Stop{}); // Playing -> Idle
machine.react(Pause{}); // [unsupported transition]
machine.react(Resume{}); // [unsupported transition]
machine.react(Stop{}); // [unsupported transition]
return 0;
}
//------------------------------------------------------------------------------