TestConstructor.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. struct SomeExternalContext
  24. {
  25. SomeExternalContext(int b):bla(b){}
  26. int bla;
  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. // A "complicated" event type that carries some data.
  37. enum DiskTypeEnum
  38. {
  39. DISK_CD=0,
  40. DISK_DVD=1
  41. };
  42. struct cd_detected
  43. {
  44. cd_detected(std::string name, DiskTypeEnum diskType)
  45. : name(name),
  46. disc_type(diskType)
  47. {}
  48. std::string name;
  49. DiskTypeEnum disc_type;
  50. };
  51. // front-end: define the FSM structure
  52. struct player_ : public msm::front::state_machine_def<player_>
  53. {
  54. player_(SomeExternalContext& context,int someint)
  55. :context_(context)
  56. {
  57. BOOST_CHECK_MESSAGE(context_.bla == 3,"Wrong context value");
  58. BOOST_CHECK_MESSAGE(someint == 5,"Wrong int value");
  59. context.bla = 10;
  60. }
  61. SomeExternalContext& context_;
  62. // The list of FSM states
  63. struct Empty : public msm::front::state<>
  64. {
  65. int data_;
  66. Empty():data_(0){}
  67. Empty(int i):data_(i){}
  68. // every (optional) entry/exit methods get the event passed.
  69. template <class Event,class FSM>
  70. void on_entry(Event const&,FSM& ) {std::cout << "entering: Empty" << std::endl;}
  71. template <class Event,class FSM>
  72. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Empty" << std::endl;}
  73. };
  74. struct Open : public msm::front::state<>
  75. {
  76. int data_;
  77. Open():data_(0){}
  78. Open(int i):data_(i){}
  79. template <class Event,class FSM>
  80. void on_entry(Event const& ,FSM&) {std::cout << "entering: Open" << std::endl;}
  81. template <class Event,class FSM>
  82. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Open" << std::endl;}
  83. };
  84. // sm_ptr still supported but deprecated as functors are a much better way to do the same thing
  85. struct Stopped : public msm::front::state<msm::front::default_base_state,msm::front::sm_ptr>
  86. {
  87. template <class Event,class FSM>
  88. void on_entry(Event const& ,FSM&) {std::cout << "entering: Stopped" << std::endl;}
  89. template <class Event,class FSM>
  90. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Stopped" << std::endl;}
  91. void set_sm_ptr(player_* pl)
  92. {
  93. m_player=pl;
  94. }
  95. player_* m_player;
  96. };
  97. struct Playing_ : public msm::front::state_machine_def<Playing_>
  98. {
  99. // when playing, the CD is loaded and we are in either pause or playing (duh)
  100. template <class Event,class FSM>
  101. void on_entry(Event const&,FSM& ) {std::cout << "entering: Playing" << std::endl;}
  102. template <class Event,class FSM>
  103. void on_exit(Event const&,FSM& ) {std::cout << "leaving: Playing" << std::endl;}
  104. // The list of FSM states
  105. struct Song1 : public msm::front::state<>
  106. {
  107. int data_;
  108. Song1():data_(0){}
  109. Song1(int i):data_(i){}
  110. template <class Event,class FSM>
  111. void on_entry(Event const&,FSM& ) {std::cout << "starting: First song" << std::endl;}
  112. template <class Event,class FSM>
  113. void on_exit(Event const&,FSM& ) {std::cout << "finishing: First Song" << std::endl;}
  114. };
  115. struct Song2 : public msm::front::state<>
  116. {
  117. template <class Event,class FSM>
  118. void on_entry(Event const&,FSM& ) {std::cout << "starting: Second song" << std::endl;}
  119. template <class Event,class FSM>
  120. void on_exit(Event const&,FSM& ) {std::cout << "finishing: Second Song" << std::endl;}
  121. };
  122. struct Song3 : public msm::front::state<>
  123. {
  124. template <class Event,class FSM>
  125. void on_entry(Event const&,FSM& ) {std::cout << "starting: Third song" << std::endl;}
  126. template <class Event,class FSM>
  127. void on_exit(Event const&,FSM& ) {std::cout << "finishing: Third Song" << std::endl;}
  128. };
  129. // the initial state. Must be defined
  130. typedef Song1 initial_state;
  131. // transition actions
  132. void start_next_song(NextSong const&) { std::cout << "Playing::start_next_song\n"; }
  133. void start_prev_song(PreviousSong const&) { std::cout << "Playing::start_prev_song\n"; }
  134. // guard conditions
  135. typedef Playing_ pl; // makes transition table cleaner
  136. // Transition table for Playing
  137. struct transition_table : mpl::vector4<
  138. // Start Event Next Action Guard
  139. // +---------+-------------+---------+---------------------+----------------------+
  140. a_row < Song1 , NextSong , Song2 , &pl::start_next_song >,
  141. a_row < Song2 , PreviousSong, Song1 , &pl::start_prev_song >,
  142. a_row < Song2 , NextSong , Song3 , &pl::start_next_song >,
  143. a_row < Song3 , PreviousSong, Song2 , &pl::start_prev_song >
  144. // +---------+-------------+---------+---------------------+----------------------+
  145. > {};
  146. // Replaces the default no-transition response.
  147. template <class FSM,class Event>
  148. void no_transition(Event const& e, FSM&,int state)
  149. {
  150. std::cout << "no transition from state " << state
  151. << " on event " << typeid(e).name() << std::endl;
  152. }
  153. };
  154. // back-end
  155. typedef msm::back::state_machine<Playing_> Playing;
  156. // state not defining any entry or exit
  157. struct Paused : public msm::front::state<>
  158. {
  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&) { std::cout << "player::start_playback\n"; }
  164. void open_drawer(open_close const&) { std::cout << "player::open_drawer\n"; }
  165. void close_drawer(open_close const&) { std::cout << "player::close_drawer\n"; }
  166. void store_cd_info(cd_detected const&) { std::cout << "player::store_cd_info\n"; }
  167. void stop_playback(stop const&) { std::cout << "player::stop_playback\n"; }
  168. void pause_playback(pause const&) { std::cout << "player::pause_playback\n"; }
  169. void resume_playback(end_pause const&) { std::cout << "player::resume_playback\n"; }
  170. void stop_and_open(open_close const&) { std::cout << "player::stop_and_open\n"; }
  171. void stopped_again(stop const&) {std::cout << "player::stopped_again\n";}
  172. // guard conditions
  173. bool good_disk_format(cd_detected const& evt)
  174. {
  175. // to test a guard condition, let's say we understand only CDs, not DVD
  176. if (evt.disc_type != DISK_CD)
  177. {
  178. std::cout << "wrong disk, sorry" << std::endl;
  179. return false;
  180. }
  181. return true;
  182. }
  183. // used to show a transition conflict. This guard will simply deactivate one transition and thus
  184. // solve the conflict
  185. bool auto_start(cd_detected const&)
  186. {
  187. return false;
  188. }
  189. typedef player_ p; // makes transition table cleaner
  190. // Transition table for player
  191. struct transition_table : mpl::vector<
  192. // Start Event Next Action Guard
  193. // +---------+-------------+---------+---------------------+----------------------+
  194. a_row < Stopped , play , Playing , &p::start_playback >,
  195. a_row < Stopped , open_close , Open , &p::open_drawer >,
  196. _row < Stopped , stop , Stopped >,
  197. // +---------+-------------+---------+---------------------+----------------------+
  198. a_row < Open , open_close , Empty , &p::close_drawer >,
  199. // +---------+-------------+---------+---------------------+----------------------+
  200. a_row < Empty , open_close , Open , &p::open_drawer >,
  201. row < Empty , cd_detected , Stopped , &p::store_cd_info ,&p::good_disk_format >,
  202. row < Empty , cd_detected , Playing , &p::store_cd_info ,&p::auto_start >,
  203. // +---------+-------------+---------+---------------------+----------------------+
  204. a_row < Playing , stop , Stopped , &p::stop_playback >,
  205. a_row < Playing , pause , Paused , &p::pause_playback >,
  206. a_row < Playing , open_close , Open , &p::stop_and_open >,
  207. // +---------+-------------+---------+---------------------+----------------------+
  208. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  209. a_row < Paused , stop , Stopped , &p::stop_playback >,
  210. a_row < Paused , open_close , Open , &p::stop_and_open >
  211. // +---------+-------------+---------+---------------------+----------------------+
  212. > {};
  213. // Replaces the default no-transition response.
  214. template <class FSM,class Event>
  215. void no_transition(Event const& , FSM&,int)
  216. {
  217. BOOST_FAIL("no_transition called!");
  218. }
  219. };
  220. // Pick a back-end
  221. typedef msm::back::state_machine<player_> player;
  222. BOOST_AUTO_TEST_CASE( my_test )
  223. {
  224. SomeExternalContext ctx(3);
  225. player p1(boost::ref(ctx),5);
  226. BOOST_CHECK_MESSAGE(p1.context_.bla == 10,"Wrong returned context value");
  227. ctx.bla = 3;
  228. player p2(msm::back::states_ << player_::Empty(1),boost::ref(ctx),5);
  229. BOOST_CHECK_MESSAGE(p2.get_state<player_::Empty&>().data_ == 1,"Wrong Empty value");
  230. p2.set_states(msm::back::states_ << player_::Empty(5));
  231. BOOST_CHECK_MESSAGE(p2.get_state<player_::Empty&>().data_ == 5,"Wrong Empty value");
  232. p2.set_states(msm::back::states_ << player_::Empty(7) << player_::Open(2));
  233. BOOST_CHECK_MESSAGE(p2.get_state<player_::Empty&>().data_ == 7,"Wrong Empty value");
  234. BOOST_CHECK_MESSAGE(p2.get_state<player_::Open&>().data_ == 2,"Wrong Open value");
  235. ctx.bla = 3;
  236. player p(msm::back::states_ << player_::Empty(1)
  237. << player_::Playing(msm::back::states_ << player_::Playing_::Song1(8)),
  238. boost::ref(ctx),5);
  239. BOOST_CHECK_MESSAGE(p.get_state<player_::Playing&>().get_state<player_::Playing_::Song1&>().data_ == 8,"Wrong Open value");
  240. }
  241. }