]>
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 <vector> | |
12 | #include <iostream> | |
13 | #include <boost/mpl/vector/vector50.hpp> | |
14 | #include <boost/msm/back/state_machine.hpp> | |
15 | #include <boost/msm/front/state_machine_def.hpp> | |
16 | #include <boost/msm/back/tools.hpp> | |
17 | ||
18 | namespace msm = boost::msm; | |
19 | namespace mpl = boost::mpl; | |
20 | ||
21 | namespace // Concrete FSM implementation | |
22 | { | |
23 | // events | |
24 | struct play {}; | |
25 | struct end_pause {}; | |
26 | struct stop {}; | |
27 | struct pause {}; | |
28 | struct open_close {}; | |
29 | struct NextSong {}; | |
30 | // event which every other event can convert to | |
31 | struct AllSongsPlayed | |
32 | { | |
33 | template <class Event> | |
34 | AllSongsPlayed(Event const&){} | |
35 | }; | |
36 | struct PreviousSong {}; | |
37 | struct error_found {}; | |
38 | struct end_error {}; | |
39 | ||
40 | // Flags. Allow information about a property of the current state | |
41 | struct CDLoaded {}; | |
42 | struct FirstSongPlaying {}; | |
43 | ||
44 | // A "complicated" event type that carries some data. | |
45 | enum DiskTypeEnum | |
46 | { | |
47 | DISK_CD=0, | |
48 | DISK_DVD=1 | |
49 | }; | |
50 | struct cd_detected | |
51 | { | |
52 | cd_detected(DiskTypeEnum diskType): disc_type(diskType) {} | |
53 | DiskTypeEnum disc_type; | |
54 | }; | |
55 | ||
56 | // Concrete FSM implementation | |
57 | struct player_ : public msm::front::state_machine_def<player_> | |
58 | { | |
59 | // The list of FSM states | |
60 | struct Empty : public msm::front::state<> | |
61 | { | |
62 | typedef mpl::vector<play> deferred_events; | |
63 | // every (optional) entry/exit methods get the event packed as boost::any. Not useful very often. | |
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 | typedef mpl::vector1<CDLoaded> flag_list; | |
72 | typedef mpl::vector<play> deferred_events; | |
73 | template <class Event,class FSM> | |
74 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Open" << std::endl;} | |
75 | template <class Event,class FSM> | |
76 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;} | |
77 | }; | |
78 | // a state needing a pointer to the containing state machine | |
79 | // and using for this the non-default policy | |
80 | // if policy used, set_sm_ptr is needed | |
81 | struct Stopped : public msm::front::state<default_base_state,msm::front::sm_ptr> | |
82 | { | |
83 | // when stopped, the CD is loaded | |
84 | typedef mpl::vector1<CDLoaded> flag_list; | |
85 | template <class Event,class FSM> | |
86 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Stopped" << std::endl;} | |
87 | template <class Event,class FSM> | |
88 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;} | |
89 | void set_sm_ptr(player_* pl){m_player=pl;} | |
90 | player_* m_player; | |
91 | }; | |
92 | // the player state machine contains a state which is himself a state machine | |
93 | // it demonstrates Shallow History: if the state gets activated with end_pause | |
94 | // then it will remember the last active state and reactivate it | |
95 | // also possible: AlwaysHistory, the last active state will always be reactivated | |
96 | // or NoHistory, always restart from the initial state | |
97 | struct Playing_ : public msm::front::state_machine_def<Playing_> | |
98 | { | |
99 | // when playing, the CD is loaded and we are in either pause or playing (duh) | |
100 | typedef mpl::vector<CDLoaded> flag_list; | |
101 | template <class Event,class FSM> | |
102 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;} | |
103 | template <class Event,class FSM> | |
104 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;} | |
105 | ||
106 | // The list of FSM states | |
107 | struct Song1 : public msm::front::state<> | |
108 | { | |
109 | typedef mpl::vector1<FirstSongPlaying> flag_list; | |
110 | template <class Event,class FSM> | |
111 | void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;} | |
112 | template <class Event,class FSM> | |
113 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;} | |
114 | }; | |
115 | struct Song2 : public msm::front::state<> | |
116 | { | |
117 | template <class Event,class FSM> | |
118 | void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;} | |
119 | template <class Event,class FSM> | |
120 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;} | |
121 | }; | |
122 | struct Song3 : public msm::front::state<> | |
123 | { | |
124 | template <class Event,class FSM> | |
125 | void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;} | |
126 | template <class Event,class FSM> | |
127 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;} | |
128 | }; | |
129 | struct CDFinished : public msm::front::exit_pseudo_state<AllSongsPlayed> | |
130 | { | |
131 | template <class Event,class FSM> | |
132 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing::CDFinished" << std::endl;} | |
133 | template <class Event,class FSM> | |
134 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing::CDFinished" << std::endl;} | |
135 | }; | |
136 | // the initial state. Must be defined | |
137 | typedef Song1 initial_state; | |
138 | // transition actions | |
139 | void start_next_song(NextSong const&) { std::cout << "Playing::start_next_song\n"; } | |
140 | void start_prev_song(PreviousSong const&) { std::cout << "Playing::start_prev_song\n"; } | |
141 | void all_songs_played(NextSong const&) { std::cout << "Playing::all_songs_played\n"; } | |
142 | // guard conditions | |
143 | ||
144 | typedef Playing_ pl; // makes transition table cleaner | |
145 | // Transition table for Playing | |
146 | struct transition_table : mpl::vector< | |
147 | // Start Event Next Action Guard | |
148 | // +---------+---------------+------------+---------------------+----------------------+ | |
149 | a_row < Song1 , NextSong , Song2 , &pl::start_next_song >, | |
150 | a_row < Song2 , PreviousSong, Song1 , &pl::start_prev_song >, | |
151 | a_row < Song2 , NextSong , Song3 , &pl::start_next_song >, | |
152 | a_row < Song3 , PreviousSong, Song2 , &pl::start_prev_song >, | |
153 | a_row < Song3 , NextSong , CDFinished , &pl::all_songs_played > | |
154 | // +---------+---------------+---------+---------------------+----------------------+ | |
155 | > {}; | |
156 | // Replaces the default no-transition response. | |
157 | template <class FSM,class Event> | |
158 | void no_transition(Event const& e, FSM&,int state) | |
159 | { | |
160 | std::cout << "no transition from state " << state | |
161 | << " on event " << typeid(e).name() << std::endl; | |
162 | } | |
163 | }; | |
164 | typedef msm::back::state_machine<Playing_,msm::back::ShallowHistory<mpl::vector<end_pause> > > Playing; | |
165 | ||
166 | // the player state machine contains a state which is himself a state machine (2 of them, Playing and Paused) | |
167 | struct Paused : public msm::front::state<> | |
168 | { | |
169 | typedef mpl::vector<CDLoaded> flag_list; | |
170 | template <class Event,class FSM> | |
171 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Paused" << std::endl;} | |
172 | template <class Event,class FSM> | |
173 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Paused" << std::endl;} | |
174 | }; | |
175 | ||
176 | struct AllOk : public msm::front::state<> | |
177 | { | |
178 | template <class Event,class FSM> | |
179 | void on_entry(Event const&,FSM& ) {std::cout << "starting: AllOk" << std::endl;} | |
180 | template <class Event,class FSM> | |
181 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: AllOk" << std::endl;} | |
182 | }; | |
183 | struct ErrorMode : //public msm::front::terminate_state<> | |
184 | public msm::front::interrupt_state<end_error> | |
185 | { | |
186 | template <class Event,class FSM> | |
187 | void on_entry(Event const&,FSM& ) {std::cout << "starting: ErrorMode" << std::endl;} | |
188 | template <class Event,class FSM> | |
189 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: ErrorMode" << std::endl;} | |
190 | }; | |
191 | ||
192 | // the initial state of the player SM. Must be defined | |
193 | typedef mpl::vector<Empty,AllOk> initial_state; | |
194 | ||
195 | // transition actions | |
196 | void start_playback(play const&) { std::cout << "player::start_playback\n"; } | |
197 | void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; } | |
198 | void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; } | |
199 | void store_cd_info(cd_detected const&) | |
200 | { | |
201 | std::cout << "player::store_cd_info\n"; | |
202 | // generate another event to test the queue | |
203 | //cd.m_player.process_event(play()); | |
204 | } | |
205 | void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; } | |
206 | void end_playback (AllSongsPlayed const&) { std::cout << "player::end_playback\n"; } | |
207 | void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; } | |
208 | void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; } | |
209 | void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; } | |
210 | void stopped_again(stop const&){std::cout << "player::stopped_again\n";} | |
211 | void report_error(error_found const&) {std::cout << "player::report_error\n";} | |
212 | void report_end_error(end_error const&) {std::cout << "player::report_end_error\n";} | |
213 | // guard conditions | |
214 | bool good_disk_format(cd_detected const& evt) | |
215 | { | |
216 | // to test a guard condition, let's say we understand only CDs, not DVD | |
217 | if (evt.disc_type != DISK_CD) | |
218 | { | |
219 | std::cout << "wrong disk, sorry" << std::endl; | |
220 | return false; | |
221 | } | |
222 | return true; | |
223 | } | |
224 | ||
225 | typedef player_ p; // makes transition table cleaner | |
226 | ||
227 | // Transition table for player | |
228 | struct transition_table : mpl::vector< | |
229 | // Start Event Next Action Guard | |
230 | // +-------------+---------------+---------+---------------------+----------------------+ | |
231 | a_row < Stopped , play , Playing , &p::start_playback >, | |
232 | a_row < Stopped , open_close , Open , &p::open_drawer >, | |
233 | a_row < Stopped , stop , Stopped , &p::stopped_again >, | |
234 | // +-------------+---------------+---------+---------------------+----------------------+ | |
235 | a_row < Open , open_close , Empty , &p::close_drawer >, | |
236 | // +-------------+---------------+---------+---------------------+----------------------+ | |
237 | a_row < Empty , open_close , Open , &p::open_drawer >, | |
238 | row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >, | |
239 | // +-------------+---------------+---------+---------------------+----------------------+ | |
240 | a_row < Playing , stop , Stopped , &p::stop_playback >, | |
241 | a_row < Playing , pause , Paused , &p::pause_playback >, | |
242 | a_row < Playing , open_close , Open , &p::stop_and_open >, | |
243 | a_row < Playing::exit_pt< | |
244 | Playing_::CDFinished> , AllSongsPlayed, Stopped , &p::end_playback >, | |
245 | // +-------------+---------------+---------+---------------------+----------------------+ | |
246 | a_row < Paused , end_pause , Playing , &p::resume_playback >, | |
247 | a_row < Paused , stop , Stopped , &p::stop_playback >, | |
248 | a_row < Paused , open_close , Open , &p::stop_and_open >, | |
249 | // +-------------+---------------+---------+---------------------+----------------------+ | |
250 | a_row < AllOk , error_found ,ErrorMode, &p::report_error >, | |
251 | a_row < ErrorMode ,end_error ,AllOk , &p::report_end_error > | |
252 | // +-------------+---------------+---------+---------------------+----------------------+ | |
253 | > {}; | |
254 | ||
255 | ||
256 | // Replaces the default no-transition response. | |
257 | template <class FSM,class Event> | |
258 | void no_transition(Event const& e, FSM&,int state) | |
259 | { | |
260 | std::cout << "no transition from state " << state | |
261 | << " on event " << typeid(e).name() << std::endl; | |
262 | } | |
263 | }; | |
264 | typedef msm::back::state_machine<player_> player; | |
265 | ||
266 | // | |
267 | // Testing utilities. | |
268 | // | |
269 | ||
270 | void pstate(player const& p) | |
271 | { | |
272 | typedef player::stt Stt; | |
273 | typedef msm::back::generate_state_set<Stt>::type all_states; | |
274 | static char const* state_names[mpl::size<all_states>::value]; | |
275 | // fill the names of the states defined in the state machine | |
276 | mpl::for_each<all_states,boost::msm::wrap<mpl::placeholders::_1> > | |
277 | (msm::back::fill_state_names<Stt>(state_names)); | |
278 | ||
279 | for (unsigned int i=0;i<player::nr_regions::value;++i) | |
280 | { | |
281 | std::cout << " -> " << state_names[p.current_state()[i]] << std::endl; | |
282 | } | |
283 | } | |
284 | ||
285 | void test() | |
286 | { | |
287 | player p; | |
288 | // needed to start the highest-level SM. This will call on_entry and mark the start of the SM | |
289 | p.start(); | |
290 | std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active<CDLoaded>() << std::endl; //=> false (no CD yet) | |
291 | ||
292 | // test deferred event | |
293 | // deferred in Empty and Open, will be handled only after event cd_detected | |
294 | std::cout << "play is not handled in the current state but is marked as delayed" << std::endl; | |
295 | p.process_event(play()); pstate(p); | |
296 | std::cout << "cd_detected will cause play to be handled also" << std::endl; | |
297 | // will be rejected, wrong disk type | |
298 | p.process_event(cd_detected(DISK_DVD)); pstate(p); | |
299 | // will be accepted, wrong disk type | |
300 | p.process_event(cd_detected(DISK_CD)); pstate(p); | |
301 | ||
302 | std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl; //=> true | |
303 | p.process_event(NextSong());pstate(p); | |
304 | // We are now in second song, Flag inactive | |
305 | std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl;//=> false | |
306 | p.process_event(NextSong());pstate(p); | |
307 | // 2nd song active | |
308 | p.process_event(PreviousSong());pstate(p); | |
309 | // Pause | |
310 | p.process_event(pause()); pstate(p); | |
311 | // go back to Playing | |
312 | // but end_pause is an event activating the History | |
313 | // => keep the last active State (SecondSong) | |
314 | p.process_event(end_pause()); pstate(p); | |
315 | // force an exit by listening all the songs | |
316 | p.process_event(NextSong()); | |
317 | p.process_event(NextSong());pstate(p); | |
318 | ||
319 | std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active<CDLoaded>() << std::endl;//=> true | |
320 | std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl;//=> false | |
321 | ||
322 | // go back to Playing | |
323 | // but play is not leading to Shallow History => do not remember the last active State (SecondSong) | |
324 | // and activate again FirstSong and LightOn | |
325 | p.process_event(play()); pstate(p); | |
326 | p.process_event(error_found()); pstate(p); | |
327 | ||
328 | // try generating more events | |
329 | std::cout << "Trying to generate another event" << std::endl; // will not work, fsm is terminated or interrupted | |
330 | p.process_event(NextSong());pstate(p); | |
331 | ||
332 | std::cout << "Trying to end the error" << std::endl; // will work only if ErrorMode is interrupt state | |
333 | p.process_event(end_error());pstate(p); | |
334 | std::cout << "Trying to generate another event" << std::endl; // will work only if ErrorMode is interrupt state | |
335 | p.process_event(NextSong());pstate(p); | |
336 | ||
337 | // the states and events of the higher level FSM (player) | |
338 | typedef player::stt Stt; | |
339 | typedef msm::back::generate_state_set<Stt>::type simple_states; | |
340 | ||
341 | std::cout << "the state list:" << std::endl; | |
342 | mpl::for_each<simple_states,boost::msm::wrap<mpl::placeholders::_1> >(msm::back::display_type ()); | |
343 | ||
344 | std::cout << "the event list:" << std::endl; | |
345 | typedef msm::back::generate_event_set<Stt>::type event_list; | |
346 | mpl::for_each<event_list,boost::msm::wrap<mpl::placeholders::_1> >(msm::back::display_type ()); | |
347 | std::cout << std::endl; | |
348 | ||
349 | // the states and events recursively searched | |
350 | typedef msm::back::recursive_get_transition_table<player>::type recursive_stt; | |
351 | ||
352 | std::cout << "the state list (including sub-SMs):" << std::endl; | |
353 | ||
354 | typedef msm::back::generate_state_set<recursive_stt>::type all_states; | |
355 | mpl::for_each<all_states,boost::msm::wrap<mpl::placeholders::_1> >(msm::back::display_type ()); | |
356 | ||
357 | std::cout << "the event list (including sub-SMs):" << std::endl; | |
358 | typedef msm::back::generate_event_set<recursive_stt>::type all_events; | |
359 | mpl::for_each<all_events,boost::msm::wrap<mpl::placeholders::_1> >(msm::back::display_type ()); | |
360 | ||
361 | } | |
362 | } | |
363 | ||
364 | int main() | |
365 | { | |
366 | test(); | |
367 | return 0; | |
368 | } | |
369 | ||
370 |