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// back-end
12#include <boost/msm/back/state_machine.hpp>
13//front-end
14#include <boost/msm/front/state_machine_def.hpp>
15#ifndef BOOST_MSM_NONSTANDALONE_TEST
16#define BOOST_TEST_MODULE MyTest
17#endif
18#include <boost/test/unit_test.hpp>
19// include headers that implement a archive in simple text format
20#include <boost/archive/text_oarchive.hpp>
21#include <boost/archive/text_iarchive.hpp>
22#include <boost/serialization/tracking.hpp>
23
24#include <sstream>
25
26namespace msm = boost::msm;
27namespace mpl = boost::mpl;
28
29namespace
30{
31 // events
32 struct play {};
33 struct end_pause {};
34 struct stop {};
35 struct pause {};
36 struct open_close {};
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 unsigned int start_playback_counter;
59 unsigned int can_close_drawer_counter;
60 int front_end_data;
61
62 player_():
63 start_playback_counter(0),
64 can_close_drawer_counter(0),
65 front_end_data(4)
66 {}
67
68 //we want to serialize some data contained by the front-end
69 // to achieve this, ask for it
70 typedef int do_serialize;
71 // and provide a serialize
72 template<class Archive>
73 void serialize(Archive & ar, const unsigned int )
74 {
75 ar & front_end_data;
76 }
77
78 // The list of FSM states
79 struct Empty : public msm::front::state<>
80 {
81 template <class Event,class FSM>
82 void on_entry(Event const&,FSM& ) {++entry_counter;}
83 template <class Event,class FSM>
84 void on_exit(Event const&,FSM& ) {++exit_counter;}
85 int entry_counter;
86 int exit_counter;
87 int some_dummy_data;
88 // we want Empty to be serialized
89 typedef int do_serialize;
90 template<class Archive>
91 void serialize(Archive & ar, const unsigned int )
92 {
93 ar & some_dummy_data;
94 }
95
96 };
97 struct Open : public msm::front::state<>
98 {
99 template <class Event,class FSM>
100 void on_entry(Event const&,FSM& ) {++entry_counter;}
101 template <class Event,class FSM>
102 void on_exit(Event const&,FSM& ) {++exit_counter;}
103 int entry_counter;
104 int exit_counter;
105 };
106
107 // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
108 struct Stopped : public msm::front::state<>
109 {
110 template <class Event,class FSM>
111 void on_entry(Event const&,FSM& ) {++entry_counter;}
112 template <class Event,class FSM>
113 void on_exit(Event const&,FSM& ) {++exit_counter;}
114 int entry_counter;
115 int exit_counter;
116 };
117
118 struct Playing : public msm::front::state<>
119 {
120 template <class Event,class FSM>
121 void on_entry(Event const&,FSM& ) {++entry_counter;}
122 template <class Event,class FSM>
123 void on_exit(Event const&,FSM& ) {++exit_counter;}
124 int entry_counter;
125 int exit_counter;
126 };
127
128 // state not defining any entry or exit
129 struct Paused : public msm::front::state<>
130 {
131 template <class Event,class FSM>
132 void on_entry(Event const&,FSM& ) {++entry_counter;}
133 template <class Event,class FSM>
134 void on_exit(Event const&,FSM& ) {++exit_counter;}
135 int entry_counter;
136 int exit_counter;
137 };
138
139 // the initial state of the player SM. Must be defined
140 typedef Empty initial_state;
141
142 // transition actions
143 void start_playback(play const&) {++start_playback_counter; }
144 void open_drawer(open_close const&) { }
145 void store_cd_info(cd_detected const&) { }
146 void stop_playback(stop const&) { }
147 void pause_playback(pause const&) { }
148 void resume_playback(end_pause const&) { }
149 void stop_and_open(open_close const&) { }
150 void stopped_again(stop const&){}
151 // guard conditions
152 bool good_disk_format(cd_detected const& evt)
153 {
154 // to test a guard condition, let's say we understand only CDs, not DVD
155 if (evt.disc_type != DISK_CD)
156 {
157 return false;
158 }
159 return true;
160 }
161 bool can_close_drawer(open_close const&)
162 {
163 ++can_close_drawer_counter;
164 return true;
165 }
166
167 typedef player_ p; // makes transition table cleaner
168
169 // Transition table for player
170 struct transition_table : mpl::vector<
171 // Start Event Next Action Guard
172 // +---------+-------------+---------+---------------------+----------------------+
173 a_row < Stopped , play , Playing , &p::start_playback >,
174 a_row < Stopped , open_close , Open , &p::open_drawer >,
175 _row < Stopped , stop , Stopped >,
176 // +---------+-------------+---------+---------------------+----------------------+
177 g_row < Open , open_close , Empty , &p::can_close_drawer >,
178 // +---------+-------------+---------+---------------------+----------------------+
179 a_row < Empty , open_close , Open , &p::open_drawer >,
180 row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >,
181 // +---------+-------------+---------+---------------------+----------------------+
182 a_row < Playing , stop , Stopped , &p::stop_playback >,
183 a_row < Playing , pause , Paused , &p::pause_playback >,
184 a_row < Playing , open_close , Open , &p::stop_and_open >,
185 // +---------+-------------+---------+---------------------+----------------------+
186 a_row < Paused , end_pause , Playing , &p::resume_playback >,
187 a_row < Paused , stop , Stopped , &p::stop_playback >,
188 a_row < Paused , open_close , Open , &p::stop_and_open >
189 // +---------+-------------+---------+---------------------+----------------------+
190 > {};
191 // Replaces the default no-transition response.
192 template <class FSM,class Event>
193 void no_transition(Event const& , FSM&,int)
194 {
195 BOOST_FAIL("no_transition called!");
196 }
197 // init counters
198 template <class Event,class FSM>
199 void on_entry(Event const&,FSM& fsm)
200 {
201 fsm.template get_state<player_::Stopped&>().entry_counter=0;
202 fsm.template get_state<player_::Stopped&>().exit_counter=0;
203 fsm.template get_state<player_::Open&>().entry_counter=0;
204 fsm.template get_state<player_::Open&>().exit_counter=0;
205 fsm.template get_state<player_::Empty&>().entry_counter=0;
206 fsm.template get_state<player_::Empty&>().exit_counter=0;
207 fsm.template get_state<player_::Empty&>().some_dummy_data=3;
208 fsm.template get_state<player_::Playing&>().entry_counter=0;
209 fsm.template get_state<player_::Playing&>().exit_counter=0;
210 fsm.template get_state<player_::Paused&>().entry_counter=0;
211 fsm.template get_state<player_::Paused&>().exit_counter=0;
212 }
213
214 };
215 // Pick a back-end
216 typedef msm::back::state_machine<player_> player;
217
218// static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
219
220
221 BOOST_AUTO_TEST_CASE( my_test )
222 {
223 player p;
224
225 p.start();
226 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 1,"Empty entry not called correctly");
227
228 p.process_event(evt: open_close());
229 BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Open should be active"); //Open
230 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 1,"Empty exit not called correctly");
231 BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().entry_counter == 1,"Open entry not called correctly");
232
233 // test the serialization
234 std::ostringstream ofs;
235 // save fsm to archive (current state is Open)
236 {
237 boost::archive::text_oarchive oa(ofs);
238 // write class instance to archive
239 oa << p;
240 }
241 // reload fsm in state Open
242 player p2;
243 {
244 // create and open an archive for input
245 std::istringstream ifs(ofs.str());
246 boost::archive::text_iarchive ia(ifs);
247 // read class state from archive
248 ia >> p2;
249 }
250 // we now use p2 as it was loaded
251 // check that we kept Empty's data value
252 BOOST_CHECK_MESSAGE(p2.get_state<player_::Empty&>().some_dummy_data == 3,"Empty not deserialized correctly");
253 BOOST_CHECK_MESSAGE(p2.front_end_data == 4,"Front-end not deserialized correctly");
254
255 p.process_event(evt: open_close());
256 BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active"); //Empty
257 BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 1,"Open exit not called correctly");
258 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 2,"Empty entry not called correctly");
259 BOOST_CHECK_MESSAGE(p.can_close_drawer_counter == 1,"guard not called correctly");
260
261 p.process_event(
262 evt: cd_detected("louie, louie",DISK_DVD));
263 BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active"); //Empty
264 BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 1,"Open exit not called correctly");
265 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 2,"Empty entry not called correctly");
266
267 p.process_event(
268 evt: cd_detected("louie, louie",DISK_CD));
269 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
270 BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 2,"Empty exit not called correctly");
271 BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 1,"Stopped entry not called correctly");
272
273 p.process_event(evt: play());
274 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
275 BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 1,"Stopped exit not called correctly");
276 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 1,"Playing entry not called correctly");
277 BOOST_CHECK_MESSAGE(p.start_playback_counter == 1,"action not called correctly");
278
279 p.process_event(evt: pause());
280 BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active"); //Paused
281 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 1,"Playing exit not called correctly");
282 BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 1,"Paused entry not called correctly");
283
284 // go back to Playing
285 p.process_event(evt: end_pause());
286 BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
287 BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().exit_counter == 1,"Paused exit not called correctly");
288 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 2,"Playing entry not called correctly");
289
290 p.process_event(evt: pause());
291 BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active"); //Paused
292 BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 2,"Playing exit not called correctly");
293 BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 2,"Paused entry not called correctly");
294
295 p.process_event(evt: stop());
296 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
297 BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().exit_counter == 2,"Paused exit not called correctly");
298 BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 2,"Stopped entry not called correctly");
299
300 p.process_event(evt: stop());
301 BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
302 BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 2,"Stopped exit not called correctly");
303 BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 3,"Stopped entry not called correctly");
304 }
305}
306// eliminate object tracking (even if serialized through a pointer)
307// at the risk of a programming error creating duplicate objects.
308// this is to get rid of warning because p is not const
309BOOST_CLASS_TRACKING(player, boost::serialization::track_never)
310
311

source code of boost/libs/msm/test/Serialize.cpp