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)
13 #include <boost/msm/back/state_machine.hpp>
15 #include <boost/msm/front/state_machine_def.hpp>
16 #define BOOST_TEST_MODULE MyTest
17 #include <boost/test/unit_test.hpp>
18 // include headers that implement a archive in simple text format
19 #include <boost/archive/text_oarchive.hpp>
20 #include <boost/archive/text_iarchive.hpp>
21 #include <boost/serialization/tracking.hpp>
25 namespace msm = boost::msm;
26 namespace mpl = boost::mpl;
37 struct PreviousSong {};
40 cd_detected(std::string name)
46 // front-end: define the FSM structure
47 struct player_ : public msm::front::state_machine_def<player_>
49 unsigned int start_playback_counter;
50 unsigned int can_close_drawer_counter;
53 start_playback_counter(0),
54 can_close_drawer_counter(0)
56 // The list of FSM states
57 struct Empty : public msm::front::state<>
59 template <class Event,class FSM>
60 void on_entry(Event const&,FSM& ) {++entry_counter;}
61 template <class Event,class FSM>
62 void on_exit(Event const&,FSM& ) {++exit_counter;}
66 struct Open : public msm::front::state<>
68 template <class Event,class FSM>
69 void on_entry(Event const&,FSM& ) {++entry_counter;}
70 template <class Event,class FSM>
71 void on_exit(Event const&,FSM& ) {++exit_counter;}
76 // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
77 struct Stopped : public msm::front::state<>
79 template <class Event,class FSM>
80 void on_entry(Event const&,FSM& ) {++entry_counter;}
81 template <class Event,class FSM>
82 void on_exit(Event const&,FSM& ) {++exit_counter;}
87 struct Playing_ : public msm::front::state_machine_def<Playing_>
89 template <class Event,class FSM>
90 void on_entry(Event const&,FSM& ) {++entry_counter;}
91 template <class Event,class FSM>
92 void on_exit(Event const&,FSM& ) {++exit_counter;}
95 unsigned int start_next_song_counter;
96 unsigned int start_prev_song_guard_counter;
99 start_next_song_counter(0),
100 start_prev_song_guard_counter(0)
103 // The list of FSM states
104 struct Song1 : public msm::front::state<>
106 template <class Event,class FSM>
107 void on_entry(Event const&,FSM& ) {++entry_counter;}
108 template <class Event,class FSM>
109 void on_exit(Event const&,FSM& ) {++exit_counter;}
113 struct Song2 : public msm::front::state<>
115 template <class Event,class FSM>
116 void on_entry(Event const&,FSM& ) {++entry_counter;}
117 template <class Event,class FSM>
118 void on_exit(Event const&,FSM& ) {++exit_counter;}
122 struct Song3 : public msm::front::state<>
124 template <class Event,class FSM>
125 void on_entry(Event const&,FSM& ) {++entry_counter;}
126 template <class Event,class FSM>
127 void on_exit(Event const&,FSM& ) {++exit_counter;}
131 // the initial state. Must be defined
132 typedef Song1 initial_state;
133 // transition actions
134 void start_next_song(NextSong const&) {++start_next_song_counter; }
135 void start_prev_song(PreviousSong const&) { }
137 bool start_prev_song_guard(PreviousSong const&) {++start_prev_song_guard_counter;return true; }
139 typedef Playing_ pl; // makes transition table cleaner
140 // Transition table for Playing
141 struct transition_table : mpl::vector4<
142 // Start Event Next Action Guard
143 // +---------+-------------+---------+---------------------+----------------------+
144 _row < Song1 , NextSong , Song2 >,
145 row < Song2 , PreviousSong, Song1 , &pl::start_prev_song,&pl::start_prev_song_guard>,
146 a_row < Song2 , NextSong , Song3 , &pl::start_next_song >,
147 g_row < Song3 , PreviousSong, Song2 ,&pl::start_prev_song_guard>
148 // +---------+-------------+---------+---------------------+----------------------+
150 // Replaces the default no-transition response.
151 template <class FSM,class Event>
152 void no_transition(Event const&, FSM&,int)
154 BOOST_FAIL("no_transition called!");
158 typedef msm::back::state_machine<Playing_,msm::back::ShallowHistory<mpl::vector<end_pause> > > Playing;
160 // state not defining any entry or exit
161 struct Paused : public msm::front::state<>
163 template <class Event,class FSM>
164 void on_entry(Event const&,FSM& ) {++entry_counter;}
165 template <class Event,class FSM>
166 void on_exit(Event const&,FSM& ) {++exit_counter;}
171 // the initial state of the player SM. Must be defined
172 typedef Empty initial_state;
174 // transition actions
175 void start_playback(play const&) {++start_playback_counter; }
176 void open_drawer(open_close const&) { }
177 void store_cd_info(cd_detected const&) { }
178 void stop_playback(stop const&) { }
179 void pause_playback(pause const&) { }
180 void resume_playback(end_pause const&) { }
181 void stop_and_open(open_close const&) { }
182 void stopped_again(stop const&) {}
184 bool can_close_drawer(open_close const&)
186 ++can_close_drawer_counter;
191 typedef player_ p; // makes transition table cleaner
193 // Transition table for player
194 struct transition_table : mpl::vector<
195 // Start Event Next Action Guard
196 // +---------+-------------+---------+---------------------+----------------------+
197 a_row < Stopped , play , Playing , &p::start_playback >,
198 a_row < Stopped , open_close , Open , &p::open_drawer >,
199 _row < Stopped , stop , Stopped >,
200 // +---------+-------------+---------+---------------------+----------------------+
201 g_row < Open , open_close , Empty , &p::can_close_drawer >,
202 // +---------+-------------+---------+---------------------+----------------------+
203 a_row < Empty , open_close , Open , &p::open_drawer >,
204 a_row < Empty , cd_detected , Stopped , &p::store_cd_info >,
205 // +---------+-------------+---------+---------------------+----------------------+
206 a_row < Playing , stop , Stopped , &p::stop_playback >,
207 a_row < Playing , pause , Paused , &p::pause_playback >,
208 a_row < Playing , open_close , Open , &p::stop_and_open >,
209 // +---------+-------------+---------+---------------------+----------------------+
210 a_row < Paused , end_pause , Playing , &p::resume_playback >,
211 a_row < Paused , stop , Stopped , &p::stop_playback >,
212 a_row < Paused , open_close , Open , &p::stop_and_open >
213 // +---------+-------------+---------+---------------------+----------------------+
215 // Replaces the default no-transition response.
216 template <class FSM,class Event>
217 void no_transition(Event const& e, FSM&,int state)
219 BOOST_FAIL("no_transition called!");
222 template <class Event,class FSM>
223 void on_entry(Event const&,FSM& fsm)
225 fsm.template get_state<player_::Stopped&>().entry_counter=0;
226 fsm.template get_state<player_::Stopped&>().exit_counter=0;
227 fsm.template get_state<player_::Open&>().entry_counter=0;
228 fsm.template get_state<player_::Open&>().exit_counter=0;
229 fsm.template get_state<player_::Empty&>().entry_counter=0;
230 fsm.template get_state<player_::Empty&>().exit_counter=0;
231 fsm.template get_state<player_::Playing&>().entry_counter=0;
232 fsm.template get_state<player_::Playing&>().exit_counter=0;
233 fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song1&>().entry_counter=0;
234 fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song1&>().exit_counter=0;
235 fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song2&>().entry_counter=0;
236 fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song2&>().exit_counter=0;
237 fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song3&>().entry_counter=0;
238 fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song3&>().exit_counter=0;
239 fsm.template get_state<player_::Paused&>().entry_counter=0;
240 fsm.template get_state<player_::Paused&>().exit_counter=0;
245 typedef msm::back::state_machine<player_> player;
247 // static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
250 BOOST_AUTO_TEST_CASE( my_test )
255 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 1,"Empty entry not called correctly");
257 p.process_event(open_close());
258 BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Open should be active"); //Open
259 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 1,"Empty exit not called correctly");
260 BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().entry_counter == 1,"Open entry not called correctly");
262 p.process_event(open_close());
263 BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active"); //Empty
264 BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 1,"Open exit not called correctly");
265 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 2,"Empty entry not called correctly");
266 BOOST_CHECK_MESSAGE(p.can_close_drawer_counter == 1,"guard not called correctly");
268 p.process_event(cd_detected("louie, louie"));
269 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
270 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 2,"Empty exit not called correctly");
271 BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 1,"Stopped entry not called correctly");
273 p.process_event(play());
274 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
275 BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 1,"Stopped exit not called correctly");
276 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 1,"Playing entry not called correctly");
277 BOOST_CHECK_MESSAGE(p.start_playback_counter == 1,"action not called correctly");
278 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 0,"Song1 should be active");
280 p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().entry_counter == 1,
281 "Song1 entry not called correctly");
283 p.process_event(NextSong());
284 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
285 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active");
287 p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 1,
288 "Song2 entry not called correctly");
290 p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().exit_counter == 1,
291 "Song1 exit not called correctly");
293 p.get_state<player_::Playing&>().start_next_song_counter == 0,
294 "submachine action not called correctly");
296 p.process_event(NextSong());
297 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
298 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 2,"Song3 should be active");
300 p.get_state<player_::Playing&>().get_state<player_::Playing::Song3&>().entry_counter == 1,
301 "Song3 entry not called correctly");
303 p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().exit_counter == 1,
304 "Song2 exit not called correctly");
306 p.get_state<player_::Playing&>().start_next_song_counter == 1,
307 "submachine action not called correctly");
309 p.process_event(PreviousSong());
310 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
311 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active");
313 p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 2,
314 "Song2 entry not called correctly");
316 p.get_state<player_::Playing&>().get_state<player_::Playing::Song3&>().exit_counter == 1,
317 "Song3 exit not called correctly");
319 p.get_state<player_::Playing&>().start_prev_song_guard_counter == 1,
320 "submachine guard not called correctly");
322 p.process_event(pause());
323 BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active"); //Paused
324 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 1,"Playing exit not called correctly");
325 BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 1,"Paused entry not called correctly");
327 std::ofstream ofs("fsm.txt");
328 // save fsm to archive (current state is Pause, Playing is in Song2)
330 boost::archive::text_oarchive oa(ofs);
331 // write class instance to archive
334 // reload fsm in state Open
337 // create and open an archive for input
338 std::ifstream ifs("fsm.txt");
339 boost::archive::text_iarchive ia(ifs);
340 // read class state from archive
343 // go back to Playing
344 p2.process_event(end_pause());
345 BOOST_CHECK_MESSAGE(p2.current_state()[0] == 3,"Playing should be active"); //Playing
346 BOOST_CHECK_MESSAGE(p2.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active");
348 p2.process_event(pause());
349 BOOST_CHECK_MESSAGE(p2.current_state()[0] == 4,"Paused should be active"); //Paused
351 p2.process_event(stop());
352 BOOST_CHECK_MESSAGE(p2.current_state()[0] == 0,"Stopped should be active"); //Stopped
354 p2.process_event(stop());
355 BOOST_CHECK_MESSAGE(p2.current_state()[0] == 0,"Stopped should be active"); //Stopped
357 p2.process_event(play());
358 BOOST_CHECK_MESSAGE(p2.get_state<player_::Playing&>().current_state()[0] == 0,"Song1 should be active");
361 // eliminate object tracking (even if serialized through a pointer)
362 // at the risk of a programming error creating duplicate objects.
363 // this is to get rid of warning because p is not const
364 BOOST_CLASS_TRACKING(player, boost::serialization::track_never)