Constructor.cpp 13 KB

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