common_states.hpp 2.8 KB

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