SerializeSimpleEuml.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #include <boost/msm/back/state_machine.hpp>
  12. #include <boost/msm/front/euml/euml.hpp>
  13. #ifndef BOOST_MSM_NONSTANDALONE_TEST
  14. #define BOOST_TEST_MODULE MyTest
  15. #endif
  16. #include <boost/test/unit_test.hpp>
  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. #include <fstream>
  22. using namespace std;
  23. using namespace boost::msm::front::euml;
  24. namespace msm = boost::msm;
  25. namespace
  26. {
  27. // A "complicated" event type that carries some data.
  28. enum DiskTypeEnum
  29. {
  30. DISK_CD=0,
  31. DISK_DVD=1
  32. };
  33. // events
  34. BOOST_MSM_EUML_EVENT(play)
  35. BOOST_MSM_EUML_EVENT(end_pause)
  36. BOOST_MSM_EUML_EVENT(stop)
  37. BOOST_MSM_EUML_EVENT(pause)
  38. BOOST_MSM_EUML_EVENT(open_close)
  39. // A "complicated" event type that carries some data.
  40. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(std::string,cd_name)
  41. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(DiskTypeEnum,cd_type)
  42. BOOST_MSM_EUML_ATTRIBUTES((attributes_ << cd_name << cd_type ), cd_detected_attributes)
  43. BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(cd_detected,cd_detected_attributes)
  44. //states
  45. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,entry_counter)
  46. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,exit_counter)
  47. BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Empty)
  48. BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Open)
  49. BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Stopped)
  50. BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Playing)
  51. BOOST_MSM_EUML_STATE(( ++state_(entry_counter),++state_(exit_counter),attributes_ << entry_counter << exit_counter),Paused)
  52. //fsm
  53. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,start_playback_counter)
  54. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,can_close_drawer_counter)
  55. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(unsigned int,test_fct_counter)
  56. BOOST_MSM_EUML_ACTION(No_Transition)
  57. {
  58. template <class FSM,class Event>
  59. void operator()(Event const&,FSM&,int)
  60. {
  61. BOOST_FAIL("no_transition called!");
  62. }
  63. };
  64. BOOST_MSM_EUML_ACTION(good_disk_format)
  65. {
  66. template <class FSM,class EVT,class SourceState,class TargetState>
  67. bool operator()(EVT const& evt,FSM&,SourceState& ,TargetState& )
  68. {
  69. if (evt.get_attribute(cd_type)!=DISK_CD)
  70. {
  71. return false;
  72. }
  73. return true;
  74. }
  75. };
  76. BOOST_MSM_EUML_TRANSITION_TABLE((
  77. Playing == Stopped + play / (++fsm_(start_playback_counter),++fsm_(test_fct_counter) ),
  78. Playing == Paused + end_pause ,
  79. // +------------------------------------------------------------------------------+
  80. Empty == Open + open_close / ++fsm_(can_close_drawer_counter),
  81. // +------------------------------------------------------------------------------+
  82. Open == Empty + open_close ,
  83. Open == Paused + open_close ,
  84. Open == Stopped + open_close ,
  85. Open == Playing + open_close ,
  86. // +------------------------------------------------------------------------------+
  87. Paused == Playing + pause ,
  88. // +------------------------------------------------------------------------------+
  89. Stopped == Playing + stop ,
  90. Stopped == Paused + stop ,
  91. Stopped == Empty + cd_detected [good_disk_format ||
  92. (event_(cd_type)==Int_<DISK_CD>())] / process_(play) ,
  93. Stopped == Stopped + stop
  94. // +------------------------------------------------------------------------------+
  95. ),transition_table)
  96. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  97. init_ << Empty, // Init State
  98. no_action, // Entry
  99. no_action, // Exit
  100. attributes_ << start_playback_counter
  101. << can_close_drawer_counter << test_fct_counter, // Attributes
  102. configure_ << no_configure_, // configuration
  103. No_Transition // no_transition handler
  104. ),
  105. player_) //fsm name
  106. typedef msm::back::state_machine<player_> player;
  107. // static char const* const state_names[] = { "Stopped", "Paused", "Open", "Empty", "Playing" };
  108. BOOST_AUTO_TEST_CASE( my_test )
  109. {
  110. player p2;
  111. p2.start();
  112. BOOST_CHECK_MESSAGE(p2.get_state<BOOST_MSM_EUML_STATE_NAME(Empty)&>().get_attribute(entry_counter) == 1,
  113. "Empty entry not called correctly");
  114. p2.process_event(open_close());
  115. BOOST_CHECK_MESSAGE(p2.current_state()[0] == 2,"Open should be active"); //Open
  116. BOOST_CHECK_MESSAGE(p2.get_state<BOOST_MSM_EUML_STATE_NAME(Empty)&>().get_attribute(exit_counter) == 1,
  117. "Empty exit not called correctly");
  118. BOOST_CHECK_MESSAGE(p2.get_state<BOOST_MSM_EUML_STATE_NAME(Open)&>().get_attribute(entry_counter) == 1,
  119. "Open entry not called correctly");
  120. // test the serialization
  121. std::ofstream ofs("fsm.txt");
  122. // save fsm to archive (current state is Open)
  123. {
  124. boost::archive::text_oarchive oa(ofs);
  125. // write class instance to archive
  126. oa << p2;
  127. }
  128. // reload fsm in state Open
  129. player p;
  130. {
  131. // create and open an archive for input
  132. std::ifstream ifs("fsm.txt");
  133. boost::archive::text_iarchive ia(ifs);
  134. // read class state from archive
  135. ia >> p;
  136. }
  137. p.process_event(open_close());
  138. BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Empty should be active"); //Empty
  139. p.process_event(
  140. cd_detected("louie, louie",DISK_DVD));
  141. BOOST_CHECK_MESSAGE(p.current_state()[0] == 3,"Empty should be active"); //Empty
  142. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Open)&>().get_attribute(exit_counter) == 1,
  143. "Open exit not called correctly");
  144. p.process_event(
  145. cd_detected("louie, louie",DISK_CD));
  146. BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Playing should be active"); //Playing
  147. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(entry_counter) == 1,
  148. "Stopped entry not called correctly");
  149. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(exit_counter) == 1,
  150. "Stopped exit not called correctly");
  151. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Playing)&>().get_attribute(entry_counter) == 1,
  152. "Playing entry not called correctly");
  153. BOOST_CHECK_MESSAGE(p.get_attribute(start_playback_counter) == 1,"action not called correctly");
  154. BOOST_CHECK_MESSAGE(p.get_attribute(test_fct_counter) == 1,"action not called correctly");
  155. p.process_event(pause());
  156. BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Paused should be active"); //Paused
  157. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Playing)&>().get_attribute(exit_counter) == 1,
  158. "Playing exit not called correctly");
  159. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Paused)&>().get_attribute(entry_counter) == 1,
  160. "Paused entry not called correctly");
  161. // go back to Playing
  162. p.process_event(end_pause());
  163. BOOST_CHECK_MESSAGE(p.current_state()[0] == 4,"Playing should be active"); //Playing
  164. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Paused)&>().get_attribute(exit_counter) == 1,
  165. "Paused exit not called correctly");
  166. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Playing)&>().get_attribute(entry_counter) == 2,
  167. "Playing entry not called correctly");
  168. p.process_event(pause());
  169. BOOST_CHECK_MESSAGE(p.current_state()[0] == 1,"Paused should be active"); //Paused
  170. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Playing)&>().get_attribute(exit_counter) == 2,
  171. "Playing exit not called correctly");
  172. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Paused)&>().get_attribute(entry_counter) == 2,
  173. "Paused entry not called correctly");
  174. p.process_event(stop());
  175. BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
  176. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Paused)&>().get_attribute(exit_counter) == 2,
  177. "Paused exit not called correctly");
  178. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(entry_counter) == 2,
  179. "Stopped entry not called correctly");
  180. p.process_event(stop());
  181. BOOST_CHECK_MESSAGE(p.current_state()[0] == 0,"Stopped should be active"); //Stopped
  182. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(exit_counter) == 2,
  183. "Stopped exit not called correctly");
  184. BOOST_CHECK_MESSAGE(p.get_state<BOOST_MSM_EUML_STATE_NAME(Stopped)&>().get_attribute(entry_counter) == 3,
  185. "Stopped entry not called correctly");
  186. }
  187. }
  188. // eliminate object tracking (even if serialized through a pointer)
  189. // at the risk of a programming error creating duplicate objects.
  190. // this is to get rid of warning because p is not const
  191. BOOST_CLASS_TRACKING(player, boost::serialization::track_never)