]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/PDF/examples/Orthogonal-deferred.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / 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)
10
11 #include <iostream>
12 // back-end
13 #include <boost/msm/back/state_machine.hpp>
14 //front-end
15 #include <boost/msm/front/state_machine_def.hpp>
16
17 namespace msm = boost::msm;
18 namespace mpl = boost::mpl;
19
20 namespace
21 {
22 // events
23 struct play {};
24 struct end_pause {};
25 struct stop {};
26 struct pause {};
27 struct open_close {};
28 struct NextSong {};
29 struct PreviousSong {};
30 struct error_found {};
31 struct end_error {};
32 struct end_error2 {};
33
34 // Flags. Allow information about a property of the current state
35 struct PlayingPaused{};
36 struct CDLoaded {};
37 struct FirstSongPlaying {};
38
39 // A "complicated" event type that carries some data.
40 struct cd_detected
41 {
42 cd_detected(std::string name)
43 : name(name)
44 {}
45
46 std::string name;
47 };
48
49 // front-end: define the FSM structure
50 struct player_ : public msm::front::state_machine_def<player_>
51 {
52 template <class Event,class FSM>
53 void on_entry(Event const& ,FSM&)
54 {
55 std::cout << "entering: Player" << std::endl;
56 }
57 template <class Event,class FSM>
58 void on_exit(Event const&,FSM& )
59 {
60 std::cout << "leaving: Player" << std::endl;
61 }
62 // The list of FSM states
63 struct Empty : public msm::front::state<>
64 {
65 // if the play event arrives in this state, defer it until a state handles it or
66 // rejects it
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;}
73 };
74 struct Open : public msm::front::state<>
75 {
76 // if the play event arrives in this state, defer it until a state handles it or
77 // rejects it
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;}
84 };
85
86 struct Stopped : public msm::front::state<>
87 {
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;}
94 };
95
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_>
100 {
101 // when playing, the CD is loaded and we are in either pause or playing (duh)
102 typedef mpl::vector2<PlayingPaused,CDLoaded> flag_list;
103
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<>
110 {
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;}
116 };
117 struct Song2 : public msm::front::state<>
118 {
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;}
123 };
124 struct Song3 : public msm::front::state<>
125 {
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;}
130 };
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"; }
136 // guard conditions
137
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 // +---------+-------------+---------+---------------------+----------------------+
148 > {};
149 // Replaces the default no-transition response.
150 template <class FSM,class Event>
151 void no_transition(Event const& e, FSM&,int state)
152 {
153 std::cout << "no transition from state " << state
154 << " on event " << typeid(e).name() << std::endl;
155 }
156 };
157 // back-end
158 typedef msm::back::state_machine<Playing_> Playing;
159
160 // state not defining any entry or exit
161 struct Paused : public msm::front::state<>
162 {
163 typedef mpl::vector2<PlayingPaused,CDLoaded> flag_list;
164 };
165 struct AllOk : public msm::front::state<>
166 {
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;}
171 };
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
176 {
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;}
181 };
182 // the initial state of the player SM. Must be defined
183 typedef mpl::vector<Empty,AllOk> initial_state;
184
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";}
197
198 // guard conditions
199
200 typedef player_ p; // makes transition table cleaner
201
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 // +---------+-------------+---------+---------------------+----------------------+
226 > {};
227
228 // Replaces the default no-transition response.
229 template <class FSM,class Event>
230 void no_transition(Event const& e, FSM&,int state)
231 {
232 std::cout << "no transition from state " << state
233 << " on event " << typeid(e).name() << std::endl;
234 }
235 };
236 // Pick a back-end
237 typedef msm::back::state_machine<player_> player;
238
239 //
240 // Testing utilities.
241 //
242 static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused","AllOk","ErrorMode" };
243
244 void pstate(player const& p)
245 {
246 // we have now several active states, which we show
247 for (unsigned int i=0;i<player::nr_regions::value;++i)
248 {
249 std::cout << " -> " << state_names[p.current_state()[i]] << std::endl;
250 }
251 }
252
253 void test()
254 {
255 player p;
256 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
257 p.start();
258 // test deferred event
259 // deferred in Empty and Open, will be handled only after event cd_detected
260 p.process_event(play());
261
262 // tests some flags
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"));
268
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
272
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
278
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
292
293 // event leading to the same state
294 p.process_event(stop()); pstate(p);
295
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;
306 p.stop();
307
308 }
309 }
310
311 int main()
312 {
313 test();
314 return 0;
315 }