state_machine.hpp 122 KB

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