]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/PDF/examples/SimpleWithFunctors2.cpp
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/msm/back/state_machine.hpp>
16 #include <boost/msm/front/state_machine_def.hpp>
18 #include <boost/msm/front/functor_row.hpp>
19 #include <boost/msm/front/euml/common.hpp>
21 #include <boost/msm/front/euml/operator.hpp>
22 // for func_state and func_state_machine
23 #include <boost/msm/front/euml/state_grammar.hpp>
26 namespace msm
= boost::msm
;
27 namespace mpl
= boost::mpl
;
28 using namespace msm::front
;
30 using namespace msm::front::euml
;
32 namespace // Concrete FSM implementation
41 // A "complicated" event type that carries some data.
49 cd_detected(std::string name
, DiskTypeEnum diskType
)
55 DiskTypeEnum disc_type
;
58 // front-end: define the FSM structure
59 struct player_
: public msm::front::state_machine_def
<player_
>
61 // The list of FSM states
62 // entry and exit functors for Empty
65 template <class Event
,class FSM
,class STATE
>
66 void operator()(Event
const&,FSM
&,STATE
& )
68 std::cout
<< "entering: Empty" << std::endl
;
73 template <class Event
,class FSM
,class STATE
>
74 void operator()(Event
const&,FSM
&,STATE
& )
76 std::cout
<< "leaving: Empty" << std::endl
;
79 // definition of Empty
81 typedef msm::front::euml::func_state
<Empty_tag
,Empty_Entry
,Empty_Exit
> Empty
;
85 template <class Event
,class FSM
,class STATE
>
86 void operator()(Event
const&,FSM
&,STATE
& )
88 std::cout
<< "entering: Open" << std::endl
;
93 template <class Event
,class FSM
,class STATE
>
94 void operator()(Event
const&,FSM
&,STATE
& )
96 std::cout
<< "leaving: Open" << std::endl
;
100 typedef msm::front::euml::func_state
<Open_tag
,Open_Entry
,Open_Exit
> Open
;
102 // states without entry/exit actions (can be declared as functor state, just without functors ;-) )
103 struct Stopped_tag
{};
104 typedef msm::front::euml::func_state
<Stopped_tag
> Stopped
;
106 struct Playing_tag
{};
107 typedef msm::front::euml::func_state
<Playing_tag
> Playing
;
109 // state not defining any entry or exit (declared as simple state. Equivalent)
110 struct Paused
: public msm::front::state
<>
114 // the initial state of the player SM. Must be defined
115 typedef Empty initial_state
;
117 // transition actions
118 // as the functors are generic on events, fsm and source/target state,
119 // you can reuse them in another machine if you wish
122 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
123 void operator()(EVT
const&, FSM
&,SourceState
& ,TargetState
& )
125 cout
<< "transition with event:" << typeid(EVT
).name() << endl
;
128 struct start_playback
130 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
131 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
133 cout
<< "player::start_playback" << endl
;
138 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
139 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
141 cout
<< "player::open_drawer" << endl
;
146 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
147 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
149 cout
<< "player::close_drawer" << endl
;
154 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
155 void operator()(EVT
const&,FSM
& fsm
,SourceState
& ,TargetState
& )
157 cout
<< "player::store_cd_info" << endl
;
158 fsm
.process_event(play());
163 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
164 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
166 cout
<< "player::stop_playback" << endl
;
169 struct pause_playback
171 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
172 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
174 cout
<< "player::pause_playback" << endl
;
177 struct resume_playback
179 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
180 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
182 cout
<< "player::resume_playback" << endl
;
187 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
188 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
190 cout
<< "player::stop_and_open" << endl
;
195 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
196 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
198 cout
<< "player::stopped_again" << endl
;
204 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
205 bool operator()(EVT
const& evt
,FSM
& fsm
,SourceState
& src
,TargetState
& tgt
)
210 struct good_disk_format
212 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
213 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
215 // to test a guard condition, let's say we understand only CDs, not DVD
216 if (evt
.disc_type
!= DISK_CD
)
218 std::cout
<< "wrong disk, sorry" << std::endl
;
226 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
227 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
232 // we want to define one row with the classic look.
233 bool auto_start(cd_detected
const& evt
)
238 typedef player_ p
; // makes transition table cleaner
240 // Transition table for player
241 struct transition_table
: mpl::vector
<
242 // Start Event Next Action Guard
243 // +---------+-------------+---------+---------------------------+----------------------+
244 Row
< Stopped
, play
, Playing
, ActionSequence_
246 TestFct
,start_playback
> >
248 Row
< Stopped
, open_close
, Open
, open_drawer
, none
>,
249 Row
< Stopped
, stop
, Stopped
, none
, none
>,
250 // +---------+-------------+---------+---------------------------+----------------------+
251 Row
< Open
, open_close
, Empty
, close_drawer
, none
>,
252 // +---------+-------------+---------+---------------------------+----------------------+
253 Row
< Empty
, open_close
, Open
, open_drawer
, none
>,
254 Row
< Empty
, cd_detected
, Stopped
, store_cd_info
, And_
<good_disk_format
,
256 // we here also mix with some "classical row"
257 g_row
< Empty
, cd_detected
, Playing
, &p::auto_start
>,
258 // +---------+-------------+---------+---------------------------+----------------------+
259 Row
< Playing
, stop
, Stopped
, stop_playback
, none
>,
260 Row
< Playing
, pause
, Paused
, pause_playback
, none
>,
261 Row
< Playing
, open_close
, Open
, stop_and_open
, none
>,
262 // +---------+-------------+---------+---------------------------+----------------------+
263 Row
< Paused
, end_pause
, Playing
, resume_playback
, none
>,
264 Row
< Paused
, stop
, Stopped
, stop_playback
, none
>,
265 Row
< Paused
, open_close
, Open
, stop_and_open
, none
>
266 // +---------+-------------+---------+---------------------------+----------------------+
268 // Replaces the default no-transition response.
269 template <class FSM
,class Event
>
270 void no_transition(Event
const& e
, FSM
&,int state
)
272 std::cout
<< "no transition from state " << state
273 << " on event " << typeid(e
).name() << std::endl
;
277 typedef msm::back::state_machine
<player_
> player
;
280 // Testing utilities.
282 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
283 void pstate(player
const& p
)
285 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
291 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
293 // go to Open, call on_exit on Empty, then action, then on_entry on Open
294 p
.process_event(open_close()); pstate(p
);
295 p
.process_event(open_close()); pstate(p
);
296 // will be rejected, wrong disk type
298 cd_detected("louie, louie",DISK_DVD
)); pstate(p
);
300 cd_detected("louie, louie",DISK_CD
)); pstate(p
);
301 // no need to call play() as the previous event does it in its action method
302 //p.process_event(play());
304 // at this point, Play is active
305 p
.process_event(pause()); pstate(p
);
306 // go back to Playing
307 p
.process_event(end_pause()); pstate(p
);
308 p
.process_event(pause()); pstate(p
);
309 p
.process_event(stop()); pstate(p
);
310 // event leading to the same state
311 // no action method called as it is not present in the transition table
312 p
.process_event(stop()); pstate(p
);