1// Copyright 2010 Christophe Henry
2// henry UNDERSCORE christophe AT hotmail DOT com
3// This is an extended version of the state machine available in the boost::mpl library
4// Distributed under the same license as the original.
5// Copyright for the original version:
6// Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
7// under the Boost Software License, Version 1.0. (See accompanying
8// file LICENSE_1_0.txt or copy at
9// http://www.boost.org/LICENSE_1_0.txt)
10
11// back-end
12#include <boost/msm/back/state_machine.hpp>
13//front-end
14#include <boost/msm/front/state_machine_def.hpp>
15#ifndef BOOST_MSM_NONSTANDALONE_TEST
16#define BOOST_TEST_MODULE MyTest
17#endif
18#include <boost/test/unit_test.hpp>
19
20namespace msm = boost::msm;
21namespace mpl = boost::mpl;
22
23namespace
24{
25 // events
26 struct play {};
27 struct end_pause {};
28 struct stop {};
29 struct pause {};
30 struct open_close {};
31 struct NextSong {};
32 struct PreviousSong {};
33 struct cd_detected
34 {
35 cd_detected(std::string name)
36 : name(name)
37 {}
38 std::string name;
39 };
40
41 // front-end: define the FSM structure
42 struct player_ : public msm::front::state_machine_def<player_>
43 {
44 unsigned int start_playback_counter;
45 unsigned int can_close_drawer_counter;
46
47 player_():
48 start_playback_counter(0),
49 can_close_drawer_counter(0)
50 {}
51 // The list of FSM states
52 struct Empty : public msm::front::state<>
53 {
54 template <class Event,class FSM>
55 void on_entry(Event const&,FSM& ) {++entry_counter;}
56 template <class Event,class FSM>
57 void on_exit(Event const&,FSM& ) {++exit_counter;}
58 int entry_counter;
59 int exit_counter;
60 };
61 struct Open : public msm::front::state<>
62 {
63 template <class Event,class FSM>
64 void on_entry(Event const&,FSM& ) {++entry_counter;}
65 template <class Event,class FSM>
66 void on_exit(Event const&,FSM& ) {++exit_counter;}
67 int entry_counter;
68 int exit_counter;
69 };
70
71 // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
72 struct Stopped : public msm::front::state<>
73 {
74 template <class Event,class FSM>
75 void on_entry(Event const&,FSM& ) {++entry_counter;}
76 template <class Event,class FSM>
77 void on_exit(Event const&,FSM& ) {++exit_counter;}
78 int entry_counter;
79 int exit_counter;
80 };
81
82 struct Playing_ : public msm::front::state_machine_def<Playing_>
83 {
84 template <class Event,class FSM>
85 void on_entry(Event const&,FSM& ) {++entry_counter;}
86 template <class Event,class FSM>
87 void on_exit(Event const&,FSM& ) {++exit_counter;}
88 int entry_counter;
89 int exit_counter;
90 unsigned int start_next_song_counter;
91 unsigned int start_prev_song_guard_counter;
92
93 Playing_():
94 start_next_song_counter(0),
95 start_prev_song_guard_counter(0)
96 {}
97
98 // The list of FSM states
99 struct Song1 : public msm::front::state<>
100 {
101 template <class Event,class FSM>
102 void on_entry(Event const&,FSM& ) {++entry_counter;}
103 template <class Event,class FSM>
104 void on_exit(Event const&,FSM& ) {++exit_counter;}
105 int entry_counter;
106 int exit_counter;
107 };
108 struct Song2 : public msm::front::state<>
109 {
110 template <class Event,class FSM>
111 void on_entry(Event const&,FSM& ) {++entry_counter;}
112 template <class Event,class FSM>
113 void on_exit(Event const&,FSM& ) {++exit_counter;}
114 int entry_counter;
115 int exit_counter;
116 };
117 struct Song3 : public msm::front::state<>
118 {
119 template <class Event,class FSM>
120 void on_entry(Event const&,FSM& ) {++entry_counter;}
121 template <class Event,class FSM>
122 void on_exit(Event const&,FSM& ) {++exit_counter;}
123 int entry_counter;
124 int exit_counter;
125 };
126 // the initial state. Must be defined
127 typedef Song1 initial_state;
128 // transition actions
129 void start_next_song(NextSong const&) {++start_next_song_counter; }
130 void start_prev_song(PreviousSong const&) { }
131 // guard conditions
132 bool start_prev_song_guard(PreviousSong const&) {++start_prev_song_guard_counter;return true; }
133
134 typedef Playing_ pl; // makes transition table cleaner
135 // Transition table for Playing
136 struct transition_table : mpl::vector4<
137 // Start Event Next Action Guard
138 // +---------+-------------+---------+---------------------+----------------------+
139 _row < Song1 , NextSong , Song2 >,
140 row < Song2 , PreviousSong, Song1 , &pl::start_prev_song,&pl::start_prev_song_guard>,
141 a_row < Song2 , NextSong , Song3 , &pl::start_next_song >,
142 g_row < Song3 , PreviousSong, Song2 ,&pl::start_prev_song_guard>
143 // +---------+-------------+---------+---------------------+----------------------+
144 > {};
145 // Replaces the default no-transition response.
146 template <class FSM,class Event>
147 void no_transition(Event const&, FSM&,int)
148 {
149 BOOST_FAIL("no_transition called!");
150 }
151 };
152 // back-end
153 typedef msm::back::state_machine<Playing_,msm::back::ShallowHistory<mpl::vector<end_pause> > > Playing;
154
155 // state not defining any entry or exit
156 struct Paused : public msm::front::state<>
157 {
158 template <class Event,class FSM>
159 void on_entry(Event const&,FSM& ) {++entry_counter;}
160 template <class Event,class FSM>
161 void on_exit(Event const&,FSM& ) {++exit_counter;}
162 int entry_counter;
163 int exit_counter;
164 };
165
166 // the initial state of the player SM. Must be defined
167 typedef Empty initial_state;
168
169 // transition actions
170 void start_playback(play const&) {++start_playback_counter; }
171 void open_drawer(open_close const&) { }
172 void store_cd_info(cd_detected const&) { }
173 void stop_playback(stop const&) { }
174 void pause_playback(pause const&) { }
175 void resume_playback(end_pause const&) { }
176 void stop_and_open(open_close const&) { }
177 void stopped_again(stop const&){}
178 //guards
179 bool can_close_drawer(open_close const&)
180 {
181 ++can_close_drawer_counter;
182 return true;
183 }
184
185
186 typedef player_ p; // makes transition table cleaner
187
188 // Transition table for player
189 struct transition_table : mpl::vector<
190 // Start Event Next Action Guard
191 // +---------+-------------+---------+---------------------+----------------------+
192 a_row < Stopped , play , Playing , &p::start_playback >,
193 a_row < Stopped , open_close , Open , &p::open_drawer >,
194 _row < Stopped , stop , Stopped >,
195 // +---------+-------------+---------+---------------------+----------------------+
196 g_row < Open , open_close , Empty , &p::can_close_drawer >,
197 // +---------+-------------+---------+---------------------+----------------------+
198 a_row < Empty , open_close , Open , &p::open_drawer >,
199 a_row < Empty , cd_detected , Stopped , &p::store_cd_info >,
200 // +---------+-------------+---------+---------------------+----------------------+
201 a_row < Playing , stop , Stopped , &p::stop_playback >,
202 a_row < Playing , pause , Paused , &p::pause_playback >,
203 a_row < Playing , open_close , Open , &p::stop_and_open >,
204 // +---------+-------------+---------+---------------------+----------------------+
205 a_row < Paused , end_pause , Playing , &p::resume_playback >,
206 a_row < Paused , stop , Stopped , &p::stop_playback >,
207 a_row < Paused , open_close , Open , &p::stop_and_open >
208 // +---------+-------------+---------+---------------------+----------------------+
209 > {};
210 // Replaces the default no-transition response.
211 template <class FSM,class Event>
212 void no_transition(Event const& , FSM&,int )
213 {
214 BOOST_FAIL("no_transition called!");
215 }
216 // init counters
217 template <class Event,class FSM>
218 void on_entry(Event const&,FSM& fsm)
219 {
220 fsm.template get_state<player_::Stopped&>().entry_counter=0;
221 fsm.template get_state<player_::Stopped&>().exit_counter=0;
222 fsm.template get_state<player_::Open&>().entry_counter=0;
223 fsm.template get_state<player_::Open&>().exit_counter=0;
224 fsm.template get_state<player_::Empty&>().entry_counter=0;
225 fsm.template get_state<player_::Empty&>().exit_counter=0;
226 fsm.template get_state<player_::Playing&>().entry_counter=0;
227 fsm.template get_state<player_::Playing&>().exit_counter=0;
228 fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song1&>().entry_counter=0;
229 fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song1&>().exit_counter=0;
230 fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song2&>().entry_counter=0;
231 fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song2&>().exit_counter=0;
232 fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song3&>().entry_counter=0;
233 fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song3&>().exit_counter=0;
234 fsm.template get_state<player_::Paused&>().entry_counter=0;
235 fsm.template get_state<player_::Paused&>().exit_counter=0;
236 }
237
238 };
239 // Pick a back-end
240 typedef msm::back::state_machine<player_> player;
241
242// static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
243
244
245 BOOST_AUTO_TEST_CASE( my_test )
246 {
247 player p;
248
249 p.start();
250 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 1,"Empty entry not called correctly");
251
252 p.process_event(evt: open_close());
253 BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Open should be active"); //Open
254 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 1,"Empty exit not called correctly");
255 BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().entry_counter == 1,"Open entry not called correctly");
256
257 p.process_event(evt: open_close());
258 BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active"); //Empty
259 BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 1,"Open exit not called correctly");
260 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 2,"Empty entry not called correctly");
261 BOOST_CHECK_MESSAGE(p.can_close_drawer_counter == 1,"guard not called correctly");
262
263 p.process_event(evt: cd_detected("louie, louie"));
264 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
265 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 2,"Empty exit not called correctly");
266 BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 1,"Stopped entry not called correctly");
267
268 p.process_event(evt: play());
269 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
270 BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 1,"Stopped exit not called correctly");
271 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 1,"Playing entry not called correctly");
272 BOOST_CHECK_MESSAGE(p.start_playback_counter == 1,"action not called correctly");
273 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 0,"Song1 should be active");
274 BOOST_CHECK_MESSAGE(
275 p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().entry_counter == 1,
276 "Song1 entry not called correctly");
277
278 p.process_event(evt: NextSong());
279 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
280 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active");
281 BOOST_CHECK_MESSAGE(
282 p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 1,
283 "Song2 entry not called correctly");
284 BOOST_CHECK_MESSAGE(
285 p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().exit_counter == 1,
286 "Song1 exit not called correctly");
287 BOOST_CHECK_MESSAGE(
288 p.get_state<player_::Playing&>().start_next_song_counter == 0,
289 "submachine action not called correctly");
290
291 p.process_event(evt: NextSong());
292 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
293 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 2,"Song3 should be active");
294 BOOST_CHECK_MESSAGE(
295 p.get_state<player_::Playing&>().get_state<player_::Playing::Song3&>().entry_counter == 1,
296 "Song3 entry not called correctly");
297 BOOST_CHECK_MESSAGE(
298 p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().exit_counter == 1,
299 "Song2 exit not called correctly");
300 BOOST_CHECK_MESSAGE(
301 p.get_state<player_::Playing&>().start_next_song_counter == 1,
302 "submachine action not called correctly");
303
304 p.process_event(evt: PreviousSong());
305 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
306 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active");
307 BOOST_CHECK_MESSAGE(
308 p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 2,
309 "Song2 entry not called correctly");
310 BOOST_CHECK_MESSAGE(
311 p.get_state<player_::Playing&>().get_state<player_::Playing::Song3&>().exit_counter == 1,
312 "Song3 exit not called correctly");
313 BOOST_CHECK_MESSAGE(
314 p.get_state<player_::Playing&>().start_prev_song_guard_counter == 1,
315 "submachine guard not called correctly");
316
317 p.process_event(evt: pause());
318 BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active"); //Paused
319 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 1,"Playing exit not called correctly");
320 BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 1,"Paused entry not called correctly");
321
322 // go back to Playing
323 p.process_event(evt: end_pause());
324 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
325 BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().exit_counter == 1,"Paused exit not called correctly");
326 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 2,"Playing entry not called correctly");
327 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active");
328 BOOST_CHECK_MESSAGE(
329 p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 3,
330 "Song2 entry not called correctly");
331
332 p.process_event(evt: pause());
333 BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active"); //Paused
334 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 2,"Playing exit not called correctly");
335 BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 2,"Paused entry not called correctly");
336
337 p.process_event(evt: stop());
338 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
339 BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().exit_counter == 2,"Paused exit not called correctly");
340 BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 2,"Stopped entry not called correctly");
341
342 p.process_event(evt: stop());
343 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
344 BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 2,"Stopped exit not called correctly");
345 BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 3,"Stopped entry not called correctly");
346
347 p.process_event(evt: play());
348 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 0,"Song1 should be active");
349 BOOST_CHECK_MESSAGE(
350 p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().entry_counter == 2,
351 "Song1 entry not called correctly");
352
353 }
354}
355
356

source code of boost/libs/msm/test/History.cpp