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/mpl/vector/vector30.hpp"
15 #include <boost/msm/back/state_machine.hpp>
16 #include <boost/msm/front/state_machine_def.hpp>
17 #include <boost/msm/back/tools.hpp>
20 namespace msm
= boost::msm
;
22 namespace // Concrete FSM implementation
31 struct PreviousSong
{};
35 struct error_found
{};
38 // Flags. Allow information about a property of the current state
39 struct PlayingPaused
{};
41 struct FirstSongPlaying
{};
43 // A "complicated" event type that carries some data.
46 cd_detected(std::string name
)
56 void visit_state(T
* astate
,int i
)
58 std::cout
<< "visiting state:" << typeid(*astate
).name()
59 << " with data:" << i
<< std::endl
;
62 // overwrite of the base state (not default)
63 struct my_visitable_state
65 // signature of the accept function
66 typedef msm::back::args
<void,SomeVisitor
&,int> accept_sig
;
68 // we also want polymorphic states
69 virtual ~my_visitable_state() {}
70 // default implementation for states who do not need to be visited
71 void accept(SomeVisitor
&,int) const {}
74 // Concrete FSM implementation
75 struct player_
: public msm::front::state_machine_def
<player_
,my_visitable_state
>
77 template <class Event
,class FSM
>
78 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: player" << std::endl
;}
79 // The list of FSM states
80 struct Empty
: public msm::front::state
<my_visitable_state
>
82 typedef mpl::vector
<play
> deferred_events
;
83 // every (optional) entry/exit methods get the event packed as boost::any. Not useful very often.
84 template <class Event
,class FSM
>
85 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Empty" << std::endl
;}
86 template <class Event
,class FSM
>
87 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Empty" << std::endl
;}
88 void accept(SomeVisitor
& vis
,int i
) const
90 vis
.visit_state(this,i
);
93 struct Open
: public msm::front::state
<my_visitable_state
>
95 typedef mpl::vector1
<CDLoaded
> flag_list
;
96 typedef mpl::vector
<play
> deferred_events
;
97 template <class Event
,class FSM
>
98 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Open" << std::endl
;}
99 template <class Event
,class FSM
>
100 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Open" << std::endl
;}
101 void accept(SomeVisitor
& vis
,int i
) const
103 vis
.visit_state(this,i
);
106 // a state needing a pointer to the containing state machine
107 // and using for this the non-default policy
108 // if policy used, set_sm_ptr is needed
109 struct Stopped
: public msm::front::state
<my_visitable_state
>
111 // when stopped, the CD is loaded
112 typedef mpl::vector1
<CDLoaded
> flag_list
;
113 template <class Event
,class FSM
>
114 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Stopped" << std::endl
;}
115 template <class Event
,class FSM
>
116 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Stopped" << std::endl
;}
118 // the player state machine contains a state which is himself a state machine
119 // it demonstrates Shallow History: if the state gets activated with end_pause
120 // then it will remember the last active state and reactivate it
121 // also possible: AlwaysHistory, the last active state will always be reactivated
122 // or NoHistory, always restart from the initial state
123 struct Playing_
: public msm::front::state_machine_def
<Playing_
,my_visitable_state
>
125 // when playing, the CD is loaded and we are in either pause or playing (duh)
126 typedef mpl::vector2
<PlayingPaused
,CDLoaded
> flag_list
;
127 template <class Event
,class FSM
>
128 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Playing" << std::endl
;}
129 template <class Event
,class FSM
>
130 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Playing" << std::endl
;}
131 void accept(SomeVisitor
& vis
,int i
) const
133 // note that visiting will recursively visit sub-states
134 vis
.visit_state(this,i
);
136 // The list of FSM states
137 // the Playing state machine contains a state which is himself a state machine
138 // so we have a SM containing a SM containing a SM
139 struct Song1_
: public msm::front::state_machine_def
<Song1_
,my_visitable_state
>
141 typedef mpl::vector1
<FirstSongPlaying
> flag_list
;
142 template <class Event
,class FSM
>
143 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: First song" << std::endl
;}
144 template <class Event
,class FSM
>
145 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: First Song" << std::endl
;}
146 void accept(SomeVisitor
& vis
,int i
) const
148 vis
.visit_state(this,i
);
150 struct LightOn
: public msm::front::state
<my_visitable_state
>
152 template <class Event
,class FSM
>
153 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: LightOn" << std::endl
;}
154 template <class Event
,class FSM
>
155 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: LightOn" << std::endl
;}
157 struct LightOff
: public msm::front::state
<my_visitable_state
>
159 template <class Event
,class FSM
>
160 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: LightOff" << std::endl
;}
161 template <class Event
,class FSM
>
162 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: LightOff" << std::endl
;}
164 // the initial state. Must be defined
165 typedef LightOn initial_state
;
166 // transition actions
167 void turn_light_off(ThreeSec
const&) { std::cout
<< "3s off::turn light off\n"; }
170 typedef Song1_ s
; // makes transition table cleaner
171 // Transition table for Song1
172 struct transition_table
: mpl::vector1
<
173 // Start Event Next Action Guard
174 // +---------+-------------+---------+---------------------+----------------------+
175 a_row
< LightOn
, ThreeSec
, LightOff
, &s::turn_light_off
>
176 // +---------+-------------+---------+---------------------+----------------------+
178 // Replaces the default no-transition response.
179 template <class FSM
,class Event
>
180 void no_transition(Event
const& e
, FSM
&,int state
)
182 std::cout
<< "no transition from state " << state
183 << " on event " << typeid(e
).name() << std::endl
;
186 typedef msm::back::state_machine
<Song1_
> Song1
;
188 struct Song2
: public msm::front::state
<my_visitable_state
>
190 template <class Event
,class FSM
>
191 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: Second song" << std::endl
;}
192 template <class Event
,class FSM
>
193 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: Second Song" << std::endl
;}
195 struct Song3
: public msm::front::state
<my_visitable_state
>
197 template <class Event
,class FSM
>
198 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: Third song" << std::endl
;}
199 template <class Event
,class FSM
>
200 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: Third Song" << std::endl
;}
202 // the initial state. Must be defined
203 typedef Song1 initial_state
;
204 // transition actions
205 void start_next_song(NextSong
const&) { std::cout
<< "Playing::start_next_song\n"; }
206 void start_prev_song(PreviousSong
const&) { std::cout
<< "Playing::start_prev_song\n"; }
209 typedef Playing_ pl
; // makes transition table cleaner
210 // Transition table for Playing
211 struct transition_table
: mpl::vector4
<
212 // Start Event Next Action Guard
213 // +---------+-------------+---------+---------------------+----------------------+
214 a_row
< Song1
, NextSong
, Song2
, &pl::start_next_song
>,
215 a_row
< Song2
, PreviousSong
, Song1
, &pl::start_prev_song
>,
216 a_row
< Song2
, NextSong
, Song3
, &pl::start_next_song
>,
217 a_row
< Song3
, PreviousSong
, Song2
, &pl::start_prev_song
>
218 // +---------+-------------+---------+---------------------+----------------------+
220 // Replaces the default no-transition response.
221 template <class FSM
,class Event
>
222 void no_transition(Event
const& e
, FSM
&,int state
)
224 std::cout
<< "no transition from state " << state
225 << " on event " << typeid(e
).name() << std::endl
;
228 typedef msm::back::state_machine
<Playing_
,msm::back::ShallowHistory
<mpl::vector
<end_pause
> > > Playing
;
230 // the player state machine contains a state which is himself a state machine (2 of them, Playing and Paused)
231 struct Paused_
: public msm::front::state_machine_def
<Paused_
,my_visitable_state
>
233 typedef mpl::vector2
<PlayingPaused
,CDLoaded
> flag_list
;
234 template <class Event
,class FSM
>
235 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Paused" << std::endl
;}
236 template <class Event
,class FSM
>
237 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Paused" << std::endl
;}
239 // The list of FSM states
240 struct StartBlinking
: public msm::front::state
<my_visitable_state
>
242 template <class Event
,class FSM
>
243 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: StartBlinking" << std::endl
;}
244 template <class Event
,class FSM
>
245 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: StartBlinking" << std::endl
;}
247 struct StopBlinking
: public msm::front::state
<my_visitable_state
>
249 template <class Event
,class FSM
>
250 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: StopBlinking" << std::endl
;}
251 template <class Event
,class FSM
>
252 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: StopBlinking" << std::endl
;}
254 // the initial state. Must be defined
255 typedef StartBlinking initial_state
;
256 // transition actions
257 void start_blinking(TenSec
const&) { std::cout
<< "Paused::start_blinking\n"; }
258 void stop_blinking(TenSec
const&) { std::cout
<< "Paused::stop_blinking\n"; }
261 typedef Paused_ pa
; // makes transition table cleaner
263 struct transition_table
: mpl::vector2
<
264 // Start Event Next Action Guard
265 // +---------------+-------------+--------------+---------------------+----------------------+
266 a_row
< StartBlinking
, TenSec
, StopBlinking
, &pa::stop_blinking
>,
267 a_row
< StopBlinking
, TenSec
, StartBlinking
, &pa::start_blinking
>
268 // +---------------+-------------+---------------+--------------------+----------------------+
270 // Replaces the default no-transition response.
271 template <class FSM
,class Event
>
272 void no_transition(Event
const& e
, FSM
&,int state
)
274 std::cout
<< "no transition from state " << state
275 << " on event " << typeid(e
).name() << std::endl
;
278 typedef msm::back::state_machine
<Paused_
> Paused
;
280 struct SleepMode
: public msm::front::state
<my_visitable_state
>
282 }; // dummy state just to test the automatic id generation
284 struct AllOk
: public msm::front::state
<my_visitable_state
>
286 template <class Event
,class FSM
>
287 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: AllOk" << std::endl
;}
288 template <class Event
,class FSM
>
289 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: AllOk" << std::endl
;}
291 struct ErrorMode
: //public terminate_state<>
292 public msm::front::interrupt_state
<end_error
,my_visitable_state
>
294 template <class Event
,class FSM
>
295 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: ErrorMode" << std::endl
;}
296 template <class Event
,class FSM
>
297 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: ErrorMode" << std::endl
;}
300 // the initial state of the player SM. Must be defined
301 typedef mpl::vector
<Empty
,AllOk
> initial_state
;
302 //typedef Empty initial_state; // this is to have only one active state
304 // transition actions
305 void start_playback(play
const&) { std::cout
<< "player::start_playback\n"; }
306 void open_drawer(open_close
const&) { std::cout
<< "player::open_drawer\n"; }
307 void close_drawer(open_close
const&) { std::cout
<< "player::close_drawer\n"; }
308 void store_cd_info(cd_detected
const&)
310 std::cout
<< "player::store_cd_info\n";
311 // generate another event to test the queue
312 //process_event(play());
314 void stop_playback(stop
const&) { std::cout
<< "player::stop_playback\n"; }
315 void pause_playback(pause
const&) { std::cout
<< "player::pause_playback\n"; }
316 void resume_playback(end_pause
const&) { std::cout
<< "player::resume_playback\n"; }
317 void stop_and_open(open_close
const&) { std::cout
<< "player::stop_and_open\n"; }
318 void stopped_again(stop
const&){std::cout
<< "player::stopped_again\n";}
319 void start_sleep(go_sleep
const&) { }
320 void report_error(error_found
const&) {std::cout
<< "player::report_error\n";}
321 void report_end_error(end_error
const&) {std::cout
<< "player::report_end_error\n";}
325 typedef player_ p
; // makes transition table cleaner
327 // Transition table for player
328 struct transition_table
: mpl::vector
<
329 // Start Event Next Action Guard
330 // +---------+-------------+---------+---------------------+----------------------+
331 a_row
< Stopped
, play
, Playing
, &p::start_playback
>,
332 a_row
< Stopped
, open_close
, Open
, &p::open_drawer
>,
333 a_row
< Stopped
, stop
, Stopped
, &p::stopped_again
>,
334 // +---------+-------------+---------+---------------------+----------------------+
335 a_row
< Open
, open_close
, Empty
, &p::close_drawer
>,
336 // +---------+-------------+---------+---------------------+----------------------+
337 a_row
< Empty
, open_close
, Open
, &p::open_drawer
>,
338 a_row
< Empty
, cd_detected
, Stopped
, &p::store_cd_info
>,
339 // +---------+-------------+---------+---------------------+----------------------+
340 a_row
< Playing
, stop
, Stopped
, &p::stop_playback
>,
341 a_row
< Playing
, pause
, Paused
, &p::pause_playback
>,
342 a_row
< Playing
, open_close
, Open
, &p::stop_and_open
>,
343 // +---------+-------------+---------+---------------------+----------------------+
344 a_row
< Paused
, end_pause
, Playing
, &p::resume_playback
>,
345 a_row
< Paused
, stop
, Stopped
, &p::stop_playback
>,
346 a_row
< Paused
, open_close
, Open
, &p::stop_and_open
>,
347 a_row
< Paused
, go_sleep
,SleepMode
, &p::start_sleep
>,
348 // +---------+-------------+---------+---------------------+----------------------+
349 a_row
< AllOk
, error_found
,ErrorMode
, &p::report_error
>,
350 a_row
<ErrorMode
,end_error
,AllOk
, &p::report_end_error
>
351 // +---------+-------------+---------+---------------------+----------------------+
354 // Replaces the default no-transition response.
355 template <class FSM
,class Event
>
356 void no_transition(Event
const& e
, FSM
&,int state
)
358 std::cout
<< "no transition from state " << state
359 << " on event " << typeid(e
).name() << std::endl
;
364 typedef msm::back::state_machine
<player_
> player
;
367 // Testing utilities.
370 void pstate(player
const& p
)
372 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused","AllOk","ErrorMode","SleepMode" };
373 for (unsigned int i
=0;i
<player::nr_regions::value
;++i
)
375 std::cout
<< " -> " << state_names
[p
.current_state()[i
]] << std::endl
;
381 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
383 std::cout
<< "CDLoaded active:" << std::boolalpha
<< p
.is_flag_active
<CDLoaded
>() << std::endl
; //=> false (no CD yet)
385 // test deferred event
386 // deferred in Empty and Open, will be handled only after event cd_detected
387 p
.process_event(play());
389 // go to Open, call on_exit on Empty, then action, then on_entry on Open
390 p
.process_event(open_close()); pstate(p
);
391 // visiting Paused and AllOk, but only Paused cares
393 p
.visit_current_states(boost::ref(vis
),1);
394 p
.process_event(open_close()); pstate(p
);
395 // visiting Empty and AllOk, but only Empty cares
396 p
.visit_current_states(boost::ref(vis
),2);
399 p
.process_event(cd_detected("louie, louie"));
400 // no need to call play() as the previous event does it in its action method
401 //p.process_event(play());
402 // at this point, Play is active, along FirstSong and LightOn
404 // visiting Playing+Song1 and AllOk, but only Playing+Song1 care
405 p
.visit_current_states(boost::ref(vis
),3);
407 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> true
408 // call on_exit on LightOn,FirstSong,Play like stated in the UML spec.
409 // and of course on_entry on Paused and StartBlinking
410 p
.process_event(pause()); pstate(p
);
411 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> true
412 // forward events to Paused
413 p
.process_event(TenSec());
414 p
.process_event(TenSec());
415 // go back to Playing
416 p
.process_event(end_pause()); pstate(p
);
417 std::cout
<< "FirstSong active:" << std::boolalpha
<< p
.is_flag_active
<FirstSongPlaying
>() << std::endl
; //=> true
418 p
.process_event(ThreeSec()); pstate(p
);
419 p
.process_event(NextSong());pstate(p
);
420 // We are now in second song, Flag inactive
421 std::cout
<< "FirstSong active:" << std::boolalpha
<< p
.is_flag_active
<FirstSongPlaying
>() << std::endl
;//=> false
422 // visiting Playing+Song2 and AllOk, but only Playing cares
423 p
.visit_current_states(boost::ref(vis
),4);
425 p
.process_event(NextSong());pstate(p
);
427 p
.process_event(PreviousSong());pstate(p
);
429 p
.process_event(pause()); pstate(p
);
430 // go back to Playing
431 // but end_pause is an event activating the History
432 // => keep the last active State (SecondSong)
433 p
.process_event(end_pause()); pstate(p
);
434 // test of an event from a state to itself. According to UML spec, call again exit/entry from Stopped
435 p
.process_event(stop()); pstate(p
);
436 p
.process_event(stop()); pstate(p
);
437 std::cout
<< "CDLoaded active:" << std::boolalpha
<< p
.is_flag_active
<CDLoaded
>() << std::endl
;//=> true
438 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> false
439 std::cout
<< "FirstSong active:" << std::boolalpha
<< p
.is_flag_active
<FirstSongPlaying
>() << std::endl
;//=> false
440 std::cout
<< "CDLoaded active with AND:" << std::boolalpha
<< p
.is_flag_active
<CDLoaded
,player::Flag_AND
>() << std::endl
;//=> false
441 std::cout
<< "CDLoaded active with OR:" << std::boolalpha
<< p
.is_flag_active
<CDLoaded
,player::Flag_OR
>() << std::endl
;//=> true
443 // go back to Playing
444 // but play is not leading to Shallow History => do not remember the last active State (SecondSong)
445 // and activate again FirstSong and LightOn
446 p
.process_event(play()); pstate(p
);
447 p
.process_event(error_found()); pstate(p
);
449 // try generating more events
450 std::cout
<< "Trying to generate another event" << std::endl
; // will not work, fsm is terminated or interrupted
451 p
.process_event(NextSong());pstate(p
);
453 std::cout
<< "Trying to end the error" << std::endl
; // will work only if ErrorMode is interrupt state
454 p
.process_event(end_error());pstate(p
);
455 std::cout
<< "Trying to generate another event" << std::endl
; // will work only if ErrorMode is interrupt state
456 p
.process_event(NextSong());pstate(p
);
458 std::cout
<< "Simulate error. Event play is not valid" << std::endl
;
459 p
.process_event(play()); pstate(p
);