Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-17 08:50:41

0001 // Copyright 2025 Christian Granzin
0002 // Copyright 2008 Christophe Henry
0003 // henry UNDERSCORE christophe AT hotmail DOT com
0004 // This is an extended version of the state machine available in the boost::mpl library
0005 // Distributed under the same license as the original.
0006 // Copyright for the original version:
0007 // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
0008 // under the Boost Software License, Version 1.0. (See accompanying
0009 // file LICENSE_1_0.txt or copy at
0010 // http://www.boost.org/LICENSE_1_0.txt)
0011 
0012 #ifndef BOOST_MSM_BACKMP11_FAVOR_COMPILE_TIME_H
0013 #define BOOST_MSM_BACKMP11_FAVOR_COMPILE_TIME_H
0014 
0015 #include <deque>
0016 #include <functional>
0017 #include <typeindex>
0018 #include <unordered_map>
0019 #include <unordered_set>
0020 
0021 #include <boost/msm/front/completion_event.hpp>
0022 #include <boost/msm/backmp11/detail/metafunctions.hpp>
0023 #include <boost/msm/backmp11/detail/dispatch_table.hpp>
0024 #include <boost/msm/backmp11/event_traits.hpp>
0025 
0026 #define BOOST_MSM_BACKMP11_GENERATE_STATE_MACHINE(smname)                      \
0027     template<>                                                                  \
0028     const smname::sm_dispatch_table& smname::sm_dispatch_table::instance()      \
0029     {                                                                           \
0030         static dispatch_table table;                                            \
0031         return table;                                                           \
0032     }
0033 
0034 namespace boost { namespace msm { namespace backmp11
0035 {
0036 
0037 struct favor_compile_time
0038 {
0039     // TODO fix adapter and remove this.
0040     using compile_policy = int;
0041 };
0042 
0043 namespace detail
0044 {
0045 
0046 template <typename Policy>
0047 struct compile_policy_impl;
0048 template <>
0049 struct compile_policy_impl<favor_compile_time>
0050 {
0051     using add_forwarding_rows = mp11::mp_false;
0052 
0053     static bool is_completion_event(const any_event& event)
0054     {
0055         return (event.type() == typeid(front::none));
0056     }
0057 
0058     template<typename Statemachine>
0059     static bool is_end_interrupt_event(Statemachine& sm, const any_event& event)
0060     {
0061         static end_interrupt_event_helper helper{sm};
0062         return helper.is_end_interrupt_event(event);
0063     }
0064 
0065     template <typename StateMachine, typename Event>
0066     static HandledEnum process_event_internal(StateMachine& sm, const Event& event, EventSource source)
0067     {
0068         return sm.process_event_internal_impl(any_event(event), source);
0069     }
0070     template <typename StateMachine>
0071     static HandledEnum process_event_internal(StateMachine& sm, const any_event& event, EventSource source)
0072     {
0073         return sm.process_event_internal_impl(event, source);
0074     }
0075 
0076     template <typename State>
0077     static const std::unordered_set<std::type_index>& get_deferred_event_type_indices()
0078     {
0079         static std::unordered_set<std::type_index> type_indices = []()
0080         {
0081             std::unordered_set<std::type_index> indices;
0082             using deferred_events = to_mp_list_t<typename State::deferred_events>;
0083             using deferred_event_identities = mp11::mp_transform<mp11::mp_identity, deferred_events>;
0084             mp11::mp_for_each<deferred_event_identities>(
0085                 [&indices](auto event_identity)
0086                 {
0087                     using Event = typename decltype(event_identity)::type;
0088                     indices.emplace(to_type_index<Event>());
0089                 }
0090             );
0091             return indices;
0092         }();
0093         return type_indices;
0094     }
0095 
0096     template <typename StateMachine>
0097     static bool is_event_deferred(StateMachine& sm, std::type_index type_index)
0098     {
0099         bool result = false;
0100         auto visitor = [&result, &type_index](auto& state) {
0101             using State = std::decay_t<decltype(state)>;
0102             auto& set = get_deferred_event_type_indices<State>();
0103             result |= (set.find(type_index) != set.end());
0104         };
0105         sm.template visit<visit_mode::active_non_recursive>(visitor);
0106         return result;
0107     }
0108     template <typename StateMachine>
0109     static bool is_event_deferred(StateMachine& sm, const any_event& event)
0110     {
0111         return is_event_deferred(sm, event.type());
0112     }
0113 
0114     template <typename StateMachine>
0115     static void defer_event(StateMachine& sm, any_event const& event)
0116     {
0117         auto& deferred_events = sm.get_deferred_events();
0118         deferred_events.queue.push_back(
0119             {
0120                 [&sm, event]()
0121                 {
0122                     return process_event_internal(
0123                         sm,
0124                         event,
0125                         EventSource::EVENT_SOURCE_DEFERRED);
0126                 },
0127                 [&sm, type_index = std::type_index{event.type()}]()
0128                 {
0129                     return is_event_deferred(sm, type_index);
0130                 },
0131                 deferred_events.cur_seq_cnt
0132             }
0133         );
0134     }
0135 
0136     template<typename Stt>
0137     struct get_real_rows
0138     {
0139         template<typename Transition>
0140         using is_real_row = mp11::mp_not<typename has_not_real_row_tag<Transition>::type>;
0141         typedef mp11::mp_copy_if<Stt, is_real_row> type;
0142     };
0143 
0144     // Convert an event to a type index.
0145     template<class Event>
0146     static std::type_index to_type_index()
0147     {
0148         return std::type_index{typeid(Event)};
0149     }
0150 
0151     // Helper class to manage end interrupt events.
0152     class end_interrupt_event_helper
0153     {
0154     public:
0155         template<class StateMachine>
0156         end_interrupt_event_helper(const StateMachine& sm)
0157         {
0158             mp11::mp_for_each<mp11::mp_transform<mp11::mp_identity, typename StateMachine::event_set_mp11>>(
0159                 [this, &sm](auto event_identity)
0160                 {
0161                     using Event = typename decltype(event_identity)::type;
0162                     using Flag = EndInterruptFlag<Event>;
0163                     m_is_flag_active_functions[to_type_index<Event>()] =
0164                         [&sm](){return sm.template is_flag_active<Flag>();};
0165                 });
0166         }
0167 
0168         bool is_end_interrupt_event(const any_event& event) const
0169         {
0170             auto it = m_is_flag_active_functions.find(event.type());
0171             if (it != m_is_flag_active_functions.end())
0172             {
0173                 return (it->second)();
0174             }
0175             return false;
0176         }
0177 
0178     private:
0179         using map = std::unordered_map<std::type_index, std::function<bool()>>;
0180         map m_is_flag_active_functions;
0181     };
0182 
0183     struct chain_row
0184     {
0185         template<typename Fsm>
0186         HandledEnum operator()(Fsm& fsm, int region, int state, any_event const& evt) const
0187         {
0188             typedef HandledEnum (*real_cell)(Fsm&, int, int, any_event const&);
0189             HandledEnum res = HandledEnum::HANDLED_FALSE;
0190             typename std::deque<generic_cell>::const_iterator it = one_state.begin();
0191             while (it != one_state.end() && (res != HandledEnum::HANDLED_TRUE && res != HandledEnum::HANDLED_DEFERRED ))
0192             {
0193                 auto fnc = reinterpret_cast<real_cell>(*it);
0194                 HandledEnum handled = (*fnc)(fsm,region,state,evt);
0195                 // reject is considered as erasing an error (HANDLED_FALSE)
0196                 if ((HandledEnum::HANDLED_FALSE==handled) && (HandledEnum::HANDLED_GUARD_REJECT==res) )
0197                     res = HandledEnum::HANDLED_GUARD_REJECT;
0198                 else
0199                     res = handled;
0200                 ++it;
0201             }
0202             return res;
0203         }
0204         // Use a deque with a generic type to avoid unnecessary template instantiations.
0205         std::deque<generic_cell> one_state;
0206     };
0207 
0208     // Generates a singleton runtime lookup table that maps current state
0209     // to a function that makes the SM take its transition on the given
0210     // Event type.
0211     template<class Fsm>
0212     class dispatch_table
0213     {
0214         using Stt = typename Fsm::complete_table;
0215     public:
0216         // Dispatch an event.
0217         static HandledEnum dispatch(Fsm& fsm, int region_id, int state_id, const any_event& event)
0218         {
0219             return instance().m_state_dispatch_tables[state_id+1].dispatch(fsm, region_id, state_id, event);
0220         }
0221 
0222         // Dispatch an event to the FSM's internal table.
0223         static HandledEnum dispatch_internal(Fsm& fsm, int region_id, int state_id, const any_event& event)
0224         {
0225             return instance().m_state_dispatch_tables[0].dispatch(fsm, region_id, state_id, event);
0226         }
0227 
0228     private:
0229         // Adapter for calling a row's execute function.
0230         template<typename Event, typename Row>
0231         static HandledEnum convert_and_execute(Fsm& fsm, int region_id, int state_id, const any_event& event)
0232         {
0233             return Row::execute(fsm, region_id, state_id, *any_cast<Event>(&event));
0234         }
0235 
0236         // Dispatch table for one state.
0237         class state_dispatch_table
0238         {
0239         public:
0240             // Initialize the submachine call for the given state.
0241             template<typename State>
0242             void init_call_submachine()
0243             {
0244                 m_call_submachine = [](Fsm& fsm, const any_event& evt)
0245                 {
0246                     return (fsm.template get_state<State&>()).process_event_internal(evt);
0247                 };
0248             }
0249 
0250             template<typename Event>
0251             chain_row& get_chain_row()
0252             {
0253                 return m_entries[to_type_index<Event>()];
0254             }
0255 
0256             // Dispatch an event.
0257             HandledEnum dispatch(Fsm& fsm, int region_id, int state_id, const any_event& event) const
0258             {
0259                 HandledEnum handled = HandledEnum::HANDLED_FALSE;
0260                 if (m_call_submachine)
0261                 {
0262                     handled = m_call_submachine(fsm, event);
0263                     if (handled)
0264                     {
0265                         return handled;
0266                     }
0267                 }
0268                 auto it = m_entries.find(event.type());
0269                 if (it != m_entries.end())
0270                 {
0271                     handled = (it->second)(fsm, region_id, state_id, event);
0272                 }
0273                 return handled;
0274             }
0275 
0276         private:
0277             std::unordered_map<std::type_index, chain_row> m_entries;
0278             // Special functor if the state is a composite
0279             std::function<HandledEnum(Fsm&, const any_event&)> m_call_submachine;
0280         };
0281 
0282         dispatch_table()
0283         {
0284             // Execute row-specific initializations.
0285             mp11::mp_for_each<typename get_real_rows<Stt>::type>(
0286                 [this](auto row)
0287                 {
0288                     using Row = decltype(row);
0289                     using Event = typename Row::transition_event;
0290                     using State = typename Row::current_state_type;
0291                     static constexpr int state_id = Fsm::template get_state_id<State>();
0292                     auto& chain_row = m_state_dispatch_tables[state_id + 1].template get_chain_row<Event>();
0293                     chain_row.one_state.push_front(reinterpret_cast<generic_cell>(&convert_and_execute<Event, Row>));
0294                 });
0295 
0296             // Execute state-specific initializations.
0297             using submachine_states = mp11::mp_copy_if<state_set, has_back_end_tag>;
0298             mp11::mp_for_each<mp11::mp_transform<mp11::mp_identity, submachine_states>>(
0299                 [this](auto state_identity)
0300                 {
0301                     using SubmachineState = typename decltype(state_identity)::type;
0302                     static constexpr int state_id = Fsm::template get_state_id<SubmachineState>();
0303                     m_state_dispatch_tables[state_id + 1].template init_call_submachine<SubmachineState>();
0304                 });
0305         }
0306 
0307         // The singleton instance.
0308         static const dispatch_table& instance();
0309 
0310         // Compute the maximum state value in the sm so we know how big
0311         // to make the table
0312         typedef typename generate_state_set<Stt>::state_set state_set;
0313         BOOST_STATIC_CONSTANT(int, max_state = (mp11::mp_size<state_set>::value));
0314         state_dispatch_table m_state_dispatch_tables[max_state+1];
0315     };
0316 };
0317 
0318 #ifndef BOOST_MSM_BACKMP11_MANUAL_GENERATION
0319 
0320 template<class Fsm>
0321 const typename compile_policy_impl<favor_compile_time>::template dispatch_table<Fsm>& 
0322 compile_policy_impl<favor_compile_time>::dispatch_table<Fsm>::instance()
0323 {
0324     static dispatch_table table;
0325     return table;
0326 }
0327 
0328 #endif
0329 
0330 
0331 } // detail
0332 
0333 }}} // boost::msm::backmp11
0334 
0335 #endif //BOOST_MSM_BACKMP11_FAVOR_COMPILE_TIME_H