common_states.hpp 2.9 KB

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