SerializeWithHistory.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  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. #include <iostream>
  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. #include <fstream>
  24. namespace msm = boost::msm;
  25. namespace mpl = boost::mpl;
  26. namespace
  27. {
  28. // events
  29. struct play {};
  30. struct end_pause {};
  31. struct stop {};
  32. struct pause {};
  33. struct open_close {};
  34. struct NextSong {};
  35. struct PreviousSong {};
  36. struct cd_detected
  37. {
  38. cd_detected(std::string name)
  39. : name(name)
  40. {}
  41. std::string name;
  42. };
  43. // front-end: define the FSM structure
  44. struct player_ : public msm::front::state_machine_def<player_>
  45. {
  46. unsigned int start_playback_counter;
  47. unsigned int can_close_drawer_counter;
  48. player_():
  49. start_playback_counter(0),
  50. can_close_drawer_counter(0)
  51. {}
  52. // The list of FSM states
  53. struct Empty : public msm::front::state<>
  54. {
  55. template <class Event,class FSM>
  56. void on_entry(Event const&,FSM& ) {++entry_counter;}
  57. template <class Event,class FSM>
  58. void on_exit(Event const&,FSM& ) {++exit_counter;}
  59. int entry_counter;
  60. int exit_counter;
  61. };
  62. struct Open : public msm::front::state<>
  63. {
  64. template <class Event,class FSM>
  65. void on_entry(Event const&,FSM& ) {++entry_counter;}
  66. template <class Event,class FSM>
  67. void on_exit(Event const&,FSM& ) {++exit_counter;}
  68. int entry_counter;
  69. int exit_counter;
  70. };
  71. // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
  72. struct Stopped : public msm::front::state<>
  73. {
  74. template <class Event,class FSM>
  75. void on_entry(Event const&,FSM& ) {++entry_counter;}
  76. template <class Event,class FSM>
  77. void on_exit(Event const&,FSM& ) {++exit_counter;}
  78. int entry_counter;
  79. int exit_counter;
  80. };
  81. struct Playing_ : public msm::front::state_machine_def<Playing_>
  82. {
  83. template <class Event,class FSM>
  84. void on_entry(Event const&,FSM& ) {++entry_counter;}
  85. template <class Event,class FSM>
  86. void on_exit(Event const&,FSM& ) {++exit_counter;}
  87. int entry_counter;
  88. int exit_counter;
  89. unsigned int start_next_song_counter;
  90. unsigned int start_prev_song_guard_counter;
  91. Playing_():
  92. start_next_song_counter(0),
  93. start_prev_song_guard_counter(0)
  94. {}
  95. // The list of FSM states
  96. struct Song1 : public msm::front::state<>
  97. {
  98. template <class Event,class FSM>
  99. void on_entry(Event const&,FSM& ) {++entry_counter;}
  100. template <class Event,class FSM>
  101. void on_exit(Event const&,FSM& ) {++exit_counter;}
  102. int entry_counter;
  103. int exit_counter;
  104. };
  105. struct Song2 : public msm::front::state<>
  106. {
  107. template <class Event,class FSM>
  108. void on_entry(Event const&,FSM& ) {++entry_counter;}
  109. template <class Event,class FSM>
  110. void on_exit(Event const&,FSM& ) {++exit_counter;}
  111. int entry_counter;
  112. int exit_counter;
  113. };
  114. struct Song3 : public msm::front::state<>
  115. {
  116. template <class Event,class FSM>
  117. void on_entry(Event const&,FSM& ) {++entry_counter;}
  118. template <class Event,class FSM>
  119. void on_exit(Event const&,FSM& ) {++exit_counter;}
  120. int entry_counter;
  121. int exit_counter;
  122. };
  123. // the initial state. Must be defined
  124. typedef Song1 initial_state;
  125. // transition actions
  126. void start_next_song(NextSong const&) {++start_next_song_counter; }
  127. void start_prev_song(PreviousSong const&) { }
  128. // guard conditions
  129. bool start_prev_song_guard(PreviousSong const&) {++start_prev_song_guard_counter;return true; }
  130. typedef Playing_ pl; // makes transition table cleaner
  131. // Transition table for Playing
  132. struct transition_table : mpl::vector4<
  133. // Start Event Next Action Guard
  134. // +---------+-------------+---------+---------------------+----------------------+
  135. _row < Song1 , NextSong , Song2 >,
  136. row < Song2 , PreviousSong, Song1 , &pl::start_prev_song,&pl::start_prev_song_guard>,
  137. a_row < Song2 , NextSong , Song3 , &pl::start_next_song >,
  138. g_row < Song3 , PreviousSong, Song2 ,&pl::start_prev_song_guard>
  139. // +---------+-------------+---------+---------------------+----------------------+
  140. > {};
  141. // Replaces the default no-transition response.
  142. template <class FSM,class Event>
  143. void no_transition(Event const&, FSM&,int)
  144. {
  145. BOOST_FAIL("no_transition called!");
  146. }
  147. };
  148. // back-end
  149. typedef msm::back::state_machine<Playing_,msm::back::ShallowHistory<mpl::vector<end_pause> > > Playing;
  150. // state not defining any entry or exit
  151. struct Paused : public msm::front::state<>
  152. {
  153. template <class Event,class FSM>
  154. void on_entry(Event const&,FSM& ) {++entry_counter;}
  155. template <class Event,class FSM>
  156. void on_exit(Event const&,FSM& ) {++exit_counter;}
  157. int entry_counter;
  158. int exit_counter;
  159. };
  160. // the initial state of the player SM. Must be defined
  161. typedef Empty initial_state;
  162. // transition actions
  163. void start_playback(play const&) {++start_playback_counter; }
  164. void open_drawer(open_close const&) { }
  165. void store_cd_info(cd_detected const&) { }
  166. void stop_playback(stop const&) { }
  167. void pause_playback(pause const&) { }
  168. void resume_playback(end_pause const&) { }
  169. void stop_and_open(open_close const&) { }
  170. void stopped_again(stop const&) {}
  171. //guards
  172. bool can_close_drawer(open_close const&)
  173. {
  174. ++can_close_drawer_counter;
  175. return true;
  176. }
  177. typedef player_ p; // makes transition table cleaner
  178. // Transition table for player
  179. struct transition_table : mpl::vector<
  180. // Start Event Next Action Guard
  181. // +---------+-------------+---------+---------------------+----------------------+
  182. a_row < Stopped , play , Playing , &p::start_playback >,
  183. a_row < Stopped , open_close , Open , &p::open_drawer >,
  184. _row < Stopped , stop , Stopped >,
  185. // +---------+-------------+---------+---------------------+----------------------+
  186. g_row < Open , open_close , Empty , &p::can_close_drawer >,
  187. // +---------+-------------+---------+---------------------+----------------------+
  188. a_row < Empty , open_close , Open , &p::open_drawer >,
  189. a_row < Empty , cd_detected , Stopped , &p::store_cd_info >,
  190. // +---------+-------------+---------+---------------------+----------------------+
  191. a_row < Playing , stop , Stopped , &p::stop_playback >,
  192. a_row < Playing , pause , Paused , &p::pause_playback >,
  193. a_row < Playing , open_close , Open , &p::stop_and_open >,
  194. // +---------+-------------+---------+---------------------+----------------------+
  195. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  196. a_row < Paused , stop , Stopped , &p::stop_playback >,
  197. a_row < Paused , open_close , Open , &p::stop_and_open >
  198. // +---------+-------------+---------+---------------------+----------------------+
  199. > {};
  200. // Replaces the default no-transition response.
  201. template <class FSM,class Event>
  202. void no_transition(Event const& e, FSM&,int state)
  203. {
  204. BOOST_FAIL("no_transition called!");
  205. }
  206. // init counters
  207. template <class Event,class FSM>
  208. void on_entry(Event const&,FSM& fsm)
  209. {
  210. fsm.template get_state<player_::Stopped&>().entry_counter=0;
  211. fsm.template get_state<player_::Stopped&>().exit_counter=0;
  212. fsm.template get_state<player_::Open&>().entry_counter=0;
  213. fsm.template get_state<player_::Open&>().exit_counter=0;
  214. fsm.template get_state<player_::Empty&>().entry_counter=0;
  215. fsm.template get_state<player_::Empty&>().exit_counter=0;
  216. fsm.template get_state<player_::Playing&>().entry_counter=0;
  217. fsm.template get_state<player_::Playing&>().exit_counter=0;
  218. fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song1&>().entry_counter=0;
  219. fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song1&>().exit_counter=0;
  220. fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song2&>().entry_counter=0;
  221. fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song2&>().exit_counter=0;
  222. fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song3&>().entry_counter=0;
  223. fsm.template get_state<player_::Playing&>().template get_state<player_::Playing::Song3&>().exit_counter=0;
  224. fsm.template get_state<player_::Paused&>().entry_counter=0;
  225. fsm.template get_state<player_::Paused&>().exit_counter=0;
  226. }
  227. };
  228. // Pick a back-end
  229. typedef msm::back::state_machine<player_> player;
  230. // static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
  231. BOOST_AUTO_TEST_CASE( my_test )
  232. {
  233. player p;
  234. p.start();
  235. BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 1,"Empty entry not called correctly");
  236. p.process_event(open_close());
  237. BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Open should be active"); //Open
  238. BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 1,"Empty exit not called correctly");
  239. BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().entry_counter == 1,"Open entry not called correctly");
  240. p.process_event(open_close());
  241. BOOST_CHECK_MESSAGE(p.current_state()[0] == 2,"Empty should be active"); //Empty
  242. BOOST_CHECK_MESSAGE(p.get_state<player_::Open&>().exit_counter == 1,"Open exit not called correctly");
  243. BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().entry_counter == 2,"Empty entry not called correctly");
  244. BOOST_CHECK_MESSAGE(p.can_close_drawer_counter == 1,"guard not called correctly");
  245. p.process_event(cd_detected("louie, louie"));
  246. BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
  247. BOOST_CHECK_MESSAGE(p.get_state<player_::Empty&>().exit_counter == 2,"Empty exit not called correctly");
  248. BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().entry_counter == 1,"Stopped entry not called correctly");
  249. p.process_event(play());
  250. BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
  251. BOOST_CHECK_MESSAGE(p.get_state<player_::Stopped&>().exit_counter == 1,"Stopped exit not called correctly");
  252. BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().entry_counter == 1,"Playing entry not called correctly");
  253. BOOST_CHECK_MESSAGE(p.start_playback_counter == 1,"action not called correctly");
  254. BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 0,"Song1 should be active");
  255. BOOST_CHECK_MESSAGE(
  256. p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().entry_counter == 1,
  257. "Song1 entry not called correctly");
  258. p.process_event(NextSong());
  259. BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
  260. BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active");
  261. BOOST_CHECK_MESSAGE(
  262. p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 1,
  263. "Song2 entry not called correctly");
  264. BOOST_CHECK_MESSAGE(
  265. p.get_state<player_::Playing&>().get_state<player_::Playing::Song1&>().exit_counter == 1,
  266. "Song1 exit not called correctly");
  267. BOOST_CHECK_MESSAGE(
  268. p.get_state<player_::Playing&>().start_next_song_counter == 0,
  269. "submachine action not called correctly");
  270. p.process_event(NextSong());
  271. BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
  272. BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 2,"Song3 should be active");
  273. BOOST_CHECK_MESSAGE(
  274. p.get_state<player_::Playing&>().get_state<player_::Playing::Song3&>().entry_counter == 1,
  275. "Song3 entry not called correctly");
  276. BOOST_CHECK_MESSAGE(
  277. p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().exit_counter == 1,
  278. "Song2 exit not called correctly");
  279. BOOST_CHECK_MESSAGE(
  280. p.get_state<player_::Playing&>().start_next_song_counter == 1,
  281. "submachine action not called correctly");
  282. p.process_event(PreviousSong());
  283. BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Playing should be active"); //Playing
  284. BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active");
  285. BOOST_CHECK_MESSAGE(
  286. p.get_state<player_::Playing&>().get_state<player_::Playing::Song2&>().entry_counter == 2,
  287. "Song2 entry not called correctly");
  288. BOOST_CHECK_MESSAGE(
  289. p.get_state<player_::Playing&>().get_state<player_::Playing::Song3&>().exit_counter == 1,
  290. "Song3 exit not called correctly");
  291. BOOST_CHECK_MESSAGE(
  292. p.get_state<player_::Playing&>().start_prev_song_guard_counter == 1,
  293. "submachine guard not called correctly");
  294. p.process_event(pause());
  295. BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Paused should be active"); //Paused
  296. BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().exit_counter == 1,"Playing exit not called correctly");
  297. BOOST_CHECK_MESSAGE(p.get_state<player_::Paused&>().entry_counter == 1,"Paused entry not called correctly");
  298. std::ofstream ofs("fsm.txt");
  299. // save fsm to archive (current state is Pause, Playing is in Song2)
  300. {
  301. boost::archive::text_oarchive oa(ofs);
  302. // write class instance to archive
  303. oa << p;
  304. }
  305. // reload fsm in state Open
  306. player p2;
  307. {
  308. // create and open an archive for input
  309. std::ifstream ifs("fsm.txt");
  310. boost::archive::text_iarchive ia(ifs);
  311. // read class state from archive
  312. ia >> p2;
  313. }
  314. // go back to Playing
  315. p2.process_event(end_pause());
  316. BOOST_CHECK_MESSAGE(p2.current_state()[0] == 3,"Playing should be active"); //Playing
  317. BOOST_CHECK_MESSAGE(p2.get_state<player_::Playing&>().current_state()[0] == 1,"Song2 should be active");
  318. p2.process_event(pause());
  319. BOOST_CHECK_MESSAGE(p2.current_state()[0] == 4,"Paused should be active"); //Paused
  320. p2.process_event(stop());
  321. BOOST_CHECK_MESSAGE(p2.current_state()[0] == 0,"Stopped should be active"); //Stopped
  322. p2.process_event(stop());
  323. BOOST_CHECK_MESSAGE(p2.current_state()[0] == 0,"Stopped should be active"); //Stopped
  324. p2.process_event(play());
  325. BOOST_CHECK_MESSAGE(p2.get_state<player_::Playing&>().current_state()[0] == 0,"Song1 should be active");
  326. }
  327. }
  328. // eliminate object tracking (even if serialized through a pointer)
  329. // at the risk of a programming error creating duplicate objects.
  330. // this is to get rid of warning because p is not const
  331. BOOST_CLASS_TRACKING(player, boost::serialization::track_never)