forked from amaiorano/hsm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.cc
111 lines (93 loc) · 2.64 KB
/
test.cc
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
#include <iostream>
#include "hsm.h"
using namespace hsm;
class MyOwner
{
public:
MyOwner();
std::string data;
private:
StateMachine mStateMachine;
};
struct MyEvent
{
struct event_open : event_base<event_open>
{
std::string data;
event_open(const std::string d) : data(d) {}
};
struct event_close : event_base<event_close>
{
};
};
struct MyStates
{
struct Second : state_with_owner<Second, MyOwner>
{
typedef hsm_vector<
MyEvent::event_open,
MyEvent::event_close
> reactions;
void on_enter()
{
std::cout << "Second enter\n";
}
void on_exit()
{
std::cout << "Second exit\n";
}
result react(const MyEvent::event_open &)
{
std::cout << owner().data << std::endl;
std::cout << "handle event_open at state Second\n";
return transit<Third>();
}
result react(const MyEvent::event_close &)
{
std::cout << "defer event_close at state Second\n";
return defer();
}
};
struct First : state_base<First, Second>
{
void on_enter()
{
std::cout << "first enter\n";
}
void on_exit()
{
std::cout << "first exit\n";
}
};
struct Third : state_base<Third>
{
typedef hsm_vector<
MyEvent::event_close
> reactions;
void on_enter()
{
std::cout << "third enter\n";
}
void on_exit()
{
std::cout << "third exit\n";
}
result react(const MyEvent::event_close &)
{
std::cout << "handle event_close at state Third\n";
return finish();
}
};
};
MyOwner::MyOwner()
{
data = "Hello World!";
mStateMachine.initialize<MyStates::First>(this);
mStateMachine.process_event(MyEvent::event_close());
mStateMachine.process_event(MyEvent::event_open("Hi"));
mStateMachine.stop();
}
int main()
{
MyOwner myOwner;
}