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)
14 #include <boost/phoenix/phoenix.hpp>
16 // add phoenix support in eUML
17 #define BOOST_MSM_EUML_PHOENIX_SUPPORT
18 #include <boost/msm/back/state_machine.hpp>
19 #include <boost/msm/front/euml/euml.hpp>
23 using namespace boost::msm::front::euml
;
24 namespace msm
= boost::msm
;
25 using namespace boost::phoenix
;
27 // entry/exit/action/guard logging functors
28 #include "logging_functors.h"
30 namespace // Concrete FSM implementation
33 BOOST_MSM_EUML_EVENT(end_pause
)
34 BOOST_MSM_EUML_EVENT(stop
)
35 BOOST_MSM_EUML_EVENT(pause
)
36 BOOST_MSM_EUML_EVENT(open_close
)
37 struct play_event
: boost::msm::front::euml::euml_event
<play_event
>
47 // A "complicated" event type that carries some data.
48 struct cd_detected_event
: boost::msm::front::euml::euml_event
<cd_detected_event
>
51 cd_detected_event(std::string
const& name
,DiskTypeEnum disk
):cd_name(name
),cd_type(disk
){}
55 // define an instance for a nicer transition table
56 cd_detected_event cd_detected
;
58 // Concrete FSM implementation
59 // The list of FSM states
60 // state not needing any entry or exit
61 BOOST_MSM_EUML_STATE((),Paused
)
63 // states with standard eUML actions
64 BOOST_MSM_EUML_STATE(( Stopped_Entry
,Stopped_Exit
),Stopped
)
65 BOOST_MSM_EUML_STATE(( Playing_Entry
,Playing_Exit
),Playing
)
67 // a "standard" msm state
68 struct Empty_impl
: public msm::front::state
<> , public euml_state
<Empty_impl
>
70 // this allows us to add some functions
71 void foo() {std::cout
<< "Empty::foo " << std::endl
;}
72 // standard entry behavior
73 template <class Event
,class FSM
>
74 void on_entry(Event
const& evt
,FSM
& fsm
)
76 std::cout
<< "entering: Empty" << std::endl
;
78 template <class Event
,class FSM
>
79 void on_exit(Event
const& evt
,FSM
& fsm
)
81 std::cout
<< "leaving: Empty" << std::endl
;
84 //instance for use in the transition table
85 Empty_impl
const Empty
;
87 // entry and exit actions as phoenix functions
88 struct open_entry_impl
90 typedef void result_type
;
93 cout
<< "entering: Open" << endl
;
96 boost::phoenix::function
<open_entry_impl
> open_entry
;
99 typedef void result_type
;
102 cout
<< "leaving: Open" << endl
;
105 boost::phoenix::function
<open_exit_impl
> open_exit
;
107 // a state using phoenix for entry/exit actions
108 BOOST_MSM_EUML_STATE(( open_entry(),open_exit() ),Open
)
110 // actions and guards using boost::phoenix
111 struct start_playback_impl
113 typedef void result_type
;
116 cout
<< "calling: start_playback" << endl
;
119 boost::phoenix::function
<start_playback_impl
> start_playback
;
121 // a guard taking the event as argument
122 struct good_disk_format_impl
124 typedef bool result_type
;
126 template <class Event
>
127 bool operator()(Event
const& evt
)
129 // to test a guard condition, let's say we understand only CDs, not DVD
130 if (evt
.cd_type
!=DISK_CD
)
132 std::cout
<< "wrong disk, sorry" << std::endl
;
135 std::cout
<< "good disk" << std::endl
;
139 boost::phoenix::function
<good_disk_format_impl
> good_disk_format
;
142 struct store_cd_info_impl
144 typedef void result_type
;
147 cout
<< "calling: store_cd_info" << endl
;
150 boost::phoenix::function
<store_cd_info_impl
> store_cd_info
;
152 // an action taking the fsm as argument and sending it a new event
153 struct process_play_impl
155 typedef void result_type
;
158 void operator()(Fsm
& fsm
)
160 cout
<< "queuing a play event" << endl
;
161 fsm
.process_event(play
);
164 // it is also possible to use BOOST_PHOENIX_ADAPT_CALLABLE to avoid defining a global variable
165 BOOST_PHOENIX_ADAPT_CALLABLE(process_play
, process_play_impl
, 1)
168 // transition table. Actions and guards are written as phoenix functions
169 BOOST_MSM_EUML_TRANSITION_TABLE((
170 //an action without arguments
171 Playing
== Stopped
+ play
/ start_playback() ,
172 Playing
== Paused
+ end_pause
,
173 // +------------------------------------------------------------------------------+
174 Empty
== Open
+ open_close
,
175 // +------------------------------------------------------------------------------+
176 Open
== Empty
+ open_close
,
177 Open
== Paused
+ open_close
,
178 Open
== Stopped
+ open_close
,
179 Open
== Playing
+ open_close
,
180 // +------------------------------------------------------------------------------+
181 Paused
== Playing
+ pause
,
182 // +------------------------------------------------------------------------------+
183 Stopped
== Playing
+ stop
,
184 Stopped
== Paused
+ stop
,
185 // a guard taking the event as argument
186 // and an action made of a phoenix expression of 2 actions
187 // _event is a placeholder for the current event
188 // _fsm is a placeholder for the current state machine
189 Stopped
== Empty
+ cd_detected
[good_disk_format(_event
)]
190 / (store_cd_info(),process_play(_fsm
)),
191 Stopped
== Stopped
+ stop
192 // +------------------------------------------------------------------------------+
195 // create a state machine "on the fly"
196 BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table
, //STT
197 init_
<< Empty
, // Init State
200 attributes_
<< no_attributes_
, // Attributes
201 configure_
<< no_configure_
, // configuration
202 Log_No_Transition
// no_transition handler
206 // or simply, if no no_transition handler needed:
207 //BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
208 // Empty // Init State
211 // choice of back-end
212 typedef msm::back::state_machine
<player_
> player
;
215 // Testing utilities.
217 static char const* const state_names
[] = { "Stopped", "Paused", "Open", "Empty", "Playing" };
218 void pstate(player
const& p
)
220 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
226 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
228 // go to Open, call on_exit on Empty, then action, then on_entry on Open
229 p
.process_event(open_close
); pstate(p
);
230 p
.process_event(open_close
); pstate(p
);
231 // will be rejected, wrong disk type
233 cd_detected_event("louie, louie",DISK_DVD
)); pstate(p
);
235 cd_detected_event("louie, louie",DISK_CD
)); pstate(p
);
236 // no need to call play as the previous event does it in its action method
237 //p.process_event(play);
239 // at this point, Play is active
240 p
.process_event(pause
); pstate(p
);
241 // go back to Playing
242 p
.process_event(end_pause
); pstate(p
);
243 p
.process_event(pause
); pstate(p
);
244 p
.process_event(stop
); pstate(p
);
245 // event leading to the same state
246 // no action method called as none is defined in the transition table
247 p
.process_event(stop
); pstate(p
);
248 // test call to no_transition
249 p
.process_event(pause
); pstate(p
);