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