states.hpp 6.2 KB

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