MsmComposite.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 <boost/msm/back/state_machine.hpp>
  11. #include <boost/msm/front/state_machine_def.hpp>
  12. namespace msm = boost::msm;
  13. namespace mpl = boost::mpl;
  14. #include <iostream>
  15. #ifdef WIN32
  16. #include "windows.h"
  17. #else
  18. #include <sys/time.h>
  19. #endif
  20. namespace test_fsm // Concrete FSM implementation
  21. {
  22. // events
  23. struct play {};
  24. struct end_pause {};
  25. struct stop {};
  26. struct pause {};
  27. struct open_close {};
  28. struct cd_detected{};
  29. struct NextSong {};
  30. struct PreviousSong {};
  31. // Concrete FSM implementation
  32. struct player_ : public msm::front::state_machine_def<player_>
  33. {
  34. // no need for exception handling or message queue
  35. typedef int no_exception_thrown;
  36. typedef int no_message_queue;
  37. // The list of FSM states
  38. struct Empty : public msm::front::state<>
  39. {
  40. // optional entry/exit methods
  41. template <class Event,class FSM>
  42. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Empty" << std::endl;*/}
  43. template <class Event,class FSM>
  44. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Empty" << std::endl;*/}
  45. };
  46. struct Open : public msm::front::state<>
  47. {
  48. template <class Event,class FSM>
  49. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Open" << std::endl;*/}
  50. template <class Event,class FSM>
  51. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Open" << std::endl;*/}
  52. };
  53. struct Stopped : public msm::front::state<>
  54. {
  55. // when stopped, the CD is loaded
  56. template <class Event,class FSM>
  57. void on_entry(Event const&,FSM& ) {/*std::cout << "entering: Stopped" << std::endl;*/}
  58. template <class Event,class FSM>
  59. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Stopped" << std::endl;*/}
  60. };
  61. struct Playing_ : public msm::front::state_machine_def<Playing_>
  62. {
  63. // no need for exception handling or message queue
  64. typedef int no_exception_thrown;
  65. typedef int no_message_queue;
  66. // The list of FSM states
  67. struct Song1 : public msm::front::state<> {};
  68. struct Song2 : public msm::front::state<> {};
  69. struct Song3 : public msm::front::state<> {};
  70. // the initial state. Must be defined
  71. typedef Song1 initial_state;
  72. // transition actions
  73. void start_next_song(NextSong const&) { /*std::cout << "Playing::start_next_song\n";*/ }
  74. void start_prev_song(PreviousSong const&) { /*std::cout << "Playing::start_prev_song\n"; */}
  75. // guard conditions
  76. typedef Playing_ pl; // makes transition table cleaner
  77. // Transition table for Playing
  78. struct transition_table : mpl::vector4<
  79. // Start Event Next Action Guard
  80. // +---------+-------------+---------+---------------------+----------------------+
  81. a_row < Song1 , NextSong , Song2 , &pl::start_next_song >,
  82. a_row < Song2 , PreviousSong, Song1 , &pl::start_prev_song >,
  83. a_row < Song2 , NextSong , Song3 , &pl::start_next_song >,
  84. a_row < Song3 , PreviousSong, Song2 , &pl::start_prev_song >
  85. // +---------+-------------+---------+---------------------+----------------------+
  86. > {};
  87. // Replaces the default no-transition response.
  88. template <class FSM,class Event>
  89. void no_transition(Event const& e, FSM&,int state)
  90. {
  91. std::cout << "no transition from state " << state
  92. << " on event " << typeid(e).name() << std::endl;
  93. }
  94. };
  95. typedef msm::back::state_machine<Playing_> Playing;
  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& ) {/*std::cout << "entering: Paused" << std::endl;*/}
  101. template <class Event,class FSM>
  102. void on_exit(Event const&,FSM& ) {/*std::cout << "leaving: Paused" << std::endl;*/}
  103. };
  104. // the initial state of the player SM. Must be defined
  105. typedef Empty initial_state;
  106. // transition actions
  107. void start_playback(play const&) { }
  108. void open_drawer(open_close const&) { }
  109. void close_drawer(open_close const&) { }
  110. void store_cd_info(cd_detected const& cd) { }
  111. void stop_playback(stop const&) { }
  112. void pause_playback(pause const&) { }
  113. void resume_playback(end_pause const&) { }
  114. void stop_and_open(open_close const&) { }
  115. void stopped_again(stop const&){}
  116. // guard conditions
  117. typedef player_ p; // makes transition table cleaner
  118. // Transition table for player
  119. struct transition_table : mpl::vector<
  120. // Start Event Next Action Guard
  121. // +---------+-------------+---------+---------------------+----------------------+
  122. a_row < Stopped , play , Playing , &p::start_playback >,
  123. a_row < Stopped , open_close , Open , &p::open_drawer >,
  124. a_row < Stopped , stop , Stopped , &p::stopped_again >,
  125. // +---------+-------------+---------+---------------------+----------------------+
  126. a_row < Open , open_close , Empty , &p::close_drawer >,
  127. // +---------+-------------+---------+---------------------+----------------------+
  128. a_row < Empty , open_close , Open , &p::open_drawer >,
  129. a_row < Empty , cd_detected , Stopped , &p::store_cd_info >,
  130. // +---------+-------------+---------+---------------------+----------------------+
  131. a_row < Playing , stop , Stopped , &p::stop_playback >,
  132. a_row < Playing , pause , Paused , &p::pause_playback >,
  133. a_row < Playing , open_close , Open , &p::stop_and_open >,
  134. // +---------+-------------+---------+---------------------+----------------------+
  135. a_row < Paused , end_pause , Playing , &p::resume_playback >,
  136. a_row < Paused , stop , Stopped , &p::stop_playback >,
  137. a_row < Paused , open_close , Open , &p::stop_and_open >
  138. // +---------+-------------+---------+---------------------+----------------------+
  139. > {};
  140. // Replaces the default no-transition response.
  141. template <class FSM,class Event>
  142. void no_transition(Event const& e, FSM&,int state)
  143. {
  144. std::cout << "no transition from state " << state
  145. << " on event " << typeid(e).name() << std::endl;
  146. }
  147. };
  148. typedef msm::back::state_machine<player_> player;
  149. //
  150. // Testing utilities.
  151. //
  152. static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
  153. void pstate(player const& p)
  154. {
  155. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  156. }
  157. }
  158. #ifndef WIN32
  159. long mtime(struct timeval& tv1,struct timeval& tv2)
  160. {
  161. return (tv2.tv_sec-tv1.tv_sec) *1000000 + ((tv2.tv_usec-tv1.tv_usec));
  162. }
  163. #endif
  164. int main()
  165. {
  166. // for timing
  167. #ifdef WIN32
  168. LARGE_INTEGER res;
  169. ::QueryPerformanceFrequency(&res);
  170. LARGE_INTEGER li,li2;
  171. #else
  172. struct timeval tv1,tv2;
  173. gettimeofday(&tv1,NULL);
  174. #endif
  175. test_fsm::player p2;
  176. p2.start();
  177. // for timing
  178. #ifdef WIN32
  179. ::QueryPerformanceCounter(&li);
  180. #else
  181. gettimeofday(&tv1,NULL);
  182. #endif
  183. for (int i=0;i<100;++i)
  184. {
  185. p2.process_event(test_fsm::open_close());
  186. p2.process_event(test_fsm::open_close());
  187. p2.process_event(test_fsm::cd_detected());
  188. p2.process_event(test_fsm::play());
  189. for (int j=0;j<100;++j)
  190. {
  191. p2.process_event(test_fsm::NextSong());
  192. p2.process_event(test_fsm::NextSong());
  193. p2.process_event(test_fsm::PreviousSong());
  194. p2.process_event(test_fsm::PreviousSong());
  195. }
  196. p2.process_event(test_fsm::pause());
  197. // go back to Playing
  198. p2.process_event(test_fsm::end_pause());
  199. p2.process_event(test_fsm::pause());
  200. p2.process_event(test_fsm::stop());
  201. // event leading to the same state
  202. p2.process_event(test_fsm::stop());
  203. p2.process_event(test_fsm::open_close());
  204. p2.process_event(test_fsm::open_close());
  205. }
  206. #ifdef WIN32
  207. ::QueryPerformanceCounter(&li2);
  208. #else
  209. gettimeofday(&tv2,NULL);
  210. #endif
  211. #ifdef WIN32
  212. std::cout << "msm took in s:" << (double)(li2.QuadPart-li.QuadPart)/res.QuadPart <<"\n" <<std::endl;
  213. #else
  214. std::cout << "msm took in us:" << mtime(tv1,tv2) <<"\n" <<std::endl;
  215. #endif
  216. return 0;
  217. }