annotate DEPENDENCIES/generic/include/boost/msm/back/state_machine.hpp @ 133:4acb5d8d80b6 tip

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