]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/PDF/examples/Orthogonal-deferred2.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 boost::msm::front
;
31 struct PreviousSong
{};
32 struct error_found
{};
35 // Flags. Allow information about a property of the current state
36 struct PlayingPaused
{};
38 struct FirstSongPlaying
{};
40 // A "complicated" event type that carries some data.
43 cd_detected(std::string name
)
50 // front-end: define the FSM structure
51 struct player_
: public msm::front::state_machine_def
<player_
>
53 // we want deferred events and no state requires deferred events (only the fsm in the
54 // transition table), so the fsm does.
55 typedef int activate_deferred_events
;
56 // The list of FSM states
57 struct Empty
: public msm::front::state
<>
59 // every (optional) entry/exit methods get the event passed.
60 template <class Event
,class FSM
>
61 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Empty" << std::endl
;}
62 template <class Event
,class FSM
>
63 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Empty" << std::endl
;}
65 struct Open
: public msm::front::state
<>
67 typedef mpl::vector1
<CDLoaded
> flag_list
;
68 template <class Event
,class FSM
>
69 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Open" << std::endl
;}
70 template <class Event
,class FSM
>
71 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Open" << std::endl
;}
74 struct Stopped
: public msm::front::state
<>
76 // when stopped, the CD is loaded
77 typedef mpl::vector1
<CDLoaded
> flag_list
;
78 template <class Event
,class FSM
>
79 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Stopped" << std::endl
;}
80 template <class Event
,class FSM
>
81 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Stopped" << std::endl
;}
84 // the player state machine contains a state which is himself a state machine
85 // as you see, no need to declare it anywhere so Playing can be developed separately
86 // by another team in another module. For simplicity I just declare it inside player
87 struct Playing_
: public msm::front::state_machine_def
<Playing_
>
89 // when playing, the CD is loaded and we are in either pause or playing (duh)
90 typedef mpl::vector2
<PlayingPaused
,CDLoaded
> flag_list
;
92 template <class Event
,class FSM
>
93 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Playing" << std::endl
;}
94 template <class Event
,class FSM
>
95 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Playing" << std::endl
;}
96 // The list of FSM states
97 struct Song1
: public msm::front::state
<>
99 typedef mpl::vector1
<FirstSongPlaying
> flag_list
;
100 template <class Event
,class FSM
>
101 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: First song" << std::endl
;}
102 template <class Event
,class FSM
>
103 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: First Song" << std::endl
;}
105 struct Song2
: public msm::front::state
<>
107 template <class Event
,class FSM
>
108 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: Second song" << std::endl
;}
109 template <class Event
,class FSM
>
110 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: Second Song" << std::endl
;}
112 struct Song3
: public msm::front::state
<>
114 template <class Event
,class FSM
>
115 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: Third song" << std::endl
;}
116 template <class Event
,class FSM
>
117 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: Third Song" << std::endl
;}
119 // the initial state. Must be defined
120 typedef Song1 initial_state
;
121 // transition actions
122 void start_next_song(NextSong
const&) { std::cout
<< "Playing::start_next_song\n"; }
123 void start_prev_song(PreviousSong
const&) { std::cout
<< "Playing::start_prev_song\n"; }
126 typedef Playing_ pl
; // makes transition table cleaner
127 // Transition table for Playing
128 struct transition_table
: mpl::vector4
<
129 // Start Event Next Action Guard
130 // +---------+-------------+---------+---------------------+----------------------+
131 a_row
< Song1
, NextSong
, Song2
, &pl::start_next_song
>,
132 a_row
< Song2
, PreviousSong
, Song1
, &pl::start_prev_song
>,
133 a_row
< Song2
, NextSong
, Song3
, &pl::start_next_song
>,
134 a_row
< Song3
, PreviousSong
, Song2
, &pl::start_prev_song
>
135 // +---------+-------------+---------+---------------------+----------------------+
137 // Replaces the default no-transition response.
138 template <class FSM
,class Event
>
139 void no_transition(Event
const& e
, FSM
&,int state
)
141 std::cout
<< "no transition from state " << state
142 << " on event " << typeid(e
).name() << std::endl
;
146 typedef msm::back::state_machine
<Playing_
> Playing
;
148 // state not defining any entry or exit
149 struct Paused
: public msm::front::state
<>
151 typedef mpl::vector2
<PlayingPaused
,CDLoaded
> flag_list
;
153 struct AllOk
: public msm::front::state
<>
155 template <class Event
,class FSM
>
156 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: AllOk" << std::endl
;}
157 template <class Event
,class FSM
>
158 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: AllOk" << std::endl
;}
160 // this state is also made terminal so that all the events are blocked
161 struct ErrorMode
: //public msm::front::terminate_state<> // ErrorMode terminates the state machine
162 public msm::front::interrupt_state
<end_error
> // ErroMode just interrupts. Will resume if
163 // the event end_error is generated
165 template <class Event
,class FSM
>
166 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: ErrorMode" << std::endl
;}
167 template <class Event
,class FSM
>
168 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: ErrorMode" << std::endl
;}
170 // the initial state of the player SM. Must be defined
171 typedef mpl::vector
<Empty
,AllOk
> initial_state
;
173 // transition actions
174 void start_playback(play
const&) { std::cout
<< "player::start_playback\n"; }
175 void open_drawer(open_close
const&) { std::cout
<< "player::open_drawer\n"; }
176 void close_drawer(open_close
const&) { std::cout
<< "player::close_drawer\n"; }
177 void store_cd_info(cd_detected
const& cd
) {std::cout
<< "player::store_cd_info\n";}
178 void stop_playback(stop
const&) { std::cout
<< "player::stop_playback\n"; }
179 void pause_playback(pause
const&) { std::cout
<< "player::pause_playback\n"; }
180 void resume_playback(end_pause
const&) { std::cout
<< "player::resume_playback\n"; }
181 void stop_and_open(open_close
const&) { std::cout
<< "player::stop_and_open\n"; }
182 void stopped_again(stop
const&){std::cout
<< "player::stopped_again\n";}
183 void report_error(error_found
const&) {std::cout
<< "player::report_error\n";}
184 void report_end_error(end_error
const&) {std::cout
<< "player::report_end_error\n";}
188 typedef player_ p
; // makes transition table cleaner
190 // Transition table for player
191 struct transition_table
: mpl::vector
<
192 // Start Event Next Action Guard
193 // +---------+-------------+---------+---------------------+----------------------+
194 a_row
< Stopped
, play
, Playing
, &p::start_playback
>,
195 a_row
< Stopped
, open_close
, Open
, &p::open_drawer
>,
196 a_row
< Stopped
, stop
, Stopped
, &p::stopped_again
>,
197 // +---------+-------------+---------+---------------------+----------------------+
198 a_row
< Open
, open_close
, Empty
, &p::close_drawer
>,
199 Row
< Open
, play
, none
, Defer
, none
>,
200 // +---------+-------------+---------+---------------------+----------------------+
201 a_row
< Empty
, open_close
, Open
, &p::open_drawer
>,
202 a_row
< Empty
, cd_detected
, Stopped
, &p::store_cd_info
>,
203 Row
< Empty
, play
, none
, Defer
, none
>,
204 // +---------+-------------+---------+---------------------+----------------------+
205 a_row
< Playing
, stop
, Stopped
, &p::stop_playback
>,
206 a_row
< Playing
, pause
, Paused
, &p::pause_playback
>,
207 a_row
< Playing
, open_close
, Open
, &p::stop_and_open
>,
208 // +---------+-------------+---------+---------------------+----------------------+
209 a_row
< Paused
, end_pause
, Playing
, &p::resume_playback
>,
210 a_row
< Paused
, stop
, Stopped
, &p::stop_playback
>,
211 a_row
< Paused
, open_close
, Open
, &p::stop_and_open
>,
212 // +---------+-------------+---------+---------------------+----------------------+
213 a_row
< AllOk
, error_found
,ErrorMode
, &p::report_error
>,
214 a_row
<ErrorMode
,end_error
,AllOk
, &p::report_end_error
>
215 // +---------+-------------+---------+---------------------+----------------------+
218 // Replaces the default no-transition response.
219 template <class FSM
,class Event
>
220 void no_transition(Event
const& e
, FSM
&,int state
)
222 std::cout
<< "no transition from state " << state
223 << " on event " << typeid(e
).name() << std::endl
;
227 typedef msm::back::state_machine
<player_
> player
;
230 // Testing utilities.
232 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused","AllOk","ErrorMode" };
234 void pstate(player
const& p
)
236 // we have now several active states, which we show
237 for (unsigned int i
=0;i
<player::nr_regions::value
;++i
)
239 std::cout
<< " -> " << state_names
[p
.current_state()[i
]] << std::endl
;
246 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
248 // test deferred event
249 // deferred in Empty and Open, will be handled only after event cd_detected
250 p
.process_event(play());
253 std::cout
<< "CDLoaded active:" << std::boolalpha
<< p
.is_flag_active
<CDLoaded
>() << std::endl
; //=> false (no CD yet)
254 // go to Open, call on_exit on Empty, then action, then on_entry on Open
255 p
.process_event(open_close()); pstate(p
);
256 p
.process_event(open_close()); pstate(p
);
257 p
.process_event(cd_detected("louie, louie"));
259 // at this point, Play is active (was deferred)
260 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> true
261 std::cout
<< "FirstSong active:" << std::boolalpha
<< p
.is_flag_active
<FirstSongPlaying
>() << std::endl
;//=> true
263 // make transition happen inside it. Player has no idea about this event but it's ok.
264 p
.process_event(NextSong());pstate(p
); //2nd song active
265 p
.process_event(NextSong());pstate(p
);//3rd song active
266 p
.process_event(PreviousSong());pstate(p
);//2nd song active
267 std::cout
<< "FirstSong active:" << std::boolalpha
<< p
.is_flag_active
<FirstSongPlaying
>() << std::endl
;//=> false
269 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> true
270 p
.process_event(pause()); pstate(p
);
271 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> true
272 // go back to Playing
273 // as you see, it starts back from the original state
274 p
.process_event(end_pause()); pstate(p
);
275 p
.process_event(pause()); pstate(p
);
276 p
.process_event(stop()); pstate(p
);
277 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> false
278 std::cout
<< "CDLoaded active:" << std::boolalpha
<< p
.is_flag_active
<CDLoaded
>() << std::endl
;//=> true
279 // by default, the flags are OR'ed but you can also use AND. Then the flag must be present in
280 // all of the active states
281 std::cout
<< "CDLoaded active with AND:" << std::boolalpha
<< p
.is_flag_active
<CDLoaded
,player::Flag_AND
>() << std::endl
;//=> false
283 // event leading to the same state
284 p
.process_event(stop()); pstate(p
);
286 // event leading to a terminal/interrupt state
287 p
.process_event(error_found()); pstate(p
);
288 // try generating more events
289 std::cout
<< "Trying to generate another event" << std::endl
; // will not work, fsm is terminated or interrupted
290 p
.process_event(play());pstate(p
);
291 std::cout
<< "Trying to end the error" << std::endl
; // will work only if ErrorMode is interrupt state
292 p
.process_event(end_error());pstate(p
);
293 std::cout
<< "Trying to generate another event" << std::endl
; // will work only if ErrorMode is interrupt state
294 p
.process_event(play());pstate(p
);