SimpleTutorialEuml2.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <vector>
  11. #include <iostream>
  12. #include <boost/msm/back/state_machine.hpp>
  13. #include <boost/msm/front/euml/euml.hpp>
  14. using namespace std;
  15. using namespace boost::msm::front::euml;
  16. namespace msm = boost::msm;
  17. // entry/exit/action/guard logging functors
  18. #include "logging_functors.h"
  19. namespace // Concrete FSM implementation
  20. {
  21. // events
  22. // note that unlike the SimpleTutorial, events must derive from euml_event.
  23. BOOST_MSM_EUML_EVENT(play)
  24. BOOST_MSM_EUML_EVENT(end_pause)
  25. BOOST_MSM_EUML_EVENT(stop)
  26. BOOST_MSM_EUML_EVENT(pause)
  27. BOOST_MSM_EUML_EVENT(open_close)
  28. // A "complicated" event type that carries some data.
  29. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(std::string,cd_name)
  30. BOOST_MSM_EUML_DECLARE_ATTRIBUTE(DiskTypeEnum,cd_type)
  31. BOOST_MSM_EUML_ATTRIBUTES((attributes_ << cd_name << cd_type ), cd_detected_attributes)
  32. BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(cd_detected,cd_detected_attributes)
  33. // Concrete FSM implementation
  34. // The list of FSM states
  35. // state not needing any entry or exit
  36. BOOST_MSM_EUML_STATE((),Paused)
  37. BOOST_MSM_EUML_STATE(( Empty_Entry,Empty_Exit ),Empty)
  38. BOOST_MSM_EUML_STATE(( Open_Entry,Open_Exit ),Open)
  39. BOOST_MSM_EUML_STATE(( Stopped_Entry,Stopped_Exit ),Stopped)
  40. BOOST_MSM_EUML_STATE(( Playing_Entry,Playing_Exit ),Playing)
  41. // guard conditions
  42. BOOST_MSM_EUML_ACTION(good_disk_format)
  43. {
  44. template <class FSM,class EVT,class SourceState,class TargetState>
  45. bool operator()(EVT const& evt,FSM&,SourceState& ,TargetState& )
  46. {
  47. // to test a guard condition, let's say we understand only CDs, not DVD
  48. if (evt.get_attribute(cd_type)!=DISK_CD)
  49. {
  50. std::cout << "wrong disk, sorry" << std::endl;
  51. // just for logging, does not block any transition
  52. return true;
  53. }
  54. std::cout << "good disk" << std::endl;
  55. return true;
  56. }
  57. };
  58. // replaces the old transition table
  59. BOOST_MSM_EUML_TRANSITION_TABLE((
  60. Stopped + play / start_playback == Playing ,
  61. Stopped + open_close / open_drawer == Open ,
  62. Stopped + stop == Stopped,
  63. // +------------------------------------------------------------------------------+
  64. Open + open_close / close_drawer == Empty ,
  65. // +------------------------------------------------------------------------------+
  66. Empty + open_close / open_drawer == Open ,
  67. Empty + cd_detected
  68. [good_disk_format &&(event_(cd_type)==Int_<DISK_CD>())]
  69. / (store_cd_info,process_(play)) == Stopped ,
  70. // +------------------------------------------------------------------------------+
  71. Playing + stop / stop_playback == Stopped ,
  72. Playing + pause / pause_playback == Paused ,
  73. Playing + open_close / stop_and_open == Open ,
  74. // +------------------------------------------------------------------------------+
  75. Paused + end_pause / resume_playback == Playing ,
  76. Paused + stop / stop_playback == Stopped ,
  77. Paused + open_close / stop_and_open == Open
  78. // +------------------------------------------------------------------------------+
  79. ),transition_table)
  80. // create a state machine "on the fly"
  81. BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table, //STT
  82. init_ << Empty, // Init State
  83. no_action, // Entry
  84. no_action, // Exit
  85. attributes_ << no_attributes_, // Attributes
  86. configure_ << no_configure_, // configuration
  87. Log_No_Transition // no_transition handler
  88. ),
  89. player_) //fsm name
  90. // choice of back-end
  91. typedef msm::back::state_machine<player_> player;
  92. //
  93. // Testing utilities.
  94. //
  95. static char const* const state_names[] = { "Stopped", "Open", "Empty", "Playing", "Paused" };
  96. void pstate(player const& p)
  97. {
  98. std::cout << " -> " << state_names[p.current_state()[0]] << std::endl;
  99. }
  100. void test()
  101. {
  102. player p;
  103. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  104. p.start();
  105. // go to Open, call on_exit on Empty, then action, then on_entry on Open
  106. p.process_event(open_close); pstate(p);
  107. p.process_event(open_close); pstate(p);
  108. // will be rejected, wrong disk type
  109. p.process_event(
  110. cd_detected("louie, louie",DISK_DVD)); pstate(p);
  111. p.process_event(
  112. cd_detected("louie, louie",DISK_CD)); pstate(p);
  113. // no need to call play as the previous event does it in its action method
  114. //p.process_event(play);
  115. // at this point, Play is active
  116. p.process_event(pause); pstate(p);
  117. // go back to Playing
  118. p.process_event(end_pause); pstate(p);
  119. p.process_event(pause); pstate(p);
  120. p.process_event(stop); pstate(p);
  121. // event leading to the same state
  122. // no action method called as none is defined in the transition table
  123. p.process_event(stop); pstate(p);
  124. // test call to no_transition
  125. p.process_event(pause); pstate(p);
  126. }
  127. }
  128. int main()
  129. {
  130. test();
  131. return 0;
  132. }