Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:41:58

0001 // Copyright 2008 Christophe Henry
0002 // henry UNDERSCORE christophe AT hotmail DOT com
0003 // This is an extended version of the state machine available in the boost::mpl library
0004 // Distributed under the same license as the original.
0005 // Copyright for the original version:
0006 // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
0007 // under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at
0009 // http://www.boost.org/LICENSE_1_0.txt)
0010 
0011 #ifndef BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H
0012 #define BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H
0013 
0014 #include <utility>
0015 #include <deque>
0016 
0017 #include <boost/mpl/filter_view.hpp>
0018 #include <boost/mpl/for_each.hpp>
0019 #include <boost/mpl/bool.hpp>
0020 #include <boost/any.hpp>
0021 
0022 #include <boost/msm/common.hpp>
0023 #include <boost/msm/back/metafunctions.hpp>
0024 #include <boost/msm/back/common_types.hpp>
0025 #include <boost/msm/back/dispatch_table.hpp>
0026 
0027 namespace boost { namespace msm { namespace back
0028 {
0029 
0030 template <class Fsm>
0031 struct process_any_event_helper
0032 {
0033     process_any_event_helper(msm::back::HandledEnum& res_,Fsm* self_,::boost::any any_event_):
0034     res(res_),self(self_),any_event(any_event_),finished(false){}
0035     template <class Event>
0036     void operator()(boost::msm::wrap<Event> const&)
0037     {
0038         if ( ! finished && ::boost::any_cast<Event>(&any_event)!=0)
0039         {
0040             finished = true;
0041             res = self->process_event_internal(::boost::any_cast<Event>(any_event));
0042         }
0043     }
0044 private:
0045     msm::back::HandledEnum&     res;
0046     Fsm*                        self;
0047     ::boost::any                any_event;
0048     bool                        finished;
0049 };
0050 
0051 #define BOOST_MSM_BACK_GENERATE_PROCESS_EVENT(fsmname)                                              \
0052     namespace boost { namespace msm { namespace back{                                               \
0053     template<>                                                                                      \
0054     ::boost::msm::back::HandledEnum fsmname::process_any_event( ::boost::any const& any_event)      \
0055     {                                                                                               \
0056         typedef ::boost::msm::back::recursive_get_transition_table<fsmname>::type stt;              \
0057         typedef ::boost::msm::back::generate_event_set<stt>::type stt_events;                       \
0058         typedef ::boost::msm::back::recursive_get_internal_transition_table<fsmname, ::boost::mpl::true_ >::type istt;    \
0059         typedef ::boost::msm::back::generate_event_set<create_real_stt<fsmname,istt>::type >::type istt_events;  \
0060         typedef ::boost::msm::back::set_insert_range<stt_events,istt_events>::type all_events;      \
0061         ::boost::msm::back::HandledEnum res= ::boost::msm::back::HANDLED_FALSE;                     \
0062         ::boost::mpl::for_each<all_events, ::boost::msm::wrap< ::boost::mpl::placeholders::_1> >    \
0063         (::boost::msm::back::process_any_event_helper<fsmname>(res,this,any_event));                \
0064         return res;                                                                                 \
0065     }                                                                                               \
0066     }}}
0067 
0068 struct favor_compile_time
0069 {
0070     typedef int compile_policy;
0071     typedef ::boost::mpl::false_ add_forwarding_rows;
0072 };
0073 
0074 // Generates a singleton runtime lookup table that maps current state
0075 // to a function that makes the SM take its transition on the given
0076 // Event type.
0077 template <class Fsm,class Stt, class Event>
0078 struct dispatch_table < Fsm, Stt, Event, ::boost::msm::back::favor_compile_time>
0079 {
0080  private:
0081     // This is a table of these function pointers.
0082     typedef HandledEnum (*cell)(Fsm&, int,int,Event const&);
0083     typedef bool (*guard)(Fsm&, Event const&);
0084 
0085     // Compute the maximum state value in the sm so we know how big
0086     // to make the table
0087     typedef typename generate_state_set<Stt>::type state_list;
0088     BOOST_STATIC_CONSTANT(int, max_state = ( ::boost::mpl::size<state_list>::value));
0089 
0090     struct chain_row
0091     {
0092         HandledEnum operator()(Fsm& fsm, int region,int state,Event const& evt) const
0093         {
0094             HandledEnum res = HANDLED_FALSE;
0095             typename std::deque<cell>::const_iterator it = one_state.begin();
0096             while (it != one_state.end() && (res != HANDLED_TRUE && res != HANDLED_DEFERRED ))
0097             {
0098                 HandledEnum handled = (*it)(fsm,region,state,evt);
0099                 // reject is considered as erasing an error (HANDLED_FALSE)
0100                 if ((HANDLED_FALSE==handled) && (HANDLED_GUARD_REJECT==res) )
0101                     res = HANDLED_GUARD_REJECT;
0102                 else
0103                     res = handled;
0104                 ++it;
0105             }
0106             return res;
0107         }
0108         std::deque<cell> one_state;
0109     };
0110     template <class TransitionState>
0111     static HandledEnum call_submachine(Fsm& fsm, int , int , Event const& evt)
0112     {
0113         return (fsm.template get_state<TransitionState&>()).process_any_event( ::boost::any(evt));
0114     }
0115     // A function object for use with mpl::for_each that stuffs
0116     // transitions into cells.
0117     struct init_cell
0118     {
0119         init_cell(dispatch_table* self_)
0120           : self(self_)
0121         {}
0122         // version for transition event not base of our event
0123         template <class Transition>
0124         typename ::boost::disable_if<
0125             typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
0126         ,void>::type
0127         init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const
0128         {
0129             typedef typename create_stt<Fsm>::type stt;
0130             BOOST_STATIC_CONSTANT(int, state_id =
0131                 (get_state_id<stt,typename Transition::current_state_type>::value));
0132             self->entries[state_id+1].one_state.push_front(reinterpret_cast<cell>(&Transition::execute));
0133         }
0134         template <class Transition>
0135         typename ::boost::enable_if<
0136             typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
0137         ,void>::type
0138         init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const
0139         {
0140             self->entries[0].one_state.push_front(reinterpret_cast<cell>(&Transition::execute));
0141         }
0142 
0143         // version for transition event base of our event
0144         template <class Transition>
0145         typename ::boost::disable_if<
0146             typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
0147         ,void>::type
0148         init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const
0149         {
0150             typedef typename create_stt<Fsm>::type stt;
0151             BOOST_STATIC_CONSTANT(int, state_id =
0152                 (get_state_id<stt,typename Transition::current_state_type>::value));
0153             self->entries[state_id+1].one_state.push_front(&Transition::execute);
0154         }
0155         template <class Transition>
0156         typename ::boost::enable_if<
0157             typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
0158         ,void>::type
0159         init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const
0160         {
0161             self->entries[0].one_state.push_front(&Transition::execute);
0162         }
0163         // Cell initializer function object, used with mpl::for_each
0164         template <class Transition>
0165         typename ::boost::enable_if<typename has_not_real_row_tag<Transition>::type,void >::type
0166             operator()(Transition const&,boost::msm::back::dummy<0> = 0) const
0167         {
0168             // version for not real rows. No problem because irrelevant for process_event
0169         }
0170         template <class Transition>
0171         typename ::boost::disable_if<typename has_not_real_row_tag<Transition>::type,void >::type
0172         operator()(Transition const& tr,boost::msm::back::dummy<1> = 0) const
0173         {
0174             //only if the transition event is a base of our event is the reinterpret_case safe
0175             init_event_base_case(tr,
0176                 ::boost::mpl::bool_<
0177                     ::boost::is_base_of<typename Transition::transition_event,Event>::type::value>() );
0178         }
0179 
0180         dispatch_table* self;
0181     };
0182 
0183     // Cell default-initializer function object, used with mpl::for_each
0184     // initializes with call_no_transition, defer_transition or default_eventless_transition
0185     // variant for non-anonymous transitions
0186     template <class EventType,class Enable=void>
0187     struct default_init_cell
0188     {
0189         default_init_cell(dispatch_table* self_,chain_row* tofill_entries_)
0190             : self(self_),tofill_entries(tofill_entries_)
0191         {}
0192         template <bool deferred,bool composite, int some_dummy=0>
0193         struct helper
0194         {};
0195         template <int some_dummy> struct helper<true,false,some_dummy>
0196         {
0197             template <class State>
0198             static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
0199             {
0200                 typedef typename create_stt<Fsm>::type stt;
0201                 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
0202                 cell call_no_transition = &Fsm::defer_transition;
0203                 tofill[state_id+1].one_state.push_back(call_no_transition);
0204             }
0205         };
0206         template <int some_dummy> struct helper<true,true,some_dummy>
0207         {
0208             template <class State>
0209             static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
0210             {
0211                 typedef typename create_stt<Fsm>::type stt;
0212                 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
0213                 cell call_no_transition = &Fsm::defer_transition;
0214                 tofill[state_id+1].one_state.push_back(call_no_transition);
0215             }
0216         };
0217         template <int some_dummy> struct helper<false,true,some_dummy>
0218         {
0219             template <class State>
0220             static
0221             typename ::boost::enable_if<
0222                 typename ::boost::is_same<State,Fsm>::type
0223             ,void>::type
0224             execute(boost::msm::wrap<State> const&,chain_row* tofill,boost::msm::back::dummy<0> = 0)
0225             {
0226                 // for internal tables
0227                 cell call_no_transition_internal = &Fsm::call_no_transition;
0228                 tofill[0].one_state.push_front(call_no_transition_internal);
0229             }
0230             template <class State>
0231             static
0232             typename ::boost::disable_if<
0233                 typename ::boost::is_same<State,Fsm>::type
0234             ,void>::type
0235             execute(boost::msm::wrap<State> const&,chain_row* tofill,boost::msm::back::dummy<1> = 0)
0236             {
0237                 typedef typename create_stt<Fsm>::type stt;
0238                 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
0239                 cell call_no_transition = &call_submachine< State >;
0240                 tofill[state_id+1].one_state.push_front(call_no_transition);
0241             }
0242         };
0243         template <int some_dummy> struct helper<false,false,some_dummy>
0244         {
0245             template <class State>
0246             static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
0247             {
0248                 typedef typename create_stt<Fsm>::type stt;
0249                 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
0250                 cell call_no_transition = &Fsm::call_no_transition;
0251                 tofill[state_id+1].one_state.push_back(call_no_transition);
0252             }
0253         };
0254         template <class State>
0255         void operator()(boost::msm::wrap<State> const& s)
0256         {
0257             helper<has_state_delayed_event<State,Event>::type::value,
0258                    is_composite_state<State>::type::value>::execute(s,tofill_entries);
0259         }
0260         dispatch_table* self;
0261         chain_row* tofill_entries;
0262     };
0263 
0264     // variant for anonymous transitions
0265     template <class EventType>
0266     struct default_init_cell<EventType,
0267                              typename ::boost::enable_if<
0268                                 typename is_completion_event<EventType>::type>::type>
0269     {
0270         default_init_cell(dispatch_table* self_,chain_row* tofill_entries_)
0271             : self(self_),tofill_entries(tofill_entries_)
0272         {}
0273 
0274         // this event is a compound one (not a real one, just one for use in event-less transitions)
0275         // Note this event cannot be used as deferred!
0276         template <class State>
0277         void operator()(boost::msm::wrap<State> const&)
0278         {
0279             typedef typename create_stt<Fsm>::type stt;
0280             BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
0281             cell call_no_transition = &Fsm::default_eventless_transition;
0282             tofill_entries[state_id+1].one_state.push_back(call_no_transition);
0283         }
0284 
0285         dispatch_table* self;
0286         chain_row* tofill_entries;
0287     };
0288 
0289  public:
0290     // initialize the dispatch table for a given Event and Fsm
0291     dispatch_table()
0292     {
0293         // Initialize cells for no transition
0294         ::boost::mpl::for_each<
0295             ::boost::mpl::filter_view<
0296                     Stt, ::boost::is_base_of<transition_event< ::boost::mpl::placeholders::_>, Event> > >
0297         (init_cell(this));
0298 
0299         ::boost::mpl::for_each<
0300             typename generate_state_set<Stt>::type,
0301             boost::msm::wrap< ::boost::mpl::placeholders::_1> >
0302          (default_init_cell<Event>(this,entries));
0303 
0304     }
0305 
0306     // The singleton instance.
0307     static const dispatch_table instance;
0308 
0309  public: // data members
0310      chain_row entries[max_state+1];
0311 };
0312 
0313 template <class Fsm,class Stt, class Event>
0314 const boost::msm::back::dispatch_table<Fsm,Stt, Event,favor_compile_time>
0315 dispatch_table<Fsm,Stt, Event,favor_compile_time>::instance;
0316 
0317 }}} // boost::msm::back
0318 
0319 #endif //BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H