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

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