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)
11 #ifndef BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H
12 #define BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H
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>
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>
27 namespace boost { namespace msm { namespace back
31 struct process_any_event_helper
33 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>
36 void operator()(boost::msm::wrap<Event> const&)
38 if ( ! finished && ::boost::any_cast<Event>(&any_event)!=0)
41 res = self->process_event_internal(::boost::any_cast<Event>(any_event));
45 msm::back::HandledEnum& res;
47 ::boost::any any_event;
51 #define BOOST_MSM_BACK_GENERATE_PROCESS_EVENT(fsmname) \
52 namespace boost { namespace msm { namespace back{ \
54 ::boost::msm::back::HandledEnum fsmname::process_any_event( ::boost::any const& any_event) \
56 typedef ::boost::msm::back::recursive_get_transition_table<fsmname>::type stt; \
57 typedef ::boost::msm::back::generate_event_set<stt>::type stt_events; \
58 typedef ::boost::msm::back::recursive_get_internal_transition_table<fsmname, ::boost::mpl::true_ >::type istt; \
59 typedef ::boost::msm::back::generate_event_set<create_real_stt<fsmname,istt>::type >::type istt_events; \
60 typedef ::boost::msm::back::set_insert_range<stt_events,istt_events>::type all_events; \
61 ::boost::msm::back::HandledEnum res= ::boost::msm::back::HANDLED_FALSE; \
62 ::boost::mpl::for_each<all_events, ::boost::msm::wrap< ::boost::mpl::placeholders::_1> > \
63 (::boost::msm::back::process_any_event_helper<fsmname>(res,this,any_event)); \
68 struct favor_compile_time
70 typedef int compile_policy;
71 typedef ::boost::mpl::false_ add_forwarding_rows;
74 // Generates a singleton runtime lookup table that maps current state
75 // to a function that makes the SM take its transition on the given
77 template <class Fsm,class Stt, class Event>
78 struct dispatch_table < Fsm, Stt, Event, ::boost::msm::back::favor_compile_time>
81 // This is a table of these function pointers.
82 typedef HandledEnum (*cell)(Fsm&, int,int,Event const&);
83 typedef bool (*guard)(Fsm&, Event const&);
85 // Compute the maximum state value in the sm so we know how big
87 typedef typename generate_state_set<Stt>::type state_list;
88 BOOST_STATIC_CONSTANT(int, max_state = ( ::boost::mpl::size<state_list>::value));
92 HandledEnum operator()(Fsm& fsm, int region,int state,Event const& evt) const
94 HandledEnum res = HANDLED_FALSE;
95 typename std::deque<cell>::const_iterator it = one_state.begin();
96 while (it != one_state.end() && (res != HANDLED_TRUE && res != HANDLED_DEFERRED ))
98 HandledEnum handled = (*it)(fsm,region,state,evt);
99 // reject is considered as erasing an error (HANDLED_FALSE)
100 if ((HANDLED_FALSE==handled) && (HANDLED_GUARD_REJECT==res) )
101 res = HANDLED_GUARD_REJECT;
108 std::deque<cell> one_state;
110 template <class TransitionState>
111 static HandledEnum call_submachine(Fsm& fsm, int , int , Event const& evt)
113 return (fsm.template get_state<TransitionState&>()).process_any_event( ::boost::any(evt));
115 // A function object for use with mpl::for_each that stuffs
116 // transitions into cells.
119 init_cell(dispatch_table* self_)
122 // version for transition event not base of our event
123 template <class Transition>
124 typename ::boost::disable_if<
125 typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
127 init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const
129 typedef typename create_stt<Fsm>::type stt;
130 BOOST_STATIC_CONSTANT(int, state_id =
131 (get_state_id<stt,typename Transition::current_state_type>::value));
132 self->entries[state_id+1].one_state.push_front(reinterpret_cast<cell>(&Transition::execute));
134 template <class Transition>
135 typename ::boost::enable_if<
136 typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
138 init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const
140 self->entries[0].one_state.push_front(reinterpret_cast<cell>(&Transition::execute));
143 // version for transition event base of our event
144 template <class Transition>
145 typename ::boost::disable_if<
146 typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
148 init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const
150 typedef typename create_stt<Fsm>::type stt;
151 BOOST_STATIC_CONSTANT(int, state_id =
152 (get_state_id<stt,typename Transition::current_state_type>::value));
153 self->entries[state_id+1].one_state.push_front(&Transition::execute);
155 template <class Transition>
156 typename ::boost::enable_if<
157 typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
159 init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const
161 self->entries[0].one_state.push_front(&Transition::execute);
163 // Cell initializer function object, used with mpl::for_each
164 template <class Transition>
165 typename ::boost::enable_if<typename has_not_real_row_tag<Transition>::type,void >::type
166 operator()(Transition const&,boost::msm::back::dummy<0> = 0) const
168 // version for not real rows. No problem because irrelevant for process_event
170 template <class Transition>
171 typename ::boost::disable_if<typename has_not_real_row_tag<Transition>::type,void >::type
172 operator()(Transition const& tr,boost::msm::back::dummy<1> = 0) const
174 //only if the transition event is a base of our event is the reinterpret_case safe
175 init_event_base_case(tr,
177 ::boost::is_base_of<typename Transition::transition_event,Event>::type::value>() );
180 dispatch_table* self;
183 // Cell default-initializer function object, used with mpl::for_each
184 // initializes with call_no_transition, defer_transition or default_eventless_transition
185 // variant for non-anonymous transitions
186 template <class EventType,class Enable=void>
187 struct default_init_cell
189 default_init_cell(dispatch_table* self_,chain_row* tofill_entries_)
190 : self(self_),tofill_entries(tofill_entries_)
192 template <bool deferred,bool composite, int some_dummy=0>
195 template <int some_dummy> struct helper<true,false,some_dummy>
197 template <class State>
198 static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
200 typedef typename create_stt<Fsm>::type stt;
201 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
202 cell call_no_transition = &Fsm::defer_transition;
203 tofill[state_id+1].one_state.push_back(call_no_transition);
206 template <int some_dummy> struct helper<true,true,some_dummy>
208 template <class State>
209 static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
211 typedef typename create_stt<Fsm>::type stt;
212 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
213 cell call_no_transition = &Fsm::defer_transition;
214 tofill[state_id+1].one_state.push_back(call_no_transition);
217 template <int some_dummy> struct helper<false,true,some_dummy>
219 template <class State>
221 typename ::boost::enable_if<
222 typename ::boost::is_same<State,Fsm>::type
224 execute(boost::msm::wrap<State> const&,chain_row* tofill,boost::msm::back::dummy<0> = 0)
226 // for internal tables
227 cell call_no_transition_internal = &Fsm::call_no_transition;
228 tofill[0].one_state.push_front(call_no_transition_internal);
230 template <class State>
232 typename ::boost::disable_if<
233 typename ::boost::is_same<State,Fsm>::type
235 execute(boost::msm::wrap<State> const&,chain_row* tofill,boost::msm::back::dummy<1> = 0)
237 typedef typename create_stt<Fsm>::type stt;
238 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
239 cell call_no_transition = &call_submachine< State >;
240 tofill[state_id+1].one_state.push_front(call_no_transition);
243 template <int some_dummy> struct helper<false,false,some_dummy>
245 template <class State>
246 static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
248 typedef typename create_stt<Fsm>::type stt;
249 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
250 cell call_no_transition = &Fsm::call_no_transition;
251 tofill[state_id+1].one_state.push_back(call_no_transition);
254 template <class State>
255 void operator()(boost::msm::wrap<State> const& s)
257 helper<has_state_delayed_event<State,Event>::type::value,
258 is_composite_state<State>::type::value>::execute(s,tofill_entries);
260 dispatch_table* self;
261 chain_row* tofill_entries;
264 // variant for anonymous transitions
265 template <class EventType>
266 struct default_init_cell<EventType,
267 typename ::boost::enable_if<
268 typename is_completion_event<EventType>::type>::type>
270 default_init_cell(dispatch_table* self_,chain_row* tofill_entries_)
271 : self(self_),tofill_entries(tofill_entries_)
274 // this event is a compound one (not a real one, just one for use in event-less transitions)
275 // Note this event cannot be used as deferred!
276 template <class State>
277 void operator()(boost::msm::wrap<State> const&)
279 typedef typename create_stt<Fsm>::type stt;
280 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
281 cell call_no_transition = &Fsm::default_eventless_transition;
282 tofill_entries[state_id+1].one_state.push_back(call_no_transition);
285 dispatch_table* self;
286 chain_row* tofill_entries;
290 // initialize the dispatch table for a given Event and Fsm
293 // Initialize cells for no transition
294 ::boost::mpl::for_each<
295 ::boost::mpl::filter_view<
296 Stt, ::boost::is_base_of<transition_event< ::boost::mpl::placeholders::_>, Event> > >
299 ::boost::mpl::for_each<
300 typename generate_state_set<Stt>::type,
301 boost::msm::wrap< ::boost::mpl::placeholders::_1> >
302 (default_init_cell<Event>(this,entries));
306 // The singleton instance.
307 static const dispatch_table instance;
309 public: // data members
310 chain_row entries[max_state+1];
313 template <class Fsm,class Stt, class Event>
314 const boost::msm::back::dispatch_table<Fsm,Stt, Event,favor_compile_time>
315 dispatch_table<Fsm,Stt, Event,favor_compile_time>::instance;
317 }}} // boost::msm::back
319 #endif //BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H