Ticket #2907: boost.ticket.2907.cpp

File boost.ticket.2907.cpp, 4.6 KB (added by George van Venrooij <george.van.venrooij@…>, 14 years ago)

Test-case working on MSVC 9 SP1 with Boost 1.37.0

Line 
1#include <boost/statechart/state_machine.hpp>
2#include <boost/statechart/event.hpp>
3#include <boost/statechart/simple_state.hpp>
4#include <boost/statechart/custom_reaction.hpp>
5
6#include <iostream>
7
8namespace bs = boost::statechart;
9
10using namespace std;
11
12// The main event, should be handles by a sub-state and the main state
13struct update_event;
14// An event triggering the sub-state to switch
15struct toggle_event;
16// A dummy event
17struct other_event;
18
19// Sub-states in 2 orthogonal regions
20struct state_1A;
21struct state_1B;
22struct state_2A;
23struct state_2B;
24
25// Main state
26struct parent_state;
27
28// State machine consisting of the main state
29struct machine : bs::state_machine<machine, parent_state>
30{
31};
32
33typedef
34boost::mpl::list
35< state_1A
36, state_2A
37> inner_states_t;
38
39// The main state has 2 orthogonal sub-states
40struct parent_state : bs::simple_state<parent_state, machine, inner_states_t>
41{
42 typedef boost::mpl::list
43 < bs::custom_reaction <update_event>
44 >
45 reactions;
46
47 bs::result react(const update_event&)
48 {
49 cout << __FUNCTION__ << endl;
50
51 return discard_event();
52 }
53};
54
55struct update_event : bs::event<update_event>
56{
57};
58
59struct other_event : bs::event<other_event>
60{
61};
62
63struct toggle_event : bs::event<toggle_event>
64{
65};
66
67struct state_1A : bs::simple_state<state_1A, parent_state::orthogonal<0>>
68{
69 typedef boost::mpl::list
70 < bs::custom_reaction <toggle_event>
71 , bs::custom_reaction <update_event>
72 >
73 reactions;
74
75 state_1A()
76 {
77 cout << __FUNCTION__ << endl;
78 }
79
80 bs::result react(const toggle_event&)
81 {
82 cout << __FUNCTION__ << endl;
83
84 return transit<state_1B>();
85 }
86
87 bs::result react(const update_event&)
88 {
89 cout << __FUNCTION__ << endl;
90
91 return forward_event();
92 }
93};
94
95struct state_1B : bs::simple_state<state_1B, parent_state::orthogonal<0>>
96{
97 typedef boost::mpl::list
98 < bs::custom_reaction <toggle_event>
99 >
100 reactions;
101
102 state_1B()
103 {
104 cout << __FUNCTION__ << endl;
105 }
106
107 bs::result react(const toggle_event&)
108 {
109 cout << __FUNCTION__ << endl;
110
111 return transit<state_1A>();
112 }
113};
114
115struct state_2A : bs::simple_state<state_2A, parent_state::orthogonal<1>>
116{
117 typedef boost::mpl::list
118 < bs::custom_reaction <other_event>
119 >
120 reactions;
121
122 state_2A()
123 {
124 cout << __FUNCTION__ << endl;
125 }
126
127 bs::result react(const other_event&)
128 {
129 cout << __FUNCTION__ << endl;
130
131 return discard_event();
132 }
133};
134
135struct state_2B : bs::simple_state<state_2B, parent_state::orthogonal<1>>
136{
137 typedef boost::mpl::list
138 < bs::custom_reaction <other_event>
139 >
140 reactions;
141
142 state_2B()
143 {
144 cout << __FUNCTION__ << endl;
145 }
146
147 bs::result react(const other_event&)
148 {
149 cout << __FUNCTION__ << endl;
150
151 return discard_event();
152 }
153};
154
155int main(int argc, char* argv[])
156{
157 machine m;
158
159 cout << "0. Initiating state machine:" << endl;
160 m.initiate();
161
162 cout << endl << "1. Processing 'other_event':" << endl;
163 m.process_event(other_event ());
164
165 cout << endl << "2. Processing 'update_event':" << endl;
166 m.process_event(update_event());
167
168 cout << endl << "3. Processing 'toggle_event':" << endl;
169 m.process_event(toggle_event());
170
171 cout << endl << "4. Processing 'update_event':" << endl;
172 m.process_event(update_event());
173
174 cout << endl << "5. Processing 'toggle_event':" << endl;
175 m.process_event(toggle_event());
176
177 cout << endl << "6. Processing 'update_event':" << endl;
178 m.process_event(update_event());
179
180 return 0;
181}
182
183//////////////////////////////////////////////////////////////////////////
184// Program output after building with VC 9 and Boost 1.37.0
185//
186// Note the different event handling in step 6. as compared to step 2.
187// although the machine should be back in the same state.
188//////////////////////////////////////////////////////////////////////////
189
190/*
191
1920. Initiating state machine:
193state_1A::state_1A
194state_2A::state_2A
195
1961. Processing 'other_event':
197state_2A::react
198
1992. Processing 'update_event':
200state_1A::react
201parent_state::react
202
2033. Processing 'toggle_event':
204state_1A::react
205state_1B::state_1B
206
2074. Processing 'update_event':
208parent_state::react
209
2105. Processing 'toggle_event':
211state_1B::react
212state_1A::state_1A
213
2146. Processing 'update_event':
215parent_state::react
216
217*/