]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/PDF/examples/Orthogonal-deferred.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 struct PreviousSong
{};
30 struct error_found
{};
34 // Flags. Allow information about a property of the current state
35 struct PlayingPaused
{};
37 struct FirstSongPlaying
{};
39 // A "complicated" event type that carries some data.
42 cd_detected(std::string name
)
49 // front-end: define the FSM structure
50 struct player_
: public msm::front::state_machine_def
<player_
>
52 template <class Event
,class FSM
>
53 void on_entry(Event
const& ,FSM
&)
55 std::cout
<< "entering: Player" << std::endl
;
57 template <class Event
,class FSM
>
58 void on_exit(Event
const&,FSM
& )
60 std::cout
<< "leaving: Player" << std::endl
;
62 // The list of FSM states
63 struct Empty
: public msm::front::state
<>
65 // if the play event arrives in this state, defer it until a state handles it or
67 typedef mpl::vector
<play
> deferred_events
;
68 // every (optional) entry/exit methods get the event passed.
69 template <class Event
,class FSM
>
70 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Empty" << std::endl
;}
71 template <class Event
,class FSM
>
72 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Empty" << std::endl
;}
74 struct Open
: public msm::front::state
<>
76 // if the play event arrives in this state, defer it until a state handles it or
78 typedef mpl::vector
<play
> deferred_events
;
79 typedef mpl::vector1
<CDLoaded
> flag_list
;
80 template <class Event
,class FSM
>
81 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Open" << std::endl
;}
82 template <class Event
,class FSM
>
83 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Open" << std::endl
;}
86 struct Stopped
: public msm::front::state
<>
88 // when stopped, the CD is loaded
89 typedef mpl::vector1
<CDLoaded
> flag_list
;
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 // the player state machine contains a state which is himself a state machine
97 // as you see, no need to declare it anywhere so Playing can be developed separately
98 // by another team in another module. For simplicity I just declare it inside player
99 struct Playing_
: public msm::front::state_machine_def
<Playing_
>
101 // when playing, the CD is loaded and we are in either pause or playing (duh)
102 typedef mpl::vector2
<PlayingPaused
,CDLoaded
> flag_list
;
104 template <class Event
,class FSM
>
105 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Playing" << std::endl
;}
106 template <class Event
,class FSM
>
107 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Playing" << std::endl
;}
108 // The list of FSM states
109 struct Song1
: public msm::front::state
<>
111 typedef mpl::vector1
<FirstSongPlaying
> flag_list
;
112 template <class Event
,class FSM
>
113 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: First song" << std::endl
;}
114 template <class Event
,class FSM
>
115 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: First Song" << std::endl
;}
117 struct Song2
: public msm::front::state
<>
119 template <class Event
,class FSM
>
120 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: Second song" << std::endl
;}
121 template <class Event
,class FSM
>
122 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: Second Song" << std::endl
;}
124 struct Song3
: public msm::front::state
<>
126 template <class Event
,class FSM
>
127 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: Third song" << std::endl
;}
128 template <class Event
,class FSM
>
129 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: Third Song" << std::endl
;}
131 // the initial state. Must be defined
132 typedef Song1 initial_state
;
133 // transition actions
134 void start_next_song(NextSong
const&) { std::cout
<< "Playing::start_next_song\n"; }
135 void start_prev_song(PreviousSong
const&) { std::cout
<< "Playing::start_prev_song\n"; }
138 typedef Playing_ pl
; // makes transition table cleaner
139 // Transition table for Playing
140 struct transition_table
: mpl::vector4
<
141 // Start Event Next Action Guard
142 // +---------+-------------+---------+---------------------+----------------------+
143 a_row
< Song1
, NextSong
, Song2
, &pl::start_next_song
>,
144 a_row
< Song2
, PreviousSong
, Song1
, &pl::start_prev_song
>,
145 a_row
< Song2
, NextSong
, Song3
, &pl::start_next_song
>,
146 a_row
< Song3
, PreviousSong
, Song2
, &pl::start_prev_song
>
147 // +---------+-------------+---------+---------------------+----------------------+
149 // Replaces the default no-transition response.
150 template <class FSM
,class Event
>
151 void no_transition(Event
const& e
, FSM
&,int state
)
153 std::cout
<< "no transition from state " << state
154 << " on event " << typeid(e
).name() << std::endl
;
158 typedef msm::back::state_machine
<Playing_
> Playing
;
160 // state not defining any entry or exit
161 struct Paused
: public msm::front::state
<>
163 typedef mpl::vector2
<PlayingPaused
,CDLoaded
> flag_list
;
165 struct AllOk
: public msm::front::state
<>
167 template <class Event
,class FSM
>
168 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: AllOk" << std::endl
;}
169 template <class Event
,class FSM
>
170 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: AllOk" << std::endl
;}
172 // this state is also made terminal so that all the events are blocked
173 struct ErrorMode
: //public msm::front::terminate_state<> // ErrorMode terminates the state machine
174 public msm::front::interrupt_state
<end_error
/*mpl::vector<end_error,end_error2>*/ > // ErroMode just interrupts. Will resume if
175 // the event end_error is generated
177 template <class Event
,class FSM
>
178 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: ErrorMode" << std::endl
;}
179 template <class Event
,class FSM
>
180 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: ErrorMode" << std::endl
;}
182 // the initial state of the player SM. Must be defined
183 typedef mpl::vector
<Empty
,AllOk
> initial_state
;
185 // transition actions
186 void start_playback(play
const&) { std::cout
<< "player::start_playback\n"; }
187 void open_drawer(open_close
const&) { std::cout
<< "player::open_drawer\n"; }
188 void close_drawer(open_close
const&) { std::cout
<< "player::close_drawer\n"; }
189 void store_cd_info(cd_detected
const& cd
) {std::cout
<< "player::store_cd_info\n";}
190 void stop_playback(stop
const&) { std::cout
<< "player::stop_playback\n"; }
191 void pause_playback(pause
const&) { std::cout
<< "player::pause_playback\n"; }
192 void resume_playback(end_pause
const&) { std::cout
<< "player::resume_playback\n"; }
193 void stop_and_open(open_close
const&) { std::cout
<< "player::stop_and_open\n"; }
194 void stopped_again(stop
const&){std::cout
<< "player::stopped_again\n";}
195 void report_error(error_found
const&) {std::cout
<< "player::report_error\n";}
196 void report_end_error(end_error
const&) {std::cout
<< "player::report_end_error\n";}
200 typedef player_ p
; // makes transition table cleaner
202 // Transition table for player
203 struct transition_table
: mpl::vector
<
204 // Start Event Next Action Guard
205 // +---------+-------------+---------+---------------------+----------------------+
206 a_row
< Stopped
, play
, Playing
, &p::start_playback
>,
207 a_row
< Stopped
, open_close
, Open
, &p::open_drawer
>,
208 a_row
< Stopped
, stop
, Stopped
, &p::stopped_again
>,
209 // +---------+-------------+---------+---------------------+----------------------+
210 a_row
< Open
, open_close
, Empty
, &p::close_drawer
>,
211 // +---------+-------------+---------+---------------------+----------------------+
212 a_row
< Empty
, open_close
, Open
, &p::open_drawer
>,
213 a_row
< Empty
, cd_detected
, Stopped
, &p::store_cd_info
>,
214 // +---------+-------------+---------+---------------------+----------------------+
215 a_row
< Playing
, stop
, Stopped
, &p::stop_playback
>,
216 a_row
< Playing
, pause
, Paused
, &p::pause_playback
>,
217 a_row
< Playing
, open_close
, Open
, &p::stop_and_open
>,
218 // +---------+-------------+---------+---------------------+----------------------+
219 a_row
< Paused
, end_pause
, Playing
, &p::resume_playback
>,
220 a_row
< Paused
, stop
, Stopped
, &p::stop_playback
>,
221 a_row
< Paused
, open_close
, Open
, &p::stop_and_open
>,
222 // +---------+-------------+---------+---------------------+----------------------+
223 a_row
< AllOk
, error_found
,ErrorMode
, &p::report_error
>,
224 a_row
<ErrorMode
,end_error
,AllOk
, &p::report_end_error
>
225 // +---------+-------------+---------+---------------------+----------------------+
228 // Replaces the default no-transition response.
229 template <class FSM
,class Event
>
230 void no_transition(Event
const& e
, FSM
&,int state
)
232 std::cout
<< "no transition from state " << state
233 << " on event " << typeid(e
).name() << std::endl
;
237 typedef msm::back::state_machine
<player_
> player
;
240 // Testing utilities.
242 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused","AllOk","ErrorMode" };
244 void pstate(player
const& p
)
246 // we have now several active states, which we show
247 for (unsigned int i
=0;i
<player::nr_regions::value
;++i
)
249 std::cout
<< " -> " << state_names
[p
.current_state()[i
]] << std::endl
;
256 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
258 // test deferred event
259 // deferred in Empty and Open, will be handled only after event cd_detected
260 p
.process_event(play());
263 std::cout
<< "CDLoaded active:" << std::boolalpha
<< p
.is_flag_active
<CDLoaded
>() << std::endl
; //=> false (no CD yet)
264 // go to Open, call on_exit on Empty, then action, then on_entry on Open
265 p
.process_event(open_close()); pstate(p
);
266 p
.process_event(open_close()); pstate(p
);
267 p
.process_event(cd_detected("louie, louie"));
269 // at this point, Play is active (was deferred)
270 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> true
271 std::cout
<< "FirstSong active:" << std::boolalpha
<< p
.is_flag_active
<FirstSongPlaying
>() << std::endl
;//=> true
273 // make transition happen inside it. Player has no idea about this event but it's ok.
274 p
.process_event(NextSong());pstate(p
); //2nd song active
275 p
.process_event(NextSong());pstate(p
);//3rd song active
276 p
.process_event(PreviousSong());pstate(p
);//2nd song active
277 std::cout
<< "FirstSong active:" << std::boolalpha
<< p
.is_flag_active
<FirstSongPlaying
>() << std::endl
;//=> false
279 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> true
280 p
.process_event(pause()); pstate(p
);
281 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> true
282 // go back to Playing
283 // as you see, it starts back from the original state
284 p
.process_event(end_pause()); pstate(p
);
285 p
.process_event(pause()); pstate(p
);
286 p
.process_event(stop()); pstate(p
);
287 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> false
288 std::cout
<< "CDLoaded active:" << std::boolalpha
<< p
.is_flag_active
<CDLoaded
>() << std::endl
;//=> true
289 // by default, the flags are OR'ed but you can also use AND. Then the flag must be present in
290 // all of the active states
291 std::cout
<< "CDLoaded active with AND:" << std::boolalpha
<< p
.is_flag_active
<CDLoaded
,player::Flag_AND
>() << std::endl
;//=> false
293 // event leading to the same state
294 p
.process_event(stop()); pstate(p
);
296 // event leading to a terminal/interrupt state
297 p
.process_event(error_found()); pstate(p
);
298 // try generating more events
299 std::cout
<< "Trying to generate another event" << std::endl
; // will not work, fsm is terminated or interrupted
300 p
.process_event(play());pstate(p
);
301 std::cout
<< "Trying to end the error" << std::endl
; // will work only if ErrorMode is interrupt state
302 p
.process_event(end_error());pstate(p
);
303 std::cout
<< "Trying to generate another event" << std::endl
; // will work only if ErrorMode is interrupt state
304 p
.process_event(play());pstate(p
);
305 std::cout
<< "stop fsm" << std::endl
;