state_machine.hpp 132 KB

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