state_machine.hpp 126 KB

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