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