state_machine.hpp 131 KB

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