]>
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 | // back-end | |
13 | #include <boost/msm/back/state_machine.hpp> | |
14 | //front-end | |
15 | #include <boost/msm/front/state_machine_def.hpp> | |
16 | ||
17 | // include headers that implement a archive in simple text format | |
18 | #include <boost/archive/text_oarchive.hpp> | |
19 | #include <boost/archive/text_iarchive.hpp> | |
20 | #include <boost/serialization/tracking.hpp> | |
21 | ||
22 | #include <fstream> | |
23 | ||
24 | namespace msm = boost::msm; | |
25 | namespace mpl = boost::mpl; | |
26 | ||
27 | namespace | |
28 | { | |
29 | // events | |
30 | struct play {}; | |
31 | struct end_pause {}; | |
32 | struct stop {}; | |
33 | struct pause {}; | |
34 | struct open_close {}; | |
35 | struct NextSong {}; | |
36 | struct PreviousSong {}; | |
37 | ||
38 | // A "complicated" event type that carries some data. | |
39 | struct cd_detected | |
40 | { | |
41 | cd_detected(std::string name) | |
42 | : name(name) | |
43 | {} | |
44 | ||
45 | std::string name; | |
46 | }; | |
47 | ||
48 | // front-end: define the FSM structure | |
49 | struct player_ : public msm::front::state_machine_def<player_> | |
50 | { | |
51 | // The list of FSM states | |
52 | struct Empty : public msm::front::state<> | |
53 | { | |
54 | // every (optional) entry/exit methods get the event passed | |
55 | template <class Event,class FSM> | |
56 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;} | |
57 | template <class Event,class FSM> | |
58 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;} | |
59 | }; | |
60 | struct Open : public msm::front::state<> | |
61 | { | |
62 | template <class Event,class FSM> | |
63 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Open" << std::endl;} | |
64 | template <class Event,class FSM> | |
65 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;} | |
66 | }; | |
67 | ||
68 | struct Stopped : public msm::front::state<> | |
69 | { | |
70 | // when stopped, the CD is loaded | |
71 | template <class Event,class FSM> | |
72 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Stopped" << std::endl;} | |
73 | template <class Event,class FSM> | |
74 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;} | |
75 | }; | |
76 | ||
77 | struct Playing_ : public msm::front::state_machine_def<Playing_> | |
78 | { | |
79 | // when playing, the CD is loaded and we are in either pause or playing (duh) | |
80 | template <class Event,class FSM> | |
81 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;} | |
82 | template <class Event,class FSM> | |
83 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;} | |
84 | ||
85 | // The list of FSM states | |
86 | struct Song1 : public msm::front::state<> | |
87 | { | |
88 | template <class Event,class FSM> | |
89 | void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;} | |
90 | template <class Event,class FSM> | |
91 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;} | |
92 | }; | |
93 | struct Song2 : public msm::front::state<> | |
94 | { | |
95 | template <class Event,class FSM> | |
96 | void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;} | |
97 | template <class Event,class FSM> | |
98 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;} | |
99 | }; | |
100 | struct Song3 : public msm::front::state<> | |
101 | { | |
102 | template <class Event,class FSM> | |
103 | void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;} | |
104 | template <class Event,class FSM> | |
105 | void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;} | |
106 | }; | |
107 | // the initial state. Must be defined | |
108 | typedef Song1 initial_state; | |
109 | // transition actions | |
110 | void start_next_song(NextSong const&) { std::cout << "Playing::start_next_song\n"; } | |
111 | void start_prev_song(PreviousSong const&) { std::cout << "Playing::start_prev_song\n"; } | |
112 | // guard conditions | |
113 | ||
114 | typedef Playing_ pl; // makes transition table cleaner | |
115 | // Transition table for Playing | |
116 | struct transition_table : mpl::vector4< | |
117 | // Start Event Next Action Guard | |
118 | // +---------+-------------+---------+---------------------+----------------------+ | |
119 | a_row < Song1 , NextSong , Song2 , &pl::start_next_song >, | |
120 | a_row < Song2 , PreviousSong, Song1 , &pl::start_prev_song >, | |
121 | a_row < Song2 , NextSong , Song3 , &pl::start_next_song >, | |
122 | a_row < Song3 , PreviousSong, Song2 , &pl::start_prev_song > | |
123 | // +---------+-------------+---------+---------------------+----------------------+ | |
124 | > {}; | |
125 | // Replaces the default no-transition response. | |
126 | template <class FSM,class Event> | |
127 | void no_transition(Event const& e, FSM&,int state) | |
128 | { | |
129 | std::cout << "no transition from state " << state | |
130 | << " on event " << typeid(e).name() << std::endl; | |
131 | } | |
132 | ||
133 | }; | |
134 | // back-end | |
135 | // demonstrates Shallow History: if the state gets activated with end_pause | |
136 | // then it will remember the last active state and reactivate it | |
137 | // also possible: AlwaysHistory, the last active state will always be reactivated | |
138 | // or NoHistory, always restart from the initial state | |
139 | typedef msm::back::state_machine<Playing_,msm::back::ShallowHistory<mpl::vector<end_pause> > > Playing; | |
140 | ||
141 | // state not defining any entry or exit | |
142 | struct Paused : public msm::front::state<> | |
143 | { | |
144 | template <class Event,class FSM> | |
145 | void on_entry(Event const&,FSM& ) {std::cout << "entering: Paused" << std::endl;} | |
146 | template <class Event,class FSM> | |
147 | void on_exit(Event const&,FSM& ) {std::cout << "leaving: Paused" << std::endl;} | |
148 | }; | |
149 | ||
150 | // the initial state of the player SM. Must be defined | |
151 | typedef Empty initial_state; | |
152 | ||
153 | // transition actions | |
154 | void start_playback(play const&) { std::cout << "player::start_playback\n"; } | |
155 | void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; } | |
156 | void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; } | |
157 | void store_cd_info(cd_detected const& cd) {std::cout << "player::store_cd_info\n";} | |
158 | void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; } | |
159 | void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; } | |
160 | void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; } | |
161 | void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; } | |
162 | void stopped_again(stop const&){std::cout << "player::stopped_again\n";} | |
163 | // guard conditions | |
164 | ||
165 | typedef player_ p; // makes transition table cleaner | |
166 | ||
167 | // Transition table for player | |
168 | struct transition_table : mpl::vector< | |
169 | // Start Event Next Action Guard | |
170 | // +---------+-------------+---------+---------------------+----------------------+ | |
171 | a_row < Stopped , play , Playing , &p::start_playback >, | |
172 | a_row < Stopped , open_close , Open , &p::open_drawer >, | |
173 | a_row < Stopped , stop , Stopped , &p::stopped_again >, | |
174 | // +---------+-------------+---------+---------------------+----------------------+ | |
175 | a_row < Open , open_close , Empty , &p::close_drawer >, | |
176 | // +---------+-------------+---------+---------------------+----------------------+ | |
177 | a_row < Empty , open_close , Open , &p::open_drawer >, | |
178 | a_row < Empty , cd_detected , Stopped , &p::store_cd_info >, | |
179 | // +---------+-------------+---------+---------------------+----------------------+ | |
180 | a_row < Playing , stop , Stopped , &p::stop_playback >, | |
181 | a_row < Playing , pause , Paused , &p::pause_playback >, | |
182 | a_row < Playing , open_close , Open , &p::stop_and_open >, | |
183 | // +---------+-------------+---------+---------------------+----------------------+ | |
184 | a_row < Paused , end_pause , Playing , &p::resume_playback >, | |
185 | a_row < Paused , stop , Stopped , &p::stop_playback >, | |
186 | a_row < Paused , open_close , Open , &p::stop_and_open > | |
187 | // +---------+-------------+---------+---------------------+----------------------+ | |
188 | > {}; | |
189 | ||
190 | // Replaces the default no-transition response. | |
191 | template <class FSM,class Event> | |
192 | void no_transition(Event const& e, FSM&,int state) | |
193 | { | |
194 | std::cout << "no transition from state " << state | |
195 | << " on event " << typeid(e).name() << std::endl; | |
196 | } | |
197 | ||
198 | }; | |
199 | // Pick a back-end | |
200 | typedef msm::back::state_machine<player_> player; | |
201 | ||
202 | // | |
203 | // Testing utilities. | |
204 | // | |
205 | static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" }; | |
206 | ||
207 | void pstate(player const& p) | |
208 | { | |
209 | std::cout << " -> " << state_names[p.current_state()[0]] << std::endl; | |
210 | } | |
211 | ||
212 | void test() | |
213 | { | |
214 | player p; | |
215 | ||
216 | // needed to start the highest-level SM. This will call on_entry and mark the start of the SM | |
217 | p.start(); | |
218 | // go to Open, call on_exit on Empty, then action, then on_entry on Open | |
219 | p.process_event(open_close()); pstate(p); | |
220 | p.process_event(open_close()); pstate(p); | |
221 | p.process_event(cd_detected("louie, louie")); | |
222 | p.process_event(play()); | |
223 | ||
224 | // at this point, Play is active | |
225 | // make transition happen inside it. Player has no idea about this event but it's ok. | |
226 | p.process_event(NextSong());pstate(p); //2nd song active | |
227 | p.process_event(NextSong());pstate(p);//3rd song active | |
228 | p.process_event(PreviousSong());pstate(p);//2nd song active | |
229 | p.process_event(pause()); pstate(p); | |
230 | std::ofstream ofs("fsm.txt"); | |
231 | // save fsm to archive (current state is Pause, Playing is in Song2) | |
232 | { | |
233 | boost::archive::text_oarchive oa(ofs); | |
234 | // write class instance to archive | |
235 | oa << p; | |
236 | } | |
237 | // reload fsm in state Open | |
238 | player p2; | |
239 | { | |
240 | // create and open an archive for input | |
241 | std::ifstream ifs("fsm.txt"); | |
242 | boost::archive::text_iarchive ia(ifs); | |
243 | // read class state from archive | |
244 | ia >> p2; | |
245 | } | |
246 | // go back to Playing | |
247 | // as you see, remembers the original state as end_pause is an history trigger | |
248 | p2.process_event(end_pause()); pstate(p2); | |
249 | p2.process_event(pause()); pstate(p2); | |
250 | p2.process_event(stop()); pstate(p2); | |
251 | // event leading to the same state | |
252 | p2.process_event(stop()); pstate(p2); | |
253 | // play does not trigger shallow history => start back from 1st song | |
254 | p2.process_event(play()); pstate(p2); | |
255 | } | |
256 | } | |
257 | // eliminate object tracking (even if serialized through a pointer) | |
258 | // at the risk of a programming error creating duplicate objects. | |
259 | // this is to get rid of warning because p is not const | |
260 | BOOST_CLASS_TRACKING(player, boost::serialization::track_never) | |
261 | ||
262 | int main() | |
263 | { | |
264 | test(); | |
265 | return 0; | |
266 | } |