]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/msm/doc/PDF/examples/SM-2Arg.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / msm / doc / PDF / examples / SM-2Arg.cpp
CommitLineData
7c673cae
FG
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#include <string>
13#include "boost/mpl/vector/vector30.hpp"
14
15#include <boost/msm/back/state_machine.hpp>
16#include <boost/msm/front/state_machine_def.hpp>
17#include <boost/msm/back/tools.hpp>
18
19using namespace std;
20namespace msm = boost::msm;
21
22namespace // Concrete FSM implementation
23{
24 // events
25 struct play {};
26 struct end_pause {};
27 struct stop {};
28 struct pause {};
29 struct open_close {};
30 struct NextSong {};
31 struct PreviousSong {};
32 struct ThreeSec {};
33 struct TenSec {};
34 struct go_sleep {};
35 struct error_found {};
36 struct end_error {};
37
38 // Flags. Allow information about a property of the current state
39 struct PlayingPaused{};
40 struct CDLoaded {};
41 struct FirstSongPlaying {};
42
43 // A "complicated" event type that carries some data.
44 struct cd_detected
45 {
46 cd_detected(std::string name)
47 : name(name)
48 {}
49
50 std::string name;
51 };
52 // an easy visitor
53 struct SomeVisitor
54 {
55 template <class T>
56 void visit_state(T* astate,int i)
57 {
58 std::cout << "visiting state:" << typeid(*astate).name()
59 << " with data:" << i << std::endl;
60 }
61 };
62 // overwrite of the base state (not default)
63 struct my_visitable_state
64 {
65 // signature of the accept function
66 typedef msm::back::args<void,SomeVisitor&,int> accept_sig;
67
68 // we also want polymorphic states
69 virtual ~my_visitable_state() {}
70 // default implementation for states who do not need to be visited
71 void accept(SomeVisitor&,int) const {}
72 };
73
74 // Concrete FSM implementation
75 struct player_ : public msm::front::state_machine_def<player_,my_visitable_state>
76 {
77 template <class Event,class FSM>
78 void on_entry(Event const&,FSM& ) {std::cout << "starting: player" << std::endl;}
79 // The list of FSM states
80 struct Empty : public msm::front::state<my_visitable_state>
81 {
82 typedef mpl::vector<play> deferred_events;
83 // every (optional) entry/exit methods get the event packed as boost::any. Not useful very often.
84 template <class Event,class FSM>
85 void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
86 template <class Event,class FSM>
87 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
88 void accept(SomeVisitor& vis,int i) const
89 {
90 vis.visit_state(this,i);
91 }
92 };
93 struct Open : public msm::front::state<my_visitable_state>
94 {
95 typedef mpl::vector1<CDLoaded> flag_list;
96 typedef mpl::vector<play> deferred_events;
97 template <class Event,class FSM>
98 void on_entry(Event const&,FSM& ) {std::cout << "entering: Open" << std::endl;}
99 template <class Event,class FSM>
100 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
101 void accept(SomeVisitor& vis,int i) const
102 {
103 vis.visit_state(this,i);
104 }
105 };
106 // a state needing a pointer to the containing state machine
107 // and using for this the non-default policy
108 // if policy used, set_sm_ptr is needed
109 struct Stopped : public msm::front::state<my_visitable_state>
110 {
111 // when stopped, the CD is loaded
112 typedef mpl::vector1<CDLoaded> flag_list;
113 template <class Event,class FSM>
114 void on_entry(Event const&,FSM& ) {std::cout << "entering: Stopped" << std::endl;}
115 template <class Event,class FSM>
116 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
117 };
118 // the player state machine contains a state which is himself a state machine
119 // it demonstrates Shallow History: if the state gets activated with end_pause
120 // then it will remember the last active state and reactivate it
121 // also possible: AlwaysHistory, the last active state will always be reactivated
122 // or NoHistory, always restart from the initial state
123 struct Playing_ : public msm::front::state_machine_def<Playing_,my_visitable_state >
124 {
125 // when playing, the CD is loaded and we are in either pause or playing (duh)
126 typedef mpl::vector2<PlayingPaused,CDLoaded> flag_list;
127 template <class Event,class FSM>
128 void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
129 template <class Event,class FSM>
130 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
131 void accept(SomeVisitor& vis,int i) const
132 {
133 // note that visiting will recursively visit sub-states
134 vis.visit_state(this,i);
135 }
136 // The list of FSM states
137 // the Playing state machine contains a state which is himself a state machine
138 // so we have a SM containing a SM containing a SM
139 struct Song1_ : public msm::front::state_machine_def<Song1_,my_visitable_state>
140 {
141 typedef mpl::vector1<FirstSongPlaying> flag_list;
142 template <class Event,class FSM>
143 void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;}
144 template <class Event,class FSM>
145 void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;}
146 void accept(SomeVisitor& vis,int i) const
147 {
148 vis.visit_state(this,i);
149 }
150 struct LightOn : public msm::front::state<my_visitable_state>
151 {
152 template <class Event,class FSM>
153 void on_entry(Event const&,FSM& ) {std::cout << "starting: LightOn" << std::endl;}
154 template <class Event,class FSM>
155 void on_exit(Event const&,FSM& ) {std::cout << "finishing: LightOn" << std::endl;}
156 };
157 struct LightOff : public msm::front::state<my_visitable_state>
158 {
159 template <class Event,class FSM>
160 void on_entry(Event const&,FSM& ) {std::cout << "starting: LightOff" << std::endl;}
161 template <class Event,class FSM>
162 void on_exit(Event const&,FSM& ) {std::cout << "finishing: LightOff" << std::endl;}
163 };
164 // the initial state. Must be defined
165 typedef LightOn initial_state;
166 // transition actions
167 void turn_light_off(ThreeSec const&) { std::cout << "3s off::turn light off\n"; }
168 // guard conditions
169
170 typedef Song1_ s; // makes transition table cleaner
171 // Transition table for Song1
172 struct transition_table : mpl::vector1<
173 // Start Event Next Action Guard
174 // +---------+-------------+---------+---------------------+----------------------+
175 a_row < LightOn , ThreeSec , LightOff, &s::turn_light_off >
176 // +---------+-------------+---------+---------------------+----------------------+
177 > {};
178 // Replaces the default no-transition response.
179 template <class FSM,class Event>
180 void no_transition(Event const& e, FSM&,int state)
181 {
182 std::cout << "no transition from state " << state
183 << " on event " << typeid(e).name() << std::endl;
184 }
185 };
186 typedef msm::back::state_machine<Song1_> Song1;
187
188 struct Song2 : public msm::front::state<my_visitable_state>
189 {
190 template <class Event,class FSM>
191 void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;}
192 template <class Event,class FSM>
193 void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;}
194 };
195 struct Song3 : public msm::front::state<my_visitable_state>
196 {
197 template <class Event,class FSM>
198 void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;}
199 template <class Event,class FSM>
200 void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;}
201 };
202 // the initial state. Must be defined
203 typedef Song1 initial_state;
204 // transition actions
205 void start_next_song(NextSong const&) { std::cout << "Playing::start_next_song\n"; }
206 void start_prev_song(PreviousSong const&) { std::cout << "Playing::start_prev_song\n"; }
207 // guard conditions
208
209 typedef Playing_ pl; // makes transition table cleaner
210 // Transition table for Playing
211 struct transition_table : mpl::vector4<
212 // Start Event Next Action Guard
213 // +---------+-------------+---------+---------------------+----------------------+
214 a_row < Song1 , NextSong , Song2 , &pl::start_next_song >,
215 a_row < Song2 , PreviousSong, Song1 , &pl::start_prev_song >,
216 a_row < Song2 , NextSong , Song3 , &pl::start_next_song >,
217 a_row < Song3 , PreviousSong, Song2 , &pl::start_prev_song >
218 // +---------+-------------+---------+---------------------+----------------------+
219 > {};
220 // Replaces the default no-transition response.
221 template <class FSM,class Event>
222 void no_transition(Event const& e, FSM&,int state)
223 {
224 std::cout << "no transition from state " << state
225 << " on event " << typeid(e).name() << std::endl;
226 }
227 };
228 typedef msm::back::state_machine<Playing_,msm::back::ShallowHistory<mpl::vector<end_pause> > > Playing;
229
230 // the player state machine contains a state which is himself a state machine (2 of them, Playing and Paused)
231 struct Paused_ : public msm::front::state_machine_def<Paused_,my_visitable_state>
232 {
233 typedef mpl::vector2<PlayingPaused,CDLoaded> flag_list;
234 template <class Event,class FSM>
235 void on_entry(Event const&,FSM& ) {std::cout << "entering: Paused" << std::endl;}
236 template <class Event,class FSM>
237 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Paused" << std::endl;}
238
239 // The list of FSM states
240 struct StartBlinking : public msm::front::state<my_visitable_state>
241 {
242 template <class Event,class FSM>
243 void on_entry(Event const&,FSM& ) {std::cout << "starting: StartBlinking" << std::endl;}
244 template <class Event,class FSM>
245 void on_exit(Event const&,FSM& ) {std::cout << "finishing: StartBlinking" << std::endl;}
246 };
247 struct StopBlinking : public msm::front::state<my_visitable_state>
248 {
249 template <class Event,class FSM>
250 void on_entry(Event const&,FSM& ) {std::cout << "starting: StopBlinking" << std::endl;}
251 template <class Event,class FSM>
252 void on_exit(Event const&,FSM& ) {std::cout << "finishing: StopBlinking" << std::endl;}
253 };
254 // the initial state. Must be defined
255 typedef StartBlinking initial_state;
256 // transition actions
257 void start_blinking(TenSec const&) { std::cout << "Paused::start_blinking\n"; }
258 void stop_blinking(TenSec const&) { std::cout << "Paused::stop_blinking\n"; }
259 // guard conditions
260
261 typedef Paused_ pa; // makes transition table cleaner
262 // Transition table
263 struct transition_table : mpl::vector2<
264 // Start Event Next Action Guard
265 // +---------------+-------------+--------------+---------------------+----------------------+
266 a_row < StartBlinking , TenSec , StopBlinking , &pa::stop_blinking >,
267 a_row < StopBlinking , TenSec , StartBlinking , &pa::start_blinking >
268 // +---------------+-------------+---------------+--------------------+----------------------+
269 > {};
270 // Replaces the default no-transition response.
271 template <class FSM,class Event>
272 void no_transition(Event const& e, FSM&,int state)
273 {
274 std::cout << "no transition from state " << state
275 << " on event " << typeid(e).name() << std::endl;
276 }
277 };
278 typedef msm::back::state_machine<Paused_> Paused;
279
280 struct SleepMode : public msm::front::state<my_visitable_state>
281 {
282 }; // dummy state just to test the automatic id generation
283
284 struct AllOk : public msm::front::state<my_visitable_state>
285 {
286 template <class Event,class FSM>
287 void on_entry(Event const&,FSM& ) {std::cout << "starting: AllOk" << std::endl;}
288 template <class Event,class FSM>
289 void on_exit(Event const&,FSM& ) {std::cout << "finishing: AllOk" << std::endl;}
290 };
291 struct ErrorMode : //public terminate_state<>
292 public msm::front::interrupt_state<end_error,my_visitable_state>
293 {
294 template <class Event,class FSM>
295 void on_entry(Event const&,FSM& ) {std::cout << "starting: ErrorMode" << std::endl;}
296 template <class Event,class FSM>
297 void on_exit(Event const&,FSM& ) {std::cout << "finishing: ErrorMode" << std::endl;}
298 };
299
300 // the initial state of the player SM. Must be defined
301 typedef mpl::vector<Empty,AllOk> initial_state;
302 //typedef Empty initial_state; // this is to have only one active state
303
304 // transition actions
305 void start_playback(play const&) { std::cout << "player::start_playback\n"; }
306 void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
307 void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
308 void store_cd_info(cd_detected const&)
309 {
310 std::cout << "player::store_cd_info\n";
311 // generate another event to test the queue
312 //process_event(play());
313 }
314 void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
315 void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
316 void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
317 void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
318 void stopped_again(stop const&){std::cout << "player::stopped_again\n";}
319 void start_sleep(go_sleep const&) { }
320 void report_error(error_found const&) {std::cout << "player::report_error\n";}
321 void report_end_error(end_error const&) {std::cout << "player::report_end_error\n";}
322 // guard conditions
323
324
325 typedef player_ p; // makes transition table cleaner
326
327 // Transition table for player
328 struct transition_table : mpl::vector<
329 // Start Event Next Action Guard
330 // +---------+-------------+---------+---------------------+----------------------+
331 a_row < Stopped , play , Playing , &p::start_playback >,
332 a_row < Stopped , open_close , Open , &p::open_drawer >,
333 a_row < Stopped , stop , Stopped , &p::stopped_again >,
334 // +---------+-------------+---------+---------------------+----------------------+
335 a_row < Open , open_close , Empty , &p::close_drawer >,
336 // +---------+-------------+---------+---------------------+----------------------+
337 a_row < Empty , open_close , Open , &p::open_drawer >,
338 a_row < Empty , cd_detected , Stopped , &p::store_cd_info >,
339 // +---------+-------------+---------+---------------------+----------------------+
340 a_row < Playing , stop , Stopped , &p::stop_playback >,
341 a_row < Playing , pause , Paused , &p::pause_playback >,
342 a_row < Playing , open_close , Open , &p::stop_and_open >,
343 // +---------+-------------+---------+---------------------+----------------------+
344 a_row < Paused , end_pause , Playing , &p::resume_playback >,
345 a_row < Paused , stop , Stopped , &p::stop_playback >,
346 a_row < Paused , open_close , Open , &p::stop_and_open >,
347 a_row < Paused , go_sleep ,SleepMode, &p::start_sleep >,
348 // +---------+-------------+---------+---------------------+----------------------+
349 a_row < AllOk , error_found ,ErrorMode, &p::report_error >,
350 a_row <ErrorMode,end_error ,AllOk , &p::report_end_error >
351 // +---------+-------------+---------+---------------------+----------------------+
352 > {};
353
354 // Replaces the default no-transition response.
355 template <class FSM,class Event>
356 void no_transition(Event const& e, FSM&,int state)
357 {
358 std::cout << "no transition from state " << state
359 << " on event " << typeid(e).name() << std::endl;
360 }
361 };
362
363 // back-end
364 typedef msm::back::state_machine<player_> player;
365
366 //
367 // Testing utilities.
368 //
369
370 void pstate(player const& p)
371 {
372 static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused","AllOk","ErrorMode","SleepMode" };
373 for (unsigned int i=0;i<player::nr_regions::value;++i)
374 {
375 std::cout << " -> " << state_names[p.current_state()[i]] << std::endl;
376 }
377 }
378 void test()
379 {
380 player p;
381 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
382 p.start();
383 std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active<CDLoaded>() << std::endl; //=> false (no CD yet)
384
385 // test deferred event
386 // deferred in Empty and Open, will be handled only after event cd_detected
387 p.process_event(play());
388
389 // go to Open, call on_exit on Empty, then action, then on_entry on Open
390 p.process_event(open_close()); pstate(p);
391 // visiting Paused and AllOk, but only Paused cares
392 SomeVisitor vis;
393 p.visit_current_states(boost::ref(vis),1);
394 p.process_event(open_close()); pstate(p);
395 // visiting Empty and AllOk, but only Empty cares
396 p.visit_current_states(boost::ref(vis),2);
397
398
399 p.process_event(cd_detected("louie, louie"));
400 // no need to call play() as the previous event does it in its action method
401 //p.process_event(play());
402 // at this point, Play is active, along FirstSong and LightOn
403 pstate(p);
404 // visiting Playing+Song1 and AllOk, but only Playing+Song1 care
405 p.visit_current_states(boost::ref(vis),3);
406
407 std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
408 // call on_exit on LightOn,FirstSong,Play like stated in the UML spec.
409 // and of course on_entry on Paused and StartBlinking
410 p.process_event(pause()); pstate(p);
411 std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
412 // forward events to Paused
413 p.process_event(TenSec());
414 p.process_event(TenSec());
415 // go back to Playing
416 p.process_event(end_pause()); pstate(p);
417 std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl; //=> true
418 p.process_event(ThreeSec()); pstate(p);
419 p.process_event(NextSong());pstate(p);
420 // We are now in second song, Flag inactive
421 std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl;//=> false
422 // visiting Playing+Song2 and AllOk, but only Playing cares
423 p.visit_current_states(boost::ref(vis),4);
424
425 p.process_event(NextSong());pstate(p);
426 // 2nd song active
427 p.process_event(PreviousSong());pstate(p);
428 // Pause
429 p.process_event(pause()); pstate(p);
430 // go back to Playing
431 // but end_pause is an event activating the History
432 // => keep the last active State (SecondSong)
433 p.process_event(end_pause()); pstate(p);
434 // test of an event from a state to itself. According to UML spec, call again exit/entry from Stopped
435 p.process_event(stop()); pstate(p);
436 p.process_event(stop()); pstate(p);
437 std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active<CDLoaded>() << std::endl;//=> true
438 std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> false
439 std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl;//=> false
440 std::cout << "CDLoaded active with AND:" << std::boolalpha << p.is_flag_active<CDLoaded,player::Flag_AND>() << std::endl;//=> false
441 std::cout << "CDLoaded active with OR:" << std::boolalpha << p.is_flag_active<CDLoaded,player::Flag_OR>() << std::endl;//=> true
442
443 // go back to Playing
444 // but play is not leading to Shallow History => do not remember the last active State (SecondSong)
445 // and activate again FirstSong and LightOn
446 p.process_event(play()); pstate(p);
447 p.process_event(error_found()); pstate(p);
448
449 // try generating more events
450 std::cout << "Trying to generate another event" << std::endl; // will not work, fsm is terminated or interrupted
451 p.process_event(NextSong());pstate(p);
452
453 std::cout << "Trying to end the error" << std::endl; // will work only if ErrorMode is interrupt state
454 p.process_event(end_error());pstate(p);
455 std::cout << "Trying to generate another event" << std::endl; // will work only if ErrorMode is interrupt state
456 p.process_event(NextSong());pstate(p);
457
458 std::cout << "Simulate error. Event play is not valid" << std::endl;
459 p.process_event(play()); pstate(p);
460 }
461}
462
463int main()
464{
465 test();
466 return 0;
467}
468
469