history_impl.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright 2025 Christian Granzin
  2. // Copyright 2008 Christophe Henry
  3. // henry UNDERSCORE christophe AT hotmail DOT com
  4. // This is an extended version of the state machine available in the boost::mpl library
  5. // Distributed under the same license as the original.
  6. // Copyright for the original version:
  7. // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
  8. // under the Boost Software License, Version 1.0. (See accompanying
  9. // file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. #ifndef BOOST_MSM_BACKMP11_HISTORY_IMPL_H
  12. #define BOOST_MSM_BACKMP11_HISTORY_IMPL_H
  13. #include <boost/msm/front/history_policies.hpp>
  14. #include <boost/mp11.hpp>
  15. namespace boost::msm::backmp11
  16. {
  17. namespace detail
  18. {
  19. // Implementations for history policies.
  20. template<typename History, int NumberOfRegions>
  21. class history_impl;
  22. template <int NumberOfRegions>
  23. class history_impl<front::no_history, NumberOfRegions>
  24. {
  25. public:
  26. void reset_active_state_ids(const std::array<int, NumberOfRegions>& initial_state_ids)
  27. {
  28. m_initial_state_ids = initial_state_ids;
  29. }
  30. template <class Event>
  31. const std::array<int, NumberOfRegions>& on_entry(Event const&)
  32. {
  33. return m_initial_state_ids;
  34. }
  35. void on_exit(const std::array<int, NumberOfRegions>&)
  36. {
  37. // ignore
  38. }
  39. // this policy deletes all waiting deferred events
  40. template <class Event>
  41. bool process_deferred_events(Event const&) const
  42. {
  43. return false;
  44. }
  45. private:
  46. // Allow access to private members for serialization.
  47. template<typename T, int N>
  48. friend void serialize(T&, history_impl<front::no_history, N>&);
  49. std::array<int, NumberOfRegions> m_initial_state_ids;
  50. };
  51. template <int NumberOfRegions>
  52. class history_impl<front::always_shallow_history, NumberOfRegions>
  53. {
  54. public:
  55. void reset_active_state_ids(const std::array<int, NumberOfRegions>& initial_state_ids)
  56. {
  57. m_last_active_state_ids = initial_state_ids;
  58. }
  59. template <class Event>
  60. const std::array<int, NumberOfRegions>& on_entry(Event const& )
  61. {
  62. return m_last_active_state_ids;
  63. }
  64. void on_exit(const std::array<int, NumberOfRegions>& active_state_ids)
  65. {
  66. m_last_active_state_ids = active_state_ids;
  67. }
  68. // the history policy keeps all deferred events until next reentry
  69. template <class Event>
  70. bool process_deferred_events(Event const&)const
  71. {
  72. return true;
  73. }
  74. private:
  75. // Allow access to private members for serialization.
  76. template<typename T, int N>
  77. friend void serialize(T&, history_impl<front::always_shallow_history, N>&);
  78. std::array<int, NumberOfRegions> m_last_active_state_ids;
  79. };
  80. template <typename... Events, int NumberOfRegions>
  81. class history_impl<front::shallow_history<Events...>, NumberOfRegions>
  82. {
  83. using events_mp11 = mp11::mp_list<Events...>;
  84. public:
  85. void reset_active_state_ids(const std::array<int, NumberOfRegions>& initial_state_ids)
  86. {
  87. m_initial_state_ids = initial_state_ids;
  88. m_last_active_state_ids = initial_state_ids;
  89. }
  90. template <class Event>
  91. const std::array<int, NumberOfRegions>& on_entry(Event const&)
  92. {
  93. if constexpr (mp11::mp_contains<events_mp11,Event>::value)
  94. {
  95. return m_last_active_state_ids;
  96. }
  97. return m_initial_state_ids;
  98. }
  99. void on_exit(const std::array<int, NumberOfRegions>& active_state_ids)
  100. {
  101. m_last_active_state_ids = active_state_ids;
  102. }
  103. // the history policy keeps deferred events until next reentry if coming from our history event
  104. template <class Event>
  105. bool process_deferred_events(Event const&) const
  106. {
  107. return mp11::mp_contains<events_mp11,Event>::value;
  108. }
  109. private:
  110. // Allow access to private members for serialization.
  111. template<typename T, typename... Es, int N>
  112. friend void serialize(T&, history_impl<front::shallow_history<Es...>, N>&);
  113. std::array<int, NumberOfRegions> m_initial_state_ids;
  114. std::array<int, NumberOfRegions> m_last_active_state_ids;
  115. };
  116. } // detail
  117. } // boost::msm::backmp11
  118. #endif // BOOST_MSM_BACKMP11_HISTORY_IMPL_H