]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/HTML/examples/SimpleTutorial.cpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / msm / doc / HTML / examples / SimpleTutorial.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
29 // A "complicated" event type that carries some data.
30 enum DiskTypeEnum
31 {
32 DISK_CD=0,
33 DISK_DVD=1
34 };
35 struct cd_detected
36 {
37 cd_detected(std::string name, DiskTypeEnum diskType)
38 : name(name),
39 disc_type(diskType)
40 {}
41
42 std::string name;
43 DiskTypeEnum disc_type;
44 };
45
46 // front-end: define the FSM structure
47 struct player_ : public msm::front::state_machine_def<player_>
48 {
49 template <class Event,class FSM>
50 void on_entry(Event const& ,FSM&)
51 {
52 std::cout << "entering: Player" << std::endl;
53 }
54 template <class Event,class FSM>
55 void on_exit(Event const&,FSM& )
56 {
57 std::cout << "leaving: Player" << std::endl;
58 }
59
60 // The list of FSM states
61 struct Empty : public msm::front::state<>
62 {
63 // every (optional) entry/exit methods get the event passed.
64 template <class Event,class FSM>
65 void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
66 template <class Event,class FSM>
67 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
68 };
69 struct Open : public msm::front::state<>
70 {
71 template <class Event,class FSM>
72 void on_entry(Event const& ,FSM&) {std::cout << "entering: Open" << std::endl;}
73 template <class Event,class FSM>
74 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
75 };
76
77 // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
78 struct Stopped : public msm::front::state<msm::front::default_base_state,msm::front::sm_ptr>
79 {
80 template <class Event,class FSM>
81 void on_entry(Event const& ,FSM&) {std::cout << "entering: Stopped" << std::endl;}
82 template <class Event,class FSM>
83 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
84 void set_sm_ptr(player_* pl)
85 {
86 m_player=pl;
87 }
88 player_* m_player;
89 };
90
91 struct Playing : public msm::front::state<>
92 {
93 template <class Event,class FSM>
94 void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
95 template <class Event,class FSM>
96 void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
97 };
98
99 // state not defining any entry or exit
100 struct Paused : public msm::front::state<>
101 {
102 };
103
104 // the initial state of the player SM. Must be defined
105 typedef Empty initial_state;
106
107 // transition actions
108 void start_playback(play const&) { std::cout << "player::start_playback\n"; }
109 void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
110 void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
111 void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; }
112 void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
113 void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
114 void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
115 void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
116 void stopped_again(stop const&) {std::cout << "player::stopped_again\n";}
117 // guard conditions
118 bool good_disk_format(cd_detected const& evt)
119 {
120 // to test a guard condition, let's say we understand only CDs, not DVD
121 if (evt.disc_type != DISK_CD)
122 {
123 std::cout << "wrong disk, sorry" << std::endl;
124 return false;
125 }
126 return true;
127 }
128 // used to show a transition conflict. This guard will simply deactivate one transition and thus
129 // solve the conflict
130 bool auto_start(cd_detected const&)
131 {
132 return false;
133 }
134
135 typedef player_ p; // makes transition table cleaner
136
137 // Transition table for player
138 struct transition_table : mpl::vector<
139 // Start Event Next Action Guard
140 // +---------+-------------+---------+---------------------+----------------------+
141 a_row < Stopped , play , Playing , &p::start_playback >,
142 a_row < Stopped , open_close , Open , &p::open_drawer >,
143 _row < Stopped , stop , Stopped >,
144 // +---------+-------------+---------+---------------------+----------------------+
145 a_row < Open , open_close , Empty , &p::close_drawer >,
146 // +---------+-------------+---------+---------------------+----------------------+
147 a_row < Empty , open_close , Open , &p::open_drawer >,
148 row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >,
149 row < Empty , cd_detected , Playing , &p::store_cd_info ,&p::auto_start >,
150 // +---------+-------------+---------+---------------------+----------------------+
151 a_row < Playing , stop , Stopped , &p::stop_playback >,
152 a_row < Playing , pause , Paused , &p::pause_playback >,
153 a_row < Playing , open_close , Open , &p::stop_and_open >,
154 // +---------+-------------+---------+---------------------+----------------------+
155 a_row < Paused , end_pause , Playing , &p::resume_playback >,
156 a_row < Paused , stop , Stopped , &p::stop_playback >,
157 a_row < Paused , open_close , Open , &p::stop_and_open >
158 // +---------+-------------+---------+---------------------+----------------------+
159 > {};
160 // Replaces the default no-transition response.
161 template <class FSM,class Event>
162 void no_transition(Event const& e, FSM&,int state)
163 {
164 std::cout << "no transition from state " << state
165 << " on event " << typeid(e).name() << std::endl;
166 }
167 };
168 // Pick a back-end
169 typedef msm::back::state_machine<player_> player;
170
171 //
172 // Testing utilities.
173 //
174 static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
175 void pstate(player const& p)
176 {
177 std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
178 }
179
180 void test()
181 {
182 player p;
183 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
184 p.start();
185 // go to Open, call on_exit on Empty, then action, then on_entry on Open
186 p.process_event(open_close()); pstate(p);
187 p.process_event(open_close()); pstate(p);
188 // will be rejected, wrong disk type
189 p.process_event(
190 cd_detected("louie, louie",DISK_DVD)); pstate(p);
191 p.process_event(
192 cd_detected("louie, louie",DISK_CD)); pstate(p);
193 p.process_event(play());
194
195 // at this point, Play is active
196 p.process_event(pause()); pstate(p);
197 // go back to Playing
198 p.process_event(end_pause()); pstate(p);
199 p.process_event(pause()); pstate(p);
200 p.process_event(stop()); pstate(p);
201 // event leading to the same state
202 // no action method called as it is not present in the transition table
203 p.process_event(stop()); pstate(p);
204 std::cout << "stop fsm" << std::endl;
205 p.stop();
206 }
207 }
208
209 int main()
210 {
211 test();
212 return 0;
213 }