]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/PDF/examples/Flags.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
;
20 namespace // Concrete FSM implementation
29 struct PreviousSong
{};
31 // Flags. Allow information about a property of the current state
32 struct PlayingPaused
{};
34 struct FirstSongPlaying
{};
36 // A "complicated" event type that carries some data.
39 cd_detected(std::string name
)
46 // front-end: define the FSM structure
47 struct player_
: public msm::front::state_machine_def
<player_
>
49 // The list of FSM states
50 struct Empty
: public msm::front::state
<>
52 // every (optional) entry/exit methods get the event passed.
53 template <class Event
,class FSM
>
54 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Empty" << std::endl
;}
55 template <class Event
,class FSM
>
56 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Empty" << std::endl
;}
58 struct Open
: public msm::front::state
<>
60 typedef mpl::vector1
<CDLoaded
> flag_list
;
61 template <class Event
,class FSM
>
62 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Open" << std::endl
;}
63 template <class Event
,class FSM
>
64 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Open" << std::endl
;}
67 struct Stopped
: public msm::front::state
<>
69 // when stopped, the CD is loaded
70 typedef mpl::vector1
<CDLoaded
> flag_list
;
71 template <class Event
,class FSM
>
72 void on_entry(Event
const&,FSM
& ) {std::cout
<< "entering: Stopped" << std::endl
;}
73 template <class Event
,class FSM
>
74 void on_exit(Event
const&,FSM
& ) {std::cout
<< "leaving: Stopped" << std::endl
;}
77 // the player state machine contains a state which is himself a state machine
78 // as you see, no need to declare it anywhere so Playing can be developed separately
79 // by another team in another module. For simplicity I just declare it inside player
80 struct Playing_
: public msm::front::state_machine_def
<Playing_
>
82 // when playing, the CD is loaded and we are in either pause or playing (duh)
83 typedef mpl::vector2
<PlayingPaused
,CDLoaded
> flag_list
;
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
;}
90 // The list of FSM states
91 struct Song1
: public msm::front::state
<>
93 typedef mpl::vector1
<FirstSongPlaying
> flag_list
;
94 template <class Event
,class FSM
>
95 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: First song" << std::endl
;}
96 template <class Event
,class FSM
>
97 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: First Song" << std::endl
;}
100 struct Song2
: public msm::front::state
<>
102 template <class Event
,class FSM
>
103 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: Second song" << std::endl
;}
104 template <class Event
,class FSM
>
105 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: Second Song" << std::endl
;}
107 struct Song3
: public msm::front::state
<>
109 template <class Event
,class FSM
>
110 void on_entry(Event
const&,FSM
& ) {std::cout
<< "starting: Third song" << std::endl
;}
111 template <class Event
,class FSM
>
112 void on_exit(Event
const&,FSM
& ) {std::cout
<< "finishing: Third Song" << std::endl
;}
114 // the initial state. Must be defined
115 typedef Song1 initial_state
;
116 // transition actions
117 void start_next_song(NextSong
const&) { std::cout
<< "Playing::start_next_song\n"; }
118 void start_prev_song(PreviousSong
const&) { std::cout
<< "Playing::start_prev_song\n"; }
121 typedef Playing_ pl
; // makes transition table cleaner
122 // Transition table for Playing
123 struct transition_table
: mpl::vector4
<
124 // Start Event Next Action Guard
125 // +---------+-------------+---------+---------------------+----------------------+
126 a_row
< Song1
, NextSong
, Song2
, &pl::start_next_song
>,
127 a_row
< Song2
, PreviousSong
, Song1
, &pl::start_prev_song
>,
128 a_row
< Song2
, NextSong
, Song3
, &pl::start_next_song
>,
129 a_row
< Song3
, PreviousSong
, Song2
, &pl::start_prev_song
>
130 // +---------+-------------+---------+---------------------+----------------------+
132 // Replaces the default no-transition response.
133 template <class FSM
,class Event
>
134 void no_transition(Event
const& e
, FSM
&,int state
)
136 std::cout
<< "no transition from state " << state
137 << " on event " << typeid(e
).name() << std::endl
;
141 typedef msm::back::state_machine
<Playing_
> Playing
;
143 // state not defining any entry or exit
144 struct Paused
: public msm::front::state
<>
146 typedef mpl::vector2
<PlayingPaused
,CDLoaded
> flag_list
;
149 // the initial state of the player SM. Must be defined
150 typedef Empty initial_state
;
152 // transition actions
153 void start_playback(play
const&) { std::cout
<< "player::start_playback\n"; }
154 void open_drawer(open_close
const&) { std::cout
<< "player::open_drawer\n"; }
155 void close_drawer(open_close
const&) { std::cout
<< "player::close_drawer\n"; }
156 void store_cd_info(cd_detected
const& cd
) {std::cout
<< "player::store_cd_info\n";}
157 void stop_playback(stop
const&) { std::cout
<< "player::stop_playback\n"; }
158 void pause_playback(pause
const&) { std::cout
<< "player::pause_playback\n"; }
159 void resume_playback(end_pause
const&) { std::cout
<< "player::resume_playback\n"; }
160 void stop_and_open(open_close
const&) { std::cout
<< "player::stop_and_open\n"; }
161 void stopped_again(stop
const&){std::cout
<< "player::stopped_again\n";}
164 typedef player_ p
; // makes transition table cleaner
166 // Transition table for player
167 struct transition_table
: mpl::vector
<
168 // Start Event Next Action Guard
169 // +---------+-------------+---------+---------------------+----------------------+
170 a_row
< Stopped
, play
, Playing
, &p::start_playback
>,
171 a_row
< Stopped
, open_close
, Open
, &p::open_drawer
>,
172 a_row
< Stopped
, stop
, Stopped
, &p::stopped_again
>,
173 // +---------+-------------+---------+---------------------+----------------------+
174 a_row
< Open
, open_close
, Empty
, &p::close_drawer
>,
175 // +---------+-------------+---------+---------------------+----------------------+
176 a_row
< Empty
, open_close
, Open
, &p::open_drawer
>,
177 a_row
< Empty
, cd_detected
, Stopped
, &p::store_cd_info
>,
178 // +---------+-------------+---------+---------------------+----------------------+
179 a_row
< Playing
, stop
, Stopped
, &p::stop_playback
>,
180 a_row
< Playing
, pause
, Paused
, &p::pause_playback
>,
181 a_row
< Playing
, open_close
, Open
, &p::stop_and_open
>,
182 // +---------+-------------+---------+---------------------+----------------------+
183 a_row
< Paused
, end_pause
, Playing
, &p::resume_playback
>,
184 a_row
< Paused
, stop
, Stopped
, &p::stop_playback
>,
185 a_row
< Paused
, open_close
, Open
, &p::stop_and_open
>
186 // +---------+-------------+---------+---------------------+----------------------+
189 // Replaces the default no-transition response.
190 template <class FSM
,class Event
>
191 void no_transition(Event
const& e
, FSM
&,int state
)
193 std::cout
<< "no transition from state " << state
194 << " on event " << typeid(e
).name() << std::endl
;
198 typedef msm::back::state_machine
<player_
> player
;
201 // Testing utilities.
203 static char const* const state_names
[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
205 void pstate(player
const& p
)
207 std::cout
<< " -> " << state_names
[p
.current_state()[0]] << std::endl
;
214 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
217 std::cout
<< "CDLoaded active:" << std::boolalpha
<< p
.is_flag_active
<CDLoaded
>() << std::endl
; //=> false (no CD yet)
218 // go to Open, call on_exit on Empty, then action, then on_entry on Open
219 p
.process_event(open_close()); pstate(p
);
220 p
.process_event(open_close()); pstate(p
);
221 p
.process_event(cd_detected("louie, louie"));
222 p
.process_event(play());
224 // at this point, Play is active
225 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> true
226 std::cout
<< "FirstSong active:" << std::boolalpha
<< p
.is_flag_active
<FirstSongPlaying
>() << std::endl
;//=> true
228 // make transition happen inside it. Player has no idea about this event but it's ok.
229 p
.process_event(NextSong());pstate(p
); //2nd song active
230 p
.process_event(NextSong());pstate(p
);//3rd song active
231 p
.process_event(PreviousSong());pstate(p
);//2nd song active
232 std::cout
<< "FirstSong active:" << std::boolalpha
<< p
.is_flag_active
<FirstSongPlaying
>() << std::endl
;//=> false
234 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> true
235 p
.process_event(pause()); pstate(p
);
236 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> true
237 // go back to Playing
238 // as you see, it starts back from the original state
239 p
.process_event(end_pause()); pstate(p
);
240 p
.process_event(pause()); pstate(p
);
241 p
.process_event(stop()); pstate(p
);
242 std::cout
<< "PlayingPaused active:" << std::boolalpha
<< p
.is_flag_active
<PlayingPaused
>() << std::endl
;//=> false
243 std::cout
<< "CDLoaded active:" << std::boolalpha
<< p
.is_flag_active
<CDLoaded
>() << std::endl
;//=> true
245 // event leading to the same state
246 p
.process_event(stop()); pstate(p
);