states.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. // Copyright 2008 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. #ifndef BOOST_MSM_FRONT_STATES_H
  11. #define BOOST_MSM_FRONT_STATES_H
  12. #include <boost/mpl/bool.hpp>
  13. #include <boost/mpl/vector.hpp>
  14. #include <boost/mpl/transform.hpp>
  15. #include <boost/fusion/include/vector.hpp>
  16. #include <boost/fusion/include/make_vector.hpp>
  17. #include <boost/msm/front/common_states.hpp>
  18. #include <boost/msm/row_tags.hpp>
  19. //#include <boost/msm/back/metafunctions.hpp>
  20. namespace boost { namespace msm { namespace front
  21. {
  22. // transformation metafunction to end interrupt flags
  23. template <class Event>
  24. struct transform_to_end_interrupt
  25. {
  26. typedef boost::msm::EndInterruptFlag<Event> type;
  27. };
  28. // transform a sequence of events into another one of EndInterruptFlag<Event>
  29. template <class Events>
  30. struct apply_end_interrupt_flag
  31. {
  32. typedef typename
  33. ::boost::mpl::transform<
  34. Events,transform_to_end_interrupt< ::boost::mpl::placeholders::_1> >::type type;
  35. };
  36. // returns a mpl vector containing all end interrupt events if sequence, otherwise the same event
  37. template <class Event>
  38. struct get_interrupt_events
  39. {
  40. typedef typename ::boost::mpl::eval_if<
  41. ::boost::mpl::is_sequence<Event>,
  42. boost::msm::front::apply_end_interrupt_flag<Event>,
  43. boost::fusion::result_of::make_vector<boost::msm::EndInterruptFlag<Event> > >::type type;
  44. };
  45. template <class Events>
  46. struct build_interrupt_state_flag_list
  47. {
  48. typedef ::boost::fusion::vector<boost::msm::InterruptedFlag> first_part;
  49. typedef typename ::boost::fusion::result_of::as_vector<
  50. typename ::boost::fusion::result_of::insert_range<
  51. first_part,
  52. typename ::boost::fusion::result_of::end< first_part >::type,
  53. Events
  54. >::type
  55. >::type type;
  56. };
  57. struct no_sm_ptr
  58. {
  59. // tags
  60. typedef ::boost::mpl::bool_<false> needs_sm;
  61. };
  62. struct sm_ptr
  63. {
  64. // tags
  65. typedef ::boost::mpl::bool_<true> needs_sm;
  66. };
  67. // kept for backward compatibility
  68. struct NoSMPtr
  69. {
  70. // tags
  71. typedef ::boost::mpl::bool_<false> needs_sm;
  72. };
  73. struct SMPtr
  74. {
  75. // tags
  76. typedef ::boost::mpl::bool_<true> needs_sm;
  77. };
  78. // provides the typedefs and interface. Concrete states derive from it.
  79. // template argument: pointer-to-fsm policy
  80. template<class BASE = default_base_state,class SMPtrPolicy = no_sm_ptr>
  81. struct state : public boost::msm::front::detail::state_base<BASE>, SMPtrPolicy
  82. {
  83. // tags
  84. // default: no flag
  85. typedef ::boost::fusion::vector0<> flag_list;
  86. typedef ::boost::fusion::vector0<> internal_flag_list;
  87. //default: no deferred events
  88. typedef ::boost::fusion::vector0<> deferred_events;
  89. };
  90. // terminate state simply defines the TerminateFlag flag
  91. // template argument: pointer-to-fsm policy
  92. template<class BASE = default_base_state,class SMPtrPolicy = no_sm_ptr>
  93. struct terminate_state : public boost::msm::front::detail::state_base<BASE>, SMPtrPolicy
  94. {
  95. // tags
  96. typedef ::boost::fusion::vector0<> flag_list;
  97. typedef ::boost::fusion::vector< boost::msm::TerminateFlag> internal_flag_list;
  98. //default: no deferred events
  99. typedef ::boost::fusion::vector0<> deferred_events;
  100. };
  101. // terminate state simply defines the InterruptedFlag and EndInterruptFlag<EndInterruptEvent> flags
  102. // template argument: event which ends the interrupt
  103. // template argument: pointer-to-fsm policy
  104. template <class EndInterruptEvent,class BASE = default_base_state,class SMPtrPolicy = no_sm_ptr>
  105. struct interrupt_state : public boost::msm::front::detail::state_base<BASE>, SMPtrPolicy
  106. {
  107. // tags
  108. typedef ::boost::fusion::vector0<> flag_list;
  109. typedef typename boost::msm::front::build_interrupt_state_flag_list<
  110. typename boost::msm::front::get_interrupt_events<EndInterruptEvent>::type
  111. >::type internal_flag_list;
  112. //default: no deferred events
  113. typedef ::boost::fusion::vector0<> deferred_events;
  114. };
  115. // not a state but a bunch of extra typedefs to handle direct entry into a composite state. To be derived from
  116. // template argument: zone index of this state
  117. template <int ZoneIndex=-1>
  118. struct explicit_entry
  119. {
  120. typedef int explicit_entry_state;
  121. enum {zone_index=ZoneIndex};
  122. };
  123. // to be derived from. Makes a type an entry (pseudo) state. Actually an almost full-fledged state
  124. // template argument: containing composite
  125. // template argument: zone index of this state
  126. // template argument: pointer-to-fsm policy
  127. template<int ZoneIndex=-1,class BASE = default_base_state,class SMPtrPolicy = no_sm_ptr>
  128. struct entry_pseudo_state
  129. : public boost::msm::front::detail::state_base<BASE>,SMPtrPolicy
  130. {
  131. // tags
  132. typedef int pseudo_entry;
  133. enum {zone_index=ZoneIndex};
  134. typedef int explicit_entry_state;
  135. // default: no flag
  136. typedef ::boost::fusion::vector0<> flag_list;
  137. typedef ::boost::fusion::vector0<> internal_flag_list;
  138. //default: no deferred events
  139. typedef ::boost::fusion::vector0<> deferred_events;
  140. };
  141. // to be derived from. Makes a state an exit (pseudo) state. Actually an almost full-fledged state
  142. // template argument: event to forward
  143. // template argument: pointer-to-fsm policy
  144. template<class Event,class BASE = default_base_state,class SMPtrPolicy = no_sm_ptr>
  145. struct exit_pseudo_state : public boost::msm::front::detail::state_base<BASE> , SMPtrPolicy
  146. {
  147. typedef Event event;
  148. typedef BASE Base;
  149. typedef SMPtrPolicy PtrPolicy;
  150. typedef int pseudo_exit;
  151. // default: no flag
  152. typedef ::boost::fusion::vector0<> flag_list;
  153. typedef ::boost::fusion::vector0<> internal_flag_list;
  154. //default: no deferred events
  155. typedef ::boost::fusion::vector0<> deferred_events;
  156. };
  157. }}}
  158. #endif //BOOST_MSM_FRONT_STATES_H