state_machine.hpp 125 KB

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