state_machine.hpp 119 KB

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