-
Notifications
You must be signed in to change notification settings - Fork 176
/
debugging_switch.cpp
122 lines (99 loc) · 3.14 KB
/
debugging_switch.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
#include <tinyfsm.hpp>
#include <iostream>
#include <cassert>
struct Off; // forward declaration
// ----------------------------------------------------------------------------
// Event Declarations
//
struct Toggle : tinyfsm::Event { }; // Event Declarations
// ----------------------------------------------------------------------------
// State Machine Declaration
//
struct Switch
: tinyfsm::Fsm<Switch>
{
static void reset(void);
// NOTE: on reset: "tinyfsm::StateList<Off, On>::reset()", copy
// constructor is used by default, so "this" points to neither
// "Off" nor "On" (see operator=() below).
Switch() : counter(0) {
std::cout << "* Switch()" << std::endl
<< " this = " << this << std::endl;
}
~Switch() {
std::cout << "* ~Switch()" << std::endl
<< " this = " << this << std::endl;
}
Switch & operator=(const Switch & other) {
std::cout << "* operator=()" << std::endl
<< " this = " << this << std::endl
<< " other = " << &other << std::endl;
counter = other.counter;
return *this;
}
virtual void react(Toggle const &) { };
void entry(void);
void exit(void);
int counter;
};
struct On : Switch {
void react(Toggle const &) override { transit<Off>(); };
};
struct Off : Switch {
void react(Toggle const &) override { transit<On>(); };
};
FSM_INITIAL_STATE(Switch, Off)
// ----------------------------------------------------------------------------
// State Machine Definitions
//
void Switch::reset() {
tinyfsm::StateList<Off, On>::reset();
}
void Switch::entry() {
counter++;
// debugging only. properly designed state machines don't need this:
if(is_in_state<On>()) { std::cout << "* On::entry()" << std::endl; }
else if(is_in_state<Off>()) { std::cout << "* Off::entry()" << std::endl; }
else assert(true);
assert(current_state_ptr == this);
std::cout << " this (cur) = " << this << std::endl
<< " state<On> = " << &state<On>() << std::endl
<< " state<Off> = " << &state<Off>() << std::endl;
}
void Switch::exit() {
assert(current_state_ptr == this);
std::cout << "* exit()" << std::endl
<< " this (cur) = " << this << std::endl
<< " state<On> = " << &state<On>() << std::endl
<< " state<Off> = " << &state<Off>() << std::endl;
}
// ----------------------------------------------------------------------------
// Main
//
int main()
{
Switch::start();
while(1)
{
char c;
std::cout << "* main()" << std::endl
<< " cur_counter = " << Switch::current_state_ptr->counter << std::endl
<< " on_counter = " << Switch::state<On>().counter << std::endl
<< " off_counter = " << Switch::state<Off>().counter << std::endl;
std::cout << std::endl << "t=Toggle, r=Restart, q=Quit ? ";
std::cin >> c;
switch(c) {
case 't':
Switch::dispatch(Toggle());
break;
case 'r':
Switch::reset();
Switch::start();
break;
case 'q':
return 0;
default:
std::cout << "> Invalid input" << std::endl;
};
}
}