]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/HTML/examples/SimpleTutorial.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>
17 namespace msm
= boost::msm
;
18 namespace mpl
= boost::mpl
;
29 // A "complicated" event type that carries some data.
37 cd_detected(std::string name
, DiskTypeEnum diskType
)
43 DiskTypeEnum disc_type
;
46 // front-end: define the FSM structure
47 struct player_
: public msm::front::state_machine_def
<player_
>
49 template <class Event
,class FSM
>
50 void on_entry(Event
const& ,FSM
&)
52 std::cout
<< "entering: Player" << std::endl
;
54 template <class Event
,class FSM
>
55 void on_exit(Event
const&,FSM
& )
57 std::cout
<< "leaving: Player" << std::endl
;
60 // The list of FSM states
61 struct Empty
: public msm::front::state
<>
63 // every (optional) entry/exit methods get the event passed.
64 template <class Event
,class FSM
>
65 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Empty" << std::endl
;}
66 template <class Event
,class FSM
>
67 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Empty" << std::endl
;}
69 struct Open
: public msm::front::state
<>
71 template <class Event
,class FSM
>
72 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Open" << std::endl
;}
73 template <class Event
,class FSM
>
74 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Open" << std::endl
;}
77 // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
78 struct Stopped
: public msm::front::state
<msm::front::default_base_state
,msm::front::sm_ptr
>
80 template <class Event
,class FSM
>
81 void on_entry(Event
const& ,FSM
&) {std::cout
<< "entering: Stopped" << std::endl
;}
82 template <class Event
,class FSM
>
83 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Stopped" << std::endl
;}
84 void set_sm_ptr(player_
* pl
)
91 struct Playing
: public msm::front::state
<>
93 template <class Event
,class FSM
>
94 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Playing" << std::endl
;}
95 template <class Event
,class FSM
>
96 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Playing" << std::endl
;}
99 // state not defining any entry or exit
100 struct Paused
: public msm::front::state
<>
104 // the initial state of the player SM. Must be defined
105 typedef Empty initial_state
;
107 // transition actions
108 void start_playback(play
const&) { std::cout
<< "player::start_playback\n"; }
109 void open_drawer(open_close
const&) { std::cout
<< "player::open_drawer\n"; }
110 void close_drawer(open_close
const&) { std::cout
<< "player::close_drawer\n"; }
111 void store_cd_info(cd_detected
const&) { std::cout
<< "player::store_cd_info\n"; }
112 void stop_playback(stop
const&) { std::cout
<< "player::stop_playback\n"; }
113 void pause_playback(pause
const&) { std::cout
<< "player::pause_playback\n"; }
114 void resume_playback(end_pause
const&) { std::cout
<< "player::resume_playback\n"; }
115 void stop_and_open(open_close
const&) { std::cout
<< "player::stop_and_open\n"; }
116 void stopped_again(stop
const&) {std::cout
<< "player::stopped_again\n";}
118 bool good_disk_format(cd_detected
const& evt
)
120 // to test a guard condition, let's say we understand only CDs, not DVD
121 if (evt
.disc_type
!= DISK_CD
)
123 std::cout
<< "wrong disk, sorry" << std::endl
;
128 // used to show a transition conflict. This guard will simply deactivate one transition and thus
129 // solve the conflict
130 bool auto_start(cd_detected
const&)
135 typedef player_ p
; // makes transition table cleaner
137 // Transition table for player
138 struct transition_table
: mpl::vector
<
139 // Start Event Next Action Guard
140 // +---------+-------------+---------+---------------------+----------------------+
141 a_row
< Stopped
, play
, Playing
, &p::start_playback
>,
142 a_row
< Stopped
, open_close
, Open
, &p::open_drawer
>,
143 _row
< Stopped
, stop
, Stopped
>,
144 // +---------+-------------+---------+---------------------+----------------------+
145 a_row
< Open
, open_close
, Empty
, &p::close_drawer
>,
146 // +---------+-------------+---------+---------------------+----------------------+
147 a_row
< Empty
, open_close
, Open
, &p::open_drawer
>,
148 row
< Empty
, cd_detected
, Stopped
, &p::store_cd_info
,&p::good_disk_format
>,
149 row
< Empty
, cd_detected
, Playing
, &p::store_cd_info
,&p::auto_start
>,
150 // +---------+-------------+---------+---------------------+----------------------+
151 a_row
< Playing
, stop
, Stopped
, &p::stop_playback
>,
152 a_row
< Playing
, pause
, Paused
, &p::pause_playback
>,
153 a_row
< Playing
, open_close
, Open
, &p::stop_and_open
>,
154 // +---------+-------------+---------+---------------------+----------------------+
155 a_row
< Paused
, end_pause
, Playing
, &p::resume_playback
>,
156 a_row
< Paused
, stop
, Stopped
, &p::stop_playback
>,
157 a_row
< Paused
, open_close
, Open
, &p::stop_and_open
>
158 // +---------+-------------+---------+---------------------+----------------------+
160 // Replaces the default no-transition response.
161 template <class FSM
,class Event
>
162 void no_transition(Event
const& e
, FSM
&,int state
)
164 std::cout
<< "no transition from state " << state
165 << " on event " << typeid(e
).name() << std::endl
;
169 typedef msm::back::state_machine
<player_
> player
;
172 // Testing utilities.
174 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
175 void pstate(player
const& p
)
177 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
183 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
185 // go to Open, call on_exit on Empty, then action, then on_entry on Open
186 p
.process_event(open_close()); pstate(p
);
187 p
.process_event(open_close()); pstate(p
);
188 // will be rejected, wrong disk type
190 cd_detected("louie, louie",DISK_DVD
)); pstate(p
);
192 cd_detected("louie, louie",DISK_CD
)); pstate(p
);
193 p
.process_event(play());
195 // at this point, Play is active
196 p
.process_event(pause()); pstate(p
);
197 // go back to Playing
198 p
.process_event(end_pause()); pstate(p
);
199 p
.process_event(pause()); pstate(p
);
200 p
.process_event(stop()); pstate(p
);
201 // event leading to the same state
202 // no action method called as it is not present in the transition table
203 p
.process_event(stop()); pstate(p
);
204 std::cout
<< "stop fsm" << std::endl
;