]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/PDF/examples/SimpleWithFunctors.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>
24 namespace msm
= boost::msm
;
25 namespace mpl
= boost::mpl
;
26 using namespace msm::front
;
28 using namespace msm::front::euml
;
30 namespace // Concrete FSM implementation
39 // A "complicated" event type that carries some data.
47 cd_detected(std::string name
, DiskTypeEnum diskType
)
53 DiskTypeEnum disc_type
;
56 // front-end: define the FSM structure
57 struct player_
: public msm::front::state_machine_def
<player_
>
59 template <class Event
,class FSM
>
60 void on_entry(Event
const& ,FSM
&)
62 std::cout
<< "entering: Player" << std::endl
;
64 template <class Event
,class FSM
>
65 void on_exit(Event
const&,FSM
& )
67 std::cout
<< "leaving: Player" << std::endl
;
70 // The list of FSM states
71 struct Empty
: public msm::front::state
<>
73 // every (optional) entry/exit methods get the event passed.
74 template <class Event
,class FSM
>
75 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Empty" << std::endl
;}
76 template <class Event
,class FSM
>
77 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Empty" << std::endl
;}
79 struct Open
: public msm::front::state
<>
81 template <class Event
,class FSM
>
82 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Open" << std::endl
;}
83 template <class Event
,class FSM
>
84 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Open" << std::endl
;}
87 struct Stopped
: public msm::front::state
<>
89 // when stopped, the CD is loaded
90 template <class Event
,class FSM
>
91 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Stopped" << std::endl
;}
92 template <class Event
,class FSM
>
93 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Stopped" << std::endl
;}
96 struct Playing
: public msm::front::state
<>
98 template <class Event
,class FSM
>
99 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Playing" << std::endl
;}
100 template <class Event
,class FSM
>
101 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Playing" << std::endl
;}
104 // state not defining any entry or exit
105 struct Paused
: public msm::front::state
<>
109 // the initial state of the player SM. Must be defined
110 typedef Empty initial_state
;
112 // transition actions
113 // as the functors are generic on events, fsm and source/target state,
114 // you can reuse them in another machine if you wish
117 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
118 void operator()(EVT
const&, FSM
&,SourceState
& ,TargetState
& )
120 cout
<< "transition with event:" << typeid(EVT
).name() << endl
;
123 struct start_playback
125 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
126 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
128 cout
<< "player::start_playback" << endl
;
133 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
134 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
136 cout
<< "player::open_drawer" << endl
;
141 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
142 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
144 cout
<< "player::close_drawer" << endl
;
149 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
150 void operator()(EVT
const&,FSM
& fsm
,SourceState
& ,TargetState
& )
152 cout
<< "player::store_cd_info" << endl
;
153 fsm
.process_event(play());
158 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
159 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
161 cout
<< "player::stop_playback" << endl
;
164 struct pause_playback
166 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
167 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
169 cout
<< "player::pause_playback" << endl
;
172 struct resume_playback
174 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
175 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
177 cout
<< "player::resume_playback" << endl
;
182 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
183 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
185 cout
<< "player::stop_and_open" << endl
;
190 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
191 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
193 cout
<< "player::stopped_again" << endl
;
199 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
200 bool operator()(EVT
const& evt
,FSM
& fsm
,SourceState
& src
,TargetState
& tgt
)
205 struct good_disk_format
207 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
208 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
210 // to test a guard condition, let's say we understand only CDs, not DVD
211 if (evt
.disc_type
!= DISK_CD
)
213 std::cout
<< "wrong disk, sorry" << std::endl
;
221 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
222 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
227 // we want to define one row with the classic look.
228 bool auto_start(cd_detected
const& evt
)
233 typedef player_ p
; // makes transition table cleaner
235 // Transition table for player
236 struct transition_table
: mpl::vector
<
237 // Start Event Next Action Guard
238 // +---------+-------------+---------+---------------------------+----------------------+
239 Row
< Stopped
, play
, Playing
, ActionSequence_
241 TestFct
,start_playback
> >
243 Row
< Stopped
, open_close
, Open
, open_drawer
, none
>,
244 Row
< Stopped
, stop
, Stopped
, none
, none
>,
245 // +---------+-------------+---------+---------------------------+----------------------+
246 Row
< Open
, open_close
, Empty
, close_drawer
, none
>,
247 // +---------+-------------+---------+---------------------------+----------------------+
248 Row
< Empty
, open_close
, Open
, open_drawer
, none
>,
249 Row
< Empty
, cd_detected
, Stopped
, store_cd_info
, And_
<good_disk_format
,
251 // we here also mix with some "classical row"
252 g_row
< Empty
, cd_detected
, Playing
, &p::auto_start
>,
253 // +---------+-------------+---------+---------------------------+----------------------+
254 Row
< Playing
, stop
, Stopped
, stop_playback
, none
>,
255 Row
< Playing
, pause
, Paused
, pause_playback
, none
>,
256 Row
< Playing
, open_close
, Open
, stop_and_open
, none
>,
257 // +---------+-------------+---------+---------------------------+----------------------+
258 Row
< Paused
, end_pause
, Playing
, resume_playback
, none
>,
259 Row
< Paused
, stop
, Stopped
, stop_playback
, none
>,
260 Row
< Paused
, open_close
, Open
, stop_and_open
, none
>
261 // +---------+-------------+---------+---------------------------+----------------------+
263 // Replaces the default no-transition response.
264 template <class FSM
,class Event
>
265 void no_transition(Event
const& e
, FSM
&,int state
)
267 std::cout
<< "no transition from state " << state
268 << " on event " << typeid(e
).name() << std::endl
;
272 typedef msm::back::state_machine
<player_
> player
;
275 // Testing utilities.
277 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
278 void pstate(player
const& p
)
280 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
286 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
288 // go to Open, call on_exit on Empty, then action, then on_entry on Open
289 p
.process_event(open_close()); pstate(p
);
290 p
.process_event(open_close()); pstate(p
);
291 // will be rejected, wrong disk type
293 cd_detected("louie, louie",DISK_DVD
)); pstate(p
);
295 cd_detected("louie, louie",DISK_CD
)); pstate(p
);
296 // no need to call play() as the previous event does it in its action method
297 //p.process_event(play());
299 // at this point, Play is active
300 p
.process_event(pause()); pstate(p
);
301 // go back to Playing
302 p
.process_event(end_pause()); pstate(p
);
303 p
.process_event(pause()); pstate(p
);
304 p
.process_event(stop()); pstate(p
);
305 // event leading to the same state
306 // no action method called as it is not present in the transition table
307 p
.process_event(stop()); pstate(p
);
308 std::cout
<< "stop fsm" << std::endl
;