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 
11 #ifndef BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H
12 #define BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H
13 
14 #include <utility>
15 #include <deque>
16 
17 #include <boost/mpl/filter_view.hpp>
18 #include <boost/mpl/for_each.hpp>
19 #include <boost/mpl/bool.hpp>
20 #include <boost/any.hpp>
21 
22 #include <boost/msm/common.hpp>
23 #include <boost/msm/back/metafunctions.hpp>
24 #include <boost/msm/back/common_types.hpp>
25 #include <boost/msm/back/dispatch_table.hpp>
26 
27 namespace boost { namespace msm { namespace back
28 {
29 
30 template <class Fsm>
31 struct process_any_event_helper
32 {
process_any_event_helperboost::msm::back::process_any_event_helper33     process_any_event_helper(msm::back::HandledEnum& res_,Fsm* self_,::boost::any any_event_):
34     res(res_),self(self_),any_event(any_event_),finished(false){}
35     template <class Event>
operator ()boost::msm::back::process_any_event_helper36     void operator()(boost::msm::wrap<Event> const&)
37     {
38         if ( ! finished && ::boost::any_cast<Event>(&any_event)!=0)
39         {
40             finished = true;
41             res = self->process_event_internal(::boost::any_cast<Event>(any_event),false);
42 
43         }
44     }
45 private:
46     msm::back::HandledEnum&     res;
47     Fsm*                        self;
48     ::boost::any                any_event;
49     bool                        finished;
50 };
51 
52 #define BOOST_MSM_BACK_GENERATE_PROCESS_EVENT(fsmname)                                              \
53     namespace boost { namespace msm { namespace back{                                               \
54     template<>                                                                                      \
55     ::boost::msm::back::HandledEnum fsmname::process_any_event( ::boost::any const& any_event)      \
56     {                                                                                               \
57         typedef ::boost::msm::back::recursive_get_transition_table<fsmname>::type stt;              \
58         typedef ::boost::msm::back::generate_event_set<stt>::type stt_events;                       \
59         typedef ::boost::msm::back::recursive_get_internal_transition_table<fsmname, ::boost::mpl::true_ >::type istt;    \
60         typedef ::boost::msm::back::generate_event_set<create_real_stt<fsmname,istt>::type >::type istt_events;  \
61         typedef ::boost::msm::back::set_insert_range<stt_events,istt_events>::type all_events;      \
62         ::boost::msm::back::HandledEnum res= ::boost::msm::back::HANDLED_FALSE;                     \
63         ::boost::mpl::for_each<all_events, ::boost::msm::wrap< ::boost::mpl::placeholders::_1> >    \
64         (::boost::msm::back::process_any_event_helper<fsmname>(res,this,any_event));                \
65         return res;                                                                                 \
66     }                                                                                               \
67     }}}
68 
69 struct favor_compile_time
70 {
71     typedef int compile_policy;
72     typedef ::boost::mpl::false_ add_forwarding_rows;
73 };
74 
75 // Generates a singleton runtime lookup table that maps current state
76 // to a function that makes the SM take its transition on the given
77 // Event type.
78 template <class Fsm,class Stt, class Event>
79 struct dispatch_table < Fsm, Stt, Event, ::boost::msm::back::favor_compile_time>
80 {
81  private:
82     // This is a table of these function pointers.
83     typedef HandledEnum (*cell)(Fsm&, int,int,Event const&);
84     typedef bool (*guard)(Fsm&, Event const&);
85 
86     // Compute the maximum state value in the sm so we know how big
87     // to make the table
88     typedef typename generate_state_set<Stt>::type state_list;
89     BOOST_STATIC_CONSTANT(int, max_state = ( ::boost::mpl::size<state_list>::value));
90 
91     struct chain_row
92     {
operator ()boost::msm::back::dispatch_table::chain_row93         HandledEnum operator()(Fsm& fsm, int region,int state,Event const& evt) const
94         {
95             HandledEnum res = HANDLED_FALSE;
96             typename std::deque<cell>::const_iterator it = one_state.begin();
97             while (it != one_state.end() && (res != HANDLED_TRUE && res != HANDLED_DEFERRED ))
98             {
99                 HandledEnum handled = (*it)(fsm,region,state,evt);
100                 // reject is considered as erasing an error (HANDLED_FALSE)
101                 if ((HANDLED_FALSE==handled) && (HANDLED_GUARD_REJECT==res) )
102                     res = HANDLED_GUARD_REJECT;
103                 else
104                     res = handled;
105                 ++it;
106             }
107             return res;
108         }
109         std::deque<cell> one_state;
110     };
111     template <class TransitionState>
call_submachineboost::msm::back::dispatch_table112     static HandledEnum call_submachine(Fsm& fsm, int , int , Event const& evt)
113     {
114         return (fsm.template get_state<TransitionState&>()).process_any_event( ::boost::any(evt));
115     }
116     // A function object for use with mpl::for_each that stuffs
117     // transitions into cells.
118     struct init_cell
119     {
init_cellboost::msm::back::dispatch_table::init_cell120         init_cell(dispatch_table* self_)
121           : self(self_)
122         {}
123         // version for transition event not base of our event
124         template <class Transition>
125         typename ::boost::disable_if<
126             typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
127         ,void>::type
init_event_base_caseboost::msm::back::dispatch_table::init_cell128         init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const
129         {
130             typedef typename create_stt<Fsm>::type stt;
131             BOOST_STATIC_CONSTANT(int, state_id =
132                 (get_state_id<stt,typename Transition::current_state_type>::value));
133             self->entries[state_id+1].one_state.push_front(reinterpret_cast<cell>(&Transition::execute));
134         }
135         template <class Transition>
136         typename ::boost::enable_if<
137             typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
138         ,void>::type
init_event_base_caseboost::msm::back::dispatch_table::init_cell139         init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const
140         {
141             self->entries[0].one_state.push_front(reinterpret_cast<cell>(&Transition::execute));
142         }
143 
144         // version for transition event base of our event
145         template <class Transition>
146         typename ::boost::disable_if<
147             typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
148         ,void>::type
init_event_base_caseboost::msm::back::dispatch_table::init_cell149         init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const
150         {
151             typedef typename create_stt<Fsm>::type stt;
152             BOOST_STATIC_CONSTANT(int, state_id =
153                 (get_state_id<stt,typename Transition::current_state_type>::value));
154             self->entries[state_id+1].one_state.push_front(&Transition::execute);
155         }
156         template <class Transition>
157         typename ::boost::enable_if<
158             typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
159         ,void>::type
init_event_base_caseboost::msm::back::dispatch_table::init_cell160         init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const
161         {
162             self->entries[0].one_state.push_front(&Transition::execute);
163         }
164         // Cell initializer function object, used with mpl::for_each
165         template <class Transition>
166         typename ::boost::enable_if<typename has_not_real_row_tag<Transition>::type,void >::type
operator ()boost::msm::back::dispatch_table::init_cell167             operator()(Transition const&,boost::msm::back::dummy<0> = 0) const
168         {
169             // version for not real rows. No problem because irrelevant for process_event
170         }
171         template <class Transition>
172         typename ::boost::disable_if<typename has_not_real_row_tag<Transition>::type,void >::type
operator ()boost::msm::back::dispatch_table::init_cell173         operator()(Transition const& tr,boost::msm::back::dummy<1> = 0) const
174         {
175             //only if the transition event is a base of our event is the reinterpret_case safe
176             init_event_base_case(tr,
177                 ::boost::mpl::bool_<
178                     ::boost::is_base_of<typename Transition::transition_event,Event>::type::value>() );
179         }
180 
181         dispatch_table* self;
182     };
183 
184     // Cell default-initializer function object, used with mpl::for_each
185     // initializes with call_no_transition, defer_transition or default_eventless_transition
186     // variant for non-anonymous transitions
187     template <class EventType,class Enable=void>
188     struct default_init_cell
189     {
default_init_cellboost::msm::back::dispatch_table::default_init_cell190         default_init_cell(dispatch_table* self_,chain_row* tofill_entries_)
191             : self(self_),tofill_entries(tofill_entries_)
192         {}
193         template <bool deferred,bool composite, int some_dummy=0>
194         struct helper
195         {};
196         template <int some_dummy> struct helper<true,false,some_dummy>
197         {
198             template <class State>
executeboost::msm::back::dispatch_table::default_init_cell::helper199             static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
200             {
201                 typedef typename create_stt<Fsm>::type stt;
202                 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
203                 cell call_no_transition = &Fsm::defer_transition;
204                 tofill[state_id+1].one_state.push_back(call_no_transition);
205             }
206         };
207         template <int some_dummy> struct helper<true,true,some_dummy>
208         {
209             template <class State>
executeboost::msm::back::dispatch_table::default_init_cell::helper210             static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
211             {
212                 typedef typename create_stt<Fsm>::type stt;
213                 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
214                 cell call_no_transition = &Fsm::defer_transition;
215                 tofill[state_id+1].one_state.push_back(call_no_transition);
216             }
217         };
218         template <int some_dummy> struct helper<false,true,some_dummy>
219         {
220             template <class State>
221             static
222             typename ::boost::enable_if<
223                 typename ::boost::is_same<State,Fsm>::type
224             ,void>::type
executeboost::msm::back::dispatch_table::default_init_cell::helper225             execute(boost::msm::wrap<State> const&,chain_row* tofill,boost::msm::back::dummy<0> = 0)
226             {
227                 // for internal tables
228                 cell call_no_transition_internal = &Fsm::call_no_transition;
229                 tofill[0].one_state.push_front(call_no_transition_internal);
230             }
231             template <class State>
232             static
233             typename ::boost::disable_if<
234                 typename ::boost::is_same<State,Fsm>::type
235             ,void>::type
executeboost::msm::back::dispatch_table::default_init_cell::helper236             execute(boost::msm::wrap<State> const&,chain_row* tofill,boost::msm::back::dummy<1> = 0)
237             {
238                 typedef typename create_stt<Fsm>::type stt;
239                 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
240                 cell call_no_transition = &call_submachine< State >;
241                 tofill[state_id+1].one_state.push_front(call_no_transition);
242             }
243         };
244         template <int some_dummy> struct helper<false,false,some_dummy>
245         {
246             template <class State>
executeboost::msm::back::dispatch_table::default_init_cell::helper247             static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
248             {
249                 typedef typename create_stt<Fsm>::type stt;
250                 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
251                 cell call_no_transition = &Fsm::call_no_transition;
252                 tofill[state_id+1].one_state.push_back(call_no_transition);
253             }
254         };
255         template <class State>
operator ()boost::msm::back::dispatch_table::default_init_cell256         void operator()(boost::msm::wrap<State> const& s)
257         {
258             helper<has_state_delayed_event<State,Event>::type::value,
259                    is_composite_state<State>::type::value>::execute(s,tofill_entries);
260         }
261         dispatch_table* self;
262         chain_row* tofill_entries;
263     };
264 
265     // variant for anonymous transitions
266     template <class EventType>
267     struct default_init_cell<EventType,
268                              typename ::boost::enable_if<
269                                 typename is_completion_event<EventType>::type>::type>
270     {
default_init_cellboost::msm::back::dispatch_table::default_init_cell271         default_init_cell(dispatch_table* self_,chain_row* tofill_entries_)
272             : self(self_),tofill_entries(tofill_entries_)
273         {}
274 
275         // this event is a compound one (not a real one, just one for use in event-less transitions)
276         // Note this event cannot be used as deferred!
277         template <class State>
operator ()boost::msm::back::dispatch_table::default_init_cell278         void operator()(boost::msm::wrap<State> const&)
279         {
280             typedef typename create_stt<Fsm>::type stt;
281             BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
282             cell call_no_transition = &Fsm::default_eventless_transition;
283             tofill_entries[state_id+1].one_state.push_back(call_no_transition);
284         }
285 
286         dispatch_table* self;
287         chain_row* tofill_entries;
288     };
289 
290  public:
291     // initialize the dispatch table for a given Event and Fsm
dispatch_tableboost::msm::back::dispatch_table292     dispatch_table()
293     {
294         // Initialize cells for no transition
295         ::boost::mpl::for_each<
296             ::boost::mpl::filter_view<
297                     Stt, ::boost::is_base_of<transition_event< ::boost::mpl::placeholders::_>, Event> > >
298         (init_cell(this));
299 
300         ::boost::mpl::for_each<
301             typename generate_state_set<Stt>::type,
302             boost::msm::wrap< ::boost::mpl::placeholders::_1> >
303          (default_init_cell<Event>(this,entries));
304 
305     }
306 
307     // The singleton instance.
308     static const dispatch_table instance;
309 
310  public: // data members
311      chain_row entries[max_state+1];
312 };
313 
314 template <class Fsm,class Stt, class Event>
315 const boost::msm::back::dispatch_table<Fsm,Stt, Event,favor_compile_time>
316 dispatch_table<Fsm,Stt, Event,favor_compile_time>::instance;
317 
318 }}} // boost::msm::back
319 
320 #endif //BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H
321