]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/HTML/examples/SimpleWithFunctors3.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
;
59 // The list of FSM states
60 // entry and exit functors for Empty
63 template <class Event
,class FSM
,class STATE
>
64 void operator()(Event
const&,FSM
&,STATE
& )
66 std::cout
<< "entering: Empty" << std::endl
;
71 template <class Event
,class FSM
,class STATE
>
72 void operator()(Event
const&,FSM
&,STATE
& )
74 std::cout
<< "leaving: Empty" << std::endl
;
77 // definition of Empty
79 typedef msm::front::euml::func_state
<Empty_tag
,Empty_Entry
,Empty_Exit
> Empty
;
83 template <class Event
,class FSM
,class STATE
>
84 void operator()(Event
const&,FSM
&,STATE
& )
86 std::cout
<< "entering: Open" << std::endl
;
91 template <class Event
,class FSM
,class STATE
>
92 void operator()(Event
const&,FSM
&,STATE
& )
94 std::cout
<< "leaving: Open" << std::endl
;
98 typedef msm::front::euml::func_state
<Open_tag
,Open_Entry
,Open_Exit
> Open
;
100 // states without entry/exit actions (can be declared as functor state, just without functors ;-) )
101 struct Stopped_tag
{};
102 typedef msm::front::euml::func_state
<Stopped_tag
> Stopped
;
104 struct Playing_tag
{};
105 typedef msm::front::euml::func_state
<Playing_tag
> Playing
;
107 // state not defining any entry or exit (declared as simple state. Equivalent)
108 struct Paused_tag
{};
109 typedef msm::front::euml::func_state
<Paused_tag
> Paused
;
111 // the initial state of the player SM. Must be defined
112 typedef Empty initial_state
;
114 // transition actions
115 // as the functors are generic on events, fsm and source/target state,
116 // you can reuse them in another machine if you wish
119 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
120 void operator()(EVT
const&, FSM
&,SourceState
& ,TargetState
& )
122 cout
<< "transition with event:" << typeid(EVT
).name() << endl
;
125 struct start_playback
127 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
128 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
130 cout
<< "player::start_playback" << endl
;
135 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
136 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
138 cout
<< "player::open_drawer" << endl
;
143 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
144 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
146 cout
<< "player::close_drawer" << endl
;
151 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
152 void operator()(EVT
const&,FSM
& fsm
,SourceState
& ,TargetState
& )
154 cout
<< "player::store_cd_info" << endl
;
155 fsm
.process_event(play());
160 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
161 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
163 cout
<< "player::stop_playback" << endl
;
166 struct pause_playback
168 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
169 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
171 cout
<< "player::pause_playback" << endl
;
174 struct resume_playback
176 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
177 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
179 cout
<< "player::resume_playback" << endl
;
184 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
185 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
187 cout
<< "player::stop_and_open" << endl
;
192 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
193 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
195 cout
<< "player::stopped_again" << endl
;
201 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
202 bool operator()(EVT
const& evt
,FSM
& fsm
,SourceState
& src
,TargetState
& tgt
)
207 struct good_disk_format
209 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
210 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
212 // to test a guard condition, let's say we understand only CDs, not DVD
213 if (evt
.disc_type
!= DISK_CD
)
215 std::cout
<< "wrong disk, sorry" << std::endl
;
223 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
224 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
230 // Transition table for player
231 struct player_transition_table
: mpl::vector
<
232 // Start Event Next Action Guard
233 // +---------+-------------+---------+---------------------------+----------------------+
234 Row
< Stopped
, play
, Playing
, ActionSequence_
236 TestFct
,start_playback
> >
238 Row
< Stopped
, open_close
, Open
, open_drawer
, none
>,
239 Row
< Stopped
, stop
, Stopped
, none
, none
>,
240 // +---------+-------------+---------+---------------------------+----------------------+
241 Row
< Open
, open_close
, Empty
, close_drawer
, none
>,
242 // +---------+-------------+---------+---------------------------+----------------------+
243 Row
< Empty
, open_close
, Open
, open_drawer
, none
>,
244 Row
< Empty
, cd_detected
, Stopped
, store_cd_info
, And_
<good_disk_format
,
246 // +---------+-------------+---------+---------------------------+----------------------+
247 Row
< Playing
, stop
, Stopped
, stop_playback
, none
>,
248 Row
< Playing
, pause
, Paused
, pause_playback
, none
>,
249 Row
< Playing
, open_close
, Open
, stop_and_open
, none
>,
250 // +---------+-------------+---------+---------------------------+----------------------+
251 Row
< Paused
, end_pause
, Playing
, resume_playback
, none
>,
252 Row
< Paused
, stop
, Stopped
, stop_playback
, none
>,
253 Row
< Paused
, open_close
, Open
, stop_and_open
, none
>
254 // +---------+-------------+---------+---------------------------+----------------------+
257 struct player_tag
{};
258 typedef msm::front::euml::func_state_machine
<Playing_tag
,
260 player_transition_table
,
264 typedef msm::back::state_machine
<player_
> player
;
267 // Testing utilities.
269 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
270 void pstate(player
const& p
)
272 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
278 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
280 // go to Open, call on_exit on Empty, then action, then on_entry on Open
281 p
.process_event(open_close()); pstate(p
);
282 p
.process_event(open_close()); pstate(p
);
283 // will be rejected, wrong disk type
285 cd_detected("louie, louie",DISK_DVD
)); pstate(p
);
287 cd_detected("louie, louie",DISK_CD
)); pstate(p
);
288 // no need to call play() as the previous event does it in its action method
289 //p.process_event(play());
291 // at this point, Play is active
292 p
.process_event(pause()); pstate(p
);
293 // go back to Playing
294 p
.process_event(end_pause()); pstate(p
);
295 p
.process_event(pause()); pstate(p
);
296 p
.process_event(stop()); pstate(p
);
297 // event leading to the same state
298 // no action method called as it is not present in the transition table
299 p
.process_event(stop()); pstate(p
);