state_machine.hpp 118 KB

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