favor_compile_time.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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_FAVOR_COMPILE_TIME_H
  12. #define BOOST_MSM_BACKMP11_FAVOR_COMPILE_TIME_H
  13. #include <deque>
  14. #include <functional>
  15. #include <typeindex>
  16. #include <unordered_map>
  17. #include <unordered_set>
  18. #include <boost/msm/front/completion_event.hpp>
  19. #include <boost/msm/backmp11/detail/metafunctions.hpp>
  20. #include <boost/msm/backmp11/detail/dispatch_table.hpp>
  21. #include <boost/msm/backmp11/event_traits.hpp>
  22. #define BOOST_MSM_BACKMP11_GENERATE_STATE_MACHINE(smname) \
  23. template<> \
  24. const smname::sm_dispatch_table& smname::sm_dispatch_table::instance() \
  25. { \
  26. static dispatch_table table; \
  27. return table; \
  28. }
  29. namespace boost { namespace msm { namespace backmp11
  30. {
  31. struct favor_compile_time
  32. {
  33. // TODO fix adapter and remove this.
  34. using compile_policy = int;
  35. };
  36. namespace detail
  37. {
  38. template <typename Policy>
  39. struct compile_policy_impl;
  40. template <>
  41. struct compile_policy_impl<favor_compile_time>
  42. {
  43. using add_forwarding_rows = mp11::mp_false;
  44. static bool is_completion_event(const any_event& event)
  45. {
  46. return (event.type() == typeid(front::none));
  47. }
  48. template<typename Statemachine>
  49. static bool is_end_interrupt_event(Statemachine& sm, const any_event& event)
  50. {
  51. static end_interrupt_event_helper helper{sm};
  52. return helper.is_end_interrupt_event(event);
  53. }
  54. template <typename StateMachine, typename Event>
  55. static HandledEnum process_event_internal(StateMachine& sm, const Event& event, EventSource source)
  56. {
  57. return sm.process_event_internal_impl(any_event(event), source);
  58. }
  59. template <typename StateMachine>
  60. static HandledEnum process_event_internal(StateMachine& sm, const any_event& event, EventSource source)
  61. {
  62. return sm.process_event_internal_impl(event, source);
  63. }
  64. template <typename State>
  65. static const std::unordered_set<std::type_index>& get_deferred_event_type_indices()
  66. {
  67. static std::unordered_set<std::type_index> type_indices = []()
  68. {
  69. std::unordered_set<std::type_index> indices;
  70. using deferred_events = to_mp_list_t<typename State::deferred_events>;
  71. using deferred_event_identities = mp11::mp_transform<mp11::mp_identity, deferred_events>;
  72. mp11::mp_for_each<deferred_event_identities>(
  73. [&indices](auto event_identity)
  74. {
  75. using Event = typename decltype(event_identity)::type;
  76. indices.emplace(to_type_index<Event>());
  77. }
  78. );
  79. return indices;
  80. }();
  81. return type_indices;
  82. }
  83. template <typename StateMachine>
  84. static bool is_event_deferred(StateMachine& sm, std::type_index type_index)
  85. {
  86. bool result = false;
  87. auto visitor = [&result, &type_index](auto& state) {
  88. using State = std::decay_t<decltype(state)>;
  89. auto& set = get_deferred_event_type_indices<State>();
  90. result |= (set.find(type_index) != set.end());
  91. };
  92. sm.template visit<visit_mode::active_non_recursive>(visitor);
  93. return result;
  94. }
  95. template <typename StateMachine>
  96. static bool is_event_deferred(StateMachine& sm, const any_event& event)
  97. {
  98. return is_event_deferred(sm, event.type());
  99. }
  100. template <typename StateMachine>
  101. static void defer_event(StateMachine& sm, any_event const& event)
  102. {
  103. auto& deferred_events = sm.get_deferred_events();
  104. deferred_events.queue.push_back(
  105. {
  106. [&sm, event]()
  107. {
  108. return process_event_internal(
  109. sm,
  110. event,
  111. EventSource::EVENT_SOURCE_DEFERRED);
  112. },
  113. [&sm, type_index = std::type_index{event.type()}]()
  114. {
  115. return is_event_deferred(sm, type_index);
  116. },
  117. deferred_events.cur_seq_cnt
  118. }
  119. );
  120. }
  121. template<typename Stt>
  122. struct get_real_rows
  123. {
  124. template<typename Transition>
  125. using is_real_row = mp11::mp_not<typename has_not_real_row_tag<Transition>::type>;
  126. typedef mp11::mp_copy_if<Stt, is_real_row> type;
  127. };
  128. // Convert an event to a type index.
  129. template<class Event>
  130. static std::type_index to_type_index()
  131. {
  132. return std::type_index{typeid(Event)};
  133. }
  134. // Helper class to manage end interrupt events.
  135. class end_interrupt_event_helper
  136. {
  137. public:
  138. template<class StateMachine>
  139. end_interrupt_event_helper(const StateMachine& sm)
  140. {
  141. mp11::mp_for_each<mp11::mp_transform<mp11::mp_identity, typename StateMachine::event_set_mp11>>(
  142. [this, &sm](auto event_identity)
  143. {
  144. using Event = typename decltype(event_identity)::type;
  145. using Flag = EndInterruptFlag<Event>;
  146. m_is_flag_active_functions[to_type_index<Event>()] =
  147. [&sm](){return sm.template is_flag_active<Flag>();};
  148. });
  149. }
  150. bool is_end_interrupt_event(const any_event& event) const
  151. {
  152. auto it = m_is_flag_active_functions.find(event.type());
  153. if (it != m_is_flag_active_functions.end())
  154. {
  155. return (it->second)();
  156. }
  157. return false;
  158. }
  159. private:
  160. using map = std::unordered_map<std::type_index, std::function<bool()>>;
  161. map m_is_flag_active_functions;
  162. };
  163. struct chain_row
  164. {
  165. template<typename Fsm>
  166. HandledEnum operator()(Fsm& fsm, int region, int state, any_event const& evt) const
  167. {
  168. typedef HandledEnum (*real_cell)(Fsm&, int, int, any_event const&);
  169. HandledEnum res = HandledEnum::HANDLED_FALSE;
  170. typename std::deque<generic_cell>::const_iterator it = one_state.begin();
  171. while (it != one_state.end() && (res != HandledEnum::HANDLED_TRUE && res != HandledEnum::HANDLED_DEFERRED ))
  172. {
  173. auto fnc = reinterpret_cast<real_cell>(*it);
  174. HandledEnum handled = (*fnc)(fsm,region,state,evt);
  175. // reject is considered as erasing an error (HANDLED_FALSE)
  176. if ((HandledEnum::HANDLED_FALSE==handled) && (HandledEnum::HANDLED_GUARD_REJECT==res) )
  177. res = HandledEnum::HANDLED_GUARD_REJECT;
  178. else
  179. res = handled;
  180. ++it;
  181. }
  182. return res;
  183. }
  184. // Use a deque with a generic type to avoid unnecessary template instantiations.
  185. std::deque<generic_cell> one_state;
  186. };
  187. // Generates a singleton runtime lookup table that maps current state
  188. // to a function that makes the SM take its transition on the given
  189. // Event type.
  190. template<class Fsm>
  191. class dispatch_table
  192. {
  193. using Stt = typename Fsm::complete_table;
  194. public:
  195. // Dispatch an event.
  196. static HandledEnum dispatch(Fsm& fsm, int region_id, int state_id, const any_event& event)
  197. {
  198. return instance().m_state_dispatch_tables[state_id+1].dispatch(fsm, region_id, state_id, event);
  199. }
  200. // Dispatch an event to the FSM's internal table.
  201. static HandledEnum dispatch_internal(Fsm& fsm, int region_id, int state_id, const any_event& event)
  202. {
  203. return instance().m_state_dispatch_tables[0].dispatch(fsm, region_id, state_id, event);
  204. }
  205. private:
  206. // Adapter for calling a row's execute function.
  207. template<typename Event, typename Row>
  208. static HandledEnum convert_and_execute(Fsm& fsm, int region_id, int state_id, const any_event& event)
  209. {
  210. return Row::execute(fsm, region_id, state_id, *any_cast<Event>(&event));
  211. }
  212. // Dispatch table for one state.
  213. class state_dispatch_table
  214. {
  215. public:
  216. // Initialize the submachine call for the given state.
  217. template<typename State>
  218. void init_call_submachine()
  219. {
  220. m_call_submachine = [](Fsm& fsm, const any_event& evt)
  221. {
  222. return (fsm.template get_state<State&>()).process_event_internal(evt);
  223. };
  224. }
  225. template<typename Event>
  226. chain_row& get_chain_row()
  227. {
  228. return m_entries[to_type_index<Event>()];
  229. }
  230. // Dispatch an event.
  231. HandledEnum dispatch(Fsm& fsm, int region_id, int state_id, const any_event& event) const
  232. {
  233. HandledEnum handled = HandledEnum::HANDLED_FALSE;
  234. if (m_call_submachine)
  235. {
  236. handled = m_call_submachine(fsm, event);
  237. if (handled)
  238. {
  239. return handled;
  240. }
  241. }
  242. auto it = m_entries.find(event.type());
  243. if (it != m_entries.end())
  244. {
  245. handled = (it->second)(fsm, region_id, state_id, event);
  246. }
  247. return handled;
  248. }
  249. private:
  250. std::unordered_map<std::type_index, chain_row> m_entries;
  251. // Special functor if the state is a composite
  252. std::function<HandledEnum(Fsm&, const any_event&)> m_call_submachine;
  253. };
  254. dispatch_table()
  255. {
  256. // Execute row-specific initializations.
  257. mp11::mp_for_each<typename get_real_rows<Stt>::type>(
  258. [this](auto row)
  259. {
  260. using Row = decltype(row);
  261. using Event = typename Row::transition_event;
  262. using State = typename Row::current_state_type;
  263. static constexpr int state_id = Fsm::template get_state_id<State>();
  264. auto& chain_row = m_state_dispatch_tables[state_id + 1].template get_chain_row<Event>();
  265. chain_row.one_state.push_front(reinterpret_cast<generic_cell>(&convert_and_execute<Event, Row>));
  266. });
  267. // Execute state-specific initializations.
  268. using submachine_states = mp11::mp_copy_if<state_set, has_back_end_tag>;
  269. mp11::mp_for_each<mp11::mp_transform<mp11::mp_identity, submachine_states>>(
  270. [this](auto state_identity)
  271. {
  272. using SubmachineState = typename decltype(state_identity)::type;
  273. static constexpr int state_id = Fsm::template get_state_id<SubmachineState>();
  274. m_state_dispatch_tables[state_id + 1].template init_call_submachine<SubmachineState>();
  275. });
  276. }
  277. // The singleton instance.
  278. static const dispatch_table& instance();
  279. // Compute the maximum state value in the sm so we know how big
  280. // to make the table
  281. typedef typename generate_state_set<Stt>::state_set state_set;
  282. BOOST_STATIC_CONSTANT(int, max_state = (mp11::mp_size<state_set>::value));
  283. state_dispatch_table m_state_dispatch_tables[max_state+1];
  284. };
  285. };
  286. #ifndef BOOST_MSM_BACKMP11_MANUAL_GENERATION
  287. template<class Fsm>
  288. const typename compile_policy_impl<favor_compile_time>::template dispatch_table<Fsm>&
  289. compile_policy_impl<favor_compile_time>::dispatch_table<Fsm>::instance()
  290. {
  291. static dispatch_table table;
  292. return table;
  293. }
  294. #endif
  295. } // detail
  296. }}} // boost::msm::backmp11
  297. #endif //BOOST_MSM_BACKMP11_FAVOR_COMPILE_TIME_H