HistoryEuml.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. using namespace std;
  14. using namespace boost::msm::front::euml;
  15. namespace msm = boost::msm;
  16. namespace mpl = boost::mpl;
  17. // entry/exit/action/guard logging functors
  18. #include "logging_functors.h"
  19. namespace // Concrete FSM implementation
  20. {
  21. // events
  22. BOOST_MSM_EUML_EVENT(play)
  23. BOOST_MSM_EUML_EVENT(end_pause)
  24. BOOST_MSM_EUML_EVENT(stop)
  25. BOOST_MSM_EUML_EVENT(pause)
  26. BOOST_MSM_EUML_EVENT(open_close)
  27. BOOST_MSM_EUML_EVENT(next_song)
  28. BOOST_MSM_EUML_EVENT(previous_song)
  29. // A "complicated" event type that carries some data.
  30. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(std::string,cd_name)
  31. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(DiskTypeEnum,cd_type)
  32. BOOST_MSM_EUML_ATTRIBUTES((attributes_ << cd_name << cd_type ), cd_detected_attributes)
  33. BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(cd_detected,cd_detected_attributes)
  34. // Concrete FSM implementation
  35. // The list of FSM states
  36. // state not defining any entry or exit
  37. BOOST_MSM_EUML_STATE((),Paused)
  38. BOOST_MSM_EUML_STATE(( Empty_Entry,Empty_Exit ),Empty)
  39. BOOST_MSM_EUML_STATE(( Open_Entry,Open_Exit ),Open)
  40. BOOST_MSM_EUML_STATE(( Stopped_Entry,Stopped_Exit ),Stopped)
  41. // Playing is now a state machine itself.
  42. // It has 3 substates
  43. BOOST_MSM_EUML_STATE(( Song1_Entry,Song1_Exit ),Song1)
  44. BOOST_MSM_EUML_STATE(( Song2_Entry,Song2_Exit ),Song2)
  45. BOOST_MSM_EUML_STATE(( Song3_Entry,Song3_Exit ),Song3)
  46. // Playing has a transition table
  47. BOOST_MSM_EUML_TRANSITION_TABLE((
  48. // +------------------------------------------------------------------------------+
  49. Song2 == Song1 + next_song / start_next_song,
  50. Song1 == Song2 + previous_song / start_prev_song,
  51. Song3 == Song2 + next_song / start_next_song,
  52. Song2 == Song3 + previous_song / start_prev_song
  53. // +------------------------------------------------------------------------------+
  54. ),playing_transition_table )
  55. // VC9 cannot compile the typedef with build_sm if one is also used for player
  56. BOOST_MSM_EUML_DECLARE_STATE_MACHINE( (playing_transition_table, //STT
  57. init_ << Song1 // Init State
  58. ),Playing_)
  59. // choice of back-end
  60. typedef msm::back::state_machine<Playing_,
  61. msm::back::ShallowHistory<mpl::vector<BOOST_MSM_EUML_EVENT_NAME(end_pause)> > > Playing_type;
  62. Playing_type const Playing;
  63. // guard conditions
  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. // to test a guard condition, let's say we understand only CDs, not DVD
  70. if (evt.get_attribute(cd_type)!=DISK_CD)
  71. {
  72. std::cout << "wrong disk, sorry" << std::endl;
  73. // just for logging, does not block any transition
  74. return true;
  75. }
  76. std::cout << "good disk" << std::endl;
  77. return true;
  78. }
  79. };
  80. // replaces the old transition table
  81. BOOST_MSM_EUML_TRANSITION_TABLE((
  82. Playing == Stopped + play / start_playback,
  83. Playing == Paused + end_pause / resume_playback,
  84. // +------------------------------------------------------------------------------+
  85. Empty == Open + open_close / close_drawer,
  86. // +------------------------------------------------------------------------------+
  87. Open == Empty + open_close / open_drawer,
  88. Open == Paused + open_close / stop_and_open,
  89. Open == Stopped + open_close / open_drawer,
  90. Open == Playing + open_close / stop_and_open,
  91. // +------------------------------------------------------------------------------+
  92. Paused == Playing + pause / pause_playback,
  93. // +------------------------------------------------------------------------------+
  94. Stopped == Playing + stop / stop_playback,
  95. Stopped == Paused + stop / stop_playback,
  96. Stopped == Empty + cd_detected [good_disk_format&&
  97. (event_(cd_type)==Int_<DISK_CD>())]
  98. / (store_cd_info,process_(play)),
  99. Stopped == Stopped + stop
  100. // +------------------------------------------------------------------------------+
  101. ),transition_table)
  102. // create a state machine "on the fly"
  103. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  104. init_ << Empty, // Init State
  105. no_action, // Entry
  106. no_action, // Exit
  107. attributes_ << no_attributes_, // Attributes
  108. configure_ << no_configure_, // configuration
  109. Log_No_Transition // no_transition handler
  110. ),
  111. player_) //fsm name
  112. // choice of back-end
  113. typedef msm::back::state_machine<player_> player;
  114. //
  115. // Testing utilities.
  116. //
  117. static char const* const state_names[] = { "Stopped", "Paused", "Open", "Empty", "Playing" };
  118. void pstate(player const& p)
  119. {
  120. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  121. }
  122. void test()
  123. {
  124. player p;
  125. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  126. p.start();
  127. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  128. p.process_event(open_close); pstate(p);
  129. p.process_event(open_close); pstate(p);
  130. // will be rejected, wrong disk type
  131. p.process_event(
  132. cd_detected("louie, louie",DISK_DVD)); pstate(p);
  133. p.process_event(
  134. cd_detected("louie, louie",DISK_CD)); pstate(p);
  135. // no need to call play as the previous event does it in its action method
  136. //p.process_event(play);
  137. // make transition happen inside it. Player has no idea about this event but it's ok.
  138. p.process_event(next_song);pstate(p); //2nd song active
  139. p.process_event(next_song);pstate(p);//3rd song active
  140. p.process_event(previous_song);pstate(p);//2nd song active
  141. // at this point, Play is active
  142. p.process_event(pause); pstate(p);
  143. // go back to Playing
  144. // as you see, remembers the original state as end_pause is an history trigger
  145. p.process_event(end_pause); pstate(p);
  146. p.process_event(pause); pstate(p);
  147. p.process_event(stop); pstate(p);
  148. // event leading to the same state
  149. // no action method called as none is defined in the transition table
  150. p.process_event(stop); pstate(p);
  151. // test call to no_transition
  152. p.process_event(play); pstate(p);
  153. }
  154. }
  155. int main()
  156. {
  157. test();
  158. return 0;
  159. }