]>
Commit | Line | Data |
---|---|---|
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 // Concrete FSM implementation | |
21 | { | |
22 | // events | |
23 | struct play {}; | |
24 | struct end_pause {}; | |
25 | struct stop {}; | |
26 | struct pause {}; | |
27 | struct open_close {}; | |
28 | struct NextSong {}; | |
29 | struct PreviousSong {}; | |
30 | ||
31 | // A "complicated" event type that carries some data. | |
32 | struct cd_detected | |
33 | { | |
34 | cd_detected(std::string name) | |
35 | : name(name) | |
36 | {} | |
37 | ||
38 | std::string name; | |
39 | }; | |
40 | ||
41 | // front-end: define the FSM structure | |
42 | struct player_ : public msm::front::state_machine_def<player_> | |
43 | { | |
44 | template <class Event,class FSM> | |
45 | void on_entry(Event const& ,FSM&) {std::cout << "entering: Player" << std::endl;} | |
46 | template <class Event,class FSM> | |
47 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Player" << std::endl;} | |
48 | ||
49 | // The list of FSM states | |
50 | struct Empty : public msm::front::state<> | |
51 | { | |
52 | // every (optional) entry/exit methods get the event passed. | |
53 | template <class Event,class FSM> | |
54 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;} | |
55 | template <class Event,class FSM> | |
56 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;} | |
57 | }; | |
58 | struct Open : public msm::front::state<> | |
59 | { | |
60 | template <class Event,class FSM> | |
61 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Open" << std::endl;} | |
62 | template <class Event,class FSM> | |
63 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;} | |
64 | }; | |
65 | ||
66 | struct Stopped : public msm::front::state<> | |
67 | { | |
68 | // when stopped, the CD is loaded | |
69 | template <class Event,class FSM> | |
70 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Stopped" << std::endl;} | |
71 | template <class Event,class FSM> | |
72 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;} | |
73 | }; | |
74 | ||
75 | // the player state machine contains a state which is himself a state machine | |
76 | // as you see, no need to declare it anywhere so Playing can be developed separately | |
77 | // by another team in another module. For simplicity I just declare it inside player | |
78 | struct Playing_ : public msm::front::state_machine_def<Playing_> | |
79 | { | |
80 | // when playing, the CD is loaded and we are in either pause or playing (duh) | |
81 | template <class Event,class FSM> | |
82 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;} | |
83 | template <class Event,class FSM> | |
84 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;} | |
85 | ||
86 | // The list of FSM states | |
87 | struct Song1 : public msm::front::state<> | |
88 | { | |
89 | template <class Event,class FSM> | |
90 | void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;} | |
91 | template <class Event,class FSM> | |
92 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;} | |
93 | ||
94 | }; | |
95 | struct Song2 : public msm::front::state<> | |
96 | { | |
97 | template <class Event,class FSM> | |
98 | void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;} | |
99 | template <class Event,class FSM> | |
100 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;} | |
101 | }; | |
102 | struct Song3 : public msm::front::state<> | |
103 | { | |
104 | template <class Event,class FSM> | |
105 | void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;} | |
106 | template <class Event,class FSM> | |
107 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;} | |
108 | }; | |
109 | // the initial state. Must be defined | |
110 | typedef Song1 initial_state; | |
111 | // transition actions | |
112 | void start_next_song(NextSong const&) { std::cout << "Playing::start_next_song\n"; } | |
113 | void start_prev_song(PreviousSong const&) { std::cout << "Playing::start_prev_song\n"; } | |
114 | // guard conditions | |
115 | ||
116 | typedef Playing_ pl; // makes transition table cleaner | |
117 | // Transition table for Playing | |
118 | struct transition_table : mpl::vector4< | |
119 | // Start Event Next Action Guard | |
120 | // +---------+-------------+---------+---------------------+----------------------+ | |
121 | a_row < Song1 , NextSong , Song2 , &pl::start_next_song >, | |
122 | a_row < Song2 , PreviousSong, Song1 , &pl::start_prev_song >, | |
123 | a_row < Song2 , NextSong , Song3 , &pl::start_next_song >, | |
124 | a_row < Song3 , PreviousSong, Song2 , &pl::start_prev_song > | |
125 | // +---------+-------------+---------+---------------------+----------------------+ | |
126 | > {}; | |
127 | // Replaces the default no-transition response. | |
128 | template <class FSM,class Event> | |
129 | void no_transition(Event const& e, FSM&,int state) | |
130 | { | |
131 | std::cout << "no transition from state " << state | |
132 | << " on event " << typeid(e).name() << std::endl; | |
133 | } | |
134 | }; | |
135 | // back-end | |
136 | typedef msm::back::state_machine<Playing_> Playing; | |
137 | ||
138 | ||
139 | // state not defining any entry or exit | |
140 | struct Paused : public msm::front::state<> | |
141 | { | |
142 | }; | |
143 | ||
144 | // the initial state of the player SM. Must be defined | |
145 | typedef Empty initial_state; | |
146 | ||
147 | // transition actions | |
148 | void start_playback(play const&) { std::cout << "player::start_playback\n"; } | |
149 | void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; } | |
150 | void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; } | |
151 | void store_cd_info(cd_detected const& cd) {std::cout << "player::store_cd_info\n";} | |
152 | void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; } | |
153 | void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; } | |
154 | void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; } | |
155 | void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; } | |
156 | void stopped_again(stop const&){std::cout << "player::stopped_again\n";} | |
157 | // guard conditions | |
158 | ||
159 | typedef player_ p; // makes transition table cleaner | |
160 | ||
161 | // Transition table for player | |
162 | struct transition_table : mpl::vector< | |
163 | // Start Event Next Action Guard | |
164 | // +---------+-------------+---------+---------------------+----------------------+ | |
165 | a_row < Stopped , play , Playing , &p::start_playback >, | |
166 | a_row < Stopped , open_close , Open , &p::open_drawer >, | |
167 | a_row < Stopped , stop , Stopped , &p::stopped_again >, | |
168 | // +---------+-------------+---------+---------------------+----------------------+ | |
169 | a_row < Open , open_close , Empty , &p::close_drawer >, | |
170 | // +---------+-------------+---------+---------------------+----------------------+ | |
171 | a_row < Empty , open_close , Open , &p::open_drawer >, | |
172 | a_row < Empty , cd_detected , Stopped , &p::store_cd_info >, | |
173 | // +---------+-------------+---------+---------------------+----------------------+ | |
174 | a_row < Playing , stop , Stopped , &p::stop_playback >, | |
175 | a_row < Playing , pause , Paused , &p::pause_playback >, | |
176 | a_row < Playing , open_close , Open , &p::stop_and_open >, | |
177 | // +---------+-------------+---------+---------------------+----------------------+ | |
178 | a_row < Paused , end_pause , Playing , &p::resume_playback >, | |
179 | a_row < Paused , stop , Stopped , &p::stop_playback >, | |
180 | a_row < Paused , open_close , Open , &p::stop_and_open > | |
181 | // +---------+-------------+---------+---------------------+----------------------+ | |
182 | > {}; | |
183 | ||
184 | // Replaces the default no-transition response. | |
185 | template <class FSM,class Event> | |
186 | void no_transition(Event const& e, FSM&,int state) | |
187 | { | |
188 | std::cout << "no transition from state " << state | |
189 | << " on event " << typeid(e).name() << std::endl; | |
190 | } | |
191 | ||
192 | }; | |
193 | // Pick a back-end | |
194 | typedef msm::back::state_machine<player_> player; | |
195 | ||
196 | // | |
197 | // Testing utilities. | |
198 | // | |
199 | static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" }; | |
200 | ||
201 | void pstate(player const& p) | |
202 | { | |
203 | std::cout << " -> " << state_names[p.current_state()[0]] << std::endl; | |
204 | } | |
205 | ||
206 | void test() | |
207 | { | |
208 | player p; | |
209 | // needed to start the highest-level SM. This will call on_entry and mark the start of the SM | |
210 | p.start(); | |
211 | // go to Open, call on_exit on Empty, then action, then on_entry on Open | |
212 | p.process_event(open_close()); pstate(p); | |
213 | p.process_event(open_close()); pstate(p); | |
214 | p.process_event(cd_detected("louie, louie")); | |
215 | p.process_event(play()); | |
216 | ||
217 | // at this point, Play is active | |
218 | // make transition happen inside it. Player has no idea about this event but it's ok. | |
219 | p.process_event(NextSong());pstate(p); //2nd song active | |
220 | p.process_event(NextSong());pstate(p);//3rd song active | |
221 | p.process_event(PreviousSong());pstate(p);//2nd song active | |
222 | ||
223 | p.process_event(pause()); pstate(p); | |
224 | // go back to Playing | |
225 | // as you see, it starts back from the original state | |
226 | p.process_event(end_pause()); pstate(p); | |
227 | p.process_event(pause()); pstate(p); | |
228 | p.process_event(stop()); pstate(p); | |
229 | // event leading to the same state | |
230 | p.process_event(stop()); pstate(p); | |
231 | // stop the fsm (call on_exit's, including the submachines) | |
232 | p.process_event(play()); | |
233 | std::cout << "stop fsm" << std::endl; | |
234 | p.stop(); | |
235 | std::cout << "restart fsm" << std::endl; | |
236 | p.start(); | |
237 | } | |
238 | } | |
239 | ||
240 | int main() | |
241 | { | |
242 | test(); | |
243 | return 0; | |
244 | } |