state_machine.hpp 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925
  1. // Copyright 2008 Christophe Henry
  2. // henry UNDERSCORE christophe AT hotmail DOT com
  3. // This is an extended version of the state machine available in the boost::mpl library
  4. // Distributed under the same license as the original.
  5. // Copyright for the original version:
  6. // Copyright 2005 David Abrahams and Aleksey Gurtovoy. Distributed
  7. // under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. #ifndef BOOST_MSM_BACK_STATEMACHINE_H
  11. #define BOOST_MSM_BACK_STATEMACHINE_H
  12. #include <exception>
  13. #include <vector>
  14. #include <functional>
  15. #include <numeric>
  16. #include <utility>
  17. #include <boost/core/no_exceptions_support.hpp>
  18. #include <boost/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/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()
  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
  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(ARG0&& t0,ARG&&... t)
  1561. :Derived(std::forward<ARG0>(t0), std::forward<ARG>(t)...)
  1562. ,m_events_queue()
  1563. ,m_deferred_events_queue()
  1564. ,m_history()
  1565. ,m_event_processing(false)
  1566. ,m_is_included(false)
  1567. ,m_visitors()
  1568. ,m_substate_list()
  1569. {
  1570. ::boost::mpl::for_each< seq_initial_states, ::boost::msm::wrap<mpl::placeholders::_1> >
  1571. (init_states(m_states));
  1572. m_history.set_initial_states(m_states);
  1573. fill_states(this);
  1574. }
  1575. template <class Expr,class... ARG,class=typename ::boost::enable_if<typename ::boost::proto::is_expr<Expr>::type >::type>
  1576. state_machine(Expr const& expr,ARG&&... t)
  1577. :Derived(std::forward<ARG>(t)...)
  1578. ,m_events_queue()
  1579. ,m_deferred_events_queue()
  1580. ,m_history()
  1581. ,m_event_processing(false)
  1582. ,m_is_included(false)
  1583. ,m_visitors()
  1584. ,m_substate_list()
  1585. {
  1586. BOOST_MPL_ASSERT_MSG(
  1587. ( ::boost::proto::matches<Expr, FoldToList>::value),
  1588. THE_STATES_EXPRESSION_PASSED_DOES_NOT_MATCH_GRAMMAR,
  1589. (FoldToList));
  1590. ::boost::mpl::for_each< seq_initial_states, ::boost::msm::wrap<mpl::placeholders::_1> >
  1591. (init_states(m_states));
  1592. m_history.set_initial_states(m_states);
  1593. set_states(expr);
  1594. fill_states(this);
  1595. }
  1596. #endif
  1597. // assignment operator using the copy policy to decide if non_copyable, shallow or deep copying is necessary
  1598. library_sm& operator= (library_sm const& rhs)
  1599. {
  1600. if (this != &rhs)
  1601. {
  1602. Derived::operator=(rhs);
  1603. do_copy(rhs);
  1604. }
  1605. return *this;
  1606. }
  1607. state_machine(library_sm const& rhs)
  1608. : Derived(rhs)
  1609. {
  1610. if (this != &rhs)
  1611. {
  1612. // initialize our list of states with the ones defined in Derived::initial_state
  1613. fill_states(this);
  1614. do_copy(rhs);
  1615. }
  1616. }
  1617. // the following 2 functions handle the terminate/interrupt states handling
  1618. // if one of these states is found, the first one is used
  1619. template <class Event>
  1620. bool is_event_handling_blocked_helper( ::boost::mpl::true_ const &)
  1621. {
  1622. // if the state machine is terminated, do not handle any event
  1623. if (is_flag_active< ::boost::msm::TerminateFlag>())
  1624. return true;
  1625. // if the state machine is interrupted, do not handle any event
  1626. // unless the event is the end interrupt event
  1627. if ( is_flag_active< ::boost::msm::InterruptedFlag>() &&
  1628. !is_flag_active< ::boost::msm::EndInterruptFlag<Event> >())
  1629. return true;
  1630. return false;
  1631. }
  1632. // otherwise simple handling, no flag => continue
  1633. template <class Event>
  1634. bool is_event_handling_blocked_helper( ::boost::mpl::false_ const &)
  1635. {
  1636. // no terminate/interrupt states detected
  1637. return false;
  1638. }
  1639. void do_handle_prio_msg_queue_deferred_queue(EventSource source, HandledEnum handled, ::boost::mpl::true_ const &)
  1640. {
  1641. // non-default. Handle msg queue with higher prio than deferred queue
  1642. if (!(EVENT_SOURCE_MSG_QUEUE & source))
  1643. {
  1644. do_post_msg_queue_helper(
  1645. ::boost::mpl::bool_<
  1646. is_no_message_queue<library_sm>::type::value>());
  1647. if (!(EVENT_SOURCE_DEFERRED & source))
  1648. {
  1649. handle_defer_helper<library_sm> defer_helper(m_deferred_events_queue);
  1650. defer_helper.do_handle_deferred(HANDLED_TRUE & handled);
  1651. }
  1652. }
  1653. }
  1654. void do_handle_prio_msg_queue_deferred_queue(EventSource source, HandledEnum handled, ::boost::mpl::false_ const &)
  1655. {
  1656. // default. Handle deferred queue with higher prio than msg queue
  1657. if (!(EVENT_SOURCE_DEFERRED & source))
  1658. {
  1659. handle_defer_helper<library_sm> defer_helper(m_deferred_events_queue);
  1660. defer_helper.do_handle_deferred(HANDLED_TRUE & handled);
  1661. // Handle any new events generated into the queue, but only if
  1662. // we're not already processing from the message queue.
  1663. if (!(EVENT_SOURCE_MSG_QUEUE & source))
  1664. {
  1665. do_post_msg_queue_helper(
  1666. ::boost::mpl::bool_<
  1667. is_no_message_queue<library_sm>::type::value>());
  1668. }
  1669. }
  1670. }
  1671. // the following functions handle pre/post-process handling of a message queue
  1672. template <class StateType,class EventType>
  1673. bool do_pre_msg_queue_helper(EventType const&, ::boost::mpl::true_ const &)
  1674. {
  1675. // no message queue needed
  1676. return true;
  1677. }
  1678. template <class StateType,class EventType>
  1679. bool do_pre_msg_queue_helper(EventType const& evt, ::boost::mpl::false_ const &)
  1680. {
  1681. execute_return (library_sm::*pf) (EventType const&, EventSource) =
  1682. &library_sm::process_event_internal;
  1683. // if we are already processing an event
  1684. if (m_event_processing)
  1685. {
  1686. // event has to be put into the queue
  1687. m_events_queue.m_events_queue.push_back(
  1688. ::boost::bind(
  1689. pf, this, evt,
  1690. static_cast<EventSource>(EVENT_SOURCE_DIRECT | EVENT_SOURCE_MSG_QUEUE)));
  1691. return false;
  1692. }
  1693. // event can be handled, processing
  1694. m_event_processing = true;
  1695. return true;
  1696. }
  1697. void do_post_msg_queue_helper( ::boost::mpl::true_ const &)
  1698. {
  1699. // no message queue needed
  1700. }
  1701. void do_post_msg_queue_helper( ::boost::mpl::false_ const &)
  1702. {
  1703. process_message_queue(this);
  1704. }
  1705. void do_allow_event_processing_after_transition( ::boost::mpl::true_ const &)
  1706. {
  1707. // no message queue needed
  1708. }
  1709. void do_allow_event_processing_after_transition( ::boost::mpl::false_ const &)
  1710. {
  1711. m_event_processing = false;
  1712. }
  1713. // the following 2 functions handle the processing either with a try/catch protection or without
  1714. template <class StateType,class EventType>
  1715. HandledEnum do_process_helper(EventType const& evt, ::boost::mpl::true_ const &, bool is_direct_call)
  1716. {
  1717. return this->do_process_event(evt,is_direct_call);
  1718. }
  1719. template <class StateType,class EventType>
  1720. HandledEnum do_process_helper(EventType const& evt, ::boost::mpl::false_ const &, bool is_direct_call)
  1721. {
  1722. // when compiling without exception support there is no formal parameter "e" in the catch handler.
  1723. // Declaring a local variable here does not hurt and will be "used" to make the code in the handler
  1724. // compilable although the code will never be executed.
  1725. std::exception e;
  1726. BOOST_TRY
  1727. {
  1728. return this->do_process_event(evt,is_direct_call);
  1729. }
  1730. BOOST_CATCH (std::exception& e)
  1731. {
  1732. // give a chance to the concrete state machine to handle
  1733. this->exception_caught(evt,*this,e);
  1734. }
  1735. BOOST_CATCH_END
  1736. return HANDLED_TRUE;
  1737. }
  1738. // handling of deferred events
  1739. // if none is found in the SM, take the following empty main version
  1740. template <class StateType, class Enable = int>
  1741. struct handle_defer_helper
  1742. {
  1743. handle_defer_helper(deferred_msg_queue_helper<library_sm>& ){}
  1744. void do_handle_deferred(bool)
  1745. {
  1746. }
  1747. };
  1748. // otherwise the standard version handling the deferred events
  1749. template <class StateType>
  1750. struct handle_defer_helper
  1751. <StateType, typename enable_if< typename ::boost::msm::back::has_fsm_deferred_events<StateType>::type,int >::type>
  1752. {
  1753. handle_defer_helper(deferred_msg_queue_helper<library_sm>& a_queue):
  1754. m_events_queue(a_queue) {}
  1755. void do_handle_deferred(bool new_seq=false)
  1756. {
  1757. // A new sequence is typically started upon initial entry to the
  1758. // state, or upon a new transition. When this occurs we want to
  1759. // process all previously deferred events by incrementing the
  1760. // correlation sequence.
  1761. if (new_seq)
  1762. {
  1763. ++m_events_queue.m_cur_seq;
  1764. }
  1765. char& cur_seq = m_events_queue.m_cur_seq;
  1766. // Iteratively process all of the events within the deferred
  1767. // queue upto (but not including) newly deferred events.
  1768. while (!m_events_queue.m_deferred_events_queue.empty())
  1769. {
  1770. typename deferred_events_queue_t::value_type& pair =
  1771. m_events_queue.m_deferred_events_queue.front();
  1772. if (cur_seq != pair.second)
  1773. {
  1774. break;
  1775. }
  1776. deferred_fct next = pair.first;
  1777. m_events_queue.m_deferred_events_queue.pop_front();
  1778. next();
  1779. }
  1780. }
  1781. private:
  1782. deferred_msg_queue_helper<library_sm>& m_events_queue;
  1783. };
  1784. // handling of eventless transitions
  1785. // if none is found in the SM, nothing to do
  1786. template <class StateType, class Enable = void>
  1787. struct handle_eventless_transitions_helper
  1788. {
  1789. handle_eventless_transitions_helper(library_sm* , bool ){}
  1790. void process_completion_event(EventSource = EVENT_SOURCE_DEFAULT){}
  1791. };
  1792. // otherwise
  1793. template <class StateType>
  1794. struct handle_eventless_transitions_helper
  1795. <StateType, typename enable_if< typename ::boost::msm::back::has_fsm_eventless_transition<StateType>::type >::type>
  1796. {
  1797. handle_eventless_transitions_helper(library_sm* self_, bool handled_):self(self_),handled(handled_){}
  1798. void process_completion_event(EventSource source = EVENT_SOURCE_DEFAULT)
  1799. {
  1800. typedef typename ::boost::mpl::deref<
  1801. typename ::boost::mpl::begin<
  1802. typename find_completion_events<StateType>::type
  1803. >::type
  1804. >::type first_completion_event;
  1805. if (handled)
  1806. {
  1807. self->process_event_internal(
  1808. first_completion_event(),
  1809. source | EVENT_SOURCE_DIRECT);
  1810. }
  1811. }
  1812. private:
  1813. library_sm* self;
  1814. bool handled;
  1815. };
  1816. // helper class called in case the event to process has been found in the fsm's internal stt and is therefore processable
  1817. template<class Event>
  1818. struct process_fsm_internal_table
  1819. {
  1820. typedef typename ::boost::mpl::has_key<processable_events_internal_table,Event>::type is_event_processable;
  1821. // forward to the correct do_process
  1822. static void process(Event const& evt,library_sm* self_,HandledEnum& result)
  1823. {
  1824. do_process(evt,self_,result,is_event_processable());
  1825. }
  1826. private:
  1827. // the event is processable, let's try!
  1828. static void do_process(Event const& evt,library_sm* self_,HandledEnum& result, ::boost::mpl::true_)
  1829. {
  1830. if (result != HANDLED_TRUE)
  1831. {
  1832. typedef dispatch_table<library_sm,complete_table,Event,CompilePolicy> table;
  1833. HandledEnum res_internal = table::instance.entries[0](*self_, 0, self_->m_states[0], evt);
  1834. result = (HandledEnum)((int)result | (int)res_internal);
  1835. }
  1836. }
  1837. // version doing nothing if the event is not in the internal stt and we can save ourselves the time trying to process
  1838. static void do_process(Event const& ,library_sm* ,HandledEnum& , ::boost::mpl::false_)
  1839. {
  1840. // do nothing
  1841. }
  1842. };
  1843. template <class StateType,class Enable=void>
  1844. struct region_processing_helper
  1845. {
  1846. public:
  1847. region_processing_helper(library_sm* self_,HandledEnum& result_)
  1848. :self(self_),result(result_){}
  1849. template<class Event>
  1850. void process(Event const& evt)
  1851. {
  1852. // use this table as if it came directly from the user
  1853. typedef dispatch_table<library_sm,complete_table,Event,CompilePolicy> table;
  1854. // +1 because index 0 is reserved for this fsm
  1855. HandledEnum res =
  1856. table::instance.entries[self->m_states[0]+1](
  1857. *self, 0, self->m_states[0], evt);
  1858. result = (HandledEnum)((int)result | (int)res);
  1859. // process the event in the internal table of this fsm if the event is processable (present in the table)
  1860. process_fsm_internal_table<Event>::process(evt,self,result);
  1861. }
  1862. library_sm* self;
  1863. HandledEnum& result;
  1864. };
  1865. // version with visitors
  1866. template <class StateType>
  1867. struct region_processing_helper<StateType,typename ::boost::enable_if<
  1868. ::boost::mpl::is_sequence<typename StateType::initial_state> >::type>
  1869. {
  1870. private:
  1871. // process event in one region
  1872. template <class region_id,int Dummy=0>
  1873. struct In
  1874. {
  1875. template<class Event>
  1876. static void process(Event const& evt,library_sm* self_,HandledEnum& result_)
  1877. {
  1878. // use this table as if it came directly from the user
  1879. typedef dispatch_table<library_sm,complete_table,Event,CompilePolicy> table;
  1880. // +1 because index 0 is reserved for this fsm
  1881. HandledEnum res =
  1882. table::instance.entries[self_->m_states[region_id::value]+1](
  1883. *self_, region_id::value , self_->m_states[region_id::value], evt);
  1884. result_ = (HandledEnum)((int)result_ | (int)res);
  1885. In< ::boost::mpl::int_<region_id::value+1> >::process(evt,self_,result_);
  1886. }
  1887. };
  1888. template <int Dummy>
  1889. struct In< ::boost::mpl::int_<nr_regions::value>,Dummy>
  1890. {
  1891. // end of processing
  1892. template<class Event>
  1893. static void process(Event const& evt,library_sm* self_,HandledEnum& result_)
  1894. {
  1895. // process the event in the internal table of this fsm if the event is processable (present in the table)
  1896. process_fsm_internal_table<Event>::process(evt,self_,result_);
  1897. }
  1898. };
  1899. public:
  1900. region_processing_helper(library_sm* self_,HandledEnum& result_)
  1901. :self(self_),result(result_){}
  1902. template<class Event>
  1903. void process(Event const& evt)
  1904. {
  1905. In< ::boost::mpl::int_<0> >::process(evt,self,result);
  1906. }
  1907. library_sm* self;
  1908. HandledEnum& result;
  1909. };
  1910. // Main function used internally to make transitions
  1911. // Can only be called for internally (for example in an action method) generated events.
  1912. template<class Event>
  1913. execute_return process_event_internal(Event const& evt,
  1914. EventSource source = EVENT_SOURCE_DEFAULT)
  1915. {
  1916. // if the state machine has terminate or interrupt flags, check them, otherwise skip
  1917. if (is_event_handling_blocked_helper<Event>
  1918. ( ::boost::mpl::bool_<has_fsm_blocking_states<library_sm>::type::value>() ) )
  1919. {
  1920. return HANDLED_TRUE;
  1921. }
  1922. // if a message queue is needed and processing is on the way
  1923. if (!do_pre_msg_queue_helper<Event>
  1924. (evt,::boost::mpl::bool_<is_no_message_queue<library_sm>::type::value>()))
  1925. {
  1926. // wait for the end of current processing
  1927. return HANDLED_TRUE;
  1928. }
  1929. else
  1930. {
  1931. // Process event
  1932. HandledEnum handled = this->do_process_helper<Event>(
  1933. evt,
  1934. ::boost::mpl::bool_<is_no_exception_thrown<library_sm>::type::value>(),
  1935. (EVENT_SOURCE_DIRECT & source));
  1936. // at this point we allow the next transition be executed without enqueing
  1937. // so that completion events and deferred events execute now (if any)
  1938. do_allow_event_processing_after_transition(
  1939. ::boost::mpl::bool_<is_no_message_queue<library_sm>::type::value>());
  1940. // Process completion transitions BEFORE any other event in the
  1941. // pool (UML Standard 2.3 15.3.14)
  1942. handle_eventless_transitions_helper<library_sm>
  1943. eventless_helper(this,(HANDLED_TRUE & handled));
  1944. eventless_helper.process_completion_event(source);
  1945. // After handling, take care of the deferred events, but only if
  1946. // we're not already processing from the deferred queue.
  1947. do_handle_prio_msg_queue_deferred_queue(
  1948. source,handled,
  1949. ::boost::mpl::bool_<has_event_queue_before_deferred_queue<library_sm>::type::value>());
  1950. return handled;
  1951. }
  1952. }
  1953. // minimum event processing without exceptions, queues, etc.
  1954. template<class Event>
  1955. HandledEnum do_process_event(Event const& evt, bool is_direct_call)
  1956. {
  1957. HandledEnum handled = HANDLED_FALSE;
  1958. // dispatch the event to every region
  1959. region_processing_helper<Derived> helper(this,handled);
  1960. helper.process(evt);
  1961. // if the event has not been handled and we have orthogonal zones, then
  1962. // generate an error on every active state
  1963. // for state machine states contained in other state machines, do not handle
  1964. // but let the containing sm handle the error, unless the event was generated in this fsm
  1965. // (by calling process_event on this fsm object, is_direct_call == true)
  1966. // completion events do not produce an error
  1967. if ( (!is_contained() || is_direct_call) && !handled && !is_completion_event<Event>::type::value)
  1968. {
  1969. for (int i=0; i<nr_regions::value;++i)
  1970. {
  1971. this->no_transition(evt,*this,this->m_states[i]);
  1972. }
  1973. }
  1974. return handled;
  1975. }
  1976. // default row arguments for the compilers which accept this
  1977. template <class Event>
  1978. bool no_guard(Event const&){return true;}
  1979. template <class Event>
  1980. void no_action(Event const&){}
  1981. #ifndef BOOST_NO_RTTI
  1982. HandledEnum process_any_event( ::boost::any const& evt);
  1983. #endif
  1984. private:
  1985. // composite accept implementation. First calls accept on the composite, then accept on all its active states.
  1986. void composite_accept()
  1987. {
  1988. this->accept();
  1989. this->visit_current_states();
  1990. }
  1991. #define MSM_COMPOSITE_ACCEPT_SUB(z, n, unused) ARG ## n vis ## n
  1992. #define MSM_COMPOSITE_ACCEPT_SUB2(z, n, unused) boost::ref( vis ## n )
  1993. #define MSM_COMPOSITE_ACCEPT_EXECUTE(z, n, unused) \
  1994. template <BOOST_PP_ENUM_PARAMS(n, class ARG)> \
  1995. void composite_accept(BOOST_PP_ENUM(n, MSM_COMPOSITE_ACCEPT_SUB, ~ ) ) \
  1996. { \
  1997. this->accept(BOOST_PP_ENUM_PARAMS(n,vis)); \
  1998. this->visit_current_states(BOOST_PP_ENUM(n,MSM_COMPOSITE_ACCEPT_SUB2, ~)); \
  1999. }
  2000. BOOST_PP_REPEAT_FROM_TO(1,BOOST_PP_ADD(BOOST_MSM_VISITOR_ARG_SIZE,1), MSM_COMPOSITE_ACCEPT_EXECUTE, ~)
  2001. #undef MSM_COMPOSITE_ACCEPT_EXECUTE
  2002. #undef MSM_COMPOSITE_ACCEPT_SUB
  2003. #undef MSM_COMPOSITE_ACCEPT_SUB2
  2004. // helper used to call the init states at the start of the state machine
  2005. template <class Event>
  2006. struct call_init
  2007. {
  2008. call_init(Event const& an_event,library_sm* self_):
  2009. evt(an_event),self(self_){}
  2010. template <class State>
  2011. void operator()(boost::msm::wrap<State> const&)
  2012. {
  2013. execute_entry(::boost::fusion::at_key<State>(self->m_substate_list),evt,*self);
  2014. }
  2015. private:
  2016. Event const& evt;
  2017. library_sm* self;
  2018. };
  2019. // helper for flag handling. Uses OR by default on orthogonal zones.
  2020. template <class Flag,bool orthogonalStates>
  2021. struct FlagHelper
  2022. {
  2023. static bool helper(library_sm const& sm,flag_handler* )
  2024. {
  2025. // by default we use OR to accumulate the flags
  2026. return sm.is_flag_active<Flag,Flag_OR>();
  2027. }
  2028. };
  2029. template <class Flag>
  2030. struct FlagHelper<Flag,false>
  2031. {
  2032. static bool helper(library_sm const& sm,flag_handler* flags_entries)
  2033. {
  2034. // just one active state, so we can call operator[] with 0
  2035. return flags_entries[sm.current_state()[0]](sm);
  2036. }
  2037. };
  2038. // handling of flag
  2039. // defines a true and false functions plus a forwarding one for composite states
  2040. template <class StateType,class Flag>
  2041. struct FlagHandler
  2042. {
  2043. static bool flag_true(library_sm const& )
  2044. {
  2045. return true;
  2046. }
  2047. static bool flag_false(library_sm const& )
  2048. {
  2049. return false;
  2050. }
  2051. static bool forward(library_sm const& fsm)
  2052. {
  2053. return ::boost::fusion::at_key<StateType>(fsm.m_substate_list).template is_flag_active<Flag>();
  2054. }
  2055. };
  2056. template <class Flag>
  2057. struct init_flags
  2058. {
  2059. private:
  2060. // helper function, helps hiding the forward function for non-state machines states.
  2061. template <class T>
  2062. void helper (flag_handler* an_entry,int offset, ::boost::mpl::true_ const & )
  2063. {
  2064. // composite => forward
  2065. an_entry[offset] = &FlagHandler<T,Flag>::forward;
  2066. }
  2067. template <class T>
  2068. void helper (flag_handler* an_entry,int offset, ::boost::mpl::false_ const & )
  2069. {
  2070. // default no flag
  2071. an_entry[offset] = &FlagHandler<T,Flag>::flag_false;
  2072. }
  2073. // attributes
  2074. flag_handler* entries;
  2075. public:
  2076. init_flags(flag_handler* entries_)
  2077. : entries(entries_)
  2078. {}
  2079. // Flags initializer function object, used with mpl::for_each
  2080. template <class StateType>
  2081. void operator()( ::boost::msm::wrap<StateType> const& )
  2082. {
  2083. typedef typename get_flag_list<StateType>::type flags;
  2084. typedef typename ::boost::mpl::contains<flags,Flag >::type found;
  2085. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,StateType>::type::value));
  2086. if (found::type::value)
  2087. {
  2088. // the type defined the flag => true
  2089. entries[state_id] = &FlagHandler<StateType,Flag>::flag_true;
  2090. }
  2091. else
  2092. {
  2093. // false or forward
  2094. typedef typename ::boost::mpl::and_<
  2095. typename is_composite_state<StateType>::type,
  2096. typename ::boost::mpl::not_<
  2097. typename has_non_forwarding_flag<Flag>::type>::type >::type composite_no_forward;
  2098. helper<StateType>(entries,state_id,::boost::mpl::bool_<composite_no_forward::type::value>());
  2099. }
  2100. }
  2101. };
  2102. // maintains for every flag a static array containing the flag value for every state
  2103. template <class Flag>
  2104. flag_handler* get_entries_for_flag() const
  2105. {
  2106. BOOST_STATIC_CONSTANT(int, max_state = (mpl::size<state_list>::value));
  2107. static flag_handler flags_entries[max_state];
  2108. // build a state list
  2109. ::boost::mpl::for_each<state_list, boost::msm::wrap< ::boost::mpl::placeholders::_1> >
  2110. (init_flags<Flag>(flags_entries));
  2111. return flags_entries;
  2112. }
  2113. // helper used to create a state using the correct constructor
  2114. template <class State, class Enable=void>
  2115. struct create_state_helper
  2116. {
  2117. static void set_sm(library_sm* )
  2118. {
  2119. // state doesn't need its sm
  2120. }
  2121. };
  2122. // create a state requiring a pointer to the state machine
  2123. template <class State>
  2124. struct create_state_helper<State,typename boost::enable_if<typename State::needs_sm >::type>
  2125. {
  2126. static void set_sm(library_sm* sm)
  2127. {
  2128. // create and set the fsm
  2129. ::boost::fusion::at_key<State>(sm->m_substate_list).set_sm_ptr(sm);
  2130. }
  2131. };
  2132. // main unspecialized helper class
  2133. template <class StateType,int ARGS>
  2134. struct visitor_args;
  2135. #define MSM_VISITOR_ARGS_SUB(z, n, unused) BOOST_PP_CAT(::boost::placeholders::_,BOOST_PP_ADD(n,1))
  2136. #define MSM_VISITOR_ARGS_TYPEDEF_SUB(z, n, unused) typename StateType::accept_sig::argument ## n
  2137. #define MSM_VISITOR_ARGS_EXECUTE(z, n, unused) \
  2138. template <class StateType> \
  2139. struct visitor_args<StateType,n> \
  2140. { \
  2141. template <class State> \
  2142. static typename enable_if_c<!is_composite_state<State>::value,void >::type \
  2143. helper (library_sm* sm, \
  2144. int id,StateType& astate) \
  2145. { \
  2146. sm->m_visitors.insert(id, boost::bind(&StateType::accept, \
  2147. ::boost::ref(astate) BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, MSM_VISITOR_ARGS_SUB, ~) )); \
  2148. } \
  2149. template <class State> \
  2150. static typename enable_if_c<is_composite_state<State>::value,void >::type \
  2151. helper (library_sm* sm, \
  2152. int id,StateType& astate) \
  2153. { \
  2154. void (StateType::*caccept)(BOOST_PP_ENUM(n, MSM_VISITOR_ARGS_TYPEDEF_SUB, ~ ) ) \
  2155. = &StateType::composite_accept; \
  2156. sm->m_visitors.insert(id, boost::bind(caccept, \
  2157. ::boost::ref(astate) BOOST_PP_COMMA_IF(n) BOOST_PP_ENUM(n, MSM_VISITOR_ARGS_SUB, ~) )); \
  2158. } \
  2159. };
  2160. BOOST_PP_REPEAT(BOOST_PP_ADD(BOOST_MSM_VISITOR_ARG_SIZE,1), MSM_VISITOR_ARGS_EXECUTE, ~)
  2161. #undef MSM_VISITOR_ARGS_EXECUTE
  2162. #undef MSM_VISITOR_ARGS_SUB
  2163. // the IBM compiler seems to have problems with nested classes
  2164. // the same seems to apply to the Apple version of gcc 4.0.1 (just in case we do for < 4.1)
  2165. // and also to MS VC < 8
  2166. #if defined (__IBMCPP__) || (__GNUC__ == 4 && __GNUC_MINOR__ < 1) || (defined(_MSC_VER) && (_MSC_VER < 1400))
  2167. public:
  2168. #endif
  2169. template<class ContainingSM>
  2170. void set_containing_sm(ContainingSM* sm)
  2171. {
  2172. m_is_included=true;
  2173. ::boost::fusion::for_each(m_substate_list,add_state<ContainingSM>(this,sm));
  2174. }
  2175. #if defined (__IBMCPP__) || (__GNUC__ == 4 && __GNUC_MINOR__ < 1) || (defined(_MSC_VER) && (_MSC_VER < 1400))
  2176. private:
  2177. #endif
  2178. // A function object for use with mpl::for_each that stuffs
  2179. // states into the state list.
  2180. template<class ContainingSM>
  2181. struct add_state
  2182. {
  2183. add_state(library_sm* self_,ContainingSM* sm)
  2184. : self(self_),containing_sm(sm){}
  2185. // State is a sub fsm with exit pseudo states and gets a pointer to this fsm, so it can build a callback
  2186. template <class StateType>
  2187. typename ::boost::enable_if<
  2188. typename is_composite_state<StateType>::type,void >::type
  2189. new_state_helper(boost::msm::back::dummy<0> = 0) const
  2190. {
  2191. ::boost::fusion::at_key<StateType>(self->m_substate_list).set_containing_sm(containing_sm);
  2192. }
  2193. // State is a sub fsm without exit pseudo states and does not get a callback to this fsm
  2194. // or state is a normal state and needs nothing except creation
  2195. template <class StateType>
  2196. typename ::boost::enable_if<
  2197. typename boost::mpl::and_<typename boost::mpl::not_
  2198. <typename is_composite_state<StateType>::type>::type,
  2199. typename boost::mpl::not_
  2200. <typename is_pseudo_exit<StateType>::type>::type
  2201. >::type,void>::type
  2202. new_state_helper( ::boost::msm::back::dummy<1> = 0) const
  2203. {
  2204. //nothing to do
  2205. }
  2206. // state is exit pseudo state and gets callback to target fsm
  2207. template <class StateType>
  2208. typename ::boost::enable_if<typename is_pseudo_exit<StateType>::type,void >::type
  2209. new_state_helper( ::boost::msm::back::dummy<2> = 0) const
  2210. {
  2211. execute_return (ContainingSM::*pf) (typename StateType::event const& evt)=
  2212. &ContainingSM::process_event;
  2213. ::boost::function<execute_return (typename StateType::event const&)> fct =
  2214. ::boost::bind(pf,containing_sm,::boost::placeholders::_1);
  2215. ::boost::fusion::at_key<StateType>(self->m_substate_list).set_forward_fct(fct);
  2216. }
  2217. // for every defined state in the sm
  2218. template <class State>
  2219. void operator()( State const&) const
  2220. {
  2221. //create a new state with the defined id and type
  2222. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,State>::value));
  2223. this->new_state_helper<State>(),
  2224. create_state_helper<State>::set_sm(self);
  2225. // create a visitor callback
  2226. visitor_helper(state_id,::boost::fusion::at_key<State>(self->m_substate_list),
  2227. ::boost::mpl::bool_<has_accept_sig<State>::type::value>());
  2228. }
  2229. private:
  2230. // support possible use of a visitor if accept_sig is defined
  2231. template <class StateType>
  2232. void visitor_helper(int id,StateType& astate, ::boost::mpl::true_ const & ) const
  2233. {
  2234. visitor_args<StateType,StateType::accept_sig::args_number>::
  2235. template helper<StateType>(self,id,astate);
  2236. }
  2237. template <class StateType>
  2238. void visitor_helper(int ,StateType& , ::boost::mpl::false_ const &) const
  2239. {
  2240. // nothing to do
  2241. }
  2242. library_sm* self;
  2243. ContainingSM* containing_sm;
  2244. };
  2245. // helper used to copy every state if needed
  2246. struct copy_helper
  2247. {
  2248. copy_helper(library_sm* sm):
  2249. m_sm(sm){}
  2250. template <class StateType>
  2251. void operator()( ::boost::msm::wrap<StateType> const& )
  2252. {
  2253. BOOST_STATIC_CONSTANT(int, state_id = (get_state_id<stt,StateType>::type::value));
  2254. // possibly also set the visitor
  2255. visitor_helper<StateType>(state_id);
  2256. // and for states that keep a pointer to the fsm, reset the pointer
  2257. create_state_helper<StateType>::set_sm(m_sm);
  2258. }
  2259. template <class StateType>
  2260. typename ::boost::enable_if<typename has_accept_sig<StateType>::type,void >::type
  2261. visitor_helper(int id) const
  2262. {
  2263. visitor_args<StateType,StateType::accept_sig::args_number>::template helper<StateType>
  2264. (m_sm,id,::boost::fusion::at_key<StateType>(m_sm->m_substate_list));
  2265. }
  2266. template <class StateType>
  2267. typename ::boost::disable_if<typename has_accept_sig<StateType>::type,void >::type
  2268. visitor_helper(int) const
  2269. {
  2270. // nothing to do
  2271. }
  2272. library_sm* m_sm;
  2273. };
  2274. // helper to copy the active states attribute
  2275. template <class region_id,int Dummy=0>
  2276. struct region_copy_helper
  2277. {
  2278. static void do_copy(library_sm* self_,library_sm const& rhs)
  2279. {
  2280. self_->m_states[region_id::value] = rhs.m_states[region_id::value];
  2281. region_copy_helper< ::boost::mpl::int_<region_id::value+1> >::do_copy(self_,rhs);
  2282. }
  2283. };
  2284. template <int Dummy>
  2285. struct region_copy_helper< ::boost::mpl::int_<nr_regions::value>,Dummy>
  2286. {
  2287. // end of processing
  2288. static void do_copy(library_sm*,library_sm const& ){}
  2289. };
  2290. // copy functions for deep copy (no need of a 2nd version for NoCopy as noncopyable handles it)
  2291. void do_copy (library_sm const& rhs,
  2292. ::boost::msm::back::dummy<0> = 0)
  2293. {
  2294. // deep copy simply assigns the data
  2295. region_copy_helper< ::boost::mpl::int_<0> >::do_copy(this,rhs);
  2296. m_events_queue = rhs.m_events_queue;
  2297. m_deferred_events_queue = rhs.m_deferred_events_queue;
  2298. m_history = rhs.m_history;
  2299. m_event_processing = rhs.m_event_processing;
  2300. m_is_included = rhs.m_is_included;
  2301. m_substate_list = rhs.m_substate_list;
  2302. // except for the states themselves, which get duplicated
  2303. ::boost::mpl::for_each<state_list, ::boost::msm::wrap< ::boost::mpl::placeholders::_1> >
  2304. (copy_helper(this));
  2305. }
  2306. // helper used to call the correct entry/exit method
  2307. // unfortunately in O(number of states in the sub-sm) but should be better than a virtual call
  2308. template<class Event,bool is_entry>
  2309. struct entry_exit_helper
  2310. {
  2311. entry_exit_helper(int id,Event const& e,library_sm* self_):
  2312. state_id(id),evt(e),self(self_){}
  2313. // helper for entry actions
  2314. template <class IsEntry,class State>
  2315. typename ::boost::enable_if<typename IsEntry::type,void >::type
  2316. helper( ::boost::msm::back::dummy<0> = 0)
  2317. {
  2318. BOOST_STATIC_CONSTANT(int, id = (get_state_id<stt,State>::value));
  2319. if (id == state_id)
  2320. {
  2321. execute_entry<State>(::boost::fusion::at_key<State>(self->m_substate_list),evt,*self);
  2322. }
  2323. }
  2324. // helper for exit actions
  2325. template <class IsEntry,class State>
  2326. typename boost::disable_if<typename IsEntry::type,void >::type
  2327. helper( ::boost::msm::back::dummy<1> = 0)
  2328. {
  2329. BOOST_STATIC_CONSTANT(int, id = (get_state_id<stt,State>::value));
  2330. if (id == state_id)
  2331. {
  2332. execute_exit<State>(::boost::fusion::at_key<State>(self->m_substate_list),evt,*self);
  2333. }
  2334. }
  2335. // iterates through all states to find the one to be activated
  2336. template <class State>
  2337. void operator()( ::boost::msm::wrap<State> const&)
  2338. {
  2339. entry_exit_helper<Event,is_entry>::template helper< ::boost::mpl::bool_<is_entry>,State >();
  2340. }
  2341. private:
  2342. int state_id;
  2343. Event const& evt;
  2344. library_sm* self;
  2345. };
  2346. // helper to start the fsm
  2347. template <class region_id,int Dummy=0>
  2348. struct region_start_helper
  2349. {
  2350. template<class Event>
  2351. static void do_start(library_sm* self_,Event const& incomingEvent)
  2352. {
  2353. //forward the event for handling by sub state machines
  2354. ::boost::mpl::for_each<state_list, ::boost::msm::wrap< ::boost::mpl::placeholders::_1> >
  2355. (entry_exit_helper<Event,true>(self_->m_states[region_id::value],incomingEvent,self_));
  2356. region_start_helper
  2357. < ::boost::mpl::int_<region_id::value+1> >::do_start(self_,incomingEvent);
  2358. }
  2359. };
  2360. template <int Dummy>
  2361. struct region_start_helper< ::boost::mpl::int_<nr_regions::value>,Dummy>
  2362. {
  2363. // end of processing
  2364. template<class Event>
  2365. static void do_start(library_sm*,Event const& ){}
  2366. };
  2367. // start for states machines which are themselves embedded in other state machines (composites)
  2368. template <class Event>
  2369. void internal_start(Event const& incomingEvent)
  2370. {
  2371. region_start_helper< ::boost::mpl::int_<0> >::do_start(this,incomingEvent);
  2372. // give a chance to handle an anonymous (eventless) transition
  2373. handle_eventless_transitions_helper<library_sm> eventless_helper(this,true);
  2374. eventless_helper.process_completion_event();
  2375. }
  2376. template <class StateType>
  2377. struct find_region_id
  2378. {
  2379. template <int region,int Dummy=0>
  2380. struct In
  2381. {
  2382. enum {region_index=region};
  2383. };
  2384. // if the user provides no region, find it!
  2385. template<int Dummy>
  2386. struct In<-1,Dummy>
  2387. {
  2388. typedef typename build_orthogonal_regions<
  2389. library_sm,
  2390. initial_states
  2391. >::type all_regions;
  2392. enum {region_index= find_region_index<all_regions,StateType>::value };
  2393. };
  2394. enum {region_index = In<StateType::zone_index>::region_index };
  2395. };
  2396. // helper used to set the correct state as active state upon entry into a fsm
  2397. struct direct_event_start_helper
  2398. {
  2399. direct_event_start_helper(library_sm* self_):self(self_){}
  2400. // this variant is for the standard case, entry due to activation of the containing FSM
  2401. template <class EventType,class FsmType>
  2402. typename ::boost::disable_if<typename has_direct_entry<EventType>::type,void>::type
  2403. operator()(EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<0> = 0)
  2404. {
  2405. (static_cast<Derived*>(self))->on_entry(evt,fsm);
  2406. self->internal_start(evt);
  2407. }
  2408. // this variant is for the direct entry case (just one entry, not a sequence of entries)
  2409. template <class EventType,class FsmType>
  2410. typename ::boost::enable_if<
  2411. typename ::boost::mpl::and_<
  2412. typename ::boost::mpl::not_< typename is_pseudo_entry<
  2413. typename EventType::active_state>::type >::type,
  2414. typename ::boost::mpl::and_<typename has_direct_entry<EventType>::type,
  2415. typename ::boost::mpl::not_<typename ::boost::mpl::is_sequence
  2416. <typename EventType::active_state>::type >::type
  2417. >::type>::type,void
  2418. >::type
  2419. operator()(EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<1> = 0)
  2420. {
  2421. (static_cast<Derived*>(self))->on_entry(evt,fsm);
  2422. int state_id = get_state_id<stt,typename EventType::active_state::wrapped_entry>::value;
  2423. BOOST_STATIC_ASSERT(find_region_id<typename EventType::active_state::wrapped_entry>::region_index >= 0);
  2424. BOOST_STATIC_ASSERT(find_region_id<typename EventType::active_state::wrapped_entry>::region_index < nr_regions::value);
  2425. // just set the correct zone, the others will be default/history initialized
  2426. self->m_states[find_region_id<typename EventType::active_state::wrapped_entry>::region_index] = state_id;
  2427. self->internal_start(evt.m_event);
  2428. }
  2429. // this variant is for the fork entry case (a sequence on entries)
  2430. template <class EventType,class FsmType>
  2431. typename ::boost::enable_if<
  2432. typename ::boost::mpl::and_<
  2433. typename ::boost::mpl::not_<
  2434. typename is_pseudo_entry<typename EventType::active_state>::type >::type,
  2435. typename ::boost::mpl::and_<typename has_direct_entry<EventType>::type,
  2436. typename ::boost::mpl::is_sequence<
  2437. typename EventType::active_state>::type
  2438. >::type>::type,void
  2439. >::type
  2440. operator()(EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<2> = 0)
  2441. {
  2442. (static_cast<Derived*>(self))->on_entry(evt,fsm);
  2443. ::boost::mpl::for_each<typename EventType::active_state,
  2444. ::boost::msm::wrap< ::boost::mpl::placeholders::_1> >
  2445. (fork_helper<EventType>(self,evt));
  2446. // set the correct zones, the others (if any) will be default/history initialized
  2447. self->internal_start(evt.m_event);
  2448. }
  2449. // this variant is for the pseudo state entry case
  2450. template <class EventType,class FsmType>
  2451. typename ::boost::enable_if<
  2452. typename is_pseudo_entry<typename EventType::active_state >::type,void
  2453. >::type
  2454. operator()(EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<3> = 0)
  2455. {
  2456. // entry on the FSM
  2457. (static_cast<Derived*>(self))->on_entry(evt,fsm);
  2458. int state_id = get_state_id<stt,typename EventType::active_state::wrapped_entry>::value;
  2459. BOOST_STATIC_ASSERT(find_region_id<typename EventType::active_state::wrapped_entry>::region_index >= 0);
  2460. BOOST_STATIC_ASSERT(find_region_id<typename EventType::active_state::wrapped_entry>::region_index < nr_regions::value);
  2461. // given region starts with the entry pseudo state as active state
  2462. self->m_states[find_region_id<typename EventType::active_state::wrapped_entry>::region_index] = state_id;
  2463. self->internal_start(evt.m_event);
  2464. // and we process the transition in the zone of the newly active state
  2465. // (entry pseudo states are, according to UML, a state connecting 1 transition outside to 1 inside
  2466. self->process_event(evt.m_event);
  2467. }
  2468. private:
  2469. // helper for the fork case, does almost like the direct entry
  2470. library_sm* self;
  2471. template <class EventType>
  2472. struct fork_helper
  2473. {
  2474. fork_helper(library_sm* self_,EventType const& evt_):
  2475. helper_self(self_),helper_evt(evt_){}
  2476. template <class StateType>
  2477. void operator()( ::boost::msm::wrap<StateType> const& )
  2478. {
  2479. int state_id = get_state_id<stt,typename StateType::wrapped_entry>::value;
  2480. BOOST_STATIC_ASSERT(find_region_id<typename StateType::wrapped_entry>::region_index >= 0);
  2481. BOOST_STATIC_ASSERT(find_region_id<typename StateType::wrapped_entry>::region_index < nr_regions::value);
  2482. helper_self->m_states[find_region_id<typename StateType::wrapped_entry>::region_index] = state_id;
  2483. }
  2484. private:
  2485. library_sm* helper_self;
  2486. EventType const& helper_evt;
  2487. };
  2488. };
  2489. // helper for entry
  2490. template <class region_id,int Dummy=0>
  2491. struct region_entry_exit_helper
  2492. {
  2493. template<class Event>
  2494. static void do_entry(library_sm* self_,Event const& incomingEvent)
  2495. {
  2496. self_->m_states[region_id::value] =
  2497. self_->m_history.history_entry(incomingEvent)[region_id::value];
  2498. region_entry_exit_helper
  2499. < ::boost::mpl::int_<region_id::value+1> >::do_entry(self_,incomingEvent);
  2500. }
  2501. template<class Event>
  2502. static void do_exit(library_sm* self_,Event const& incomingEvent)
  2503. {
  2504. ::boost::mpl::for_each<state_list, ::boost::msm::wrap< ::boost::mpl::placeholders::_1> >
  2505. (entry_exit_helper<Event,false>(self_->m_states[region_id::value],incomingEvent,self_));
  2506. region_entry_exit_helper
  2507. < ::boost::mpl::int_<region_id::value+1> >::do_exit(self_,incomingEvent);
  2508. }
  2509. };
  2510. template <int Dummy>
  2511. struct region_entry_exit_helper< ::boost::mpl::int_<nr_regions::value>,Dummy>
  2512. {
  2513. // end of processing
  2514. template<class Event>
  2515. static void do_entry(library_sm*,Event const& ){}
  2516. template<class Event>
  2517. static void do_exit(library_sm*,Event const& ){}
  2518. };
  2519. // entry/exit for states machines which are themselves embedded in other state machines (composites)
  2520. template <class Event,class FsmType>
  2521. void do_entry(Event const& incomingEvent,FsmType& fsm)
  2522. {
  2523. // by default we activate the history/init states, can be overwritten by direct_event_start_helper
  2524. region_entry_exit_helper< ::boost::mpl::int_<0> >::do_entry(this,incomingEvent);
  2525. // block immediate handling of events
  2526. m_event_processing = true;
  2527. // if the event is generating a direct entry/fork, set the current state(s) to the direct state(s)
  2528. direct_event_start_helper(this)(incomingEvent,fsm);
  2529. // handle messages which were generated and blocked in the init calls
  2530. m_event_processing = false;
  2531. // look for deferred events waiting
  2532. handle_defer_helper<library_sm> defer_helper(m_deferred_events_queue);
  2533. defer_helper.do_handle_deferred(true);
  2534. process_message_queue(this);
  2535. }
  2536. template <class Event,class FsmType>
  2537. void do_exit(Event const& incomingEvent,FsmType& fsm)
  2538. {
  2539. // first recursively exit the sub machines
  2540. // forward the event for handling by sub state machines
  2541. region_entry_exit_helper< ::boost::mpl::int_<0> >::do_exit(this,incomingEvent);
  2542. // then call our own exit
  2543. (static_cast<Derived*>(this))->on_exit(incomingEvent,fsm);
  2544. // give the history a chance to handle this (or not).
  2545. m_history.history_exit(this->m_states);
  2546. // history decides what happens with deferred events
  2547. if (!m_history.process_deferred_events(incomingEvent))
  2548. {
  2549. clear_deferred_queue();
  2550. }
  2551. }
  2552. // the IBM and VC<8 compilers seem to have problems with the friend declaration of dispatch_table
  2553. #if defined (__IBMCPP__) || (defined(_MSC_VER) && (_MSC_VER < 1400))
  2554. public:
  2555. #endif
  2556. // no transition for event.
  2557. template <class Event>
  2558. static HandledEnum call_no_transition(library_sm& , int , int , Event const& )
  2559. {
  2560. return HANDLED_FALSE;
  2561. }
  2562. // no transition for event for internal transitions (not an error).
  2563. template <class Event>
  2564. static HandledEnum call_no_transition_internal(library_sm& , int , int , Event const& )
  2565. {
  2566. //// reject to give others a chance to handle
  2567. //return HANDLED_GUARD_REJECT;
  2568. return HANDLED_FALSE;
  2569. }
  2570. // called for deferred events. Address set in the dispatch_table at init
  2571. template <class Event>
  2572. static HandledEnum defer_transition(library_sm& fsm, int , int , Event const& e)
  2573. {
  2574. fsm.defer_event(e);
  2575. return HANDLED_DEFERRED;
  2576. }
  2577. // called for completion events. Default address set in the dispatch_table at init
  2578. // prevents no-transition detection for completion events
  2579. template <class Event>
  2580. static HandledEnum default_eventless_transition(library_sm&, int, int , Event const&)
  2581. {
  2582. return HANDLED_FALSE;
  2583. }
  2584. #if defined (__IBMCPP__) || (defined(_MSC_VER) && (_MSC_VER < 1400))
  2585. private:
  2586. #endif
  2587. // removes one event from the message queue and processes it
  2588. template <class StateType>
  2589. void process_message_queue(StateType*,
  2590. typename ::boost::disable_if<typename is_no_message_queue<StateType>::type,void >::type* = 0)
  2591. {
  2592. // Iteratively process all events from the message queue.
  2593. while (!m_events_queue.m_events_queue.empty())
  2594. {
  2595. transition_fct next = m_events_queue.m_events_queue.front();
  2596. m_events_queue.m_events_queue.pop_front();
  2597. next();
  2598. }
  2599. }
  2600. template <class StateType>
  2601. void process_message_queue(StateType*,
  2602. typename ::boost::enable_if<typename is_no_message_queue<StateType>::type,void >::type* = 0)
  2603. {
  2604. // nothing to process
  2605. }
  2606. // helper function. In cases where the event is wrapped (target is a direct entry states)
  2607. // we want to send only the real event to on_entry, not the wrapper.
  2608. template <class EventType>
  2609. static
  2610. typename boost::enable_if<typename has_direct_entry<EventType>::type,typename EventType::contained_event const& >::type
  2611. remove_direct_entry_event_wrapper(EventType const& evt,boost::msm::back::dummy<0> = 0)
  2612. {
  2613. return evt.m_event;
  2614. }
  2615. template <class EventType>
  2616. static typename boost::disable_if<typename has_direct_entry<EventType>::type,EventType const& >::type
  2617. remove_direct_entry_event_wrapper(EventType const& evt,boost::msm::back::dummy<1> = 0)
  2618. {
  2619. // identity. No wrapper
  2620. return evt;
  2621. }
  2622. // calls the entry/exit or on_entry/on_exit depending on the state type
  2623. // (avoids calling virtually)
  2624. // variant for FSMs
  2625. template <class StateType,class EventType,class FsmType>
  2626. static
  2627. typename boost::enable_if<typename is_composite_state<StateType>::type,void >::type
  2628. execute_entry(StateType& astate,EventType const& evt,FsmType& fsm,boost::msm::back::dummy<0> = 0)
  2629. {
  2630. // calls on_entry on the fsm then handles direct entries, fork, entry pseudo state
  2631. astate.do_entry(evt,fsm);
  2632. }
  2633. // variant for states
  2634. template <class StateType,class EventType,class FsmType>
  2635. static
  2636. typename ::boost::disable_if<
  2637. typename ::boost::mpl::or_<typename is_composite_state<StateType>::type,
  2638. typename is_pseudo_exit<StateType>::type >::type,void >::type
  2639. execute_entry(StateType& astate,EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<1> = 0)
  2640. {
  2641. // simple call to on_entry
  2642. astate.on_entry(remove_direct_entry_event_wrapper(evt),fsm);
  2643. }
  2644. // variant for exit pseudo states
  2645. template <class StateType,class EventType,class FsmType>
  2646. static
  2647. typename ::boost::enable_if<typename is_pseudo_exit<StateType>::type,void >::type
  2648. execute_entry(StateType& astate,EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<2> = 0)
  2649. {
  2650. // calls on_entry on the state then forward the event to the transition which should be defined inside the
  2651. // contained fsm
  2652. astate.on_entry(evt,fsm);
  2653. astate.forward_event(evt);
  2654. }
  2655. template <class StateType,class EventType,class FsmType>
  2656. static
  2657. typename ::boost::enable_if<typename is_composite_state<StateType>::type,void >::type
  2658. execute_exit(StateType& astate,EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<0> = 0)
  2659. {
  2660. astate.do_exit(evt,fsm);
  2661. }
  2662. template <class StateType,class EventType,class FsmType>
  2663. static
  2664. typename ::boost::disable_if<typename is_composite_state<StateType>::type,void >::type
  2665. execute_exit(StateType& astate,EventType const& evt,FsmType& fsm, ::boost::msm::back::dummy<1> = 0)
  2666. {
  2667. // simple call to on_exit
  2668. astate.on_exit(evt,fsm);
  2669. }
  2670. // helper allowing special handling of direct entries / fork
  2671. template <class StateType,class TargetType,class EventType,class FsmType>
  2672. static
  2673. typename ::boost::disable_if<
  2674. typename ::boost::mpl::or_<typename has_explicit_entry_state<TargetType>::type,
  2675. ::boost::mpl::is_sequence<TargetType> >::type,void>::type
  2676. convert_event_and_execute_entry(StateType& astate,EventType const& evt, FsmType& fsm, ::boost::msm::back::dummy<1> = 0)
  2677. {
  2678. // if the target is a normal state, do the standard entry handling
  2679. execute_entry<StateType>(astate,evt,fsm);
  2680. }
  2681. template <class StateType,class TargetType,class EventType,class FsmType>
  2682. static
  2683. typename ::boost::enable_if<
  2684. typename ::boost::mpl::or_<typename has_explicit_entry_state<TargetType>::type,
  2685. ::boost::mpl::is_sequence<TargetType> >::type,void >::type
  2686. convert_event_and_execute_entry(StateType& astate,EventType const& evt, FsmType& fsm, ::boost::msm::back::dummy<0> = 0)
  2687. {
  2688. // for the direct entry, pack the event in a wrapper so that we handle it differently during fsm entry
  2689. execute_entry(astate,msm::back::direct_entry_event<TargetType,EventType>(evt),fsm);
  2690. }
  2691. // creates all the states
  2692. template <class ContainingSM>
  2693. void fill_states(ContainingSM* containing_sm=0)
  2694. {
  2695. // checks that regions are truly orthogonal
  2696. FsmCheckPolicy::template check_orthogonality<library_sm>();
  2697. // checks that all states are reachable
  2698. FsmCheckPolicy::template check_unreachable_states<library_sm>();
  2699. BOOST_STATIC_CONSTANT(int, max_state = (mpl::size<state_list>::value));
  2700. // allocate the place without reallocation
  2701. m_visitors.fill_visitors(max_state);
  2702. ::boost::fusion::for_each(m_substate_list,add_state<ContainingSM>(this,containing_sm));
  2703. }
  2704. private:
  2705. template <class StateType,class Enable=void>
  2706. struct msg_queue_helper
  2707. {
  2708. public:
  2709. msg_queue_helper():m_events_queue(){}
  2710. events_queue_t m_events_queue;
  2711. };
  2712. template <class StateType>
  2713. struct msg_queue_helper<StateType,
  2714. typename ::boost::enable_if<typename is_no_message_queue<StateType>::type >::type>
  2715. {
  2716. };
  2717. template <class Fsm,class Stt, class Event, class Compile>
  2718. friend struct dispatch_table;
  2719. // data members
  2720. int m_states[nr_regions::value];
  2721. msg_queue_helper<library_sm> m_events_queue;
  2722. deferred_msg_queue_helper
  2723. <library_sm> m_deferred_events_queue;
  2724. concrete_history m_history;
  2725. bool m_event_processing;
  2726. bool m_is_included;
  2727. visitor_fct_helper<BaseState> m_visitors;
  2728. substate_list m_substate_list;
  2729. };
  2730. } } }// boost::msm::back
  2731. #endif //BOOST_MSM_BACK_STATEMACHINE_H