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),false);
46 msm::back::HandledEnum& res;
48 ::boost::any any_event;
52 #define BOOST_MSM_BACK_GENERATE_PROCESS_EVENT(fsmname) \
53 namespace boost { namespace msm { namespace back{ \
55 ::boost::msm::back::HandledEnum fsmname::process_any_event( ::boost::any const& any_event) \
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)); \
69 struct favor_compile_time
71 typedef int compile_policy;
72 typedef ::boost::mpl::false_ add_forwarding_rows;
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
78 template <class Fsm,class Stt, class Event>
79 struct dispatch_table < Fsm, Stt, Event, ::boost::msm::back::favor_compile_time>
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&);
86 // Compute the maximum state value in the sm so we know how big
88 typedef typename generate_state_set<Stt>::type state_list;
89 BOOST_STATIC_CONSTANT(int, max_state = ( ::boost::mpl::size<state_list>::value));
93 HandledEnum operator()(Fsm& fsm, int region,int state,Event const& evt) const
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 ))
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;
109 std::deque<cell> one_state;
111 template <class TransitionState>
112 static HandledEnum call_submachine(Fsm& fsm, int , int , Event const& evt)
114 return (fsm.template get_state<TransitionState&>()).process_any_event( ::boost::any(evt));
116 // A function object for use with mpl::for_each that stuffs
117 // transitions into cells.
120 init_cell(dispatch_table* self_)
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
128 init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const
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));
135 template <class Transition>
136 typename ::boost::enable_if<
137 typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
139 init_event_base_case(Transition const&, ::boost::mpl::true_ const &) const
141 self->entries[0].one_state.push_front(reinterpret_cast<cell>(&Transition::execute));
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
149 init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const
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);
156 template <class Transition>
157 typename ::boost::enable_if<
158 typename ::boost::is_same<typename Transition::current_state_type,Fsm>::type
160 init_event_base_case(Transition const&, ::boost::mpl::false_ const &) const
162 self->entries[0].one_state.push_front(&Transition::execute);
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
167 operator()(Transition const&,boost::msm::back::dummy<0> = 0) const
169 // version for not real rows. No problem because irrelevant for process_event
171 template <class Transition>
172 typename ::boost::disable_if<typename has_not_real_row_tag<Transition>::type,void >::type
173 operator()(Transition const& tr,boost::msm::back::dummy<1> = 0) const
175 //only if the transition event is a base of our event is the reinterpret_case safe
176 init_event_base_case(tr,
178 ::boost::is_base_of<typename Transition::transition_event,Event>::type::value>() );
181 dispatch_table* self;
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
190 default_init_cell(dispatch_table* self_,chain_row* tofill_entries_)
191 : self(self_),tofill_entries(tofill_entries_)
193 template <bool deferred,bool composite, int some_dummy=0>
196 template <int some_dummy> struct helper<true,false,some_dummy>
198 template <class State>
199 static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
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);
207 template <int some_dummy> struct helper<true,true,some_dummy>
209 template <class State>
210 static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
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);
218 template <int some_dummy> struct helper<false,true,some_dummy>
220 template <class State>
222 typename ::boost::enable_if<
223 typename ::boost::is_same<State,Fsm>::type
225 execute(boost::msm::wrap<State> const&,chain_row* tofill,boost::msm::back::dummy<0> = 0)
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);
231 template <class State>
233 typename ::boost::disable_if<
234 typename ::boost::is_same<State,Fsm>::type
236 execute(boost::msm::wrap<State> const&,chain_row* tofill,boost::msm::back::dummy<1> = 0)
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);
244 template <int some_dummy> struct helper<false,false,some_dummy>
246 template <class State>
247 static void execute(boost::msm::wrap<State> const&,chain_row* tofill)
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);
255 template <class State>
256 void operator()(boost::msm::wrap<State> const& s)
258 helper<has_state_delayed_event<State,Event>::type::value,
259 is_composite_state<State>::type::value>::execute(s,tofill_entries);
261 dispatch_table* self;
262 chain_row* tofill_entries;
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>
271 default_init_cell(dispatch_table* self_,chain_row* tofill_entries_)
272 : self(self_),tofill_entries(tofill_entries_)
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>
278 void operator()(boost::msm::wrap<State> const&)
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);
286 dispatch_table* self;
287 chain_row* tofill_entries;
291 // initialize the dispatch table for a given Event and Fsm
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> > >
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));
307 // The singleton instance.
308 static const dispatch_table instance;
310 public: // data members
311 chain_row entries[max_state+1];
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;
318 }}} // boost::msm::back
320 #endif //BOOST_MSM_BACK_FAVOR_COMPILE_TIME_H