common_states.hpp 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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_DETAILS_COMMON_STATES_H
  11. #define BOOST_MSM_FRONT_DETAILS_COMMON_STATES_H
  12. #include <boost/mpl/int.hpp>
  13. #include <boost/mpl/vector.hpp>
  14. #include <boost/fusion/container/vector.hpp>
  15. #include <boost/fusion/container/map.hpp>
  16. #include <boost/fusion/include/at_key.hpp>
  17. #include <boost/type_traits/add_const.hpp>
  18. namespace boost { namespace msm { namespace front {namespace detail
  19. {
  20. struct state_tag {};
  21. struct composite_state_tag {};
  22. template <class Attributes= ::boost::fusion::map<> >
  23. struct inherit_attributes
  24. {
  25. inherit_attributes():m_attributes(){}
  26. inherit_attributes(Attributes const& the_attributes):m_attributes(the_attributes){}
  27. // on the fly attribute creation capability
  28. typedef Attributes attributes_type;
  29. template <class Index>
  30. typename ::boost::fusion::result_of::at_key<attributes_type,
  31. Index>::type
  32. get_attribute(Index const&)
  33. {
  34. return ::boost::fusion::at_key<Index>(m_attributes);
  35. }
  36. template <class Index>
  37. typename ::boost::add_const<
  38. typename ::boost::fusion::result_of::at_key<attributes_type,
  39. Index>::type>::type
  40. get_attribute(Index const&)const
  41. {
  42. return const_cast<
  43. typename ::boost::add_const<
  44. typename ::boost::fusion::result_of::at_key< attributes_type,
  45. Index >::type>::type>
  46. (::boost::fusion::at_key<Index>(m_attributes));
  47. }
  48. private:
  49. // attributes
  50. Attributes m_attributes;
  51. };
  52. // the interface for all states. Defines entry and exit functions. Overwrite to implement for any state needing it.
  53. template<class USERBASE,class Attributes= ::boost::fusion::map<> >
  54. struct state_base : public inherit_attributes<Attributes>, USERBASE
  55. {
  56. typedef USERBASE user_state_base;
  57. typedef Attributes attributes_type;
  58. struct internal
  59. {
  60. typedef state_tag tag;
  61. };
  62. // empty implementation for the states not wishing to define an entry condition
  63. // will not be called polymorphic way
  64. template <class Event,class FSM>
  65. void on_entry(Event const& ,FSM&){}
  66. template <class Event,class FSM>
  67. void on_exit(Event const&,FSM& ){}
  68. // default (empty) transition table;
  69. typedef ::boost::mpl::vector<> internal_transition_table;
  70. typedef ::boost::fusion::vector<> internal_transition_table11;
  71. typedef ::boost::fusion::vector<> transition_table;
  72. };
  73. }}}}
  74. #endif //BOOST_MSM_FRONT_DETAILS_COMMON_STATES_H