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 #include <boost/msm/front/functor_row.hpp>
17 #include <boost/msm/front/euml/common.hpp>
19 #include <boost/msm/front/euml/operator.hpp>
22 namespace msm
= boost::msm
;
23 using namespace msm::front
;
24 namespace mpl
= boost::mpl
;
26 using namespace msm::front::euml
;
37 // A "complicated" event type that carries some data.
45 cd_detected(std::string name
, DiskTypeEnum diskType
)
51 DiskTypeEnum disc_type
;
54 // front-end: define the FSM structure
55 struct player_
: public msm::front::state_machine_def
<player_
>
57 // The list of FSM states
58 struct Empty
: public msm::front::state
<>
60 // every (optional) entry/exit methods get the event passed.
61 template <class Event
,class FSM
>
62 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Empty" << std::endl
;}
63 template <class Event
,class FSM
>
64 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Empty" << std::endl
;}
65 struct internal_guard_fct
67 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
68 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
70 std::cout
<< "Empty::internal_transition_table guard\n";
74 struct internal_action_fct
76 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
77 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
79 std::cout
<< "Empty::internal_transition_table action" << std::endl
;
82 // Transition table for Empty
83 struct internal_transition_table
: mpl::vector
<
84 // Start Event Next Action Guard
85 Internal
< cd_detected
, internal_action_fct
,internal_guard_fct
>
86 // +---------+-------------+---------+---------------------+----------------------+
89 struct Open
: public msm::front::state
<>
91 template <class Event
,class FSM
>
92 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Open" << std::endl
;}
93 template <class Event
,class FSM
>
94 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Open" << std::endl
;}
97 struct Stopped
: public msm::front::state
<>
99 template <class Event
,class FSM
>
100 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Stopped" << std::endl
;}
101 template <class Event
,class FSM
>
102 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Stopped" << std::endl
;}
105 struct Playing
: public msm::front::state
<>
107 template <class Event
,class FSM
>
108 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Playing" << std::endl
;}
109 template <class Event
,class FSM
>
110 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Playing" << std::endl
;}
113 // state not defining any entry or exit
114 struct Paused
: public msm::front::state
<>
118 // the initial state of the player SM. Must be defined
119 typedef Empty initial_state
;
121 // transition actions
122 // as the functors are generic on events, fsm and source/target state,
123 // you can reuse them in another machine if you wish
126 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
127 void operator()(EVT
const&, FSM
&,SourceState
& ,TargetState
& )
129 cout
<< "transition with event:" << typeid(EVT
).name() << endl
;
132 struct start_playback
134 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
135 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
137 cout
<< "player::start_playback" << endl
;
142 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
143 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
145 cout
<< "player::open_drawer" << endl
;
150 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
151 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
153 cout
<< "player::close_drawer" << endl
;
158 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
159 void operator()(EVT
const&,FSM
& fsm
,SourceState
& ,TargetState
& )
161 cout
<< "player::store_cd_info" << endl
;
162 fsm
.process_event(play());
167 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
168 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
170 cout
<< "player::stop_playback" << endl
;
173 struct pause_playback
175 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
176 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
178 cout
<< "player::pause_playback" << endl
;
181 struct resume_playback
183 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
184 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
186 cout
<< "player::resume_playback" << endl
;
191 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
192 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
194 cout
<< "player::stop_and_open" << endl
;
199 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
200 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
202 cout
<< "player::stopped_again" << endl
;
208 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
209 bool operator()(EVT
const& evt
,FSM
& fsm
,SourceState
& src
,TargetState
& tgt
)
214 struct good_disk_format
216 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
217 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
219 // to test a guard condition, let's say we understand only CDs, not DVD
220 if (evt
.disc_type
!= DISK_CD
)
222 std::cout
<< "wrong disk, sorry" << std::endl
;
230 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
231 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
236 struct internal_guard
238 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
239 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
241 std::cout
<< "Empty::internal guard\n";
245 // Transition table for player
246 struct transition_table
: mpl::vector
<
247 // Start Event Next Action Guard
248 // +---------+-------------+---------+---------------------------+----------------------+
249 Row
< Stopped
, play
, Playing
, ActionSequence_
251 TestFct
,start_playback
> >
253 Row
< Stopped
, open_close
, Open
, open_drawer
, none
>,
254 Row
< Stopped
, stop
, Stopped
, none
, none
>,
255 // +---------+-------------+---------+---------------------------+----------------------+
256 Row
< Open
, open_close
, Empty
, close_drawer
, none
>,
257 // +---------+-------------+---------+---------------------------+----------------------+
258 Row
< Empty
, open_close
, Open
, open_drawer
, none
>,
259 Row
< Empty
, cd_detected
, Stopped
, store_cd_info
, And_
<good_disk_format
,
261 // internal transition inside the stt: none as Target
262 Row
< Empty
, cd_detected
, none
, none
, internal_guard
>,
263 // +---------+-------------+---------+---------------------------+----------------------+
264 Row
< Playing
, stop
, Stopped
, stop_playback
, none
>,
265 Row
< Playing
, pause
, Paused
, pause_playback
, none
>,
266 Row
< Playing
, open_close
, Open
, stop_and_open
, none
>,
267 // +---------+-------------+---------+---------------------------+----------------------+
268 Row
< Paused
, end_pause
, Playing
, resume_playback
, none
>,
269 Row
< Paused
, stop
, Stopped
, stop_playback
, none
>,
270 Row
< Paused
, open_close
, Open
, stop_and_open
, none
>
271 // +---------+-------------+---------+---------------------------+----------------------+
273 // Replaces the default no-transition response.
274 template <class FSM
,class Event
>
275 void no_transition(Event
const& e
, FSM
&,int state
)
277 std::cout
<< "no transition from state " << state
278 << " on event " << typeid(e
).name() << std::endl
;
282 typedef msm::back::state_machine
<player_
> player
;
285 // Testing utilities.
287 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
288 void pstate(player
const& p
)
290 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
296 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
298 // go to Open, call on_exit on Empty, then action, then on_entry on Open
299 p
.process_event(open_close()); pstate(p
);
300 p
.process_event(open_close()); pstate(p
);
301 // will be rejected, wrong disk type
303 cd_detected("louie, louie",DISK_DVD
)); pstate(p
);
305 cd_detected("louie, louie",DISK_CD
)); pstate(p
);
306 // no need to call play() as the previous transition does it in its action method
307 //p.process_event(play());
309 // at this point, Play is active
310 p
.process_event(pause()); pstate(p
);
311 // go back to Playing
312 p
.process_event(end_pause()); pstate(p
);
313 p
.process_event(pause()); pstate(p
);
314 p
.process_event(stop()); pstate(p
);
315 // event leading to the same state
316 // no action method called as it is not present in the transition table
317 p
.process_event(stop()); pstate(p
);