]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/HTML/examples/SimpleTutorialInternal.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)
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 namespace mpl
= boost::mpl
;
20 using namespace msm::front
;
32 // A "complicated" event type that carries some data.
40 cd_detected(std::string name
, DiskTypeEnum diskType
)
46 DiskTypeEnum disc_type
;
49 // front-end: define the FSM structure
50 struct player_
: public msm::front::state_machine_def
<player_
>
52 // The list of FSM states
53 struct Empty
: public msm::front::state
<>
55 // every (optional) entry/exit methods get the event passed.
56 template <class Event
,class FSM
>
57 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Empty" << std::endl
;}
58 template <class Event
,class FSM
>
59 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Empty" << std::endl
;}
61 struct Open
: public msm::front::state
<>
63 template <class Event
,class FSM
>
64 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Open" << std::endl
;}
65 template <class Event
,class FSM
>
66 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Open" << std::endl
;}
69 // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
70 struct Stopped
: public msm::front::state
<msm::front::default_base_state
,msm::front::sm_ptr
>
72 template <class Event
,class FSM
>
73 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Stopped" << std::endl
;}
74 template <class Event
,class FSM
>
75 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Stopped" << std::endl
;}
76 void set_sm_ptr(player_
* pl
)
83 struct Playing
: public msm::front::state
<>
85 template <class Event
,class FSM
>
86 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Playing" << std::endl
;}
87 template <class Event
,class FSM
>
88 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Playing" << std::endl
;}
91 // state not defining any entry or exit
92 struct Paused
: public msm::front::state
<>
96 // the initial state of the player SM. Must be defined
97 typedef Empty initial_state
;
100 void start_playback(play
const&) { std::cout
<< "player::start_playback\n"; }
101 void open_drawer(open_close
const&) { std::cout
<< "player::open_drawer\n"; }
102 void close_drawer(open_close
const&) { std::cout
<< "player::close_drawer\n"; }
103 void store_cd_info(cd_detected
const&) { std::cout
<< "player::store_cd_info\n"; }
104 void stop_playback(stop
const&) { std::cout
<< "player::stop_playback\n"; }
105 void pause_playback(pause
const&) { std::cout
<< "player::pause_playback\n"; }
106 void resume_playback(end_pause
const&) { std::cout
<< "player::resume_playback\n"; }
107 void stop_and_open(open_close
const&) { std::cout
<< "player::stop_and_open\n"; }
108 void stopped_again(stop
const&) {std::cout
<< "player::stopped_again\n";}
110 bool good_disk_format(cd_detected
const& evt
)
112 // to test a guard condition, let's say we understand only CDs, not DVD
113 if (evt
.disc_type
!= DISK_CD
)
115 std::cout
<< "wrong disk, sorry" << std::endl
;
120 // transitions internal to Empty
121 void internal_action(cd_detected
const&){ std::cout
<< "Empty::internal action\n"; }
122 bool internal_guard(cd_detected
const&)
124 std::cout
<< "Empty::internal guard\n";
127 struct internal_guard_fct
129 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
130 bool operator()(EVT
const& evt
,FSM
&,SourceState
& ,TargetState
& )
132 std::cout
<< "Empty::internal guard functor\n";
136 struct internal_action_fct
138 template <class EVT
,class FSM
,class SourceState
,class TargetState
>
139 void operator()(EVT
const& ,FSM
& ,SourceState
& ,TargetState
& )
141 std::cout
<< "Empty::internal action functor" << std::endl
;
145 typedef player_ p
; // makes transition table cleaner
147 // Transition table for player
148 struct transition_table
: mpl::vector
<
149 // Start Event Next Action Guard
150 // +---------+-------------+---------+---------------------+----------------------+
151 a_row
< Stopped
, play
, Playing
, &p::start_playback
>,
152 a_row
< Stopped
, open_close
, Open
, &p::open_drawer
>,
153 _row
< Stopped
, stop
, Stopped
>,
154 // +---------+-------------+---------+---------------------+----------------------+
155 a_row
< Open
, open_close
, Empty
, &p::close_drawer
>,
156 // +---------+-------------+---------+---------------------+----------------------+
157 a_row
< Empty
, open_close
, Open
, &p::open_drawer
>,
158 row
< Empty
, cd_detected
, Stopped
, &p::store_cd_info
,&p::good_disk_format
>,
159 irow
< Empty
, cd_detected
, &p::internal_action
,&p::internal_guard
>,
160 _irow
< Empty
, to_ignore
>,
161 g_irow
< Empty
, cd_detected
,&p::internal_guard
>,
162 Row
< Empty
, cd_detected
, none
, internal_action_fct
,internal_guard_fct
>,
163 // +---------+-------------+---------+---------------------+----------------------+
164 a_row
< Playing
, stop
, Stopped
, &p::stop_playback
>,
165 a_row
< Playing
, pause
, Paused
, &p::pause_playback
>,
166 a_row
< Playing
, open_close
, Open
, &p::stop_and_open
>,
167 // +---------+-------------+---------+---------------------+----------------------+
168 a_row
< Paused
, end_pause
, Playing
, &p::resume_playback
>,
169 a_row
< Paused
, stop
, Stopped
, &p::stop_playback
>,
170 a_row
< Paused
, open_close
, Open
, &p::stop_and_open
>
171 // +---------+-------------+---------+---------------------+----------------------+
173 // Replaces the default no-transition response.
174 template <class FSM
,class Event
>
175 void no_transition(Event
const& e
, FSM
&,int state
)
177 std::cout
<< "no transition from state " << state
178 << " on event " << typeid(e
).name() << std::endl
;
182 typedef msm::back::state_machine
<player_
> player
;
185 // Testing utilities.
187 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
188 void pstate(player
const& p
)
190 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
196 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
198 // this event will be ignored and not call no_transition
199 p
.process_event(to_ignore());
200 // go to Open, call on_exit on Empty, then action, then on_entry on Open
201 p
.process_event(open_close()); pstate(p
);
202 p
.process_event(open_close()); pstate(p
);
203 // will be rejected, wrong disk type
205 cd_detected("louie, louie",DISK_DVD
)); pstate(p
);
207 cd_detected("louie, louie",DISK_CD
)); pstate(p
);
208 p
.process_event(play());
210 // at this point, Play is active
211 p
.process_event(pause()); pstate(p
);
212 // go back to Playing
213 p
.process_event(end_pause()); pstate(p
);
214 p
.process_event(pause()); pstate(p
);
215 p
.process_event(stop()); pstate(p
);
216 // event leading to the same state
217 // no action method called as it is not present in the transition table
218 p
.process_event(stop()); pstate(p
);