state_machine.hpp 127 KB

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