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)
12 #include "boost/mpl/vector/vector30.hpp"
15 #include <boost/msm/back/state_machine.hpp>
17 #include <boost/msm/front/state_machine_def.hpp>
18 #include <boost/msm/front/functor_row.hpp>
19 #include <boost/msm/front/internal_row.hpp>
21 namespace msm
= boost::msm
;
22 namespace mpl
= boost::mpl
;
23 using namespace msm::front
;
32 struct internal_event
{};
35 struct PreviousSong
{};
38 // A "complicated" event type that carries some data.
46 cd_detected(std::string name
, DiskTypeEnum diskType
)
52 DiskTypeEnum disc_type
;
55 // front-end: define the FSM structure
56 struct player_
: public msm::front::state_machine_def
<player_
>
59 void start_playback(play
const&) { std::cout
<< "player::start_playback\n"; }
60 void open_drawer(open_close
const&) { std::cout
<< "player::open_drawer\n"; }
61 void close_drawer(open_close
const&) { std::cout
<< "player::close_drawer\n"; }
62 void store_cd_info(cd_detected
const&) { std::cout
<< "player::store_cd_info\n"; }
63 void stop_playback(stop
const&) { std::cout
<< "player::stop_playback\n"; }
64 void pause_playback(pause
const&) { std::cout
<< "player::pause_playback\n"; }
65 void resume_playback(end_pause
const&) { std::cout
<< "player::resume_playback\n"; }
66 void stop_and_open(open_close
const&) { std::cout
<< "player::stop_and_open\n"; }
67 void stopped_again(stop
const&) {std::cout
<< "player::stopped_again\n";}
69 bool good_disk_format(cd_detected
const& evt
)
71 // to test a guard condition, let's say we understand only CDs, not DVD
72 if (evt
.disc_type
!= DISK_CD
)
74 std::cout
<< "wrong disk, sorry" << std::endl
;
79 // transitions internal to Empty
80 void internal_action(cd_detected
const&){ std::cout
<< "Empty::internal action\n"; }
81 bool internal_guard(cd_detected
const&)
83 std::cout
<< "Empty::internal guard\n";
86 void internal_action(internal_event
const&){ std::cout
<< "Playing::internal action\n"; }
87 bool internal_guard(internal_event
const&)
89 std::cout
<< "Playing::internal guard\n";
93 // The list of FSM states
94 struct Empty
: public msm::front::state
<>
96 // every (optional) entry/exit methods get the event passed.
97 template <class Event
,class FSM
>
98 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Empty" << std::endl
;}
99 template <class Event
,class FSM
>
100 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Empty" << std::endl
;}
102 struct internal_guard_fct
104 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
105 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
107 std::cout
<< "Empty::internal guard functor\n";
111 struct internal_action_fct
113 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
114 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
116 std::cout
<< "Empty::internal action functor" << std::endl
;
119 void internal_action(to_ignore
const&) { std::cout
<< "Empty::(almost)ignoring event\n"; }
120 // Transition table for Empty
121 struct internal_transition_table
: mpl::vector
<
122 // Start Event Next Action Guard
123 Row
< Empty
, cd_detected
, none
, internal_action_fct
,internal_guard_fct
>,
124 Internal
< cd_detected
, internal_action_fct
,internal_guard_fct
>,
125 a_internal
< to_ignore
, Empty
,&Empty::internal_action
>
126 // +---------+-------------+----------+------------------------+----------------------+
129 struct Open
: public msm::front::state
<>
131 template <class Event
,class FSM
>
132 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Open" << std::endl
;}
133 template <class Event
,class FSM
>
134 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Open" << std::endl
;}
137 struct Stopped
: public msm::front::state
<>
139 template <class Event
,class FSM
>
140 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Stopped" << std::endl
;}
141 template <class Event
,class FSM
>
142 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Stopped" << std::endl
;}
145 struct Playing_
: public msm::front::state_machine_def
<Playing_
>
147 // when playing, the CD is loaded and we are in either pause or playing (duh)
148 template <class Event
,class FSM
>
149 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Playing" << std::endl
;}
150 template <class Event
,class FSM
>
151 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Playing" << std::endl
;}
153 // The list of FSM states
154 struct Song1
: public msm::front::state
<>
156 template <class Event
,class FSM
>
157 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: First song" << std::endl
;}
158 template <class Event
,class FSM
>
159 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: First Song" << std::endl
;}
162 struct Song2
: public msm::front::state
<>
164 template <class Event
,class FSM
>
165 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: Second song" << std::endl
;}
166 template <class Event
,class FSM
>
167 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: Second Song" << std::endl
;}
169 struct Song3
: public msm::front::state
<>
171 template <class Event
,class FSM
>
172 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: Third song" << std::endl
;}
173 template <class Event
,class FSM
>
174 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: Third Song" << std::endl
;}
176 // the initial state. Must be defined
177 typedef Song1 initial_state
;
178 // transition actions
179 void start_next_song(NextSong
const&){std::cout
<< "Playing: start_next_song" << std::endl
;}
180 void start_prev_song(PreviousSong
const&){std::cout
<< "Playing: start_prev_song" << std::endl
;}
182 struct playing_internal_guard
184 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
185 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
187 std::cout
<< "Playing::internal guard fct\n";
191 struct playing_false_guard
193 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
194 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
196 std::cout
<< "Playing::false guard\n";
200 struct playing_internal_fct
202 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
203 void operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
205 std::cout
<< "Playing::internal fct\n";
208 typedef Playing_ pl
; // makes transition table cleaner
209 // Transition table for Playing
210 struct transition_table
: mpl::vector4
<
211 // Start Event Next Action Guard
212 // +---------+---------------+---------+---------------------+----------------------+
213 a_row
< Song1
, NextSong
, Song2
, &pl::start_next_song
>,
214 a_row
< Song2
, PreviousSong
, Song1
, &pl::start_prev_song
>,
215 a_row
< Song2
, NextSong
, Song3
, &pl::start_next_song
>,
216 a_row
< Song3
, PreviousSong
, Song2
, &pl::start_prev_song
>
217 // +---------+-------------+---------+---------------------+----------------------+
219 // Internal transition table for Playing
220 // +---------+----------------+---------+---------------------+-----------------------+
221 struct internal_transition_table
: mpl::vector
<
222 // normal internal transition
223 // Start Event Next Action Guard
224 Internal
< internal_event
, playing_internal_fct
,playing_internal_guard
>,
225 // conflict between internal and the external defined above
226 Internal
< PreviousSong
, playing_internal_fct
,playing_false_guard
>,
227 internal
< internal_event
, player_
,&player_::internal_action
,
228 player_
,&player_::internal_guard
>
229 // +---------+----------------+---------+---------------------+-----------------------+
232 // Replaces the default no-transition response.
233 template <class FSM
,class Event
>
234 void no_transition(Event
const& e
, FSM
&,int state
)
236 std::cout
<< "no transition from state " << state
237 << " on event " << typeid(e
).name() << std::endl
;
241 typedef msm::back::state_machine
<Playing_
> Playing
;
243 // state not defining any entry or exit
244 struct Paused
: public msm::front::state
<>
248 // the initial state of the player SM. Must be defined
249 typedef Empty initial_state
;
251 typedef player_ p
; // makes transition table cleaner
253 // Transition table for player
254 struct transition_table
: mpl::vector
<
255 // Start Event Next Action Guard
256 // +---------+-------------+---------+---------------------+----------------------+
257 a_row
< Stopped
, play
, Playing
, &p::start_playback
>,
258 a_row
< Stopped
, open_close
, Open
, &p::open_drawer
>,
259 _row
< Stopped
, stop
, Stopped
>,
260 // +---------+-------------+---------+---------------------+----------------------+
261 a_row
< Open
, open_close
, Empty
, &p::close_drawer
>,
262 // +---------+-------------+---------+---------------------+----------------------+
263 a_row
< Empty
, open_close
, Open
, &p::open_drawer
>,
264 // conflict between a normal and 2 internal transitions (irow/g_irow)
265 // + a state-defined internals defined 2 ways (see Empty)
266 row
< Empty
, cd_detected
, Stopped
, &p::store_cd_info
,&p::good_disk_format
>,
267 irow
< Empty
, cd_detected
, &p::internal_action
,&p::internal_guard
>,
268 g_irow
< Empty
, cd_detected
,&p::internal_guard
>,
269 // +---------+-------------+---------+---------------------+----------------------+
270 a_row
< Playing
, stop
, Stopped
, &p::stop_playback
>,
271 a_row
< Playing
, pause
, Paused
, &p::pause_playback
>,
272 a_row
< Playing
, open_close
, Open
, &p::stop_and_open
>,
273 // +---------+-------------+---------+---------------------+----------------------+
274 a_row
< Paused
, end_pause
, Playing
, &p::resume_playback
>,
275 a_row
< Paused
, stop
, Stopped
, &p::stop_playback
>,
276 a_row
< Paused
, open_close
, Open
, &p::stop_and_open
>
277 // +---------+-------------+---------+---------------------+----------------------+
279 // Replaces the default no-transition response.
280 template <class FSM
,class Event
>
281 void no_transition(Event
const& e
, FSM
&,int state
)
283 std::cout
<< "no transition from state " << state
284 << " on event " << typeid(e
).name() << std::endl
;
288 typedef msm::back::state_machine
<player_
> player
;
291 // Testing utilities.
293 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
294 void pstate(player
const& p
)
296 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
302 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
304 // this event will be ignored and not call no_transition
305 p
.process_event(to_ignore());
306 // go to Open, call on_exit on Empty, then action, then on_entry on Open
307 p
.process_event(open_close()); pstate(p
);
308 p
.process_event(open_close()); pstate(p
);
309 // will be rejected, wrong disk type
311 cd_detected("louie, louie",DISK_DVD
)); pstate(p
);
313 cd_detected("louie, louie",DISK_CD
)); pstate(p
);
314 p
.process_event(play());
315 p
.process_event(NextSong());
316 std::cout
<< "sending an internal event" << std::endl
;
317 p
.process_event(internal_event());
318 std::cout
<< "conflict between the internal and normal transition. Internal is tried last" << std::endl
;
319 p
.process_event(PreviousSong());
321 // at this point, Play is active
322 p
.process_event(pause()); pstate(p
);
323 // go back to Playing
324 p
.process_event(end_pause()); pstate(p
);
325 p
.process_event(pause()); pstate(p
);
326 p
.process_event(stop()); pstate(p
);
327 // event leading to the same state
328 // no action method called as it is not present in the transition table
329 p
.process_event(stop()); pstate(p
);