-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.cpp
62 lines (51 loc) · 1.9 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
#include "hsm/hsm.h"
#include <cassert>
#include <iostream>
// States
struct Locked {
};
struct Unlocked {
};
// Events
struct Push {
};
struct Coin {
};
// Guards
const auto noError = [](auto /*event*/, auto /*source*/, auto /*target*/) { return true; };
// Actions
constexpr auto beep
= [](auto /*event*/, auto /*source*/, auto /*target*/) { std::cout << "beep!" << std::endl; };
constexpr auto blink = [](auto /*event*/, auto /*source*/, auto /*target*/) {
std::cout << "blink, blink, blink!" << std::endl;
};
struct Turnstile {
static constexpr auto make_transition_table()
{
// clang-format off
return hsm::transition_table(
// Source + Event [Guard] / Action = Target
// +-------------------+-----------------+---------+--------+----------------------+
* hsm::state<Locked> + hsm::event<Push> / beep = hsm::state<Locked> ,
hsm::state<Locked> + hsm::event<Coin> [noError] / blink = hsm::state<Unlocked>,
// +--------------------+---------------------+---------+--------+------------------------+
hsm::state<Unlocked> + hsm::event<Push> [noError] = hsm::state<Locked> ,
hsm::state<Unlocked> + hsm::event<Coin> / blink = hsm::state<Unlocked>
// +--------------------+---------------------+---------+--------+------------------------+
);
// clang-format on
}
};
auto main() -> int
{
hsm::sm<Turnstile> turnstileSm;
// The turnstile is initially locked
assert(turnstileSm.is(hsm::state<Locked>));
// Inserting a coin unlocks it
turnstileSm.process_event(Coin {});
assert(turnstileSm.is(hsm::state<Unlocked>));
// Entering the turnstile will lock it again
turnstileSm.process_event(Push {});
assert(turnstileSm.is(hsm::state<Locked>));
return 0;
}