]>
Commit | Line | Data |
---|---|---|
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 "boost/mpl/vector/vector30.hpp" | |
13 | ||
14 | // back-end | |
15 | #include <boost/msm/back/state_machine.hpp> | |
16 | //front-end | |
17 | #include <boost/msm/front/state_machine_def.hpp> | |
18 | #include <boost/msm/front/functor_row.hpp> | |
19 | #include <boost/msm/front/internal_row.hpp> | |
20 | ||
21 | namespace msm = boost::msm; | |
22 | namespace mpl = boost::mpl; | |
23 | using namespace msm::front; | |
24 | ||
25 | namespace | |
26 | { | |
27 | // events | |
28 | struct play {}; | |
29 | struct end_pause {}; | |
30 | struct stop {}; | |
31 | struct pause {}; | |
32 | struct internal_event {}; | |
33 | struct open_close {}; | |
34 | struct NextSong {}; | |
35 | struct PreviousSong {}; | |
36 | struct to_ignore {}; | |
37 | ||
38 | // A "complicated" event type that carries some data. | |
39 | enum DiskTypeEnum | |
40 | { | |
41 | DISK_CD=0, | |
42 | DISK_DVD=1 | |
43 | }; | |
44 | struct cd_detected | |
45 | { | |
46 | cd_detected(std::string name, DiskTypeEnum diskType) | |
47 | : name(name), | |
48 | disc_type(diskType) | |
49 | {} | |
50 | ||
51 | std::string name; | |
52 | DiskTypeEnum disc_type; | |
53 | }; | |
54 | ||
55 | // front-end: define the FSM structure | |
56 | struct player_ : public msm::front::state_machine_def<player_> | |
57 | { | |
58 | // transition actions | |
59 | void start_playback(play const&) { std::cout << "player::start_playback\n"; } | |
60 | void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; } | |
61 | void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; } | |
62 | void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; } | |
63 | void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; } | |
64 | void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; } | |
65 | void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; } | |
66 | void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; } | |
67 | void stopped_again(stop const&) {std::cout << "player::stopped_again\n";} | |
68 | // guard conditions | |
69 | bool good_disk_format(cd_detected const& evt) | |
70 | { | |
71 | // to test a guard condition, let's say we understand only CDs, not DVD | |
72 | if (evt.disc_type != DISK_CD) | |
73 | { | |
74 | std::cout << "wrong disk, sorry" << std::endl; | |
75 | return false; | |
76 | } | |
77 | return true; | |
78 | } | |
79 | // transitions internal to Empty | |
80 | void internal_action(cd_detected const&){ std::cout << "Empty::internal action\n"; } | |
81 | bool internal_guard(cd_detected const&) | |
82 | { | |
83 | std::cout << "Empty::internal guard\n"; | |
84 | return false; | |
85 | } | |
86 | void internal_action(internal_event const&){ std::cout << "Playing::internal action\n"; } | |
87 | bool internal_guard(internal_event const&) | |
88 | { | |
89 | std::cout << "Playing::internal guard\n"; | |
90 | return false; | |
91 | } | |
92 | ||
93 | // The list of FSM states | |
94 | struct Empty : public msm::front::state<> | |
95 | { | |
96 | // every (optional) entry/exit methods get the event passed. | |
97 | template <class Event,class FSM> | |
98 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;} | |
99 | template <class Event,class FSM> | |
100 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;} | |
101 | ||
102 | struct internal_guard_fct | |
103 | { | |
104 | template <class EVT,class FSM,class SourceState,class TargetState> | |
105 | bool operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& ) | |
106 | { | |
107 | std::cout << "Empty::internal guard functor\n"; | |
108 | return false; | |
109 | } | |
110 | }; | |
111 | struct internal_action_fct | |
112 | { | |
113 | template <class EVT,class FSM,class SourceState,class TargetState> | |
114 | void operator()(EVT const& ,FSM& ,SourceState& ,TargetState& ) | |
115 | { | |
116 | std::cout << "Empty::internal action functor" << std::endl; | |
117 | } | |
118 | }; | |
119 | void internal_action(to_ignore const&) { std::cout << "Empty::(almost)ignoring event\n"; } | |
120 | // Transition table for Empty | |
121 | struct internal_transition_table : mpl::vector< | |
122 | // Start Event Next Action Guard | |
123 | Row < Empty , cd_detected , none , internal_action_fct ,internal_guard_fct >, | |
124 | Internal < cd_detected , internal_action_fct ,internal_guard_fct >, | |
125 | a_internal< to_ignore , Empty,&Empty::internal_action > | |
126 | // +---------+-------------+----------+------------------------+----------------------+ | |
127 | > {}; | |
128 | }; | |
129 | struct Open : public msm::front::state<> | |
130 | { | |
131 | template <class Event,class FSM> | |
132 | void on_entry(Event const& ,FSM&) {std::cout << "entering: Open" << std::endl;} | |
133 | template <class Event,class FSM> | |
134 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;} | |
135 | }; | |
136 | ||
137 | struct Stopped : public msm::front::state<> | |
138 | { | |
139 | template <class Event,class FSM> | |
140 | void on_entry(Event const& ,FSM&) {std::cout << "entering: Stopped" << std::endl;} | |
141 | template <class Event,class FSM> | |
142 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;} | |
143 | }; | |
144 | ||
145 | struct Playing_ : public msm::front::state_machine_def<Playing_> | |
146 | { | |
147 | // when playing, the CD is loaded and we are in either pause or playing (duh) | |
148 | template <class Event,class FSM> | |
149 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;} | |
150 | template <class Event,class FSM> | |
151 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;} | |
152 | ||
153 | // The list of FSM states | |
154 | struct Song1 : public msm::front::state<> | |
155 | { | |
156 | template <class Event,class FSM> | |
157 | void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;} | |
158 | template <class Event,class FSM> | |
159 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;} | |
160 | ||
161 | }; | |
162 | struct Song2 : public msm::front::state<> | |
163 | { | |
164 | template <class Event,class FSM> | |
165 | void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;} | |
166 | template <class Event,class FSM> | |
167 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;} | |
168 | }; | |
169 | struct Song3 : public msm::front::state<> | |
170 | { | |
171 | template <class Event,class FSM> | |
172 | void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;} | |
173 | template <class Event,class FSM> | |
174 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;} | |
175 | }; | |
176 | // the initial state. Must be defined | |
177 | typedef Song1 initial_state; | |
178 | // transition actions | |
179 | void start_next_song(NextSong const&){std::cout << "Playing: start_next_song" << std::endl;} | |
180 | void start_prev_song(PreviousSong const&){std::cout << "Playing: start_prev_song" << std::endl;} | |
181 | // guard conditions | |
182 | struct playing_internal_guard | |
183 | { | |
184 | template <class EVT,class FSM,class SourceState,class TargetState> | |
185 | bool operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& ) | |
186 | { | |
187 | std::cout << "Playing::internal guard fct\n"; | |
188 | return true; | |
189 | } | |
190 | }; | |
191 | struct playing_false_guard | |
192 | { | |
193 | template <class EVT,class FSM,class SourceState,class TargetState> | |
194 | bool operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& ) | |
195 | { | |
196 | std::cout << "Playing::false guard\n"; | |
197 | return false; | |
198 | } | |
199 | }; | |
200 | struct playing_internal_fct | |
201 | { | |
202 | template <class EVT,class FSM,class SourceState,class TargetState> | |
203 | void operator()(EVT const& evt ,FSM&,SourceState& ,TargetState& ) | |
204 | { | |
205 | std::cout << "Playing::internal fct\n"; | |
206 | } | |
207 | }; | |
208 | typedef Playing_ pl; // makes transition table cleaner | |
209 | // Transition table for Playing | |
210 | struct transition_table : mpl::vector4< | |
211 | // Start Event Next Action Guard | |
212 | // +---------+---------------+---------+---------------------+----------------------+ | |
213 | a_row < Song1 , NextSong , Song2 , &pl::start_next_song >, | |
214 | a_row < Song2 , PreviousSong , Song1 , &pl::start_prev_song >, | |
215 | a_row < Song2 , NextSong , Song3 , &pl::start_next_song >, | |
216 | a_row < Song3 , PreviousSong , Song2 , &pl::start_prev_song > | |
217 | // +---------+-------------+---------+---------------------+----------------------+ | |
218 | > {}; | |
219 | // Internal transition table for Playing | |
220 | // +---------+----------------+---------+---------------------+-----------------------+ | |
221 | struct internal_transition_table : mpl::vector< | |
222 | // normal internal transition | |
223 | // Start Event Next Action Guard | |
224 | Internal < internal_event , playing_internal_fct,playing_internal_guard >, | |
225 | // conflict between internal and the external defined above | |
226 | Internal < PreviousSong , playing_internal_fct,playing_false_guard >, | |
227 | internal < internal_event , player_,&player_::internal_action, | |
228 | player_,&player_::internal_guard > | |
229 | // +---------+----------------+---------+---------------------+-----------------------+ | |
230 | > {}; | |
231 | ||
232 | // Replaces the default no-transition response. | |
233 | template <class FSM,class Event> | |
234 | void no_transition(Event const& e, FSM&,int state) | |
235 | { | |
236 | std::cout << "no transition from state " << state | |
237 | << " on event " << typeid(e).name() << std::endl; | |
238 | } | |
239 | }; | |
240 | // back-end | |
241 | typedef msm::back::state_machine<Playing_> Playing; | |
242 | ||
243 | // state not defining any entry or exit | |
244 | struct Paused : public msm::front::state<> | |
245 | { | |
246 | }; | |
247 | ||
248 | // the initial state of the player SM. Must be defined | |
249 | typedef Empty initial_state; | |
250 | ||
251 | typedef player_ p; // makes transition table cleaner | |
252 | ||
253 | // Transition table for player | |
254 | struct transition_table : mpl::vector< | |
255 | // Start Event Next Action Guard | |
256 | // +---------+-------------+---------+---------------------+----------------------+ | |
257 | a_row < Stopped , play , Playing , &p::start_playback >, | |
258 | a_row < Stopped , open_close , Open , &p::open_drawer >, | |
259 | _row < Stopped , stop , Stopped >, | |
260 | // +---------+-------------+---------+---------------------+----------------------+ | |
261 | a_row < Open , open_close , Empty , &p::close_drawer >, | |
262 | // +---------+-------------+---------+---------------------+----------------------+ | |
263 | a_row < Empty , open_close , Open , &p::open_drawer >, | |
264 | // conflict between a normal and 2 internal transitions (irow/g_irow) | |
265 | // + a state-defined internals defined 2 ways (see Empty) | |
266 | row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >, | |
267 | irow < Empty , cd_detected , &p::internal_action ,&p::internal_guard >, | |
268 | g_irow < Empty , cd_detected ,&p::internal_guard >, | |
269 | // +---------+-------------+---------+---------------------+----------------------+ | |
270 | a_row < Playing , stop , Stopped , &p::stop_playback >, | |
271 | a_row < Playing , pause , Paused , &p::pause_playback >, | |
272 | a_row < Playing , open_close , Open , &p::stop_and_open >, | |
273 | // +---------+-------------+---------+---------------------+----------------------+ | |
274 | a_row < Paused , end_pause , Playing , &p::resume_playback >, | |
275 | a_row < Paused , stop , Stopped , &p::stop_playback >, | |
276 | a_row < Paused , open_close , Open , &p::stop_and_open > | |
277 | // +---------+-------------+---------+---------------------+----------------------+ | |
278 | > {}; | |
279 | // Replaces the default no-transition response. | |
280 | template <class FSM,class Event> | |
281 | void no_transition(Event const& e, FSM&,int state) | |
282 | { | |
283 | std::cout << "no transition from state " << state | |
284 | << " on event " << typeid(e).name() << std::endl; | |
285 | } | |
286 | }; | |
287 | // Pick a back-end | |
288 | typedef msm::back::state_machine<player_> player; | |
289 | ||
290 | // | |
291 | // Testing utilities. | |
292 | // | |
293 | static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" }; | |
294 | void pstate(player const& p) | |
295 | { | |
296 | std::cout << " -> " << state_names[p.current_state()[0]] << std::endl; | |
297 | } | |
298 | ||
299 | void test() | |
300 | { | |
301 | player p; | |
302 | // needed to start the highest-level SM. This will call on_entry and mark the start of the SM | |
303 | p.start(); | |
304 | // this event will be ignored and not call no_transition | |
305 | p.process_event(to_ignore()); | |
306 | // go to Open, call on_exit on Empty, then action, then on_entry on Open | |
307 | p.process_event(open_close()); pstate(p); | |
308 | p.process_event(open_close()); pstate(p); | |
309 | // will be rejected, wrong disk type | |
310 | p.process_event( | |
311 | cd_detected("louie, louie",DISK_DVD)); pstate(p); | |
312 | p.process_event( | |
313 | cd_detected("louie, louie",DISK_CD)); pstate(p); | |
314 | p.process_event(play()); | |
315 | p.process_event(NextSong()); | |
316 | std::cout << "sending an internal event" << std::endl; | |
317 | p.process_event(internal_event()); | |
318 | std::cout << "conflict between the internal and normal transition. Internal is tried last" << std::endl; | |
319 | p.process_event(PreviousSong()); | |
320 | ||
321 | // at this point, Play is active | |
322 | p.process_event(pause()); pstate(p); | |
323 | // go back to Playing | |
324 | p.process_event(end_pause()); pstate(p); | |
325 | p.process_event(pause()); pstate(p); | |
326 | p.process_event(stop()); pstate(p); | |
327 | // event leading to the same state | |
328 | // no action method called as it is not present in the transition table | |
329 | p.process_event(stop()); pstate(p); | |
330 | } | |
331 | } | |
332 | ||
333 | int main() | |
334 | { | |
335 | test(); | |
336 | return 0; | |
337 | } |