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>
18 namespace msm
= boost::msm
;
19 using namespace msm::front
;
30 // A "complicated" event type that carries some data.
38 cd_detected(std::string name
, DiskTypeEnum diskType
)
44 DiskTypeEnum disc_type
;
47 // front-end: define the FSM structure
48 struct player_
: public msm::front::state_machine_def
<player_
>
50 // actions for Empty's internal transitions
51 void internal_action(cd_detected
const&){ std::cout
<< "Empty::internal action\n"; }
52 bool internal_guard(cd_detected
const&)
54 std::cout
<< "Empty::internal guard\n";
57 struct internal_guard_fct
59 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
60 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
62 std::cout
<< "Empty::internal guard functor\n";
66 struct internal_action_fct
68 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
69 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
71 std::cout
<< "Empty::internal action functor" << std::endl
;
75 // The list of FSM states
76 struct Empty
: public msm::front::state
<>
78 // every (optional) entry/exit methods get the event passed.
79 template <class Event
,class FSM
>
80 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Empty" << std::endl
;}
81 template <class Event
,class FSM
>
82 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Empty" << std::endl
;}
84 // Transition table for Empty
85 struct internal_transition_table
: mpl::vector
<
86 // Start Event Next Action Guard
87 Internal
< cd_detected
, internal_action_fct
,internal_guard_fct
>
88 // +---------+-------------+---------+---------------------+----------------------+
91 struct Open
: public msm::front::state
<>
93 template <class Event
,class FSM
>
94 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Open" << std::endl
;}
95 template <class Event
,class FSM
>
96 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Open" << std::endl
;}
99 // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
100 struct Stopped
: public msm::front::state
<msm::front::default_base_state
,msm::front::sm_ptr
>
102 template <class Event
,class FSM
>
103 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Stopped" << std::endl
;}
104 template <class Event
,class FSM
>
105 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Stopped" << std::endl
;}
106 void set_sm_ptr(player_
* pl
)
113 struct Playing
: public msm::front::state
<>
115 template <class Event
,class FSM
>
116 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Playing" << std::endl
;}
117 template <class Event
,class FSM
>
118 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Playing" << std::endl
;}
121 // state not defining any entry or exit
122 struct Paused
: public msm::front::state
<>
126 // the initial state of the player SM. Must be defined
127 typedef Empty initial_state
;
129 // transition actions
130 void start_playback(play
const&) { std::cout
<< "player::start_playback\n"; }
131 void open_drawer(open_close
const&) { std::cout
<< "player::open_drawer\n"; }
132 void close_drawer(open_close
const&) { std::cout
<< "player::close_drawer\n"; }
133 void store_cd_info(cd_detected
const&) { std::cout
<< "player::store_cd_info\n"; }
134 void stop_playback(stop
const&) { std::cout
<< "player::stop_playback\n"; }
135 void pause_playback(pause
const&) { std::cout
<< "player::pause_playback\n"; }
136 void resume_playback(end_pause
const&) { std::cout
<< "player::resume_playback\n"; }
137 void stop_and_open(open_close
const&) { std::cout
<< "player::stop_and_open\n"; }
138 void stopped_again(stop
const&) {std::cout
<< "player::stopped_again\n";}
140 bool good_disk_format(cd_detected
const& evt
)
142 // to test a guard condition, let's say we understand only CDs, not DVD
143 if (evt
.disc_type
!= DISK_CD
)
145 std::cout
<< "wrong disk, sorry" << std::endl
;
151 typedef player_ p
; // makes transition table cleaner
153 // Transition table for player
154 struct transition_table
: mpl::vector
<
155 // Start Event Next Action Guard
156 // +---------+-------------+---------+---------------------+----------------------+
157 a_row
< Stopped
, play
, Playing
, &p::start_playback
>,
158 a_row
< Stopped
, open_close
, Open
, &p::open_drawer
>,
159 _row
< Stopped
, stop
, Stopped
>,
160 // +---------+-------------+---------+---------------------+----------------------+
161 a_row
< Open
, open_close
, Empty
, &p::close_drawer
>,
162 // +---------+-------------+---------+---------------------+----------------------+
163 a_row
< Empty
, open_close
, Open
, &p::open_drawer
>,
164 // conflict between a normal and 2 internal transitions (irow/g_irow)
165 row
< Empty
, cd_detected
, Stopped
, &p::store_cd_info
,&p::good_disk_format
>,
166 irow
< Empty
, cd_detected
, &p::internal_action
,&p::internal_guard
>,
167 g_irow
< Empty
, cd_detected
,&p::internal_guard
>,
168 // +---------+-------------+---------+---------------------+----------------------+
169 a_row
< Playing
, stop
, Stopped
, &p::stop_playback
>,
170 a_row
< Playing
, pause
, Paused
, &p::pause_playback
>,
171 a_row
< Playing
, open_close
, Open
, &p::stop_and_open
>,
172 // +---------+-------------+---------+---------------------+----------------------+
173 a_row
< Paused
, end_pause
, Playing
, &p::resume_playback
>,
174 a_row
< Paused
, stop
, Stopped
, &p::stop_playback
>,
175 a_row
< Paused
, open_close
, Open
, &p::stop_and_open
>
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
;
185 typedef int no_message_queue
;
189 typedef msm::back::state_machine
<player_
> player
;
192 // Testing utilities.
194 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
195 void pstate(player
const& p
)
197 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
203 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
205 // go to Open, call on_exit on Empty, then action, then on_entry on Open
206 p
.process_event(open_close()); pstate(p
);
207 p
.process_event(open_close()); pstate(p
);
208 // will be rejected, wrong disk type
210 cd_detected("louie, louie",DISK_DVD
)); pstate(p
);
212 cd_detected("louie, louie",DISK_CD
)); pstate(p
);
213 p
.process_event(play());
215 // at this point, Play is active
216 p
.process_event(pause()); pstate(p
);
217 // go back to Playing
218 p
.process_event(end_pause()); pstate(p
);
219 p
.process_event(pause()); pstate(p
);
220 p
.process_event(stop()); pstate(p
);
221 // event leading to the same state
222 // no action method called as it is not present in the transition table
223 p
.process_event(stop()); pstate(p
);