forked from erikzenker/hsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhsm.cpp
89 lines (79 loc) · 2.8 KB
/
hsm.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
//
// Copyright (c) 2016-2019 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "benchmark.hpp"
#include <hsm/hsm.h>
using namespace hsm;
struct Empty{};
struct Open{};
struct Stopped{};
struct Playing{};
struct Pause{};
struct play {};
struct end_pause {};
struct stop {};
struct pause2 {
};
struct open_close {};
struct cd_detected {};
auto start_playback = [](auto, auto, auto) {};
auto resume_playback = [](auto, auto, auto) {};
auto close_drawer = [](auto, auto, auto) {};
auto open_drawer = [](auto, auto, auto) {};
auto stop_and_open = [](auto, auto, auto) {};
auto stopped_again = [](auto, auto, auto) {};
auto store_cd_info = [](auto, auto, auto) {};
auto pause_playback = [](auto, auto, auto) {};
auto stop_playback = [](auto, auto, auto) {};
struct player {
static constexpr auto make_transition_table()
{
// clang-format off
return transition_table(
state<Stopped> {} + event<play>{} / start_playback = state<Playing> {},
state<Pause> {} + event<end_pause>{} / resume_playback = state<Playing> {},
state<Open> {} + event<open_close>{} / close_drawer = state<Empty> {},
* state<Empty> {} + event<open_close>{} / open_drawer = state<Open> {},
state<Pause> {} + event<open_close>{} / stop_and_open = state<Open> {},
state<Stopped> {} + event<open_close>{} / open_drawer = state<Open> {},
state<Playing> {} + event<open_close>{} / stop_and_open = state<Open> {},
state<Playing> {} + event<pause2>{} / pause_playback = state<Pause> {},
state<Playing> {} + event<stop>{} / stop_playback = state<Stopped> {},
state<Pause> {} + event<stop>{} / stop_playback = state<Stopped> {},
state<Empty> {} + event<cd_detected>{} / store_cd_info = state<Stopped> {},
state<Stopped> {} + event<stop>{} / stopped_again = state<Stopped> {}
);
// clang-format on
}
};
auto main() -> int{
hsm::sm<player> sm;
auto a = open_close{};
auto b = cd_detected{};
auto c = play{};
auto d = pause2 {};
auto e = stop{};
auto f = end_pause{};
const auto nRuns = 1'000'000;
benchmark_execution_speed([&] {
for (auto i = 0; i < nRuns; ++i) {
sm.process_event(a);
sm.process_event(a);
sm.process_event(b);
sm.process_event(c);
sm.process_event(d);
// go back to Playing
sm.process_event(f);
sm.process_event(d);
sm.process_event(e);
// event leading to the same state
sm.process_event(e);
sm.process_event(a);
sm.process_event(a);
}
});
benchmark_memory_usage(sm);
}