Mercurial > hg > vamp-build-and-test
comparison DEPENDENCIES/generic/include/boost/msm/back/state_machine.hpp @ 16:2665513ce2d3
Add boost headers
author | Chris Cannam |
---|---|
date | Tue, 05 Aug 2014 11:11:38 +0100 |
parents | |
children | c530137014c0 |
comparison
equal
deleted
inserted
replaced
15:663ca0da4350 | 16:2665513ce2d3 |
---|---|
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_STATEMACHINE_H | |
12 #define BOOST_MSM_BACK_STATEMACHINE_H | |
13 | |
14 #include <exception> | |
15 #include <vector> | |
16 #include <functional> | |
17 #include <numeric> | |
18 #include <utility> | |
19 | |
20 #include <boost/detail/no_exceptions_support.hpp> | |
21 | |
22 #include <boost/mpl/contains.hpp> | |
23 #include <boost/mpl/deref.hpp> | |
24 #include <boost/mpl/assert.hpp> | |
25 | |
26 #include <boost/fusion/container/vector/convert.hpp> | |
27 #include <boost/fusion/include/as_vector.hpp> | |
28 #include <boost/fusion/include/as_set.hpp> | |
29 #include <boost/fusion/container/set.hpp> | |
30 #include <boost/fusion/include/set.hpp> | |
31 #include <boost/fusion/include/set_fwd.hpp> | |
32 #include <boost/fusion/include/mpl.hpp> | |
33 #include <boost/fusion/sequence/intrinsic/at_key.hpp> | |
34 #include <boost/fusion/include/at_key.hpp> | |
35 #include <boost/fusion/algorithm/iteration/for_each.hpp> | |
36 #include <boost/fusion/include/for_each.hpp> | |
37 | |
38 #include <boost/assert.hpp> | |
39 #include <boost/ref.hpp> | |
40 #include <boost/type_traits.hpp> | |
41 #include <boost/utility/enable_if.hpp> | |
42 #include <boost/type_traits/is_convertible.hpp> | |
43 | |
44 #include <boost/bind.hpp> | |
45 #include <boost/bind/apply.hpp> | |
46 #include <boost/function.hpp> | |
47 #ifndef BOOST_NO_RTTI | |
48 #include <boost/any.hpp> | |
49 #endif | |
50 | |
51 #include <boost/serialization/base_object.hpp> | |
52 | |
53 #include <boost/parameter.hpp> | |
54 | |
55 #include <boost/msm/active_state_switching_policies.hpp> | |
56 #include <boost/msm/row_tags.hpp> | |
57 #include <boost/msm/msm_grammar.hpp> | |
58 #include <boost/msm/back/fold_to_list.hpp> | |
59 #include <boost/msm/back/metafunctions.hpp> | |
60 #include <boost/msm/back/history_policies.hpp> | |
61 #include <boost/msm/back/common_types.hpp> | |
62 #include <boost/msm/back/args.hpp> | |
63 #include <boost/msm/back/default_compile_policy.hpp> | |
64 #include <boost/msm/back/dispatch_table.hpp> | |
65 #include <boost/msm/back/no_fsm_check.hpp> | |
66 #include <boost/msm/back/queue_container_deque.hpp> | |
67 | |
68 BOOST_MPL_HAS_XXX_TRAIT_DEF(accept_sig) | |
69 BOOST_MPL_HAS_XXX_TRAIT_DEF(no_automatic_create) | |
70 BOOST_MPL_HAS_XXX_TRAIT_DEF(non_forwarding_flag) | |
71 BOOST_MPL_HAS_XXX_TRAIT_DEF(direct_entry) | |
72 BOOST_MPL_HAS_XXX_TRAIT_DEF(initial_event) | |
73 BOOST_MPL_HAS_XXX_TRAIT_DEF(final_event) | |
74 BOOST_MPL_HAS_XXX_TRAIT_DEF(do_serialize) | |
75 BOOST_MPL_HAS_XXX_TRAIT_DEF(history_policy) | |
76 BOOST_MPL_HAS_XXX_TRAIT_DEF(fsm_check) | |
77 BOOST_MPL_HAS_XXX_TRAIT_DEF(compile_policy) | |
78 BOOST_MPL_HAS_XXX_TRAIT_DEF(queue_container_policy) | |
79 BOOST_MPL_HAS_XXX_TRAIT_DEF(using_declared_table) | |
80 | |
81 #ifndef BOOST_MSM_CONSTRUCTOR_ARG_SIZE | |
82 #define BOOST_MSM_CONSTRUCTOR_ARG_SIZE 5 // default max number of arguments for constructors | |
83 #endif | |
84 | |
85 namespace boost { namespace msm { namespace back | |
86 { | |
87 // event used internally for wrapping a direct entry | |
88 template <class StateType,class Event> | |
89 struct direct_entry_event | |
90 { | |
91 typedef int direct_entry; | |
92 typedef StateType active_state; | |
93 typedef Event contained_event; | |
94 | |
95 direct_entry_event(Event const& evt):m_event(evt){} | |
96 Event const& m_event; | |
97 }; | |
98 | |
99 // This declares the statically-initialized dispatch_table instance. | |
100 template <class Fsm,class Stt, class Event,class CompilePolicy> | |
101 const boost::msm::back::dispatch_table<Fsm,Stt, Event,CompilePolicy> | |
102 dispatch_table<Fsm,Stt, Event,CompilePolicy>::instance; | |
103 | |
104 BOOST_PARAMETER_TEMPLATE_KEYWORD(front_end) | |
105 BOOST_PARAMETER_TEMPLATE_KEYWORD(history_policy) | |
106 BOOST_PARAMETER_TEMPLATE_KEYWORD(compile_policy) | |
107 BOOST_PARAMETER_TEMPLATE_KEYWORD(fsm_check_policy) | |
108 BOOST_PARAMETER_TEMPLATE_KEYWORD(queue_container_policy) | |
109 | |
110 typedef ::boost::parameter::parameters< | |
111 ::boost::parameter::required< ::boost::msm::back::tag::front_end > | |
112 , ::boost::parameter::optional< | |
113 ::boost::parameter::deduced< ::boost::msm::back::tag::history_policy>, has_history_policy< ::boost::mpl::_ > | |
114 > | |
115 , ::boost::parameter::optional< | |
116 ::boost::parameter::deduced< ::boost::msm::back::tag::compile_policy>, has_compile_policy< ::boost::mpl::_ > | |
117 > | |
118 , ::boost::parameter::optional< | |
119 ::boost::parameter::deduced< ::boost::msm::back::tag::fsm_check_policy>, has_fsm_check< ::boost::mpl::_ > | |
120 > | |
121 , ::boost::parameter::optional< | |
122 ::boost::parameter::deduced< ::boost::msm::back::tag::queue_container_policy>, | |
123 has_queue_container_policy< ::boost::mpl::_ > | |
124 > | |
125 > state_machine_signature; | |
126 | |
127 // just here to disable use of proto when not needed | |
128 template <class T, class F,class Enable=void> | |
129 struct make_euml_terminal; | |
130 template <class T,class F> | |
131 struct make_euml_terminal<T,F,typename ::boost::disable_if<has_using_declared_table<F> >::type> | |
132 {}; | |
133 template <class T,class F> | |
134 struct make_euml_terminal<T,F,typename ::boost::enable_if<has_using_declared_table<F> >::type> | |
135 : public proto::extends<typename proto::terminal< boost::msm::state_tag>::type, T, boost::msm::state_domain> | |
136 {}; | |
137 | |
138 // library-containing class for state machines. Pass the actual FSM class as | |
139 // the Concrete parameter. | |
140 // A0=Derived,A1=NoHistory,A2=CompilePolicy,A3=FsmCheckPolicy > | |
141 template < | |
142 class A0 | |
143 , class A1 = parameter::void_ | |
144 , class A2 = parameter::void_ | |
145 , class A3 = parameter::void_ | |
146 , class A4 = parameter::void_ | |
147 > | |
148 class state_machine : //public Derived | |
149 public ::boost::parameter::binding< | |
150 typename state_machine_signature::bind<A0,A1,A2,A3,A4>::type, ::boost::msm::back::tag::front_end | |
151 >::type | |
152 , public make_euml_terminal<state_machine<A0,A1,A2,A3,A4>, | |
153 typename ::boost::parameter::binding< | |
154 typename state_machine_signature::bind<A0,A1,A2,A3,A4>::type, ::boost::msm::back::tag::front_end | |
155 >::type | |
156 > | |
157 { | |
158 public: | |
159 // Create ArgumentPack | |
160 typedef typename | |
161 state_machine_signature::bind<A0,A1,A2,A3,A4>::type | |
162 state_machine_args; | |
163 | |
164 // Extract first logical parameter. | |
165 typedef typename ::boost::parameter::binding< | |
166 state_machine_args, ::boost::msm::back::tag::front_end>::type Derived; | |
167 | |
168 typedef typename ::boost::parameter::binding< | |
169 state_machine_args, ::boost::msm::back::tag::history_policy, NoHistory >::type HistoryPolicy; | |
170 | |
171 typedef typename ::boost::parameter::binding< | |
172 state_machine_args, ::boost::msm::back::tag::compile_policy, favor_runtime_speed >::type CompilePolicy; | |
173 | |
174 typedef typename ::boost::parameter::binding< | |
175 state_machine_args, ::boost::msm::back::tag::fsm_check_policy, no_fsm_check >::type FsmCheckPolicy; | |
176 | |
177 typedef typename ::boost::parameter::binding< | |
178 state_machine_args, ::boost::msm::back::tag::queue_container_policy, | |
179 queue_container_deque >::type QueueContainerPolicy; | |
180 | |
181 private: | |
182 | |
183 typedef boost::msm::back::state_machine< | |
184 A0,A1,A2,A3,A4> library_sm; | |
185 | |
186 typedef ::boost::function< | |
187 execute_return ()> transition_fct; | |
188 typedef ::boost::function< | |
189 execute_return () > deferred_fct; | |
190 typedef typename QueueContainerPolicy:: | |
191 template In< | |
192 std::pair<deferred_fct,bool> >::type deferred_events_queue_t; | |
193 typedef typename QueueContainerPolicy:: | |
194 template In<transition_fct>::type events_queue_t; | |
195 | |
196 typedef typename boost::mpl::eval_if< | |
197 typename is_active_state_switch_policy<Derived>::type, | |
198 get_active_state_switch_policy<Derived>, | |
199 // default | |
200 ::boost::mpl::identity<active_state_switch_after_entry> | |
201 >::type active_state_switching; | |
202 | |
203 typedef bool (*flag_handler)(library_sm const&); | |
204 | |
205 // all state machines are friend with each other to allow embedding any of them in another fsm | |
206 template <class ,class , class, class, class | |
207 > friend class boost::msm::back::state_machine; | |
208 | |
209 // helper to add, if needed, visitors to all states | |
210 // version without visitors | |
211 template <class StateType,class Enable=void> | |
212 struct visitor_fct_helper | |
213 { | |
214 public: | |
215 visitor_fct_helper(){} | |
216 void fill_visitors(int) | |
217 { | |
218 } | |
219 template <class FCT> | |
220 void insert(int,FCT) | |
221 { | |
222 } | |
223 template <class VISITOR> | |
224 void execute(int,VISITOR) | |
225 { | |
226 } | |
227 }; | |
228 // version with visitors | |
229 template <class StateType> | |
230 struct visitor_fct_helper<StateType,typename ::boost::enable_if<has_accept_sig<StateType> >::type> | |
231 { | |
232 public: | |
233 visitor_fct_helper():m_state_visitors(){} | |
234 void fill_visitors(int number_of_states) | |
235 { | |
236 m_state_visitors.resize(number_of_states); | |
237 } | |
238 template <class FCT> | |
239 void insert(int index,FCT fct) | |
240 { | |
241 m_state_visitors[index]=fct; | |
242 } | |
243 void execute(int index) | |
244 { | |
245 m_state_visitors[index](); | |
246 } | |
247 | |
248 #define MSM_VISITOR_HELPER_EXECUTE_SUB(z, n, unused) ARG ## n vis ## n | |
249 #define MSM_VISITOR_HELPER_EXECUTE(z, n, unused) \ | |
250 template <BOOST_PP_ENUM_PARAMS(n, class ARG)> \ | |
251 void execute(int index BOOST_PP_COMMA_IF(n) \ | |
252 BOOST_PP_ENUM(n, MSM_VISITOR_HELPER_EXECUTE_SUB, ~ ) ) \ | |
253 { \ | |
254 m_state_visitors[index](BOOST_PP_ENUM_PARAMS(n,vis)); \ | |
255 } | |
256 BOOST_PP_REPEAT_FROM_TO(1,BOOST_PP_ADD(BOOST_MSM_VISITOR_ARG_SIZE,1), MSM_VISITOR_HELPER_EXECUTE, ~) | |
257 #undef MSM_VISITOR_HELPER_EXECUTE | |
258 #undef MSM_VISITOR_HELPER_EXECUTE_SUB | |
259 private: | |
260 typedef typename StateType::accept_sig::type visitor_fct; | |
261 typedef std::vector<visitor_fct> visitors; | |
262 | |
263 visitors m_state_visitors; | |
264 }; | |
265 | |
266 template <class StateType,class Enable=int> | |
267 struct deferred_msg_queue_helper | |
268 { | |
269 }; | |
270 template <class StateType> | |
271 struct deferred_msg_queue_helper<StateType, | |
272 typename ::boost::enable_if< | |
273 typename ::boost::msm::back::has_fsm_deferred_events<StateType>::type,int >::type> | |
274 { | |
275 public: | |
276 deferred_msg_queue_helper():m_deferred_events_queue(){} | |
277 deferred_events_queue_t m_deferred_events_queue; | |
278 }; | |
279 | |
280 public: | |
281 // tags | |
282 typedef int composite_tag; | |
283 | |
284 // in case someone needs to know | |
285 typedef HistoryPolicy history_policy; | |
286 | |
287 struct InitEvent { }; | |
288 struct ExitEvent { }; | |
289 // flag handling | |
290 struct Flag_AND | |
291 { | |
292 typedef std::logical_and<bool> type; | |
293 }; | |
294 struct Flag_OR | |
295 { | |
296 typedef std::logical_or<bool> type; | |
297 }; | |
298 typedef typename Derived::BaseAllStates BaseState; | |
299 typedef Derived ConcreteSM; | |
300 | |
301 // if the front-end fsm provides an initial_event typedef, replace InitEvent by this one | |
302 typedef typename ::boost::mpl::eval_if< | |
303 typename has_initial_event<Derived>::type, | |
304 get_initial_event<Derived>, | |
305 ::boost::mpl::identity<InitEvent> | |
306 >::type fsm_initial_event; | |
307 | |
308 // if the front-end fsm provides an exit_event typedef, replace ExitEvent by this one | |
309 typedef typename ::boost::mpl::eval_if< | |
310 typename has_final_event<Derived>::type, | |
311 get_final_event<Derived>, | |
312 ::boost::mpl::identity<ExitEvent> | |
313 >::type fsm_final_event; | |
314 | |
315 template <class ExitPoint> | |
316 struct exit_pt : public ExitPoint | |
317 { | |
318 // tags | |
319 typedef ExitPoint wrapped_exit; | |
320 typedef int pseudo_exit; | |
321 typedef library_sm owner; | |
322 typedef int no_automatic_create; | |
323 typedef typename | |
324 ExitPoint::event Event; | |
325 typedef ::boost::function<execute_return (Event const&)> | |
326 forwarding_function; | |
327 | |
328 // forward event to the higher-level FSM | |
329 template <class ForwardEvent> | |
330 void forward_event(ForwardEvent const& incomingEvent) | |
331 { | |
332 // use helper to forward or not | |
333 ForwardHelper< ::boost::is_convertible<ForwardEvent,Event>::value>::helper(incomingEvent,m_forward); | |
334 } | |
335 void set_forward_fct(::boost::function<execute_return (Event const&)> fct) | |
336 { | |
337 m_forward = fct; | |
338 } | |
339 exit_pt():m_forward(){} | |
340 // by assignments, we keep our forwarding functor unchanged as our containing SM did not change | |
341 template <class RHS> | |
342 exit_pt(RHS& rhs):m_forward(){} | |
343 exit_pt<ExitPoint>& operator= (const exit_pt<ExitPoint>& ) | |
344 { | |
345 return *this; | |
346 } | |
347 private: | |
348 forwarding_function m_forward; | |
349 | |
350 // using partial specialization instead of enable_if because of VC8 bug | |
351 template <bool OwnEvent, int Dummy=0> | |
352 struct ForwardHelper | |
353 { | |
354 template <class ForwardEvent> | |
355 static void helper(ForwardEvent const& ,forwarding_function& ) | |
356 { | |
357 // Not our event, assert | |
358 BOOST_ASSERT(false); | |
359 } | |
360 }; | |
361 template <int Dummy> | |
362 struct ForwardHelper<true,Dummy> | |
363 { | |
364 template <class ForwardEvent> | |
365 static void helper(ForwardEvent const& incomingEvent,forwarding_function& forward_fct) | |
366 { | |
367 // call if handler set, if not, this state is simply a terminate state | |
368 if (forward_fct) | |
369 forward_fct(incomingEvent); | |
370 } | |
371 }; | |
372 | |
373 }; | |
374 template <class EntryPoint> | |
375 struct entry_pt : public EntryPoint | |
376 { | |
377 // tags | |
378 typedef EntryPoint wrapped_entry; | |
379 typedef int pseudo_entry; | |
380 typedef library_sm owner; | |
381 typedef int no_automatic_create; | |
382 }; | |
383 template <class EntryPoint> | |
384 struct direct : public EntryPoint | |
385 { | |
386 // tags | |
387 typedef EntryPoint wrapped_entry; | |
388 typedef int explicit_entry_state; | |
389 typedef library_sm owner; | |
390 typedef int no_automatic_create; | |
391 }; | |
392 typedef typename get_number_of_regions<typename Derived::initial_state>::type nr_regions; | |
393 // Template used to form rows in the transition table | |
394 template< | |
395 typename ROW | |
396 > | |
397 struct row_ | |
398 { | |
399 //typedef typename ROW::Source T1; | |
400 typedef typename make_entry<typename ROW::Source,library_sm>::type T1; | |
401 typedef typename make_exit<typename ROW::Target,library_sm>::type T2; | |
402 typedef typename ROW::Evt transition_event; | |
403 // if the source is an exit pseudo state, then | |
404 // current_state_type becomes the result of get_owner | |
405 // meaning the containing SM from which the exit occurs | |
406 typedef typename ::boost::mpl::eval_if< | |
407 typename has_pseudo_exit<T1>::type, | |
408 get_owner<T1,library_sm>, | |
409 ::boost::mpl::identity<typename ROW::Source> >::type current_state_type; | |
410 | |
411 // if Target is a sequence, then we have a fork and expect a sequence of explicit_entry | |
412 // else if Target is an explicit_entry, next_state_type becomes the result of get_owner | |
413 // meaning the containing SM if the row is "outside" the containing SM or else the explicit_entry state itself | |
414 typedef typename ::boost::mpl::eval_if< | |
415 typename ::boost::mpl::is_sequence<T2>::type, | |
416 get_fork_owner<T2,library_sm>, | |
417 ::boost::mpl::eval_if< | |
418 typename has_no_automatic_create<T2>::type, | |
419 get_owner<T2,library_sm>, | |
420 ::boost::mpl::identity<T2> > | |
421 >::type next_state_type; | |
422 | |
423 // if a guard condition is here, call it to check that the event is accepted | |
424 static bool check_guard(library_sm& fsm,transition_event const& evt) | |
425 { | |
426 if ( ROW::guard_call(fsm,evt, | |
427 ::boost::fusion::at_key<current_state_type>(fsm.m_substate_list), | |
428 ::boost::fusion::at_key<next_state_type>(fsm.m_substate_list), | |
429 fsm.m_substate_list ) ) | |
430 return true; | |
431 return false; | |
432 } | |
433 // Take the transition action and return the next state. | |
434 static HandledEnum execute(library_sm& fsm, int region_index, int state, transition_event const& evt) | |
435 { | |
436 | |
437 BOOST_STATIC_CONSTANT(int, current_state = (get_state_id<stt,current_state_type>::type::value)); | |
438 BOOST_STATIC_CONSTANT(int, next_state = (get_state_id<stt,next_state_type>::type::value)); | |
439 BOOST_ASSERT(state == (current_state)); | |
440 // if T1 is an exit pseudo state, then take the transition only if the pseudo exit state is active | |
441 if (has_pseudo_exit<T1>::type::value && | |
442 !is_exit_state_active<T1,get_owner<T1,library_sm> >(fsm)) | |
443 { | |
444 return HANDLED_FALSE; | |
445 } | |
446 if (!check_guard(fsm,evt)) | |
447 { | |
448 // guard rejected the event, we stay in the current one | |
449 return HANDLED_GUARD_REJECT; | |
450 } | |
451 fsm.m_states[region_index] = active_state_switching::after_guard(current_state,next_state); | |
452 | |
453 // the guard condition has already been checked | |
454 execute_exit<current_state_type> | |
455 (::boost::fusion::at_key<current_state_type>(fsm.m_substate_list),evt,fsm); | |
456 fsm.m_states[region_index] = active_state_switching::after_exit(current_state,next_state); | |
457 | |
458 // then call the action method | |
459 HandledEnum res = ROW::action_call(fsm,evt, | |
460 ::boost::fusion::at_key<current_state_type>(fsm.m_substate_list), | |
461 ::boost::fusion::at_key<next_state_type>(fsm.m_substate_list), | |
462 fsm.m_substate_list); | |
463 fsm.m_states[region_index] = active_state_switching::after_action(current_state,next_state); | |
464 | |
465 // and finally the entry method of the new current state | |
466 convert_event_and_execute_entry<next_state_type,T2> | |
467 (::boost::fusion::at_key<next_state_type>(fsm.m_substate_list),evt,fsm); | |
468 fsm.m_states[region_index] = active_state_switching::after_entry(current_state,next_state); | |
469 return res; | |
470 } | |
471 }; | |
472 | |
473 // row having only a guard condition | |
474 template< | |
475 typename ROW | |
476 > | |
477 struct g_row_ | |
478 { | |
479 //typedef typename ROW::Source T1; | |
480 typedef typename make_entry<typename ROW::Source,library_sm>::type T1; | |
481 typedef typename make_exit<typename ROW::Target,library_sm>::type T2; | |
482 typedef typename ROW::Evt transition_event; | |
483 // if the source is an exit pseudo state, then | |
484 // current_state_type becomes the result of get_owner | |
485 // meaning the containing SM from which the exit occurs | |
486 typedef typename ::boost::mpl::eval_if< | |
487 typename has_pseudo_exit<T1>::type, | |
488 get_owner<T1,library_sm>, | |
489 ::boost::mpl::identity<typename ROW::Source> >::type current_state_type; | |
490 | |
491 // if Target is a sequence, then we have a fork and expect a sequence of explicit_entry | |
492 // else if Target is an explicit_entry, next_state_type becomes the result of get_owner | |
493 // meaning the containing SM if the row is "outside" the containing SM or else the explicit_entry state itself | |
494 typedef typename ::boost::mpl::eval_if< | |
495 typename ::boost::mpl::is_sequence<T2>::type, | |
496 get_fork_owner<T2,library_sm>, | |
497 ::boost::mpl::eval_if< | |
498 typename has_no_automatic_create<T2>::type, | |
499 get_owner<T2,library_sm>, | |
500 ::boost::mpl::identity<T2> > | |
501 >::type next_state_type; | |
502 | |
503 // if a guard condition is defined, call it to check that the event is accepted | |
504 static bool check_guard(library_sm& fsm,transition_event const& evt) | |
505 { | |
506 if ( ROW::guard_call(fsm,evt, | |
507 ::boost::fusion::at_key<current_state_type>(fsm.m_substate_list), | |
508 ::boost::fusion::at_key<next_state_type>(fsm.m_substate_list), | |
509 fsm.m_substate_list )) | |
510 return true; | |
511 return false; | |
512 } | |
513 // Take the transition action and return the next state. | |
514 static HandledEnum execute(library_sm& fsm, int region_index, int state, transition_event const& evt) | |
515 { | |
516 BOOST_STATIC_CONSTANT(int, current_state = (get_state_id<stt,current_state_type>::type::value)); | |
517 BOOST_STATIC_CONSTANT(int, next_state = (get_state_id<stt,next_state_type>::type::value)); | |
518 BOOST_ASSERT(state == (current_state)); | |
519 // if T1 is an exit pseudo state, then take the transition only if the pseudo exit state is active | |
520 if (has_pseudo_exit<T1>::type::value && | |
521 !is_exit_state_active<T1,get_owner<T1,library_sm> >(fsm)) | |
522 { | |
523 return HANDLED_FALSE; | |
524 } | |
525 if (!check_guard(fsm,evt)) | |
526 { | |
527 // guard rejected the event, we stay in the current one | |
528 return HANDLED_GUARD_REJECT; | |
529 } | |
530 fsm.m_states[region_index] = active_state_switching::after_guard(current_state,next_state); | |
531 | |
532 // the guard condition has already been checked | |
533 execute_exit<current_state_type> | |
534 (::boost::fusion::at_key<current_state_type>(fsm.m_substate_list),evt,fsm); | |
535 fsm.m_states[region_index] = active_state_switching::after_exit(current_state,next_state); | |
536 fsm.m_states[region_index] = active_state_switching::after_action(current_state,next_state); | |
537 | |
538 // and finally the entry method of the new current state | |
539 convert_event_and_execute_entry<next_state_type,T2> | |
540 (::boost::fusion::at_key<next_state_type>(fsm.m_substate_list),evt,fsm); | |
541 fsm.m_states[region_index] = active_state_switching::after_entry(current_state,next_state); | |
542 return HANDLED_TRUE; | |
543 } | |
544 }; | |
545 | |
546 // row having only an action method | |
547 template< | |
548 typename ROW | |
549 > | |
550 struct a_row_ | |
551 { | |
552 //typedef typename ROW::Source T1; | |
553 typedef typename make_entry<typename ROW::Source,library_sm>::type T1; | |
554 typedef typename make_exit<typename ROW::Target,library_sm>::type T2; | |
555 typedef typename ROW::Evt transition_event; | |
556 // if the source is an exit pseudo state, then | |
557 // current_state_type becomes the result of get_owner | |
558 // meaning the containing SM from which the exit occurs | |
559 typedef typename ::boost::mpl::eval_if< | |
560 typename has_pseudo_exit<T1>::type, | |
561 get_owner<T1,library_sm>, | |
562 ::boost::mpl::identity<typename ROW::Source> >::type current_state_type; | |
563 | |
564 // if Target is a sequence, then we have a fork and expect a sequence of explicit_entry | |
565 // else if Target is an explicit_entry, next_state_type becomes the result of get_owner | |
566 // meaning the containing SM if the row is "outside" the containing SM or else the explicit_entry state itself | |
567 typedef typename ::boost::mpl::eval_if< | |
568 typename ::boost::mpl::is_sequence<T2>::type, | |
569 get_fork_owner<T2,library_sm>, | |
570 ::boost::mpl::eval_if< | |
571 typename has_no_automatic_create<T2>::type, | |
572 get_owner<T2,library_sm>, | |
573 ::boost::mpl::identity<T2> > | |
574 >::type next_state_type; | |
575 | |
576 // Take the transition action and return the next state. | |
577 static HandledEnum execute(library_sm& fsm, int region_index, int state, transition_event const& evt) | |
578 { | |
579 BOOST_STATIC_CONSTANT(int, current_state = (get_state_id<stt,current_state_type>::type::value)); | |
580 BOOST_STATIC_CONSTANT(int, next_state = (get_state_id<stt,next_state_type>::type::value)); | |
581 BOOST_ASSERT(state == (current_state)); | |
582 | |
583 // if T1 is an exit pseudo state, then take the transition only if the pseudo exit state is active | |
584 if (has_pseudo_exit<T1>::type::value && | |
585 !is_exit_state_active<T1,get_owner<T1,library_sm> >(fsm)) | |
586 { | |
587 return HANDLED_FALSE; | |
588 } | |
589 fsm.m_states[region_index] = active_state_switching::after_guard(current_state,next_state); | |
590 | |
591 // no need to check the guard condition | |
592 // first call the exit method of the current state | |
593 execute_exit<current_state_type> | |
594 (::boost::fusion::at_key<current_state_type>(fsm.m_substate_list),evt,fsm); | |
595 fsm.m_states[region_index] = active_state_switching::after_exit(current_state,next_state); | |
596 | |
597 // then call the action method | |
598 HandledEnum res = ROW::action_call(fsm,evt, | |
599 ::boost::fusion::at_key<current_state_type>(fsm.m_substate_list), | |
600 ::boost::fusion::at_key<next_state_type>(fsm.m_substate_list), | |
601 fsm.m_substate_list); | |
602 fsm.m_states[region_index] = active_state_switching::after_action(current_state,next_state); | |
603 | |
604 // and finally the entry method of the new current state | |
605 convert_event_and_execute_entry<next_state_type,T2> | |
606 (::boost::fusion::at_key<next_state_type>(fsm.m_substate_list),evt,fsm); | |
607 fsm.m_states[region_index] = active_state_switching::after_entry(current_state,next_state); | |
608 return res; | |
609 } | |
610 }; | |
611 | |
612 // row having no guard condition or action, simply transitions | |
613 template< | |
614 typename ROW | |
615 > | |
616 struct _row_ | |
617 { | |
618 //typedef typename ROW::Source T1; | |
619 typedef typename make_entry<typename ROW::Source,library_sm>::type T1; | |
620 typedef typename make_exit<typename ROW::Target,library_sm>::type T2; | |
621 typedef typename ROW::Evt transition_event; | |
622 // if the source is an exit pseudo state, then | |
623 // current_state_type becomes the result of get_owner | |
624 // meaning the containing SM from which the exit occurs | |
625 typedef typename ::boost::mpl::eval_if< | |
626 typename has_pseudo_exit<T1>::type, | |
627 get_owner<T1,library_sm>, | |
628 ::boost::mpl::identity<typename ROW::Source> >::type current_state_type; | |
629 | |
630 // if Target is a sequence, then we have a fork and expect a sequence of explicit_entry | |
631 // else if Target is an explicit_entry, next_state_type becomes the result of get_owner | |
632 // meaning the containing SM if the row is "outside" the containing SM or else the explicit_entry state itself | |
633 typedef typename ::boost::mpl::eval_if< | |
634 typename ::boost::mpl::is_sequence<T2>::type, | |
635 get_fork_owner<T2,library_sm>, | |
636 ::boost::mpl::eval_if< | |
637 typename has_no_automatic_create<T2>::type, | |
638 get_owner<T2,library_sm>, | |
639 ::boost::mpl::identity<T2> > | |
640 >::type next_state_type; | |
641 | |
642 // Take the transition action and return the next state. | |
643 static HandledEnum execute(library_sm& fsm, int region_index, int state, transition_event const& evt) | |
644 { | |
645 BOOST_STATIC_CONSTANT(int, current_state = (get_state_id<stt,current_state_type>::type::value)); | |
646 BOOST_STATIC_CONSTANT(int, next_state = (get_state_id<stt,next_state_type>::type::value)); | |
647 BOOST_ASSERT(state == (current_state)); | |
648 | |
649 // if T1 is an exit pseudo state, then take the transition only if the pseudo exit state is active | |
650 if (has_pseudo_exit<T1>::type::value && | |
651 !is_exit_state_active<T1,get_owner<T1,library_sm> >(fsm)) | |
652 { | |
653 return HANDLED_FALSE; | |
654 } | |
655 fsm.m_states[region_index] = active_state_switching::after_guard(current_state,next_state); | |
656 | |
657 // first call the exit method of the current state | |
658 execute_exit<current_state_type> | |
659 (::boost::fusion::at_key<current_state_type>(fsm.m_substate_list),evt,fsm); | |
660 fsm.m_states[region_index] = active_state_switching::after_exit(current_state,next_state); | |
661 fsm.m_states[region_index] = active_state_switching::after_action(current_state,next_state); | |
662 | |
663 | |
664 // and finally the entry method of the new current state | |
665 convert_event_and_execute_entry<next_state_type,T2> | |
666 (::boost::fusion::at_key<next_state_type>(fsm.m_substate_list),evt,fsm); | |
667 fsm.m_states[region_index] = active_state_switching::after_entry(current_state,next_state); | |
668 return HANDLED_TRUE; | |
669 } | |
670 }; | |
671 // "i" rows are rows for internal transitions | |
672 template< | |
673 typename ROW | |
674 > | |
675 struct irow_ | |
676 { | |
677 typedef typename make_entry<typename ROW::Source,library_sm>::type T1; | |
678 typedef typename make_exit<typename ROW::Target,library_sm>::type T2; | |
679 typedef typename ROW::Evt transition_event; | |
680 typedef typename ROW::Source current_state_type; | |
681 typedef T2 next_state_type; | |
682 | |
683 // if a guard condition is here, call it to check that the event is accepted | |
684 static bool check_guard(library_sm& fsm,transition_event const& evt) | |
685 { | |
686 if ( ROW::guard_call(fsm,evt, | |
687 ::boost::fusion::at_key<current_state_type>(fsm.m_substate_list), | |
688 ::boost::fusion::at_key<next_state_type>(fsm.m_substate_list), | |
689 fsm.m_substate_list)) | |
690 return true; | |
691 return false; | |
692 } | |
693 // Take the transition action and return the next state. | |
694 static HandledEnum execute(library_sm& fsm, int , int state, transition_event const& evt) | |
695 { | |
696 | |
697 BOOST_STATIC_CONSTANT(int, current_state = (get_state_id<stt,current_state_type>::type::value)); | |
698 BOOST_ASSERT(state == (current_state)); | |
699 if (!check_guard(fsm,evt)) | |
700 { | |
701 // guard rejected the event, we stay in the current one | |
702 return HANDLED_GUARD_REJECT; | |
703 } | |
704 | |
705 // call the action method | |
706 HandledEnum res = ROW::action_call(fsm,evt, | |
707 ::boost::fusion::at_key<current_state_type>(fsm.m_substate_list), | |
708 ::boost::fusion::at_key<next_state_type>(fsm.m_substate_list), | |
709 fsm.m_substate_list); | |
710 return res; | |
711 } | |
712 }; | |
713 | |
714 // row having only a guard condition | |
715 template< | |
716 typename ROW | |
717 > | |
718 struct g_irow_ | |
719 { | |
720 typedef typename make_entry<typename ROW::Source,library_sm>::type T1; | |
721 typedef typename make_exit<typename ROW::Target,library_sm>::type T2; | |
722 typedef typename ROW::Evt transition_event; | |
723 typedef typename ROW::Source current_state_type; | |
724 typedef T2 next_state_type; | |
725 | |
726 // if a guard condition is defined, call it to check that the event is accepted | |
727 static bool check_guard(library_sm& fsm,transition_event const& evt) | |
728 { | |
729 if ( ROW::guard_call(fsm,evt, | |
730 ::boost::fusion::at_key<current_state_type>(fsm.m_substate_list), | |
731 ::boost::fusion::at_key<next_state_type>(fsm.m_substate_list), | |
732 fsm.m_substate_list) ) | |
733 return true; | |
734 return false; | |
735 } | |
736 // Take the transition action and return the next state. | |
737 static HandledEnum execute(library_sm& fsm, int , int state, transition_event const& evt) | |
738 { | |
739 BOOST_STATIC_CONSTANT(int, current_state = (get_state_id<stt,current_state_type>::type::value)); | |
740 BOOST_ASSERT(state == (current_state)); | |
741 if (!check_guard(fsm,evt)) | |
742 { | |
743 // guard rejected the event, we stay in the current one | |
744 return HANDLED_GUARD_REJECT; | |
745 } | |
746 return HANDLED_TRUE; | |
747 } | |
748 }; | |
749 | |
750 // row having only an action method | |
751 template< | |
752 typename ROW | |
753 > | |
754 struct a_irow_ | |
755 { | |
756 typedef typename make_entry<typename ROW::Source,library_sm>::type T1; | |
757 typedef typename make_exit<typename ROW::Target,library_sm>::type T2; | |
758 | |
759 typedef typename ROW::Evt transition_event; | |
760 typedef typename ROW::Source current_state_type; | |
761 typedef T2 next_state_type; | |
762 | |
763 // Take the transition action and return the next state. | |
764 static HandledEnum execute(library_sm& fsm, int , int state, transition_event const& evt) | |
765 { | |
766 BOOST_STATIC_CONSTANT(int, current_state = (get_state_id<stt,current_state_type>::type::value)); | |
767 BOOST_ASSERT(state == (current_state)); | |
768 | |
769 // call the action method | |
770 HandledEnum res = ROW::action_call(fsm,evt, | |
771 ::boost::fusion::at_key<current_state_type>(fsm.m_substate_list), | |
772 ::boost::fusion::at_key<next_state_type>(fsm.m_substate_list), | |
773 fsm.m_substate_list); | |
774 | |
775 return res; | |
776 } | |
777 }; | |
778 // row simply ignoring the event | |
779 template< | |
780 typename ROW | |
781 > | |
782 struct _irow_ | |
783 { | |
784 typedef typename make_entry<typename ROW::Source,library_sm>::type T1; | |
785 typedef typename make_exit<typename ROW::Target,library_sm>::type T2; | |
786 typedef typename ROW::Evt transition_event; | |
787 typedef typename ROW::Source current_state_type; | |
788 typedef T2 next_state_type; | |
789 | |
790 // Take the transition action and return the next state. | |
791 static HandledEnum execute(library_sm& , int , int state, transition_event const& ) | |
792 { | |
793 BOOST_STATIC_CONSTANT(int, current_state = (get_state_id<stt,current_state_type>::type::value)); | |
794 BOOST_ASSERT(state == (current_state)); | |
795 return HANDLED_TRUE; | |
796 } | |
797 }; | |
798 // transitions internal to this state machine (no substate involved) | |
799 template< | |
800 typename ROW, | |
801 typename StateType | |
802 > | |
803 struct internal_ | |
804 { | |
805 typedef StateType current_state_type; | |
806 typedef StateType next_state_type; | |
807 typedef typename ROW::Evt transition_event; | |
808 | |
809 // if a guard condition is here, call it to check that the event is accepted | |
810 static bool check_guard(library_sm& fsm,transition_event const& evt) | |
811 { | |
812 if ( ROW::guard_call(fsm,evt, | |
813 ::boost::fusion::at_key<StateType>(fsm.m_substate_list), | |
814 ::boost::fusion::at_key<StateType>(fsm.m_substate_list), | |
815 fsm.m_substate_list) ) | |
816 return true; | |
817 return false; | |
818 } | |
819 // Take the transition action and return the next state. | |
820 static HandledEnum execute(library_sm& fsm, int , int , transition_event const& evt) | |
821 { | |
822 if (!check_guard(fsm,evt)) | |
823 { | |
824 // guard rejected the event, we stay in the current one | |
825 return HANDLED_GUARD_REJECT; | |
826 } | |
827 | |
828 // then call the action method | |
829 HandledEnum res = ROW::action_call(fsm,evt, | |
830 ::boost::fusion::at_key<StateType>(fsm.m_substate_list), | |
831 ::boost::fusion::at_key<StateType>(fsm.m_substate_list), | |
832 fsm.m_substate_list); | |
833 return res; | |
834 } | |
835 }; | |
836 template< | |
837 typename ROW | |
838 > | |
839 struct internal_ <ROW,library_sm> | |
840 { | |
841 typedef library_sm current_state_type; | |
842 typedef library_sm next_state_type; | |
843 typedef typename ROW::Evt transition_event; | |
844 | |
845 // if a guard condition is here, call it to check that the event is accepted | |
846 static bool check_guard(library_sm& fsm,transition_event const& evt) | |
847 { | |
848 if ( ROW::guard_call(fsm,evt, | |
849 fsm, | |
850 fsm, | |
851 fsm.m_substate_list) ) | |
852 return true; | |
853 return false; | |
854 } | |
855 // Take the transition action and return the next state. | |
856 static HandledEnum execute(library_sm& fsm, int , int , transition_event const& evt) | |
857 { | |
858 if (!check_guard(fsm,evt)) | |
859 { | |
860 // guard rejected the event, we stay in the current one | |
861 return HANDLED_GUARD_REJECT; | |
862 } | |
863 | |
864 // then call the action method | |
865 HandledEnum res = ROW::action_call(fsm,evt, | |
866 fsm, | |
867 fsm, | |
868 fsm.m_substate_list); | |
869 return res; | |
870 } | |
871 }; | |
872 | |
873 template< | |
874 typename ROW, | |
875 typename StateType | |
876 > | |
877 struct a_internal_ | |
878 { | |
879 typedef StateType current_state_type; | |
880 typedef StateType next_state_type; | |
881 typedef typename ROW::Evt transition_event; | |
882 | |
883 // Take the transition action and return the next state. | |
884 static HandledEnum execute(library_sm& fsm, int, int, transition_event const& evt) | |
885 { | |
886 // then call the action method | |
887 HandledEnum res = ROW::action_call(fsm,evt, | |
888 ::boost::fusion::at_key<StateType>(fsm.m_substate_list), | |
889 ::boost::fusion::at_key<StateType>(fsm.m_substate_list), | |
890 fsm.m_substate_list); | |
891 return res; | |
892 } | |
893 }; | |
894 template< | |
895 typename ROW | |
896 > | |
897 struct a_internal_ <ROW,library_sm> | |
898 { | |
899 typedef library_sm current_state_type; | |
900 typedef library_sm next_state_type; | |
901 typedef typename ROW::Evt transition_event; | |
902 | |
903 // Take the transition action and return the next state. | |
904 static HandledEnum execute(library_sm& fsm, int, int, transition_event const& evt) | |
905 { | |
906 // then call the action method | |
907 HandledEnum res = ROW::action_call(fsm,evt, | |
908 fsm, | |
909 fsm, | |
910 fsm.m_substate_list); | |
911 return res; | |
912 } | |
913 }; | |
914 template< | |
915 typename ROW, | |
916 typename StateType | |
917 > | |
918 struct g_internal_ | |
919 { | |
920 typedef StateType current_state_type; | |
921 typedef StateType next_state_type; | |
922 typedef typename ROW::Evt transition_event; | |
923 | |
924 // if a guard condition is here, call it to check that the event is accepted | |
925 static bool check_guard(library_sm& fsm,transition_event const& evt) | |
926 { | |
927 if ( ROW::guard_call(fsm,evt, | |
928 ::boost::fusion::at_key<StateType>(fsm.m_substate_list), | |
929 ::boost::fusion::at_key<StateType>(fsm.m_substate_list), | |
930 fsm.m_substate_list) ) | |
931 return true; | |
932 return false; | |
933 } | |
934 // Take the transition action and return the next state. | |
935 static HandledEnum execute(library_sm& fsm, int, int, transition_event const& evt) | |
936 { | |
937 if (!check_guard(fsm,evt)) | |
938 { | |
939 // guard rejected the event, we stay in the current one | |
940 return HANDLED_GUARD_REJECT; | |
941 } | |
942 return HANDLED_TRUE; | |
943 } | |
944 }; | |
945 template< | |
946 typename ROW | |
947 > | |
948 struct g_internal_ <ROW,library_sm> | |
949 { | |
950 typedef library_sm current_state_type; | |
951 typedef library_sm next_state_type; | |
952 typedef typename ROW::Evt transition_event; | |
953 | |
954 // if a guard condition is here, call it to check that the event is accepted | |
955 static bool check_guard(library_sm& fsm,transition_event const& evt) | |
956 { | |
957 if ( ROW::guard_call(fsm,evt, | |
958 fsm, | |
959 fsm, | |
960 fsm.m_substate_list) ) | |
961 return true; | |
962 return false; | |
963 } | |
964 // Take the transition action and return the next state. | |
965 static HandledEnum execute(library_sm& fsm, int, int, transition_event const& evt) | |
966 { | |
967 if (!check_guard(fsm,evt)) | |
968 { | |
969 // guard rejected the event, we stay in the current one | |
970 return HANDLED_GUARD_REJECT; | |
971 } | |
972 return HANDLED_TRUE; | |
973 } | |
974 }; | |
975 template< | |
976 typename ROW, | |
977 typename StateType | |
978 > | |
979 struct _internal_ | |
980 { | |
981 typedef StateType current_state_type; | |
982 typedef StateType next_state_type; | |
983 typedef typename ROW::Evt transition_event; | |
984 static HandledEnum execute(library_sm& , int , int , transition_event const& ) | |
985 { | |
986 return HANDLED_TRUE; | |
987 } | |
988 }; | |
989 template< | |
990 typename ROW | |
991 > | |
992 struct _internal_ <ROW,library_sm> | |
993 { | |
994 typedef library_sm current_state_type; | |
995 typedef library_sm next_state_type; | |
996 typedef typename ROW::Evt transition_event; | |
997 static HandledEnum execute(library_sm& , int , int , transition_event const& ) | |
998 { | |
999 return HANDLED_TRUE; | |
1000 } | |
1001 }; | |
1002 // Template used to form forwarding rows in the transition table for every row of a composite SM | |
1003 template< | |
1004 typename T1 | |
1005 , class Evt | |
1006 > | |
1007 struct frow | |
1008 { | |
1009 typedef T1 current_state_type; | |
1010 typedef T1 next_state_type; | |
1011 typedef Evt transition_event; | |
1012 // tag to find out if a row is a forwarding row | |
1013 typedef int is_frow; | |
1014 | |
1015 // Take the transition action and return the next state. | |
1016 static HandledEnum execute(library_sm& fsm, int region_index, int , transition_event const& evt) | |
1017 { | |
1018 // false as second parameter because this event is forwarded from outer fsm | |
1019 execute_return res = | |
1020 (::boost::fusion::at_key<current_state_type>(fsm.m_substate_list)).process_event_internal(evt,false); | |
1021 fsm.m_states[region_index]=get_state_id<stt,T1>::type::value; | |
1022 return res; | |
1023 } | |
1024 // helper metafunctions used by dispatch table and give the frow a new event | |
1025 // (used to avoid double entries in a table because of base events) | |
1026 template <class NewEvent> | |
1027 struct replace_event | |
1028 { | |
1029 typedef frow<T1,NewEvent> type; | |
1030 }; | |
1031 }; | |
1032 | |
1033 template <class Tag, class Transition,class StateType> | |
1034 struct create_backend_stt | |
1035 { | |
1036 }; | |
1037 template <class Transition,class StateType> | |
1038 struct create_backend_stt<g_row_tag,Transition,StateType> | |
1039 { | |
1040 typedef g_row_<Transition> type; | |
1041 }; | |
1042 template <class Transition,class StateType> | |
1043 struct create_backend_stt<a_row_tag,Transition,StateType> | |
1044 { | |
1045 typedef a_row_<Transition> type; | |
1046 }; | |
1047 template <class Transition,class StateType> | |
1048 struct create_backend_stt<_row_tag,Transition,StateType> | |
1049 { | |
1050 typedef _row_<Transition> type; | |
1051 }; | |
1052 template <class Transition,class StateType> | |
1053 struct create_backend_stt<row_tag,Transition,StateType> | |
1054 { | |
1055 typedef row_<Transition> type; | |
1056 }; | |
1057 // internal transitions | |
1058 template <class Transition,class StateType> | |
1059 struct create_backend_stt<g_irow_tag,Transition,StateType> | |
1060 { | |
1061 typedef g_irow_<Transition> type; | |
1062 }; | |
1063 template <class Transition,class StateType> | |
1064 struct create_backend_stt<a_irow_tag,Transition,StateType> | |
1065 { | |
1066 typedef a_irow_<Transition> type; | |
1067 }; | |
1068 template <class Transition,class StateType> | |
1069 struct create_backend_stt<irow_tag,Transition,StateType> | |
1070 { | |
1071 typedef irow_<Transition> type; | |
1072 }; | |
1073 template <class Transition,class StateType> | |
1074 struct create_backend_stt<_irow_tag,Transition,StateType> | |
1075 { | |
1076 typedef _irow_<Transition> type; | |
1077 }; | |
1078 template <class Transition,class StateType> | |
1079 struct create_backend_stt<sm_a_i_row_tag,Transition,StateType> | |
1080 { | |
1081 typedef a_internal_<Transition,StateType> type; | |
1082 }; | |
1083 template <class Transition,class StateType> | |
1084 struct create_backend_stt<sm_g_i_row_tag,Transition,StateType> | |
1085 { | |
1086 typedef g_internal_<Transition,StateType> type; | |
1087 }; | |
1088 template <class Transition,class StateType> | |
1089 struct create_backend_stt<sm_i_row_tag,Transition,StateType> | |
1090 { | |
1091 typedef internal_<Transition,StateType> type; | |
1092 }; | |
1093 template <class Transition,class StateType> | |
1094 struct create_backend_stt<sm__i_row_tag,Transition,StateType> | |
1095 { | |
1096 typedef _internal_<Transition,StateType> type; | |
1097 }; | |
1098 template <class Transition,class StateType=void> | |
1099 struct make_row_tag | |
1100 { | |
1101 typedef typename create_backend_stt<typename Transition::row_type_tag,Transition,StateType>::type type; | |
1102 }; | |
1103 | |
1104 // add to the stt the initial states which could be missing (if not being involved in a transition) | |
1105 template <class BaseType, class stt_simulated = typename BaseType::transition_table> | |
1106 struct create_real_stt | |
1107 { | |
1108 //typedef typename BaseType::transition_table stt_simulated; | |
1109 typedef typename ::boost::mpl::fold< | |
1110 stt_simulated,mpl::vector0<>, | |
1111 ::boost::mpl::push_back< ::boost::mpl::placeholders::_1, | |
1112 make_row_tag< ::boost::mpl::placeholders::_2 , BaseType > > | |
1113 >::type type; | |
1114 }; | |
1115 | |
1116 template <class Table,class Intermediate,class StateType> | |
1117 struct add_forwarding_row_helper | |
1118 { | |
1119 typedef typename generate_event_set<Table>::type all_events; | |
1120 typedef typename ::boost::mpl::fold< | |
1121 all_events, Intermediate, | |
1122 ::boost::mpl::push_back< ::boost::mpl::placeholders::_1, | |
1123 frow<StateType, ::boost::mpl::placeholders::_2> > >::type type; | |
1124 }; | |
1125 // gets the transition table from a composite and make from it a forwarding row | |
1126 template <class StateType,class IsComposite> | |
1127 struct get_internal_transition_table | |
1128 { | |
1129 // first get the table of a composite | |
1130 typedef typename recursive_get_transition_table<StateType>::type original_table; | |
1131 | |
1132 // we now look for the events the composite has in its internal transitions | |
1133 // the internal ones are searched recursively in sub-sub... states | |
1134 // we go recursively because our states can also have internal tables or substates etc. | |
1135 typedef typename recursive_get_internal_transition_table<StateType, ::boost::mpl::true_>::type recursive_istt; | |
1136 typedef typename ::boost::mpl::fold< | |
1137 recursive_istt,::boost::mpl::vector0<>, | |
1138 ::boost::mpl::push_back< ::boost::mpl::placeholders::_1, | |
1139 make_row_tag< ::boost::mpl::placeholders::_2 , StateType> > | |
1140 >::type recursive_istt_with_tag; | |
1141 | |
1142 typedef typename ::boost::mpl::insert_range< original_table, typename ::boost::mpl::end<original_table>::type, | |
1143 recursive_istt_with_tag>::type table_with_all_events; | |
1144 | |
1145 // and add for every event a forwarding row | |
1146 typedef typename ::boost::mpl::eval_if< | |
1147 typename CompilePolicy::add_forwarding_rows, | |
1148 add_forwarding_row_helper<table_with_all_events,::boost::mpl::vector0<>,StateType>, | |
1149 ::boost::mpl::identity< ::boost::mpl::vector0<> > | |
1150 >::type type; | |
1151 }; | |
1152 template <class StateType> | |
1153 struct get_internal_transition_table<StateType, ::boost::mpl::false_ > | |
1154 { | |
1155 typedef typename create_real_stt<StateType, typename StateType::internal_transition_table >::type type; | |
1156 }; | |
1157 // typedefs used internally | |
1158 typedef typename create_real_stt<Derived>::type real_transition_table; | |
1159 typedef typename create_stt<library_sm>::type stt; | |
1160 typedef typename get_initial_states<typename Derived::initial_state>::type initial_states; | |
1161 typedef typename generate_state_set<stt>::type state_list; | |
1162 typedef typename HistoryPolicy::template apply<nr_regions::value>::type concrete_history; | |
1163 | |
1164 typedef typename ::boost::fusion::result_of::as_set<state_list>::type substate_list; | |
1165 typedef typename ::boost::msm::back::generate_event_set< | |
1166 typename create_real_stt<library_sm, typename library_sm::internal_transition_table >::type | |
1167 >::type processable_events_internal_table; | |
1168 | |
1169 // extends the transition table with rows from composite states | |
1170 template <class Composite> | |
1171 struct extend_table | |
1172 { | |
1173 // add the init states | |
1174 //typedef typename create_stt<Composite>::type stt; | |
1175 typedef typename Composite::stt Stt; | |
1176 | |
1177 // add the internal events defined in the internal_transition_table | |
1178 // Note: these are added first because they must have a lesser prio | |
1179 // than the deeper transitions in the sub regions | |
1180 // table made of a stt + internal transitions of composite | |
1181 typedef typename ::boost::mpl::fold< | |
1182 typename Composite::internal_transition_table,::boost::mpl::vector0<>, | |
1183 ::boost::mpl::push_back< ::boost::mpl::placeholders::_1, | |
1184 make_row_tag< ::boost::mpl::placeholders::_2 , Composite> > | |
1185 >::type internal_stt; | |
1186 | |
1187 typedef typename ::boost::mpl::insert_range< | |
1188 Stt, | |
1189 typename ::boost::mpl::end<Stt>::type, | |
1190 internal_stt | |
1191 //typename get_internal_transition_table<Composite, ::boost::mpl::true_ >::type | |
1192 >::type stt_plus_internal; | |
1193 | |
1194 // for every state, add its transition table (if any) | |
1195 // transformed as frow | |
1196 typedef typename ::boost::mpl::fold<state_list,stt_plus_internal, | |
1197 ::boost::mpl::insert_range< | |
1198 ::boost::mpl::placeholders::_1, | |
1199 ::boost::mpl::end< ::boost::mpl::placeholders::_1>, | |
1200 get_internal_transition_table< | |
1201 ::boost::mpl::placeholders::_2, | |
1202 is_composite_state< ::boost::mpl::placeholders::_2> > > | |
1203 >::type type; | |
1204 }; | |
1205 // extend the table with tables from composite states | |
1206 typedef typename extend_table<library_sm>::type complete_table; | |
1207 // build a sequence of regions | |
1208 typedef typename get_regions_as_sequence<typename Derived::initial_state>::type seq_initial_states; | |
1209 // Member functions | |
1210 | |
1211 // start the state machine (calls entry of the initial state) | |
1212 void start() | |
1213 { | |
1214 // reinitialize our list of currently active states with the ones defined in Derived::initial_state | |
1215 ::boost::mpl::for_each< seq_initial_states, ::boost::msm::wrap<mpl::placeholders::_1> > | |
1216 (init_states(m_states)); | |
1217 // call on_entry on this SM | |
1218 (static_cast<Derived*>(this))->on_entry(fsm_initial_event(),*this); | |
1219 ::boost::mpl::for_each<initial_states, boost::msm::wrap<mpl::placeholders::_1> > | |
1220 (call_init<fsm_initial_event>(fsm_initial_event(),this)); | |
1221 // give a chance to handle an anonymous (eventless) transition | |
1222 handle_eventless_transitions_helper<library_sm> eventless_helper(this,true); | |
1223 eventless_helper.process_completion_event(); | |
1224 } | |
1225 | |
1226 // start the state machine (calls entry of the initial state passing incomingEvent to on_entry's) | |
1227 template <class Event> | |
1228 void start(Event const& incomingEvent) | |
1229 { | |
1230 // reinitialize our list of currently active states with the ones defined in Derived::initial_state | |
1231 ::boost::mpl::for_each< seq_initial_states, ::boost::msm::wrap<mpl::placeholders::_1> > | |
1232 (init_states(m_states)); | |
1233 // call on_entry on this SM | |
1234 (static_cast<Derived*>(this))->on_entry(incomingEvent,*this); | |
1235 ::boost::mpl::for_each<initial_states, boost::msm::wrap<mpl::placeholders::_1> > | |
1236 (call_init<Event>(incomingEvent,this)); | |
1237 // give a chance to handle an anonymous (eventless) transition | |
1238 handle_eventless_transitions_helper<library_sm> eventless_helper(this,true); | |
1239 eventless_helper.process_completion_event(); | |
1240 } | |
1241 | |
1242 // stop the state machine (calls exit of the current state) | |
1243 void stop() | |
1244 { | |
1245 do_exit(fsm_final_event(),*this); | |
1246 } | |
1247 | |
1248 // stop the state machine (calls exit of the current state passing finalEvent to on_exit's) | |
1249 template <class Event> | |
1250 void stop(Event const& finalEvent) | |
1251 { | |
1252 do_exit(finalEvent,*this); | |
1253 } | |
1254 | |
1255 // Main function used by clients of the derived FSM to make transitions. | |
1256 template<class Event> | |
1257 execute_return process_event(Event const& evt) | |
1258 { | |
1259 return process_event_internal(evt,true); | |
1260 } | |
1261 | |
1262 template <class EventType> | |
1263 void enqueue_event_helper(EventType const& evt, ::boost::mpl::false_ const &) | |
1264 { | |
1265 execute_return (library_sm::*pf) (EventType const& evt) = | |
1266 &library_sm::process_event; | |
1267 | |
1268 transition_fct f = ::boost::bind(pf,this,evt); | |
1269 m_events_queue.m_events_queue.push_back(f); | |
1270 } | |
1271 template <class EventType> | |
1272 void enqueue_event_helper(EventType const& evt, ::boost::mpl::true_ const &) | |
1273 { | |
1274 // no queue | |
1275 } | |
1276 | |
1277 void execute_queued_events_helper(::boost::mpl::false_ const &) | |
1278 { | |
1279 transition_fct to_call = m_events_queue.m_events_queue.front(); | |
1280 m_events_queue.m_events_queue.pop_front(); | |
1281 to_call(); | |
1282 } | |
1283 void execute_queued_events_helper(::boost::mpl::true_ const &) | |
1284 { | |
1285 // no queue required | |
1286 } | |
1287 | |
1288 // enqueues an event in the message queue | |
1289 // call execute_queued_events to process all queued events. | |
1290 // Be careful if you do this during event processing, the event will be processed immediately | |
1291 // and not kept in the queue | |
1292 template <class EventType> | |
1293 void enqueue_event(EventType const& evt) | |
1294 { | |
1295 enqueue_event_helper<EventType>(evt, typename is_no_message_queue<library_sm>::type()); | |
1296 } | |
1297 | |
1298 // empty the queue and process events | |
1299 void execute_queued_events() | |
1300 { | |
1301 execute_queued_events_helper(typename is_no_message_queue<library_sm>::type()); | |
1302 } | |
1303 | |
1304 typename events_queue_t::size_type get_message_queue_size() const | |
1305 { | |
1306 return m_events_queue.m_events_queue.size(); | |
1307 } | |
1308 | |
1309 events_queue_t& get_message_queue() | |
1310 { | |
1311 return m_events_queue.m_events_queue; | |
1312 } | |
1313 | |
1314 const events_queue_t& get_message_queue() const | |
1315 { | |
1316 return m_events_queue.m_events_queue; | |
1317 } | |
1318 | |
1319 deferred_events_queue_t& get_deferred_queue() | |
1320 { | |
1321 return m_deferred_events_queue.m_deferred_events_queue; | |
1322 } | |
1323 | |
1324 const deferred_events_queue_t& get_deferred_queue() const | |
1325 { | |
1326 return m_deferred_events_queue.m_deferred_events_queue; | |
1327 } | |
1328 | |
1329 // Getter that returns the current state of the FSM | |
1330 const int* current_state() const | |
1331 { | |
1332 return this->m_states; | |
1333 } | |
1334 | |
1335 template <class Archive> | |
1336 struct serialize_state | |
1337 { | |
1338 serialize_state(Archive& ar):ar_(ar){} | |
1339 | |
1340 template<typename T> | |
1341 typename ::boost::enable_if< | |
1342 typename ::boost::mpl::or_< | |
1343 typename has_do_serialize<T>::type, | |
1344 typename is_composite_state<T>::type | |
1345 >::type | |
1346 ,void | |
1347 >::type | |
1348 operator()(T& t) const | |
1349 { | |
1350 ar_ & t; | |
1351 } | |
1352 template<typename T> | |
1353 typename ::boost::disable_if< | |
1354 typename ::boost::mpl::or_< | |
1355 typename has_do_serialize<T>::type, | |
1356 typename is_composite_state<T>::type | |
1357 >::type | |
1358 ,void | |
1359 >::type | |
1360 operator()(T& t) const | |
1361 { | |
1362 // no state to serialize | |
1363 } | |
1364 Archive& ar_; | |
1365 }; | |
1366 | |
1367 template<class Archive> | |
1368 void serialize(Archive & ar, const unsigned int) | |
1369 { | |
1370 // invoke serialization of the base class | |
1371 (serialize_state<Archive>(ar))(boost::serialization::base_object<Derived>(*this)); | |
1372 // now our attributes | |
1373 ar & m_states; | |
1374 // queues cannot be serialized => skip | |
1375 ar & m_history; | |
1376 ar & m_event_processing; | |
1377 ar & m_is_included; | |
1378 // visitors cannot be serialized => skip | |
1379 ::boost::fusion::for_each(m_substate_list, serialize_state<Archive>(ar)); | |
1380 } | |
1381 | |
1382 // linearly search for the state with the given id | |
1383 struct get_state_id_helper | |
1384 { | |
1385 get_state_id_helper(int id,const BaseState** res,const library_sm* self_): | |
1386 result_state(res),searched_id(id),self(self_) {} | |
1387 | |
1388 template <class StateType> | |
1389 void operator()(boost::msm::wrap<StateType> const&) | |
1390 { | |
1391 // look for the state id until found | |
1392 BOOST_STATIC_CONSTANT(int, id = (get_state_id<stt,StateType>::value)); | |
1393 if (!*result_state && (id == searched_id)) | |
1394 { | |
1395 *result_state = &::boost::fusion::at_key<StateType>(self->m_substate_list); | |
1396 } | |
1397 } | |
1398 const BaseState** result_state; | |
1399 int searched_id; | |
1400 const library_sm* self; | |
1401 }; | |
1402 // return the state whose id is passed or 0 if not found | |
1403 // caution if you need this, you probably need polymorphic states | |
1404 // complexity: O(number of states) | |
1405 BaseState* get_state_by_id(int id) | |
1406 { | |
1407 const BaseState* result_state=0; | |
1408 ::boost::mpl::for_each<state_list, | |
1409 ::boost::msm::wrap< ::boost::mpl::placeholders::_1> > (get_state_id_helper(id,&result_state,this)); | |
1410 return const_cast<BaseState*>(result_state); | |
1411 } | |
1412 const BaseState* get_state_by_id(int id) const | |
1413 { | |
1414 const BaseState* result_state=0; | |
1415 ::boost::mpl::for_each<state_list, | |
1416 ::boost::msm::wrap< ::boost::mpl::placeholders::_1> > (get_state_id_helper(id,&result_state,this)); | |
1417 return result_state; | |
1418 } | |
1419 // true if the sm is used in another sm | |
1420 bool is_contained() const | |
1421 { | |
1422 return m_is_included; | |
1423 } | |
1424 // get the history policy class | |
1425 concrete_history& get_history() | |
1426 { | |
1427 return m_history; | |
1428 } | |
1429 concrete_history const& get_history() const | |
1430 { | |
1431 return m_history; | |
1432 } | |
1433 // get a state (const version) | |
1434 // as a pointer | |
1435 template <class State> | |
1436 typename ::boost::enable_if<typename ::boost::is_pointer<State>::type,State >::type | |
1437 get_state(::boost::msm::back::dummy<0> = 0) const | |
1438 { | |
1439 return const_cast<State > | |
1440 (& | |
1441 (::boost::fusion::at_key< | |
1442 typename ::boost::remove_const<typename ::boost::remove_pointer<State>::type>::type>(m_substate_list))); | |
1443 } | |
1444 // as a reference | |
1445 template <class State> | |
1446 typename ::boost::enable_if<typename ::boost::is_reference<State>::type,State >::type | |
1447 get_state(::boost::msm::back::dummy<1> = 0) const | |
1448 { | |
1449 return const_cast<State > | |
1450 ( ::boost::fusion::at_key< | |
1451 typename ::boost::remove_const<typename ::boost::remove_reference<State>::type>::type>(m_substate_list) ); | |
1452 } | |
1453 // get a state (non const version) | |
1454 // as a pointer | |
1455 template <class State> | |
1456 typename ::boost::enable_if<typename ::boost::is_pointer<State>::type,State >::type | |
1457 get_state(::boost::msm::back::dummy<0> = 0) | |
1458 { | |
1459 return &(static_cast<typename boost::add_reference<typename ::boost::remove_pointer<State>::type>::type > | |
1460 (::boost::fusion::at_key<typename ::boost::remove_pointer<State>::type>(m_substate_list))); | |
1461 } | |
1462 // as a reference | |
1463 template <class State> | |
1464 typename ::boost::enable_if<typename ::boost::is_reference<State>::type,State >::type | |
1465 get_state(::boost::msm::back::dummy<1> = 0) | |
1466 { | |
1467 return ::boost::fusion::at_key<typename ::boost::remove_reference<State>::type>(m_substate_list); | |
1468 } | |
1469 // checks if a flag is active using the BinaryOp as folding function | |
1470 template <class Flag,class BinaryOp> | |
1471 bool is_flag_active() const | |
1472 { | |
1473 flag_handler* flags_entries = get_entries_for_flag<Flag>(); | |
1474 bool res = (*flags_entries[ m_states[0] ])(*this); | |
1475 for (int i = 1; i < nr_regions::value ; ++i) | |
1476 { | |
1477 res = typename BinaryOp::type() (res,(*flags_entries[ m_states[i] ])(*this)); | |
1478 } | |
1479 return res; | |
1480 } | |
1481 // checks if a flag is active using no binary op if 1 region, or OR if > 1 regions | |
1482 template <class Flag> | |
1483 bool is_flag_active() const | |
1484 { | |
1485 return FlagHelper<Flag,(nr_regions::value>1)>::helper(*this,get_entries_for_flag<Flag>()); | |
1486 } | |
1487 // visit the currently active states (if these are defined as visitable | |
1488 // by implementing accept) | |
1489 void visit_current_states() | |
1490 { | |
1491 for (int i=0; i<nr_regions::value;++i) | |
1492 { | |
1493 m_visitors.execute(m_states[i]); | |
1494 } | |
1495 } | |
1496 #define MSM_VISIT_STATE_SUB(z, n, unused) ARG ## n vis ## n | |
1497 #define MSM_VISIT_STATE_EXECUTE(z, n, unused) \ | |
1498 template <BOOST_PP_ENUM_PARAMS(n, class ARG)> \ | |
1499 void visit_current_states(BOOST_PP_ENUM(n, MSM_VISIT_STATE_SUB, ~ ) ) \ | |
1500 { \ | |
1501 for (int i=0; i<nr_regions::value;++i) \ | |
1502 { \ | |
1503 m_visitors.execute(m_states[i],BOOST_PP_ENUM_PARAMS(n,vis)); \ | |
1504 } \ | |
1505 } | |
1506 BOOST_PP_REPEAT_FROM_TO(1,BOOST_PP_ADD(BOOST_MSM_VISITOR_ARG_SIZE,1), MSM_VISIT_STATE_EXECUTE, ~) | |
1507 #undef MSM_VISIT_STATE_EXECUTE | |
1508 #undef MSM_VISIT_STATE_SUB | |
1509 | |
1510 // puts the given event into the deferred queue | |
1511 template <class Event> | |
1512 void defer_event(Event const& e) | |
1513 { | |
1514 // to call this function, you need either a state with a deferred_events typedef | |
1515 // or that the fsm provides the activate_deferred_events typedef | |
1516 BOOST_MPL_ASSERT(( has_fsm_deferred_events<library_sm> )); | |
1517 execute_return (library_sm::*pf) (Event const& evt)= &library_sm::process_event; | |
1518 Event temp (e); | |
1519 ::boost::function<execute_return () > f= ::boost::bind(pf, this,temp); | |
1520 post_deferred_event(f); | |
1521 } | |
1522 | |
1523 protected: // interface for the derived class | |
1524 | |
1525 // helper used to fill the initial states | |
1526 struct init_states | |
1527 { | |
1528 init_states(int* const init):m_initial_states(init),m_index(-1){} | |
1529 | |
1530 // History initializer function object, used with mpl::for_each | |
1531 template <class State> | |
1532 void operator()(::boost::msm::wrap<State> const&) | |
1533 { | |
1534 m_initial_states[++m_index]=get_state_id<stt,State>::type::value; | |
1535 } | |
1536 int* const m_initial_states; | |
1537 int m_index; | |
1538 }; | |
1539 public: | |
1540 struct update_state | |
1541 { | |
1542 update_state(substate_list& to_overwrite_):to_overwrite(&to_overwrite_){} | |
1543 template<typename StateType> | |
1544 void operator()(StateType const& astate) const | |
1545 { | |
1546 ::boost::fusion::at_key<StateType>(*to_overwrite)=astate; | |
1547 } | |
1548 substate_list* to_overwrite; | |
1549 }; | |
1550 template <class Expr> | |
1551 void set_states(Expr const& expr) | |
1552 { | |
1553 ::boost::fusion::for_each( | |
1554 ::boost::fusion::as_vector(FoldToList()(expr, boost::fusion::nil())),update_state(this->m_substate_list)); | |
1555 } | |
1556 | |
1557 // Construct with the default initial states | |
1558 state_machine<A0,A1,A2,A3,A4 >() | |
1559 :Derived() | |
1560 ,m_events_queue() | |
1561 ,m_deferred_events_queue() | |
1562 ,m_history() | |
1563 ,m_event_processing(false) | |
1564 ,m_is_included(false) | |
1565 ,m_visitors() | |
1566 ,m_substate_list() | |
1567 { | |
1568 // initialize our list of states with the ones defined in Derived::initial_state | |
1569 ::boost::mpl::for_each< seq_initial_states, ::boost::msm::wrap<mpl::placeholders::_1> > | |
1570 (init_states(m_states)); | |
1571 m_history.set_initial_states(m_states); | |
1572 // create states | |
1573 fill_states(this); | |
1574 } | |
1575 template <class Expr> | |
1576 state_machine<A0,A1,A2,A3,A4 > | |
1577 (Expr const& expr,typename ::boost::enable_if<typename ::boost::proto::is_expr<Expr>::type >::type* =0) | |
1578 :Derived() | |
1579 ,m_events_queue() | |
1580 ,m_deferred_events_queue() | |
1581 ,m_history() | |
1582 ,m_event_processing(false) | |
1583 ,m_is_included(false) | |
1584 ,m_visitors() | |
1585 ,m_substate_list() | |
1586 { | |
1587 BOOST_MPL_ASSERT_MSG( | |
1588 ( ::boost::proto::matches<Expr, FoldToList>::value), | |
1589 THE_STATES_EXPRESSION_PASSED_DOES_NOT_MATCH_GRAMMAR, | |
1590 (FoldToList)); | |
1591 | |
1592 // initialize our list of states with the ones defined in Derived::initial_state | |
1593 ::boost::mpl::for_each< seq_initial_states, ::boost::msm::wrap<mpl::placeholders::_1> > | |
1594 (init_states(m_states)); | |
1595 m_history.set_initial_states(m_states); | |
1596 // create states | |
1597 set_states(expr); | |
1598 fill_states(this); | |
1599 } | |
1600 // Construct with the default initial states and some default argument(s) | |
1601 #define MSM_CONSTRUCTOR_HELPER_EXECUTE_SUB(z, n, unused) ARG ## n t ## n | |
1602 #define MSM_CONSTRUCTOR_HELPER_EXECUTE(z, n, unused) \ | |
1603 template <BOOST_PP_ENUM_PARAMS(n, class ARG)> \ | |
1604 state_machine<A0,A1,A2,A3,A4 \ | |
1605 >(BOOST_PP_ENUM(n, MSM_CONSTRUCTOR_HELPER_EXECUTE_SUB, ~ ), \ | |
1606 typename ::boost::disable_if<typename ::boost::proto::is_expr<ARG0>::type >::type* =0 ) \ | |
1607 :Derived(BOOST_PP_ENUM_PARAMS(n,t)) \ | |
1608 ,m_events_queue() \ | |
1609 ,m_deferred_events_queue() \ | |
1610 ,m_history() \ | |
1611 ,m_event_processing(false) \ | |
1612 ,m_is_included(false) \ | |
1613 ,m_visitors() \ | |
1614 ,m_substate_list() \ | |
1615 { \ | |
1616 ::boost::mpl::for_each< seq_initial_states, ::boost::msm::wrap<mpl::placeholders::_1> > \ | |
1617 (init_states(m_states)); \ | |
1618 m_history.set_initial_states(m_states); \ | |
1619 fill_states(this); \ | |
1620 } \ | |
1621 template <class Expr,BOOST_PP_ENUM_PARAMS(n, class ARG)> \ | |
1622 state_machine<A0,A1,A2,A3,A4 \ | |
1623 >(Expr const& expr,BOOST_PP_ENUM(n, MSM_CONSTRUCTOR_HELPER_EXECUTE_SUB, ~ ), \ | |
1624 typename ::boost::enable_if<typename ::boost::proto::is_expr<Expr>::type >::type* =0 ) \ | |
1625 :Derived(BOOST_PP_ENUM_PARAMS(n,t)) \ | |
1626 ,m_events_queue() \ | |
1627 ,m_deferred_events_queue() \ | |
1628 ,m_history() \ | |
1629 ,m_event_processing(false) \ | |
1630 ,m_is_included(false) \ | |
1631 ,m_visitors() \ | |
1632 ,m_substate_list() \ | |
1633 { \ | |
1634 BOOST_MPL_ASSERT_MSG( \ | |
1635 ( ::boost::proto::matches<Expr, FoldToList>::value), \ | |
1636 THE_STATES_EXPRESSION_PASSED_DOES_NOT_MATCH_GRAMMAR, \ | |
1637 (FoldToList)); \ | |
1638 ::boost::mpl::for_each< seq_initial_states, ::boost::msm::wrap<mpl::placeholders::_1> > \ | |
1639 (init_states(m_states)); \ | |
1640 m_history.set_initial_states(m_states); \ | |
1641 set_states(expr); \ | |
1642 fill_states(this); \ | |
1643 } | |
1644 | |
1645 BOOST_PP_REPEAT_FROM_TO(1,BOOST_PP_ADD(BOOST_MSM_CONSTRUCTOR_ARG_SIZE,1), MSM_CONSTRUCTOR_HELPER_EXECUTE, ~) | |
1646 #undef MSM_CONSTRUCTOR_HELPER_EXECUTE | |
1647 #undef MSM_CONSTRUCTOR_HELPER_EXECUTE_SUB | |
1648 | |
1649 | |
1650 | |
1651 // assignment operator using the copy policy to decide if non_copyable, shallow or deep copying is necessary | |
1652 library_sm& operator= (library_sm const& rhs) | |
1653 { | |
1654 if (this != &rhs) | |
1655 { | |
1656 Derived::operator=(rhs); | |
1657 do_copy(rhs); | |
1658 } | |
1659 return *this; | |
1660 } | |
1661 state_machine<A0,A1,A2,A3,A4> | |
1662 (library_sm const& rhs) | |
1663 : Derived(rhs) | |
1664 { | |
1665 if (this != &rhs) | |
1666 { | |
1667 // initialize our list of states with the ones defined in Derived::initial_state | |
1668 fill_states(this); | |
1669 do_copy(rhs); | |
1670 } | |
1671 } | |
1672 | |
1673 // the following 2 functions handle the terminate/interrupt states handling | |
1674 // if one of these states is found, the first one is used | |
1675 template <class Event> | |
1676 bool is_event_handling_blocked_helper( ::boost::mpl::true_ const &) | |
1677 { | |
1678 // if the state machine is terminated, do not handle any event | |
1679 if (is_flag_active< ::boost::msm::TerminateFlag>()) | |
1680 return true; | |
1681 // if the state machine is interrupted, do not handle any event | |
1682 // unless the event is the end interrupt event | |
1683 if ( is_flag_active< ::boost::msm::InterruptedFlag>() && | |
1684 !is_flag_active< ::boost::msm::EndInterruptFlag<Event> >()) | |
1685 return true; | |
1686 return false; | |
1687 } | |
1688 // otherwise simple handling, no flag => continue | |
1689 template <class Event> | |
1690 bool is_event_handling_blocked_helper( ::boost::mpl::false_ const &) | |
1691 { | |
1692 // no terminate/interrupt states detected | |
1693 return false; | |
1694 } | |
1695 // the following functions handle pre/post-process handling of a message queue | |
1696 template <class StateType,class EventType> | |
1697 bool do_pre_msg_queue_helper(EventType const& evt, ::boost::mpl::true_ const &) | |
1698 { | |
1699 // no message queue needed | |
1700 return true; | |
1701 } | |
1702 template <class StateType,class EventType> | |
1703 bool do_pre_msg_queue_helper(EventType const& evt, ::boost::mpl::false_ const &) | |
1704 { | |
1705 execute_return (library_sm::*pf) (EventType const& evt) = | |
1706 &library_sm::process_event; | |
1707 // if we are already processing an event | |
1708 if (m_event_processing) | |
1709 { | |
1710 // event has to be put into the queue | |
1711 transition_fct f = ::boost::bind(pf,this,evt); | |
1712 m_events_queue.m_events_queue.push_back(f); | |
1713 return false; | |
1714 } | |
1715 // event can be handled, processing | |
1716 m_event_processing = true; | |
1717 return true; | |
1718 } | |
1719 void do_post_msg_queue_helper( ::boost::mpl::true_ const &) | |
1720 { | |
1721 // no message queue needed | |
1722 } | |
1723 void do_post_msg_queue_helper( ::boost::mpl::false_ const &) | |
1724 { | |
1725 m_event_processing = false; | |
1726 process_message_queue(this); | |
1727 } | |
1728 // the following 2 functions handle the processing either with a try/catch protection or without | |
1729 template <class StateType,class EventType> | |
1730 HandledEnum do_process_helper(EventType const& evt, ::boost::mpl::true_ const &, bool is_direct_call) | |
1731 { | |
1732 return this->do_process_event(evt,is_direct_call); | |
1733 } | |
1734 template <class StateType,class EventType> | |
1735 HandledEnum do_process_helper(EventType const& evt, ::boost::mpl::false_ const &, bool is_direct_call) | |
1736 { | |
1737 // when compiling without exception support there is no formal parameter "e" in the catch handler. | |
1738 // Declaring a local variable here does not hurt and will be "used" to make the code in the handler | |
1739 // compilable although the code will never be executed. | |
1740 std::exception e; | |
1741 BOOST_TRY | |
1742 { | |
1743 return this->do_process_event(evt,is_direct_call); | |
1744 } | |
1745 BOOST_CATCH (std::exception& e) | |
1746 { | |
1747 // give a chance to the concrete state machine to handle | |
1748 this->exception_caught(evt,*this,e); | |
1749 } | |
1750 BOOST_CATCH_END | |
1751 return HANDLED_FALSE; | |
1752 } | |
1753 // handling of deferred events | |
1754 // if none is found in the SM, take the following empty main version | |
1755 template <class StateType, class Enable = int> | |
1756 struct handle_defer_helper | |
1757 { | |
1758 handle_defer_helper(deferred_msg_queue_helper<library_sm>& ){} | |
1759 void do_pre_handle_deferred() | |
1760 { | |
1761 } | |
1762 | |
1763 void do_post_handle_deferred(HandledEnum) | |
1764 { | |
1765 } | |
1766 }; | |
1767 // otherwise the standard version handling the deferred events | |
1768 template <class StateType> | |
1769 struct handle_defer_helper | |
1770 <StateType, typename enable_if< typename ::boost::msm::back::has_fsm_deferred_events<StateType>::type,int >::type> | |
1771 { | |
1772 handle_defer_helper(deferred_msg_queue_helper<library_sm>& a_queue): | |
1773 events_queue(a_queue),next_deferred_event(){} | |
1774 void do_pre_handle_deferred() | |
1775 { | |
1776 } | |
1777 | |
1778 void do_post_handle_deferred(HandledEnum handled) | |
1779 { | |
1780 if (handled == HANDLED_TRUE) | |
1781 { | |
1782 // a transition has been taken, it makes sense again to try processing waiting deferred events | |
1783 // reset all events to not tested | |
1784 for (std::size_t i = 0; i < events_queue.m_deferred_events_queue.size(); ++i) | |
1785 { | |
1786 events_queue.m_deferred_events_queue[i].second=false; | |
1787 } | |
1788 // test first event | |
1789 if (!events_queue.m_deferred_events_queue.empty()) | |
1790 { | |
1791 deferred_fct next = events_queue.m_deferred_events_queue.front().first; | |
1792 events_queue.m_deferred_events_queue.pop_front(); | |
1793 next(); | |
1794 } | |
1795 } | |
1796 else | |
1797 { | |
1798 // look for next deferred event, if any | |
1799 typename deferred_events_queue_t::iterator it = | |
1800 std::find_if(events_queue.m_deferred_events_queue.begin(), | |
1801 events_queue.m_deferred_events_queue.end(), | |
1802 boost::bind(&std::pair<deferred_fct,bool>::second, _1) == false); | |
1803 if (it != events_queue.m_deferred_events_queue.end()) | |
1804 { | |
1805 (*it).second = true; | |
1806 deferred_fct next = (*it).first; | |
1807 events_queue.m_deferred_events_queue.erase(it); | |
1808 next(); | |
1809 } | |
1810 } | |
1811 } | |
1812 | |
1813 private: | |
1814 deferred_msg_queue_helper<library_sm>& events_queue; | |
1815 deferred_fct next_deferred_event; | |
1816 }; | |
1817 | |
1818 // handling of eventless transitions | |
1819 // if none is found in the SM, nothing to do | |
1820 template <class StateType, class Enable = void> | |
1821 struct handle_eventless_transitions_helper | |
1822 { | |
1823 handle_eventless_transitions_helper(library_sm* , bool ){} | |
1824 void process_completion_event(){} | |
1825 }; | |
1826 // otherwise | |
1827 template <class StateType> | |
1828 struct handle_eventless_transitions_helper | |
1829 <StateType, typename enable_if< typename ::boost::msm::back::has_fsm_eventless_transition<StateType>::type >::type> | |
1830 { | |
1831 handle_eventless_transitions_helper(library_sm* self_, bool handled_):self(self_),handled(handled_){} | |
1832 void process_completion_event() | |
1833 { | |
1834 typedef typename ::boost::mpl::deref< | |
1835 typename ::boost::mpl::begin< | |
1836 typename find_completion_events<StateType>::type | |
1837 >::type | |
1838 >::type first_completion_event; | |
1839 if (handled) | |
1840 { | |
1841 self->process_event(first_completion_event() ); | |
1842 } | |
1843 } | |
1844 | |
1845 private: | |
1846 library_sm* self; | |
1847 bool handled; | |
1848 }; | |
1849 | |
1850 // helper class called in case the event to process has been found in the fsm's internal stt and is therefore processable | |
1851 template<class Event> | |
1852 struct process_fsm_internal_table | |
1853 { | |
1854 typedef typename ::boost::mpl::has_key<processable_events_internal_table,Event>::type is_event_processable; | |
1855 | |
1856 // forward to the correct do_process | |
1857 static void process(Event const& evt,library_sm* self_,HandledEnum& result) | |
1858 { | |
1859 do_process(evt,self_,result,is_event_processable()); | |
1860 } | |
1861 private: | |
1862 // the event is processable, let's try! | |
1863 static void do_process(Event const& evt,library_sm* self_,HandledEnum& result, ::boost::mpl::true_) | |
1864 { | |
1865 if (result != HANDLED_TRUE) | |
1866 { | |
1867 typedef dispatch_table<library_sm,complete_table,Event,CompilePolicy> table; | |
1868 HandledEnum res_internal = table::instance.entries[0](*self_, 0, self_->m_states[0], evt); | |
1869 result = (HandledEnum)((int)result | (int)res_internal); | |
1870 } | |
1871 } | |
1872 // version doing nothing if the event is not in the internal stt and we can save ourselves the time trying to process | |
1873 static void do_process(Event const& ,library_sm* ,HandledEnum& , ::boost::mpl::false_) | |
1874 { | |
1875 // do nothing | |
1876 } | |
1877 }; | |
1878 | |
1879 template <class StateType,class Enable=void> | |
1880 struct region_processing_helper | |
1881 { | |
1882 public: | |
1883 region_processing_helper(library_sm* self_,HandledEnum& result_) | |
1884 :self(self_),result(result_){} | |
1885 template<class Event> | |
1886 void process(Event const& evt) | |
1887 { | |
1888 // use this table as if it came directly from the user | |
1889 typedef dispatch_table<library_sm,complete_table,Event,CompilePolicy> table; | |
1890 // +1 because index 0 is reserved for this fsm | |
1891 HandledEnum res = | |
1892 table::instance.entries[self->m_states[0]+1]( | |
1893 *self, 0, self->m_states[0], evt); | |
1894 result = (HandledEnum)((int)result | (int)res); | |
1895 // process the event in the internal table of this fsm if the event is processable (present in the table) | |
1896 process_fsm_internal_table<Event>::process(evt,self,result); | |
1897 } | |
1898 library_sm* self; | |
1899 HandledEnum& result; | |
1900 }; | |
1901 // version with visitors | |
1902 template <class StateType> | |
1903 struct region_processing_helper<StateType,typename ::boost::enable_if< | |
1904 ::boost::mpl::is_sequence<typename StateType::initial_state> >::type> | |
1905 { | |
1906 private: | |
1907 // process event in one region | |
1908 template <class region_id,int Dummy=0> | |
1909 struct In | |
1910 { | |
1911 template<class Event> | |
1912 static void process(Event const& evt,library_sm* self_,HandledEnum& result_) | |
1913 { | |
1914 // use this table as if it came directly from the user | |
1915 typedef dispatch_table<library_sm,complete_table,Event,CompilePolicy> table; | |
1916 // +1 because index 0 is reserved for this fsm | |
1917 HandledEnum res = | |
1918 table::instance.entries[self_->m_states[region_id::value]+1]( | |
1919 *self_, region_id::value , self_->m_states[region_id::value], evt); | |
1920 result_ = (HandledEnum)((int)result_ | (int)res); | |
1921 In< ::boost::mpl::int_<region_id::value+1> >::process(evt,self_,result_); | |
1922 } | |
1923 }; | |
1924 template <int Dummy> | |
1925 struct In< ::boost::mpl::int_<nr_regions::value>,Dummy> | |
1926 { | |
1927 // end of processing | |
1928 template<class Event> | |
1929 static void process(Event const& evt,library_sm* self_,HandledEnum& result_) | |
1930 { | |
1931 // process the event in the internal table of this fsm if the event is processable (present in the table) | |
1932 process_fsm_internal_table<Event>::process(evt,self_,result_); | |
1933 } | |
1934 }; | |
1935 public: | |
1936 region_processing_helper(library_sm* self_,HandledEnum& result_) | |
1937 :self(self_),result(result_){} | |
1938 template<class Event> | |
1939 void process(Event const& evt) | |
1940 { | |
1941 In< ::boost::mpl::int_<0> >::process(evt,self,result); | |
1942 } | |
1943 | |
1944 library_sm* self; | |
1945 HandledEnum& result; | |
1946 }; | |
1947 | |
1948 // Main function used internally to make transitions | |
1949 // Can only be called for internally (for example in an action method) generated events. | |
1950 template<class Event> | |
1951 execute_return process_event_internal(Event const& evt, bool is_direct_call) | |
1952 { | |
1953 HandledEnum ret_handled=HANDLED_FALSE; | |
1954 // if the state machine has terminate or interrupt flags, check them, otherwise skip | |
1955 if (is_event_handling_blocked_helper<Event> | |
1956 ( ::boost::mpl::bool_<has_fsm_blocking_states<library_sm>::type::value>() ) ) | |
1957 return HANDLED_TRUE; | |
1958 // if a message queue is needed and processing is on the way | |
1959 if (!do_pre_msg_queue_helper<Event> | |
1960 (evt,::boost::mpl::bool_<is_no_message_queue<library_sm>::type::value>()) ) | |
1961 { | |
1962 // wait for the end of current processing | |
1963 return HANDLED_TRUE; | |
1964 } | |
1965 else | |
1966 { | |
1967 // prepare the next deferred event for handling | |
1968 // if one defer is found in the SM, otherwise skip | |
1969 handle_defer_helper<library_sm> defer_helper(m_deferred_events_queue); | |
1970 defer_helper.do_pre_handle_deferred(); | |
1971 // process event | |
1972 HandledEnum handled = this->do_process_helper<Event> | |
1973 (evt,::boost::mpl::bool_<is_no_exception_thrown<library_sm>::type::value>(),is_direct_call); | |
1974 if (handled) | |
1975 { | |
1976 ret_handled = handled; | |
1977 } | |
1978 | |
1979 // process completion transitions BEFORE any other event in the pool (UML Standard 2.3 15.3.14) | |
1980 handle_eventless_transitions_helper<library_sm> eventless_helper(this,(handled == HANDLED_TRUE)); | |
1981 eventless_helper.process_completion_event(); | |
1982 | |
1983 // after handling, take care of the deferred events | |
1984 defer_helper.do_post_handle_deferred(handled); | |
1985 | |
1986 // now check if some events were generated in a transition and was not handled | |
1987 // because of another processing, and if yes, start handling them | |
1988 do_post_msg_queue_helper(::boost::mpl::bool_<is_no_message_queue<library_sm>::type::value>()); | |
1989 | |
1990 return ret_handled; | |
1991 } | |
1992 } | |
1993 | |
1994 // minimum event processing without exceptions, queues, etc. | |
1995 template<class Event> | |
1996 HandledEnum do_process_event(Event const& evt, bool is_direct_call) | |
1997 { | |
1998 HandledEnum handled = HANDLED_FALSE; | |
1999 // dispatch the event to every region | |
2000 region_processing_helper<Derived> helper(this,handled); | |
2001 helper.process(evt); | |
2002 | |
2003 // if the event has not been handled and we have orthogonal zones, then | |
2004 // generate an error on every active state | |
2005 // for state machine states contained in other state machines, do not handle | |
2006 // but let the containing sm handle the error, unless the event was generated in this fsm | |
2007 // (by calling process_event on this fsm object, is_direct_call == true) | |
2008 // completion events do not produce an error | |
2009 if ( (!is_contained() || is_direct_call) && !handled && !is_completion_event<Event>::type::value) | |
2010 { | |
2011 for (int i=0; i<nr_regions::value;++i) | |
2012 { | |
2013 this->no_transition(evt,*this,this->m_states[i]); | |
2014 } | |
2015 } | |
2016 return handled; | |
2017 } | |
2018 | |
2019 // default row arguments for the compilers which accept this | |
2020 template <class Event> | |
2021 bool no_guard(Event const&){return true;} | |
2022 template <class Event> | |
2023 void no_action(Event const&){} | |
2024 | |
2025 #ifndef BOOST_NO_RTTI | |
2026 HandledEnum process_any_event( ::boost::any const& evt); | |
2027 #endif | |
2028 | |
2029 private: | |
2030 // composite accept implementation. First calls accept on the composite, then accept on all its active states. | |
2031 void composite_accept() | |
2032 { | |
2033 this->accept(); | |
2034 this->visit_current_states(); | |
2035 } | |
2036 | |
2037 #define MSM_COMPOSITE_ACCEPT_SUB(z, n, unused) ARG ## n vis ## n | |
2038 #define MSM_COMPOSITE_ACCEPT_SUB2(z, n, unused) boost::ref( vis ## n ) | |
2039 #define MSM_COMPOSITE_ACCEPT_EXECUTE(z, n, unused) \ | |
2040 template <BOOST_PP_ENUM_PARAMS(n, class ARG)> \ | |
2041 void composite_accept(BOOST_PP_ENUM(n, MSM_COMPOSITE_ACCEPT_SUB, ~ ) ) \ | |
2042 { \ | |
2043 this->accept(BOOST_PP_ENUM_PARAMS(n,vis)); \ | |
2044 this->visit_current_states(BOOST_PP_ENUM(n,MSM_COMPOSITE_ACCEPT_SUB2, ~)); \ | |
2045 } | |
2046 BOOST_PP_REPEAT_FROM_TO(1,BOOST_PP_ADD(BOOST_MSM_VISITOR_ARG_SIZE,1), MSM_COMPOSITE_ACCEPT_EXECUTE, ~) | |
2047 #undef MSM_COMPOSITE_ACCEPT_EXECUTE | |
2048 #undef MSM_COMPOSITE_ACCEPT_SUB | |
2049 #undef MSM_COMPOSITE_ACCEPT_SUB2 | |
2050 | |
2051 // helper used to call the init states at the start of the state machine | |
2052 template <class Event> | |
2053 struct call_init | |
2054 { | |
2055 call_init(Event const& an_event,library_sm* self_): | |
2056 evt(an_event),self(self_){} | |
2057 template <class State> | |
2058 void operator()(boost::msm::wrap<State> const&) | |
2059 { | |
2060 execute_entry(::boost::fusion::at_key<State>(self->m_substate_list),evt,*self); | |
2061 } | |
2062 private: | |
2063 Event const& evt; | |
2064 library_sm* self; | |
2065 }; | |
2066 // helper for flag handling. Uses OR by default on orthogonal zones. | |
2067 template <class Flag,bool orthogonalStates> | |
2068 struct FlagHelper | |
2069 { | |
2070 static bool helper(library_sm const& sm,flag_handler* ) | |
2071 { | |
2072 // by default we use OR to accumulate the flags | |
2073 return sm.is_flag_active<Flag,Flag_OR>(); | |
2074 } | |
2075 }; | |
2076 template <class Flag> | |
2077 struct FlagHelper<Flag,false> | |
2078 { | |
2079 static bool helper(library_sm const& sm,flag_handler* flags_entries) | |
2080 { | |
2081 // just one active state, so we can call operator[] with 0 | |
2082 return flags_entries[sm.current_state()[0]](sm); | |
2083 } | |
2084 }; | |
2085 // handling of flag | |
2086 // defines a true and false functions plus a forwarding one for composite states | |
2087 template <class StateType,class Flag> | |
2088 struct FlagHandler | |
2089 { | |
2090 static bool flag_true(library_sm const& ) | |
2091 { | |
2092 return true; | |
2093 } | |
2094 static bool flag_false(library_sm const& ) | |
2095 { | |
2096 return false; | |
2097 } | |
2098 static bool forward(library_sm const& fsm) | |
2099 { | |
2100 return ::boost::fusion::at_key<StateType>(fsm.m_substate_list).template is_flag_active<Flag>(); | |
2101 } | |
2102 }; | |
2103 template <class Flag> | |
2104 struct init_flags | |
2105 { | |
2106 private: | |
2107 // helper function, helps hiding the forward function for non-state machines states. | |
2108 template <class T> | |
2109 void helper (flag_handler* an_entry,int offset, ::boost::mpl::true_ const & ) | |
2110 { | |
2111 // composite => forward | |
2112 an_entry[offset] = &FlagHandler<T,Flag>::forward; | |
2113 } | |
2114 template <class T> | |
2115 void helper (flag_handler* an_entry,int offset, ::boost::mpl::false_ const & ) | |
2116 { | |
2117 // default no flag | |
2118 an_entry[offset] = &FlagHandler<T,Flag>::flag_false; | |
2119 } | |
2120 // attributes | |
2121 flag_handler* entries; | |
2122 | |
2123 public: | |
2124 init_flags(flag_handler* entries_) | |
2125 : entries(entries_) | |
2126 {} | |
2127 | |
2128 // Flags initializer function object, used with mpl::for_each | |
2129 template <class StateType> | |
2130 void operator()( ::boost::msm::wrap<StateType> const& ) | |
2131 { | |
2132 typedef typename get_flag_list<StateType>::type flags; | |
2133 typedef typename ::boost::mpl::contains<flags,Flag >::type found; | |
2134 typedef typename is_composite_state<StateType>::type composite; | |
2135 | |
2136 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,StateType>::type::value)); | |
2137 if (found::type::value) | |
2138 { | |
2139 // the type defined the flag => true | |
2140 entries[state_id] = &FlagHandler<StateType,Flag>::flag_true; | |
2141 } | |
2142 else | |
2143 { | |
2144 // false or forward | |
2145 typedef typename ::boost::mpl::and_< | |
2146 typename is_composite_state<StateType>::type, | |
2147 typename ::boost::mpl::not_< | |
2148 typename has_non_forwarding_flag<Flag>::type>::type >::type composite_no_forward; | |
2149 | |
2150 helper<StateType>(entries,state_id,::boost::mpl::bool_<composite_no_forward::type::value>()); | |
2151 } | |
2152 } | |
2153 }; | |
2154 // maintains for every flag a static array containing the flag value for every state | |
2155 template <class Flag> | |
2156 flag_handler* get_entries_for_flag() const | |
2157 { | |
2158 BOOST_STATIC_CONSTANT(int, max_state = (mpl::size<state_list>::value)); | |
2159 | |
2160 static flag_handler flags_entries[max_state]; | |
2161 // build a state list | |
2162 ::boost::mpl::for_each<state_list, boost::msm::wrap< ::boost::mpl::placeholders::_1> > | |
2163 (init_flags<Flag>(flags_entries)); | |
2164 return flags_entries; | |
2165 } | |
2166 | |
2167 // helper used to create a state using the correct constructor | |
2168 template <class State, class Enable=void> | |
2169 struct create_state_helper | |
2170 { | |
2171 static void set_sm(library_sm* ) | |
2172 { | |
2173 // state doesn't need its sm | |
2174 } | |
2175 }; | |
2176 // create a state requiring a pointer to the state machine | |
2177 template <class State> | |
2178 struct create_state_helper<State,typename boost::enable_if<typename State::needs_sm >::type> | |
2179 { | |
2180 static void set_sm(library_sm* sm) | |
2181 { | |
2182 // create and set the fsm | |
2183 ::boost::fusion::at_key<State>(sm->m_substate_list).set_sm_ptr(sm); | |
2184 } | |
2185 }; | |
2186 // main unspecialized helper class | |
2187 template <class StateType,int ARGS> | |
2188 struct visitor_args; | |
2189 | |
2190 #define MSM_VISITOR_ARGS_SUB(z, n, unused) BOOST_PP_CAT(_,BOOST_PP_ADD(n,1)) | |
2191 #define MSM_VISITOR_ARGS_TYPEDEF_SUB(z, n, unused) typename StateType::accept_sig::argument ## n | |
2192 | |
2193 #define MSM_VISITOR_ARGS_EXECUTE(z, n, unused) \ | |
2194 template <class StateType> \ | |
2195 struct visitor_args<StateType,n> \ | |
2196 { \ | |
2197 template <class State> \ | |
2198 static typename enable_if_c<!is_composite_state<State>::value,void >::type \ | |
2199 helper (library_sm* sm, \ | |
2200 int id,StateType& astate) \ | |
2201 { \ | |
2202 sm->m_visitors.insert(id, boost::bind(&StateType::accept, \ | |
2203 ::boost::ref(astate) BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, MSM_VISITOR_ARGS_SUB, ~) )); \ | |
2204 } \ | |
2205 template <class State> \ | |
2206 static typename enable_if_c<is_composite_state<State>::value,void >::type \ | |
2207 helper (library_sm* sm, \ | |
2208 int id,StateType& astate) \ | |
2209 { \ | |
2210 void (StateType::*caccept)(BOOST_PP_ENUM(n, MSM_VISITOR_ARGS_TYPEDEF_SUB, ~ ) ) \ | |
2211 = &StateType::composite_accept; \ | |
2212 sm->m_visitors.insert(id, boost::bind(caccept, \ | |
2213 ::boost::ref(astate) BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, MSM_VISITOR_ARGS_SUB, ~) )); \ | |
2214 } \ | |
2215 }; | |
2216 BOOST_PP_REPEAT(BOOST_PP_ADD(BOOST_MSM_VISITOR_ARG_SIZE,1), MSM_VISITOR_ARGS_EXECUTE, ~) | |
2217 #undef MSM_VISITOR_ARGS_EXECUTE | |
2218 #undef MSM_VISITOR_ARGS_SUB | |
2219 | |
2220 // the IBM compiler seems to have problems with nested classes | |
2221 // the same seems to apply to the Apple version of gcc 4.0.1 (just in case we do for < 4.1) | |
2222 // and also to MS VC < 8 | |
2223 #if defined (__IBMCPP__) || (__GNUC__ == 4 && __GNUC_MINOR__ < 1) || (defined(_MSC_VER) && (_MSC_VER < 1400)) | |
2224 public: | |
2225 #endif | |
2226 template<class ContainingSM> | |
2227 void set_containing_sm(ContainingSM* sm) | |
2228 { | |
2229 m_is_included=true; | |
2230 ::boost::fusion::for_each(m_substate_list,add_state<ContainingSM>(this,sm)); | |
2231 } | |
2232 #if defined (__IBMCPP__) || (__GNUC__ == 4 && __GNUC_MINOR__ < 1) || (defined(_MSC_VER) && (_MSC_VER < 1400)) | |
2233 private: | |
2234 #endif | |
2235 // A function object for use with mpl::for_each that stuffs | |
2236 // states into the state list. | |
2237 template<class ContainingSM> | |
2238 struct add_state | |
2239 { | |
2240 add_state(library_sm* self_,ContainingSM* sm) | |
2241 : self(self_),containing_sm(sm){} | |
2242 | |
2243 // State is a sub fsm with exit pseudo states and gets a pointer to this fsm, so it can build a callback | |
2244 template <class StateType> | |
2245 typename ::boost::enable_if< | |
2246 typename is_composite_state<StateType>::type,void >::type | |
2247 new_state_helper(boost::msm::back::dummy<0> = 0) const | |
2248 { | |
2249 ::boost::fusion::at_key<StateType>(self->m_substate_list).set_containing_sm(containing_sm); | |
2250 } | |
2251 // State is a sub fsm without exit pseudo states and does not get a callback to this fsm | |
2252 // or state is a normal state and needs nothing except creation | |
2253 template <class StateType> | |
2254 typename ::boost::enable_if< | |
2255 typename boost::mpl::and_<typename boost::mpl::not_ | |
2256 <typename is_composite_state<StateType>::type>::type, | |
2257 typename boost::mpl::not_ | |
2258 <typename is_pseudo_exit<StateType>::type>::type | |
2259 >::type,void>::type | |
2260 new_state_helper( ::boost::msm::back::dummy<1> = 0) const | |
2261 { | |
2262 //nothing to do | |
2263 } | |
2264 // state is exit pseudo state and gets callback to target fsm | |
2265 template <class StateType> | |
2266 typename ::boost::enable_if<typename is_pseudo_exit<StateType>::type,void >::type | |
2267 new_state_helper( ::boost::msm::back::dummy<2> = 0) const | |
2268 { | |
2269 execute_return (ContainingSM::*pf) (typename StateType::event const& evt)= | |
2270 &ContainingSM::process_event; | |
2271 ::boost::function<execute_return (typename StateType::event const&)> fct = | |
2272 ::boost::bind(pf,containing_sm,_1); | |
2273 ::boost::fusion::at_key<StateType>(self->m_substate_list).set_forward_fct(fct); | |
2274 } | |
2275 // for every defined state in the sm | |
2276 template <class State> | |
2277 void operator()( State const&) const | |
2278 { | |
2279 //create a new state with the defined id and type | |
2280 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value)); | |
2281 | |
2282 this->new_state_helper<State>(), | |
2283 create_state_helper<State>::set_sm(self); | |
2284 // create a visitor callback | |
2285 visitor_helper(state_id,::boost::fusion::at_key<State>(self->m_substate_list), | |
2286 ::boost::mpl::bool_<has_accept_sig<State>::type::value>()); | |
2287 } | |
2288 private: | |
2289 // support possible use of a visitor if accept_sig is defined | |
2290 template <class StateType> | |
2291 void visitor_helper(int id,StateType& astate, ::boost::mpl::true_ const & ) const | |
2292 { | |
2293 visitor_args<StateType,StateType::accept_sig::args_number>:: | |
2294 template helper<StateType>(self,id,astate); | |
2295 } | |
2296 template <class StateType> | |
2297 void visitor_helper(int ,StateType& , ::boost::mpl::false_ const &) const | |
2298 { | |
2299 // nothing to do | |
2300 } | |
2301 | |
2302 library_sm* self; | |
2303 ContainingSM* containing_sm; | |
2304 }; | |
2305 | |
2306 // helper used to copy every state if needed | |
2307 struct copy_helper | |
2308 { | |
2309 copy_helper(library_sm* sm): | |
2310 m_sm(sm){} | |
2311 template <class StateType> | |
2312 void operator()( ::boost::msm::wrap<StateType> const& ) | |
2313 { | |
2314 BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,StateType>::type::value)); | |
2315 // possibly also set the visitor | |
2316 visitor_helper<StateType>(state_id); | |
2317 | |
2318 // and for states that keep a pointer to the fsm, reset the pointer | |
2319 create_state_helper<StateType>::set_sm(m_sm); | |
2320 } | |
2321 template <class StateType> | |
2322 typename ::boost::enable_if<typename has_accept_sig<StateType>::type,void >::type | |
2323 visitor_helper(int id) const | |
2324 { | |
2325 visitor_args<StateType,StateType::accept_sig::args_number>::template helper<StateType> | |
2326 (m_sm,id,::boost::fusion::at_key<StateType>(m_sm->m_substate_list)); | |
2327 } | |
2328 template <class StateType> | |
2329 typename ::boost::disable_if<typename has_accept_sig<StateType>::type,void >::type | |
2330 visitor_helper(int) const | |
2331 { | |
2332 // nothing to do | |
2333 } | |
2334 | |
2335 library_sm* m_sm; | |
2336 }; | |
2337 // helper to copy the active states attribute | |
2338 template <class region_id,int Dummy=0> | |
2339 struct region_copy_helper | |
2340 { | |
2341 static void do_copy(library_sm* self_,library_sm const& rhs) | |
2342 { | |
2343 self_->m_states[region_id::value] = rhs.m_states[region_id::value]; | |
2344 region_copy_helper< ::boost::mpl::int_<region_id::value+1> >::do_copy(self_,rhs); | |
2345 } | |
2346 }; | |
2347 template <int Dummy> | |
2348 struct region_copy_helper< ::boost::mpl::int_<nr_regions::value>,Dummy> | |
2349 { | |
2350 // end of processing | |
2351 static void do_copy(library_sm*,library_sm const& ){} | |
2352 }; | |
2353 // copy functions for deep copy (no need of a 2nd version for NoCopy as noncopyable handles it) | |
2354 void do_copy (library_sm const& rhs, | |
2355 ::boost::msm::back::dummy<0> = 0) | |
2356 { | |
2357 // deep copy simply assigns the data | |
2358 region_copy_helper< ::boost::mpl::int_<0> >::do_copy(this,rhs); | |
2359 m_events_queue = rhs.m_events_queue; | |
2360 m_deferred_events_queue = rhs.m_deferred_events_queue; | |
2361 m_history = rhs.m_history; | |
2362 m_event_processing = rhs.m_event_processing; | |
2363 m_is_included = rhs.m_is_included; | |
2364 m_substate_list = rhs.m_substate_list; | |
2365 // except for the states themselves, which get duplicated | |
2366 | |
2367 ::boost::mpl::for_each<state_list, ::boost::msm::wrap< ::boost::mpl::placeholders::_1> > | |
2368 (copy_helper(this)); | |
2369 } | |
2370 | |
2371 // helper used to call the correct entry/exit method | |
2372 // unfortunately in O(number of states in the sub-sm) but should be better than a virtual call | |
2373 template<class Event,bool is_entry> | |
2374 struct entry_exit_helper | |
2375 { | |
2376 entry_exit_helper(int id,Event const& e,library_sm* self_): | |
2377 state_id(id),evt(e),self(self_){} | |
2378 // helper for entry actions | |
2379 template <class IsEntry,class State> | |
2380 typename ::boost::enable_if<typename IsEntry::type,void >::type | |
2381 helper( ::boost::msm::back::dummy<0> = 0) | |
2382 { | |
2383 BOOST_STATIC_CONSTANT(int, id = (get_state_id<stt,State>::value)); | |
2384 if (id == state_id) | |
2385 { | |
2386 execute_entry<State>(::boost::fusion::at_key<State>(self->m_substate_list),evt,*self); | |
2387 } | |
2388 } | |
2389 // helper for exit actions | |
2390 template <class IsEntry,class State> | |
2391 typename boost::disable_if<typename IsEntry::type,void >::type | |
2392 helper( ::boost::msm::back::dummy<1> = 0) | |
2393 { | |
2394 BOOST_STATIC_CONSTANT(int, id = (get_state_id<stt,State>::value)); | |
2395 if (id == state_id) | |
2396 { | |
2397 execute_exit<State>(::boost::fusion::at_key<State>(self->m_substate_list),evt,*self); | |
2398 } | |
2399 } | |
2400 // iterates through all states to find the one to be activated | |
2401 template <class State> | |
2402 void operator()( ::boost::msm::wrap<State> const&) | |
2403 { | |
2404 entry_exit_helper<Event,is_entry>::template helper< ::boost::mpl::bool_<is_entry>,State >(); | |
2405 } | |
2406 private: | |
2407 int state_id; | |
2408 Event const& evt; | |
2409 library_sm* self; | |
2410 }; | |
2411 | |
2412 // helper to start the fsm | |
2413 template <class region_id,int Dummy=0> | |
2414 struct region_start_helper | |
2415 { | |
2416 template<class Event> | |
2417 static void do_start(library_sm* self_,Event const& incomingEvent) | |
2418 { | |
2419 //forward the event for handling by sub state machines | |
2420 ::boost::mpl::for_each<state_list, ::boost::msm::wrap< ::boost::mpl::placeholders::_1> > | |
2421 (entry_exit_helper<Event,true>(self_->m_states[region_id::value],incomingEvent,self_)); | |
2422 region_start_helper | |
2423 < ::boost::mpl::int_<region_id::value+1> >::do_start(self_,incomingEvent); | |
2424 } | |
2425 }; | |
2426 template <int Dummy> | |
2427 struct region_start_helper< ::boost::mpl::int_<nr_regions::value>,Dummy> | |
2428 { | |
2429 // end of processing | |
2430 template<class Event> | |
2431 static void do_start(library_sm*,Event const& ){} | |
2432 }; | |
2433 // start for states machines which are themselves embedded in other state machines (composites) | |
2434 template <class Event> | |
2435 void internal_start(Event const& incomingEvent) | |
2436 { | |
2437 region_start_helper< ::boost::mpl::int_<0> >::do_start(this,incomingEvent); | |
2438 // give a chance to handle an anonymous (eventless) transition | |
2439 handle_eventless_transitions_helper<library_sm> eventless_helper(this,true); | |
2440 eventless_helper.process_completion_event(); | |
2441 } | |
2442 | |
2443 template <class StateType> | |
2444 struct find_region_id | |
2445 { | |
2446 template <int region,int Dummy=0> | |
2447 struct In | |
2448 { | |
2449 enum {region_index=region}; | |
2450 }; | |
2451 // if the user provides no region, find it! | |
2452 template<int Dummy> | |
2453 struct In<-1,Dummy> | |
2454 { | |
2455 typedef typename build_orthogonal_regions< | |
2456 library_sm, | |
2457 initial_states | |
2458 >::type all_regions; | |
2459 enum {region_index= find_region_index<all_regions,StateType>::value }; | |
2460 }; | |
2461 enum {region_index = In<StateType::zone_index>::region_index }; | |
2462 }; | |
2463 // helper used to set the correct state as active state upon entry into a fsm | |
2464 struct direct_event_start_helper | |
2465 { | |
2466 direct_event_start_helper(library_sm* self_):self(self_){} | |
2467 // this variant is for the standard case, entry due to activation of the containing FSM | |
2468 template <class EventType,class FsmType> | |
2469 typename ::boost::disable_if<typename has_direct_entry<EventType>::type,void>::type | |
2470 operator()(EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<0> = 0) | |
2471 { | |
2472 (static_cast<Derived*>(self))->on_entry(evt,fsm); | |
2473 self->internal_start(evt); | |
2474 } | |
2475 | |
2476 // this variant is for the direct entry case (just one entry, not a sequence of entries) | |
2477 template <class EventType,class FsmType> | |
2478 typename ::boost::enable_if< | |
2479 typename ::boost::mpl::and_< | |
2480 typename ::boost::mpl::not_< typename is_pseudo_entry< | |
2481 typename EventType::active_state>::type >::type, | |
2482 typename ::boost::mpl::and_<typename has_direct_entry<EventType>::type, | |
2483 typename ::boost::mpl::not_<typename ::boost::mpl::is_sequence | |
2484 <typename EventType::active_state>::type >::type | |
2485 >::type>::type,void | |
2486 >::type | |
2487 operator()(EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<1> = 0) | |
2488 { | |
2489 (static_cast<Derived*>(self))->on_entry(evt,fsm); | |
2490 int state_id = get_state_id<stt,typename EventType::active_state::wrapped_entry>::value; | |
2491 BOOST_STATIC_ASSERT(find_region_id<typename EventType::active_state::wrapped_entry>::region_index >= 0); | |
2492 BOOST_STATIC_ASSERT(find_region_id<typename EventType::active_state::wrapped_entry>::region_index < nr_regions::value); | |
2493 // just set the correct zone, the others will be default/history initialized | |
2494 self->m_states[find_region_id<typename EventType::active_state::wrapped_entry>::region_index] = state_id; | |
2495 self->internal_start(evt.m_event); | |
2496 } | |
2497 | |
2498 // this variant is for the fork entry case (a sequence on entries) | |
2499 template <class EventType,class FsmType> | |
2500 typename ::boost::enable_if< | |
2501 typename ::boost::mpl::and_< | |
2502 typename ::boost::mpl::not_< | |
2503 typename is_pseudo_entry<typename EventType::active_state>::type >::type, | |
2504 typename ::boost::mpl::and_<typename has_direct_entry<EventType>::type, | |
2505 typename ::boost::mpl::is_sequence< | |
2506 typename EventType::active_state>::type | |
2507 >::type>::type,void | |
2508 >::type | |
2509 operator()(EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<2> = 0) | |
2510 { | |
2511 (static_cast<Derived*>(self))->on_entry(evt,fsm); | |
2512 ::boost::mpl::for_each<typename EventType::active_state, | |
2513 ::boost::msm::wrap< ::boost::mpl::placeholders::_1> > | |
2514 (fork_helper<EventType>(self,evt)); | |
2515 // set the correct zones, the others (if any) will be default/history initialized | |
2516 self->internal_start(evt.m_event); | |
2517 } | |
2518 | |
2519 // this variant is for the pseudo state entry case | |
2520 template <class EventType,class FsmType> | |
2521 typename ::boost::enable_if< | |
2522 typename is_pseudo_entry<typename EventType::active_state >::type,void | |
2523 >::type | |
2524 operator()(EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<3> = 0) | |
2525 { | |
2526 // entry on the FSM | |
2527 (static_cast<Derived*>(self))->on_entry(evt,fsm); | |
2528 int state_id = get_state_id<stt,typename EventType::active_state::wrapped_entry>::value; | |
2529 BOOST_STATIC_ASSERT(find_region_id<typename EventType::active_state::wrapped_entry>::region_index >= 0); | |
2530 BOOST_STATIC_ASSERT(find_region_id<typename EventType::active_state::wrapped_entry>::region_index < nr_regions::value); | |
2531 // given region starts with the entry pseudo state as active state | |
2532 self->m_states[find_region_id<typename EventType::active_state::wrapped_entry>::region_index] = state_id; | |
2533 self->internal_start(evt.m_event); | |
2534 // and we process the transition in the zone of the newly active state | |
2535 // (entry pseudo states are, according to UML, a state connecting 1 transition outside to 1 inside | |
2536 self->process_event(evt.m_event); | |
2537 } | |
2538 private: | |
2539 // helper for the fork case, does almost like the direct entry | |
2540 library_sm* self; | |
2541 template <class EventType> | |
2542 struct fork_helper | |
2543 { | |
2544 fork_helper(library_sm* self_,EventType const& evt_): | |
2545 helper_self(self_),helper_evt(evt_){} | |
2546 template <class StateType> | |
2547 void operator()( ::boost::msm::wrap<StateType> const& ) | |
2548 { | |
2549 int state_id = get_state_id<stt,typename StateType::wrapped_entry>::value; | |
2550 BOOST_STATIC_ASSERT(find_region_id<typename StateType::wrapped_entry>::region_index >= 0); | |
2551 BOOST_STATIC_ASSERT(find_region_id<typename StateType::wrapped_entry>::region_index < nr_regions::value); | |
2552 helper_self->m_states[find_region_id<typename StateType::wrapped_entry>::region_index] = state_id; | |
2553 } | |
2554 private: | |
2555 library_sm* helper_self; | |
2556 EventType const& helper_evt; | |
2557 }; | |
2558 }; | |
2559 | |
2560 // helper for entry | |
2561 template <class region_id,int Dummy=0> | |
2562 struct region_entry_exit_helper | |
2563 { | |
2564 template<class Event> | |
2565 static void do_entry(library_sm* self_,Event const& incomingEvent) | |
2566 { | |
2567 self_->m_states[region_id::value] = | |
2568 self_->m_history.history_entry(incomingEvent)[region_id::value]; | |
2569 region_entry_exit_helper | |
2570 < ::boost::mpl::int_<region_id::value+1> >::do_entry(self_,incomingEvent); | |
2571 } | |
2572 template<class Event> | |
2573 static void do_exit(library_sm* self_,Event const& incomingEvent) | |
2574 { | |
2575 ::boost::mpl::for_each<state_list, ::boost::msm::wrap< ::boost::mpl::placeholders::_1> > | |
2576 (entry_exit_helper<Event,false>(self_->m_states[region_id::value],incomingEvent,self_)); | |
2577 region_entry_exit_helper | |
2578 < ::boost::mpl::int_<region_id::value+1> >::do_exit(self_,incomingEvent); | |
2579 } | |
2580 }; | |
2581 template <int Dummy> | |
2582 struct region_entry_exit_helper< ::boost::mpl::int_<nr_regions::value>,Dummy> | |
2583 { | |
2584 // end of processing | |
2585 template<class Event> | |
2586 static void do_entry(library_sm*,Event const& ){} | |
2587 template<class Event> | |
2588 static void do_exit(library_sm*,Event const& ){} | |
2589 }; | |
2590 // entry/exit for states machines which are themselves embedded in other state machines (composites) | |
2591 template <class Event,class FsmType> | |
2592 void do_entry(Event const& incomingEvent,FsmType& fsm) | |
2593 { | |
2594 // by default we activate the history/init states, can be overwritten by direct_event_start_helper | |
2595 region_entry_exit_helper< ::boost::mpl::int_<0> >::do_entry(this,incomingEvent); | |
2596 // block immediate handling of events | |
2597 m_event_processing = true; | |
2598 // if the event is generating a direct entry/fork, set the current state(s) to the direct state(s) | |
2599 direct_event_start_helper(this)(incomingEvent,fsm); | |
2600 // handle messages which were generated and blocked in the init calls | |
2601 m_event_processing = false; | |
2602 process_message_queue(this); | |
2603 } | |
2604 template <class Event,class FsmType> | |
2605 void do_exit(Event const& incomingEvent,FsmType& fsm) | |
2606 { | |
2607 // first recursively exit the sub machines | |
2608 // forward the event for handling by sub state machines | |
2609 region_entry_exit_helper< ::boost::mpl::int_<0> >::do_exit(this,incomingEvent); | |
2610 // then call our own exit | |
2611 (static_cast<Derived*>(this))->on_exit(incomingEvent,fsm); | |
2612 // give the history a chance to handle this (or not). | |
2613 m_history.history_exit(this->m_states); | |
2614 } | |
2615 | |
2616 // the IBM and VC<8 compilers seem to have problems with the friend declaration of dispatch_table | |
2617 #if defined (__IBMCPP__) || (defined(_MSC_VER) && (_MSC_VER < 1400)) | |
2618 public: | |
2619 #endif | |
2620 // no transition for event. | |
2621 template <class Event> | |
2622 static HandledEnum call_no_transition(library_sm& , int , int , Event const& ) | |
2623 { | |
2624 return HANDLED_FALSE; | |
2625 } | |
2626 // no transition for event for internal transitions (not an error). | |
2627 template <class Event> | |
2628 static HandledEnum call_no_transition_internal(library_sm& , int , int , Event const& ) | |
2629 { | |
2630 //// reject to give others a chance to handle | |
2631 //return HANDLED_GUARD_REJECT; | |
2632 return HANDLED_FALSE; | |
2633 } | |
2634 // called for deferred events. Address set in the dispatch_table at init | |
2635 template <class Event> | |
2636 static HandledEnum defer_transition(library_sm& fsm, int , int , Event const& e) | |
2637 { | |
2638 fsm.defer_event(e); | |
2639 return HANDLED_DEFERRED; | |
2640 } | |
2641 // called for completion events. Default address set in the dispatch_table at init | |
2642 // prevents no-transition detection for completion events | |
2643 template <class Event> | |
2644 static HandledEnum default_eventless_transition(library_sm&, int, int , Event const&) | |
2645 { | |
2646 return HANDLED_FALSE; | |
2647 } | |
2648 #if defined (__IBMCPP__) || (defined(_MSC_VER) && (_MSC_VER < 1400)) | |
2649 private: | |
2650 #endif | |
2651 // puts a deferred event in the queue | |
2652 void post_deferred_event(deferred_fct& deferred) | |
2653 { | |
2654 m_deferred_events_queue.m_deferred_events_queue.push_back(std::make_pair(deferred,true)); | |
2655 } | |
2656 // removes one event from the message queue and processes it | |
2657 template <class StateType> | |
2658 void process_message_queue(StateType*, | |
2659 typename ::boost::disable_if<typename is_no_message_queue<StateType>::type,void >::type* = 0) | |
2660 { | |
2661 if (!m_events_queue.m_events_queue.empty()) | |
2662 { | |
2663 transition_fct to_call = m_events_queue.m_events_queue.front(); | |
2664 m_events_queue.m_events_queue.pop_front(); | |
2665 to_call(); | |
2666 } | |
2667 } | |
2668 template <class StateType> | |
2669 void process_message_queue(StateType*, | |
2670 typename ::boost::enable_if<typename is_no_message_queue<StateType>::type,void >::type* = 0) | |
2671 { | |
2672 // nothing to process | |
2673 } | |
2674 // helper function. In cases where the event is wrapped (target is a direct entry states) | |
2675 // we want to send only the real event to on_entry, not the wrapper. | |
2676 template <class EventType> | |
2677 static | |
2678 typename boost::enable_if<typename has_direct_entry<EventType>::type,typename EventType::contained_event const& >::type | |
2679 remove_direct_entry_event_wrapper(EventType const& evt,boost::msm::back::dummy<0> = 0) | |
2680 { | |
2681 return evt.m_event; | |
2682 } | |
2683 template <class EventType> | |
2684 static typename boost::disable_if<typename has_direct_entry<EventType>::type,EventType const& >::type | |
2685 remove_direct_entry_event_wrapper(EventType const& evt,boost::msm::back::dummy<1> = 0) | |
2686 { | |
2687 // identity. No wrapper | |
2688 return evt; | |
2689 } | |
2690 // calls the entry/exit or on_entry/on_exit depending on the state type | |
2691 // (avoids calling virtually) | |
2692 // variant for FSMs | |
2693 template <class StateType,class EventType,class FsmType> | |
2694 static | |
2695 typename boost::enable_if<typename is_composite_state<StateType>::type,void >::type | |
2696 execute_entry(StateType& astate,EventType const& evt,FsmType& fsm,boost::msm::back::dummy<0> = 0) | |
2697 { | |
2698 // calls on_entry on the fsm then handles direct entries, fork, entry pseudo state | |
2699 astate.do_entry(evt,fsm); | |
2700 } | |
2701 // variant for states | |
2702 template <class StateType,class EventType,class FsmType> | |
2703 static | |
2704 typename ::boost::disable_if< | |
2705 typename ::boost::mpl::or_<typename is_composite_state<StateType>::type, | |
2706 typename is_pseudo_exit<StateType>::type >::type,void >::type | |
2707 execute_entry(StateType& astate,EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<1> = 0) | |
2708 { | |
2709 // simple call to on_entry | |
2710 astate.on_entry(remove_direct_entry_event_wrapper(evt),fsm); | |
2711 } | |
2712 // variant for exit pseudo states | |
2713 template <class StateType,class EventType,class FsmType> | |
2714 static | |
2715 typename ::boost::enable_if<typename is_pseudo_exit<StateType>::type,void >::type | |
2716 execute_entry(StateType& astate,EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<2> = 0) | |
2717 { | |
2718 // calls on_entry on the state then forward the event to the transition which should be defined inside the | |
2719 // contained fsm | |
2720 astate.on_entry(evt,fsm); | |
2721 astate.forward_event(evt); | |
2722 } | |
2723 template <class StateType,class EventType,class FsmType> | |
2724 static | |
2725 typename ::boost::enable_if<typename is_composite_state<StateType>::type,void >::type | |
2726 execute_exit(StateType& astate,EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<0> = 0) | |
2727 { | |
2728 astate.do_exit(evt,fsm); | |
2729 } | |
2730 template <class StateType,class EventType,class FsmType> | |
2731 static | |
2732 typename ::boost::disable_if<typename is_composite_state<StateType>::type,void >::type | |
2733 execute_exit(StateType& astate,EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<1> = 0) | |
2734 { | |
2735 // simple call to on_exit | |
2736 astate.on_exit(evt,fsm); | |
2737 } | |
2738 | |
2739 // helper allowing special handling of direct entries / fork | |
2740 template <class StateType,class TargetType,class EventType,class FsmType> | |
2741 static | |
2742 typename ::boost::disable_if< | |
2743 typename ::boost::mpl::or_<typename has_explicit_entry_state<TargetType>::type, | |
2744 ::boost::mpl::is_sequence<TargetType> >::type,void>::type | |
2745 convert_event_and_execute_entry(StateType& astate,EventType const& evt, FsmType& fsm, ::boost::msm::back::dummy<1> = 0) | |
2746 { | |
2747 // if the target is a normal state, do the standard entry handling | |
2748 execute_entry<StateType>(astate,evt,fsm); | |
2749 } | |
2750 template <class StateType,class TargetType,class EventType,class FsmType> | |
2751 static | |
2752 typename ::boost::enable_if< | |
2753 typename ::boost::mpl::or_<typename has_explicit_entry_state<TargetType>::type, | |
2754 ::boost::mpl::is_sequence<TargetType> >::type,void >::type | |
2755 convert_event_and_execute_entry(StateType& astate,EventType const& evt, FsmType& fsm, ::boost::msm::back::dummy<0> = 0) | |
2756 { | |
2757 // for the direct entry, pack the event in a wrapper so that we handle it differently during fsm entry | |
2758 execute_entry(astate,msm::back::direct_entry_event<TargetType,EventType>(evt),fsm); | |
2759 } | |
2760 | |
2761 // creates all the states | |
2762 template <class ContainingSM> | |
2763 void fill_states(ContainingSM* containing_sm=0) | |
2764 { | |
2765 // checks that regions are truly orthogonal | |
2766 FsmCheckPolicy::template check_orthogonality<library_sm>(); | |
2767 // checks that all states are reachable | |
2768 FsmCheckPolicy::template check_unreachable_states<library_sm>(); | |
2769 | |
2770 BOOST_STATIC_CONSTANT(int, max_state = (mpl::size<state_list>::value)); | |
2771 // allocate the place without reallocation | |
2772 m_visitors.fill_visitors(max_state); | |
2773 ::boost::fusion::for_each(m_substate_list,add_state<ContainingSM>(this,containing_sm)); | |
2774 | |
2775 } | |
2776 | |
2777 private: | |
2778 template <class StateType,class Enable=void> | |
2779 struct msg_queue_helper | |
2780 { | |
2781 public: | |
2782 msg_queue_helper():m_events_queue(){} | |
2783 events_queue_t m_events_queue; | |
2784 }; | |
2785 template <class StateType> | |
2786 struct msg_queue_helper<StateType, | |
2787 typename ::boost::enable_if<typename is_no_message_queue<StateType>::type >::type> | |
2788 { | |
2789 }; | |
2790 | |
2791 template <class Fsm,class Stt, class Event, class Compile> | |
2792 friend struct dispatch_table; | |
2793 | |
2794 // data members | |
2795 int m_states[nr_regions::value]; | |
2796 msg_queue_helper<library_sm> m_events_queue; | |
2797 deferred_msg_queue_helper | |
2798 <library_sm> m_deferred_events_queue; | |
2799 concrete_history m_history; | |
2800 bool m_event_processing; | |
2801 bool m_is_included; | |
2802 visitor_fct_helper<BaseState> m_visitors; | |
2803 substate_list m_substate_list; | |
2804 | |
2805 | |
2806 }; | |
2807 | |
2808 } } }// boost::msm::back | |
2809 #endif //BOOST_MSM_BACK_STATEMACHINE_H | |
2810 |