SerializeCompositeAndHistory.cpp 12 KB

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