]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/HTML/examples/SimpleTutorial2.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/row2.hpp>
18 namespace msm
= boost::msm
;
19 namespace mpl
= boost::mpl
;
20 using namespace msm::front
;
31 // A "complicated" event type that carries some data.
39 cd_detected(std::string name
, DiskTypeEnum diskType
)
45 DiskTypeEnum disc_type
;
48 // front-end: define the FSM structure
49 struct player_
: public msm::front::state_machine_def
<player_
>
51 template <class Event
,class FSM
>
52 void on_entry(Event
const& ,FSM
&)
54 std::cout
<< "entering: Player" << std::endl
;
56 template <class Event
,class FSM
>
57 void on_exit(Event
const&,FSM
& )
59 std::cout
<< "leaving: Player" << std::endl
;
62 // The list of FSM states
63 struct Empty
: public msm::front::state
<>
65 // every (optional) entry/exit methods get the event passed.
66 template <class Event
,class FSM
>
67 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Empty" << std::endl
;}
68 template <class Event
,class FSM
>
69 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Empty" << std::endl
;}
70 void open_drawer(open_close
const&) { std::cout
<< "Empty::open_drawer\n"; }
71 // actions for Empty's internal transitions
72 void internal_action(cd_detected
const&){ std::cout
<< "Empty::internal action\n"; }
73 bool internal_guard(cd_detected
const&)
75 std::cout
<< "Empty::internal guard\n";
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
;}
85 void close_drawer(open_close
const&) { std::cout
<< "Open::close_drawer\n"; }
86 void stop_and_open(open_close
const&) { std::cout
<< "Open::stop_and_open\n"; }
89 // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
90 struct Stopped
: public msm::front::state
<msm::front::default_base_state
,msm::front::sm_ptr
>
92 template <class Event
,class FSM
>
93 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Stopped" << std::endl
;}
94 template <class Event
,class FSM
>
95 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Stopped" << std::endl
;}
96 void set_sm_ptr(player_
* pl
)
101 void start_playback(play
const&) { std::cout
<< "Stopped::start_playback\n"; }
102 void stop_playback(stop
const&) { std::cout
<< "Stopped::stop_playback\n"; }
105 struct Playing
: public msm::front::state
<>
107 template <class Event
,class FSM
>
108 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Playing" << std::endl
;}
109 template <class Event
,class FSM
>
110 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Playing" << std::endl
;}
112 // used to show a transition conflict. This guard will simply deactivate one transition and thus
113 // solve the conflict
114 bool auto_start(cd_detected
const&)
120 // state not defining any entry or exit
121 struct Paused
: public msm::front::state
<>
123 void pause_playback(pause
const&) { std::cout
<< "Paused::pause_playback\n"; }
124 void resume_playback(end_pause
const&) { std::cout
<< "Paused::resume_playback\n"; }
128 void store_cd_info(cd_detected
const&) { std::cout
<< "Player::store_cd_info\n"; }
131 bool good_disk_format(cd_detected
const& evt
)
133 // to test a guard condition, let's say we understand only CDs, not DVD
134 if (evt
.disc_type
!= DISK_CD
)
136 std::cout
<< "wrong disk, sorry" << std::endl
;
141 // the initial state of the player SM. Must be defined
142 typedef Empty initial_state
;
144 // Transition table for player
145 struct transition_table
: mpl::vector
<
146 // Start Event Next Action/Guard
147 // +---------+-------------+---------+---------------------+----------------------+
148 a_row2
< Stopped
, play
, Playing
, Stopped
, &Stopped::start_playback
>,
149 a_row2
< Stopped
, open_close
, Open
, Empty
, &Empty::open_drawer
>,
150 _row
< Stopped
, stop
, Stopped
>,
151 // +---------+-------------+---------+---------------------+----------------------+
152 a_row2
< Open
, open_close
, Empty
, Open
, &Open::close_drawer
>,
153 // +---------+-------------+---------+---------------------+----------------------+
154 a_row2
< Empty
, open_close
, Open
, Empty
,&Empty::open_drawer
>,
155 row2
< Empty
, cd_detected
, Stopped
, player_
,&player_::store_cd_info
156 , player_
,&player_::good_disk_format
>,
157 row2
< Empty
, cd_detected
, Playing
, player_
,&player_::store_cd_info
158 , Playing
,&Playing::auto_start
>,
159 // conflict with some internal rows
160 irow2
< Empty
, cd_detected
, Empty
,&Empty::internal_action
161 , Empty
,&Empty::internal_guard
>,
162 g_irow2
< Empty
, cd_detected
, Empty
,&Empty::internal_guard
>,
163 // +---------+-------------+---------+---------------------+----------------------+
164 a_row2
< Playing
, stop
, Stopped
, Stopped
,&Stopped::stop_playback
>,
165 a_row2
< Playing
, pause
, Paused
, Paused
,&Paused::pause_playback
>,
166 a_row2
< Playing
, open_close
, Open
, Open
,&Open::stop_and_open
>,
167 // +---------+-------------+---------+---------------------+----------------------+
168 a_row2
< Paused
, end_pause
, Playing
, Paused
,&Paused::resume_playback
>,
169 a_row2
< Paused
, stop
, Stopped
, Stopped
,&Stopped::stop_playback
>,
170 a_row2
< Paused
, open_close
, Open
, Open
,&Open::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 // go to Open, call on_exit on Empty, then action, then on_entry on Open
199 p
.process_event(open_close()); pstate(p
);
200 p
.process_event(open_close()); pstate(p
);
201 // will be rejected, wrong disk type
203 cd_detected("louie, louie",DISK_DVD
)); pstate(p
);
205 cd_detected("louie, louie",DISK_CD
)); pstate(p
);
206 p
.process_event(play());
208 // at this point, Play is active
209 p
.process_event(pause()); pstate(p
);
210 // go back to Playing
211 p
.process_event(end_pause()); pstate(p
);
212 p
.process_event(pause()); pstate(p
);
213 p
.process_event(stop()); pstate(p
);
214 // event leading to the same state
215 // no action method called as it is not present in the transition table
216 p
.process_event(stop()); pstate(p
);
217 std::cout
<< "stop fsm" << std::endl
;