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 | #include <boost/msm/front/functor_row.hpp>
|
---|
17 |
|
---|
18 | namespace msm = boost::msm;
|
---|
19 | namespace mpl = boost::mpl;
|
---|
20 | using namespace boost::msm::front;
|
---|
21 | using namespace std;
|
---|
22 | namespace
|
---|
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 error_found {};
|
---|
33 | struct end_error {};
|
---|
34 |
|
---|
35 | // Flags. Allow information about a property of the current state
|
---|
36 | struct PlayingPaused{};
|
---|
37 | struct CDLoaded {};
|
---|
38 | struct FirstSongPlaying {};
|
---|
39 |
|
---|
40 | // A "complicated" event type that carries some data.
|
---|
41 | struct cd_detected
|
---|
42 | {
|
---|
43 | cd_detected(std::string name)
|
---|
44 | : name(name)
|
---|
45 | {}
|
---|
46 |
|
---|
47 | std::string name;
|
---|
48 | };
|
---|
49 |
|
---|
50 | // front-end: define the FSM structure
|
---|
51 | struct player_ : public msm::front::state_machine_def<player_>
|
---|
52 | {
|
---|
53 | // we want deferred events and no state requires deferred events (only the fsm in the
|
---|
54 | // transition table), so the fsm does.
|
---|
55 | typedef int activate_deferred_events;
|
---|
56 | // The list of FSM states
|
---|
57 | struct Empty : public msm::front::state<>
|
---|
58 | {
|
---|
59 | // every (optional) entry/exit methods get the event passed.
|
---|
60 | template <class Event,class FSM>
|
---|
61 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
|
---|
62 | template <class Event,class FSM>
|
---|
63 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
|
---|
64 | };
|
---|
65 | struct Open : public msm::front::state<>
|
---|
66 | {
|
---|
67 | typedef mpl::vector1<CDLoaded> flag_list;
|
---|
68 | template <class Event,class FSM>
|
---|
69 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Open" << std::endl;}
|
---|
70 | template <class Event,class FSM>
|
---|
71 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
|
---|
72 | };
|
---|
73 |
|
---|
74 | struct Stopped : public msm::front::state<>
|
---|
75 | {
|
---|
76 | // when stopped, the CD is loaded
|
---|
77 | typedef mpl::vector1<CDLoaded> flag_list;
|
---|
78 | template <class Event,class FSM>
|
---|
79 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Stopped" << std::endl;}
|
---|
80 | template <class Event,class FSM>
|
---|
81 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
|
---|
82 | };
|
---|
83 |
|
---|
84 | // the player state machine contains a state which is himself a state machine
|
---|
85 | // as you see, no need to declare it anywhere so Playing can be developed separately
|
---|
86 | // by another team in another module. For simplicity I just declare it inside player
|
---|
87 | struct Playing_ : public msm::front::state_machine_def<Playing_>
|
---|
88 | {
|
---|
89 | // when playing, the CD is loaded and we are in either pause or playing (duh)
|
---|
90 | typedef mpl::vector2<PlayingPaused,CDLoaded> flag_list;
|
---|
91 |
|
---|
92 | template <class Event,class FSM>
|
---|
93 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
|
---|
94 | template <class Event,class FSM>
|
---|
95 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
|
---|
96 | // The list of FSM states
|
---|
97 | struct Song1 : public msm::front::state<>
|
---|
98 | {
|
---|
99 | typedef mpl::vector1<FirstSongPlaying> flag_list;
|
---|
100 | template <class Event,class FSM>
|
---|
101 | void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;}
|
---|
102 | template <class Event,class FSM>
|
---|
103 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;}
|
---|
104 | };
|
---|
105 | struct Song2 : public msm::front::state<>
|
---|
106 | {
|
---|
107 | template <class Event,class FSM>
|
---|
108 | void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;}
|
---|
109 | template <class Event,class FSM>
|
---|
110 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;}
|
---|
111 | };
|
---|
112 | struct Song3 : public msm::front::state<>
|
---|
113 | {
|
---|
114 | template <class Event,class FSM>
|
---|
115 | void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;}
|
---|
116 | template <class Event,class FSM>
|
---|
117 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;}
|
---|
118 | };
|
---|
119 | // the initial state. Must be defined
|
---|
120 | typedef Song1 initial_state;
|
---|
121 | // transition actions
|
---|
122 | void start_next_song(NextSong const&) { std::cout << "Playing::start_next_song\n"; }
|
---|
123 | void start_prev_song(PreviousSong const&) { std::cout << "Playing::start_prev_song\n"; }
|
---|
124 | // guard conditions
|
---|
125 |
|
---|
126 | typedef Playing_ pl; // makes transition table cleaner
|
---|
127 | // Transition table for Playing
|
---|
128 | struct transition_table : mpl::vector4<
|
---|
129 | // Start Event Next Action Guard
|
---|
130 | // +---------+-------------+---------+---------------------+----------------------+
|
---|
131 | a_row < Song1 , NextSong , Song2 , &pl::start_next_song >,
|
---|
132 | a_row < Song2 , PreviousSong, Song1 , &pl::start_prev_song >,
|
---|
133 | a_row < Song2 , NextSong , Song3 , &pl::start_next_song >,
|
---|
134 | a_row < Song3 , PreviousSong, Song2 , &pl::start_prev_song >
|
---|
135 | // +---------+-------------+---------+---------------------+----------------------+
|
---|
136 | > {};
|
---|
137 | // Replaces the default no-transition response.
|
---|
138 | template <class FSM,class Event>
|
---|
139 | void no_transition(Event const& e, FSM&,int state)
|
---|
140 | {
|
---|
141 | std::cout << "no transition from state " << state
|
---|
142 | << " on event " << typeid(e).name() << std::endl;
|
---|
143 | }
|
---|
144 | };
|
---|
145 | // back-end
|
---|
146 | typedef msm::back::state_machine<Playing_> Playing;
|
---|
147 |
|
---|
148 | // state not defining any entry or exit
|
---|
149 | struct Paused : public msm::front::state<>
|
---|
150 | {
|
---|
151 | typedef mpl::vector2<PlayingPaused,CDLoaded> flag_list;
|
---|
152 | };
|
---|
153 | struct AllOk : public msm::front::state<>
|
---|
154 | {
|
---|
155 | template <class Event,class FSM>
|
---|
156 | void on_entry(Event const&,FSM& ) {std::cout << "starting: AllOk" << std::endl;}
|
---|
157 | template <class Event,class FSM>
|
---|
158 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: AllOk" << std::endl;}
|
---|
159 | };
|
---|
160 | // this state is also made terminal so that all the events are blocked
|
---|
161 | struct ErrorMode : //public msm::front::terminate_state<> // ErrorMode terminates the state machine
|
---|
162 | public msm::front::interrupt_state<end_error> // ErroMode just interrupts. Will resume if
|
---|
163 | // the event end_error is generated
|
---|
164 | {
|
---|
165 | template <class Event,class FSM>
|
---|
166 | void on_entry(Event const&,FSM& ) {std::cout << "starting: ErrorMode" << std::endl;}
|
---|
167 | template <class Event,class FSM>
|
---|
168 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: ErrorMode" << std::endl;}
|
---|
169 | };
|
---|
170 | // the initial state of the player SM. Must be defined
|
---|
171 | typedef mpl::vector<Empty,AllOk> initial_state;
|
---|
172 |
|
---|
173 | // transition actions
|
---|
174 | void start_playback(play const&) { std::cout << "player::start_playback\n"; }
|
---|
175 | void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
|
---|
176 | void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
|
---|
177 | void store_cd_info(cd_detected const& cd) {std::cout << "player::store_cd_info\n";}
|
---|
178 | void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
|
---|
179 | void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
|
---|
180 | void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
|
---|
181 | void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
|
---|
182 | void stopped_again(stop const&){std::cout << "player::stopped_again\n";}
|
---|
183 | void report_error(error_found const&) {std::cout << "player::report_error\n";}
|
---|
184 | void report_end_error(end_error const&) {std::cout << "player::report_end_error\n";}
|
---|
185 |
|
---|
186 | // guard conditions
|
---|
187 |
|
---|
188 | typedef player_ p; // makes transition table cleaner
|
---|
189 |
|
---|
190 | // Transition table for player
|
---|
191 | struct transition_table : mpl::vector<
|
---|
192 | // Start Event Next Action Guard
|
---|
193 | // +---------+-------------+---------+---------------------+----------------------+
|
---|
194 | a_row < Stopped , play , Playing , &p::start_playback >,
|
---|
195 | a_row < Stopped , open_close , Open , &p::open_drawer >,
|
---|
196 | a_row < Stopped , stop , Stopped , &p::stopped_again >,
|
---|
197 | // +---------+-------------+---------+---------------------+----------------------+
|
---|
198 | a_row < Open , open_close , Empty , &p::close_drawer >,
|
---|
199 | Row < Open , play , none , Defer , none >,
|
---|
200 | // +---------+-------------+---------+---------------------+----------------------+
|
---|
201 | a_row < Empty , open_close , Open , &p::open_drawer >,
|
---|
202 | a_row < Empty , cd_detected , Stopped , &p::store_cd_info >,
|
---|
203 | Row < Empty , play , none , Defer , none >,
|
---|
204 | // +---------+-------------+---------+---------------------+----------------------+
|
---|
205 | a_row < Playing , stop , Stopped , &p::stop_playback >,
|
---|
206 | a_row < Playing , pause , Paused , &p::pause_playback >,
|
---|
207 | a_row < Playing , open_close , Open , &p::stop_and_open >,
|
---|
208 | // +---------+-------------+---------+---------------------+----------------------+
|
---|
209 | a_row < Paused , end_pause , Playing , &p::resume_playback >,
|
---|
210 | a_row < Paused , stop , Stopped , &p::stop_playback >,
|
---|
211 | a_row < Paused , open_close , Open , &p::stop_and_open >,
|
---|
212 | // +---------+-------------+---------+---------------------+----------------------+
|
---|
213 | a_row < AllOk , error_found ,ErrorMode, &p::report_error >,
|
---|
214 | a_row <ErrorMode,end_error ,AllOk , &p::report_end_error >
|
---|
215 | // +---------+-------------+---------+---------------------+----------------------+
|
---|
216 | > {};
|
---|
217 |
|
---|
218 | // Replaces the default no-transition response.
|
---|
219 | template <class FSM,class Event>
|
---|
220 | void no_transition(Event const& e, FSM&,int state)
|
---|
221 | {
|
---|
222 | std::cout << "no transition from state " << state
|
---|
223 | << " on event " << typeid(e).name() << std::endl;
|
---|
224 | }
|
---|
225 | };
|
---|
226 | // Pick a back-end
|
---|
227 | typedef msm::back::state_machine<player_> player;
|
---|
228 |
|
---|
229 | //
|
---|
230 | // Testing utilities.
|
---|
231 | //
|
---|
232 | static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused","AllOk","ErrorMode" };
|
---|
233 |
|
---|
234 | void pstate(player const& p)
|
---|
235 | {
|
---|
236 | // we have now several active states, which we show
|
---|
237 | for (unsigned int i=0;i<player::nr_regions::value;++i)
|
---|
238 | {
|
---|
239 | std::cout << " -> " << state_names[p.current_state()[i]] << std::endl;
|
---|
240 | }
|
---|
241 | }
|
---|
242 |
|
---|
243 | void test()
|
---|
244 | {
|
---|
245 | player p;
|
---|
246 | // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
|
---|
247 | p.start();
|
---|
248 | // test deferred event
|
---|
249 | // deferred in Empty and Open, will be handled only after event cd_detected
|
---|
250 | p.process_event(play());
|
---|
251 |
|
---|
252 | // tests some flags
|
---|
253 | std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active<CDLoaded>() << std::endl; //=> false (no CD yet)
|
---|
254 | // go to Open, call on_exit on Empty, then action, then on_entry on Open
|
---|
255 | cout << "--------------------" << endl;
|
---|
256 | p.process_event(open_close()); pstate(p);
|
---|
257 | p.process_event( play() ); pstate(p);
|
---|
258 | cout << "--------------------" << endl;
|
---|
259 | p.process_event(open_close()); pstate(p);
|
---|
260 | p.process_event(cd_detected("louie, louie"));
|
---|
261 |
|
---|
262 | // at this point, Play is active (was deferred)
|
---|
263 | std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
|
---|
264 | std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl;//=> true
|
---|
265 |
|
---|
266 | // make transition happen inside it. Player has no idea about this event but it's ok.
|
---|
267 | p.process_event(NextSong());pstate(p); //2nd song active
|
---|
268 | p.process_event(NextSong());pstate(p);//3rd song active
|
---|
269 | p.process_event(PreviousSong());pstate(p);//2nd song active
|
---|
270 | std::cout << "FirstSong active:" << std::boolalpha << p.is_flag_active<FirstSongPlaying>() << std::endl;//=> false
|
---|
271 |
|
---|
272 | std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
|
---|
273 | p.process_event(pause()); pstate(p);
|
---|
274 | std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> true
|
---|
275 | // go back to Playing
|
---|
276 | // as you see, it starts back from the original state
|
---|
277 | p.process_event(end_pause()); pstate(p);
|
---|
278 | p.process_event(pause()); pstate(p);
|
---|
279 | p.process_event(stop()); pstate(p);
|
---|
280 | std::cout << "PlayingPaused active:" << std::boolalpha << p.is_flag_active<PlayingPaused>() << std::endl;//=> false
|
---|
281 | std::cout << "CDLoaded active:" << std::boolalpha << p.is_flag_active<CDLoaded>() << std::endl;//=> true
|
---|
282 | // by default, the flags are OR'ed but you can also use AND. Then the flag must be present in
|
---|
283 | // all of the active states
|
---|
284 | std::cout << "CDLoaded active with AND:" << std::boolalpha << p.is_flag_active<CDLoaded,player::Flag_AND>() << std::endl;//=> false
|
---|
285 |
|
---|
286 | // event leading to the same state
|
---|
287 | p.process_event(stop()); pstate(p);
|
---|
288 |
|
---|
289 | // event leading to a terminal/interrupt state
|
---|
290 | p.process_event(error_found()); pstate(p);
|
---|
291 | // try generating more events
|
---|
292 | std::cout << "Trying to generate another event" << std::endl; // will not work, fsm is terminated or interrupted
|
---|
293 | p.process_event(play());pstate(p);
|
---|
294 | std::cout << "Trying to end the error" << std::endl; // will work only if ErrorMode is interrupt state
|
---|
295 | p.process_event(end_error());pstate(p);
|
---|
296 | std::cout << "Trying to generate another event" << std::endl; // will work only if ErrorMode is interrupt state
|
---|
297 | p.process_event(play());pstate(p);
|
---|
298 |
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | int main()
|
---|
303 | {
|
---|
304 | test();
|
---|
305 | return 0;
|
---|
306 | }
|
---|