annotate DEPENDENCIES/generic/include/boost/msm/back/state_machine.hpp @ 16:2665513ce2d3

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