Constructor.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. namespace msm = boost::msm;
  20. namespace mpl = boost::mpl;
  21. namespace
  22. {
  23. // events
  24. struct play {};
  25. struct end_pause {};
  26. struct stop {};
  27. struct pause {};
  28. struct open_close {};
  29. // A "complicated" event type that carries some data.
  30. enum DiskTypeEnum
  31. {
  32. DISK_CD=0,
  33. DISK_DVD=1
  34. };
  35. struct cd_detected
  36. {
  37. cd_detected(std::string name, DiskTypeEnum diskType)
  38. : name(name),
  39. disc_type(diskType)
  40. {}
  41. std::string name;
  42. DiskTypeEnum disc_type;
  43. };
  44. struct SomeExternalContext
  45. {
  46. SomeExternalContext(int b):bla(b){}
  47. int bla;
  48. };
  49. // front-end: define the FSM structure
  50. struct player_ : public msm::front::state_machine_def<player_>
  51. {
  52. player_(SomeExternalContext& context,int someint):
  53. context_(context),
  54. someint_(someint)
  55. {}
  56. SomeExternalContext& context_;
  57. int someint_;
  58. // The list of FSM states
  59. struct Empty : public msm::front::state<>
  60. {
  61. template <class Event,class FSM>
  62. void on_entry(Event const&,FSM& ) {++entry_counter;}
  63. template <class Event,class FSM>
  64. void on_exit(Event const&,FSM& ) {++exit_counter;}
  65. int entry_counter;
  66. int exit_counter;
  67. };
  68. struct Open : public msm::front::state<>
  69. {
  70. template <class Event,class FSM>
  71. void on_entry(Event const&,FSM& ) {++entry_counter;}
  72. template <class Event,class FSM>
  73. void on_exit(Event const&,FSM& ) {++exit_counter;}
  74. int entry_counter;
  75. int exit_counter;
  76. };
  77. // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
  78. struct Stopped : public msm::front::state<>
  79. {
  80. template <class Event,class FSM>
  81. void on_entry(Event const&,FSM& ) {++entry_counter;}
  82. template <class Event,class FSM>
  83. void on_exit(Event const&,FSM& ) {++exit_counter;}
  84. int entry_counter;
  85. int exit_counter;
  86. };
  87. struct Playing : public msm::front::state<>
  88. {
  89. template <class Event,class FSM>
  90. void on_entry(Event const&,FSM& ) {++entry_counter;}
  91. template <class Event,class FSM>
  92. void on_exit(Event const&,FSM& ) {++exit_counter;}
  93. int entry_counter;
  94. int exit_counter;
  95. };
  96. // state not defining any entry or exit
  97. struct Paused : 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. // the initial state of the player SM. Must be defined
  107. typedef Empty initial_state;
  108. // transition actions
  109. void start_playback(play const&) {}
  110. void open_drawer(open_close const&) { }
  111. void store_cd_info(cd_detected const&) { }
  112. void stop_playback(stop const&) { }
  113. void pause_playback(pause const&) { }
  114. void resume_playback(end_pause const&) { }
  115. void stop_and_open(open_close const&) { }
  116. void stopped_again(stop const&){}
  117. // guard conditions
  118. bool good_disk_format(cd_detected const& evt)
  119. {
  120. // to test a guard condition, let's say we understand only CDs, not DVD
  121. if (evt.disc_type != DISK_CD)
  122. {
  123. return false;
  124. }
  125. return true;
  126. }
  127. bool can_close_drawer(open_close const&)
  128. {
  129. return true;
  130. }
  131. typedef player_ p; // makes transition table cleaner
  132. // Transition table for player
  133. struct transition_table : mpl::vector<
  134. // Start Event Next Action Guard
  135. // +---------+-------------+---------+---------------------+----------------------+
  136. a_row < Stopped , play , Playing , &p::start_playback >,
  137. a_row < Stopped , open_close , Open , &p::open_drawer >,
  138. _row < Stopped , stop , Stopped >,
  139. // +---------+-------------+---------+---------------------+----------------------+
  140. g_row < Open , open_close , Empty , &p::can_close_drawer >,
  141. // +---------+-------------+---------+---------------------+----------------------+
  142. a_row < Empty , open_close , Open , &p::open_drawer >,
  143. row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >,
  144. // +---------+-------------+---------+---------------------+----------------------+
  145. a_row < Playing , stop , Stopped , &p::stop_playback >,
  146. a_row < Playing , pause , Paused , &p::pause_playback >,
  147. a_row < Playing , open_close , Open , &p::stop_and_open >,
  148. // +---------+-------------+---------+---------------------+----------------------+
  149. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  150. a_row < Paused , stop , Stopped , &p::stop_playback >,
  151. a_row < Paused , open_close , Open , &p::stop_and_open >
  152. // +---------+-------------+---------+---------------------+----------------------+
  153. > {};
  154. // Replaces the default no-transition response.
  155. template <class FSM,class Event>
  156. void no_transition(Event const&, FSM&,int)
  157. {
  158. BOOST_FAIL("no_transition called!");
  159. }
  160. // init counters
  161. template <class Event,class FSM>
  162. void on_entry(Event const&,FSM& fsm)
  163. {
  164. fsm.template get_state<player_::Stopped&>().entry_counter=0;
  165. fsm.template get_state<player_::Stopped&>().exit_counter=0;
  166. fsm.template get_state<player_::Open&>().entry_counter=0;
  167. fsm.template get_state<player_::Open&>().exit_counter=0;
  168. fsm.template get_state<player_::Empty&>().entry_counter=0;
  169. fsm.template get_state<player_::Empty&>().exit_counter=0;
  170. fsm.template get_state<player_::Playing&>().entry_counter=0;
  171. fsm.template get_state<player_::Playing&>().exit_counter=0;
  172. fsm.template get_state<player_::Paused&>().entry_counter=0;
  173. fsm.template get_state<player_::Paused&>().exit_counter=0;
  174. fsm.context_ = 20;
  175. }
  176. };
  177. // Pick a back-end
  178. typedef msm::back::state_machine<player_> player;
  179. // static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
  180. BOOST_AUTO_TEST_CASE( my_test )
  181. {
  182. SomeExternalContext ctx(3);
  183. player p(boost::ref(ctx),5);
  184. BOOST_CHECK_MESSAGE(p.context_.bla == 3,"wrong value passed");
  185. BOOST_CHECK_MESSAGE(p.someint_ == 5,"wrong value passed");
  186. ctx.bla = 10;
  187. BOOST_CHECK_MESSAGE(p.context_.bla == 10,"error with reference");
  188. p.start();
  189. BOOST_CHECK_MESSAGE(p.context_.bla == 20,"error with fsm entry behavior");
  190. }
  191. }