metafunctions.hpp 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981
  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_METAFUNCTIONS_H
  11. #define BOOST_MSM_BACK_METAFUNCTIONS_H
  12. #include <algorithm>
  13. #include <boost/mpl/set.hpp>
  14. #include <boost/mpl/at.hpp>
  15. #include <boost/mpl/pair.hpp>
  16. #include <boost/mpl/map.hpp>
  17. #include <boost/mpl/int.hpp>
  18. #include <boost/mpl/has_xxx.hpp>
  19. #include <boost/mpl/find.hpp>
  20. #include <boost/mpl/count_if.hpp>
  21. #include <boost/mpl/fold.hpp>
  22. #include <boost/mpl/if.hpp>
  23. #include <boost/mpl/has_key.hpp>
  24. #include <boost/mpl/insert.hpp>
  25. #include <boost/mpl/next_prior.hpp>
  26. #include <boost/mpl/map.hpp>
  27. #include <boost/mpl/push_back.hpp>
  28. #include <boost/mpl/vector.hpp>
  29. #include <boost/mpl/is_sequence.hpp>
  30. #include <boost/mpl/size.hpp>
  31. #include <boost/mpl/transform.hpp>
  32. #include <boost/mpl/begin_end.hpp>
  33. #include <boost/mpl/bool.hpp>
  34. #include <boost/mpl/empty.hpp>
  35. #include <boost/mpl/identity.hpp>
  36. #include <boost/mpl/eval_if.hpp>
  37. #include <boost/mpl/insert_range.hpp>
  38. #include <boost/mpl/front.hpp>
  39. #include <boost/mpl/logical.hpp>
  40. #include <boost/mpl/plus.hpp>
  41. #include <boost/mpl/copy_if.hpp>
  42. #include <boost/mpl/back_inserter.hpp>
  43. #include <boost/mpl/transform.hpp>
  44. #include <boost/fusion/include/as_vector.hpp>
  45. #include <boost/fusion/include/insert_range.hpp>
  46. #include <boost/type_traits/is_same.hpp>
  47. #include <boost/utility/enable_if.hpp>
  48. #include <boost/msm/row_tags.hpp>
  49. // mpl_graph graph implementation and depth first search
  50. #include <boost/msm/mpl_graph/incidence_list_graph.hpp>
  51. #include <boost/msm/mpl_graph/depth_first_search.hpp>
  52. #include <boost/msm/back/traits.hpp>
  53. namespace boost { namespace msm { namespace back
  54. {
  55. template <typename Sequence, typename Range>
  56. struct set_insert_range
  57. {
  58. typedef typename ::boost::mpl::fold<
  59. Range,Sequence,
  60. ::boost::mpl::insert< ::boost::mpl::placeholders::_1, ::boost::mpl::placeholders::_2 >
  61. >::type type;
  62. };
  63. // returns the current state type of a transition
  64. template <class Transition>
  65. struct transition_source_type
  66. {
  67. typedef typename Transition::current_state_type type;
  68. };
  69. // returns the target state type of a transition
  70. template <class Transition>
  71. struct transition_target_type
  72. {
  73. typedef typename Transition::next_state_type type;
  74. };
  75. // helper functions for generate_state_ids
  76. // create a pair of a state and a passed id for source and target states
  77. template <class Id,class Transition>
  78. struct make_pair_source_state_id
  79. {
  80. typedef typename ::boost::mpl::pair<typename Transition::current_state_type,Id> type;
  81. };
  82. template <class Id,class Transition>
  83. struct make_pair_target_state_id
  84. {
  85. typedef typename ::boost::mpl::pair<typename Transition::next_state_type,Id> type;
  86. };
  87. // iterates through a transition table and automatically generates ids starting at 0
  88. // first the source states, transition up to down
  89. // then the target states, up to down
  90. template <class stt>
  91. struct generate_state_ids
  92. {
  93. typedef typename
  94. ::boost::mpl::fold<
  95. stt,::boost::mpl::pair< ::boost::mpl::map< >, ::boost::mpl::int_<0> >,
  96. ::boost::mpl::pair<
  97. ::boost::mpl::if_<
  98. ::boost::mpl::has_key< ::boost::mpl::first< ::boost::mpl::placeholders::_1>,
  99. transition_source_type< ::boost::mpl::placeholders::_2> >,
  100. ::boost::mpl::first< ::boost::mpl::placeholders::_1>,
  101. ::boost::mpl::insert< ::boost::mpl::first<mpl::placeholders::_1>,
  102. make_pair_source_state_id< ::boost::mpl::second< ::boost::mpl::placeholders::_1 >,
  103. ::boost::mpl::placeholders::_2> >
  104. >,
  105. ::boost::mpl::if_<
  106. ::boost::mpl::has_key< ::boost::mpl::first< ::boost::mpl::placeholders::_1>,
  107. transition_source_type< ::boost::mpl::placeholders::_2> >,
  108. ::boost::mpl::second< ::boost::mpl::placeholders::_1 >,
  109. ::boost::mpl::next< ::boost::mpl::second<mpl::placeholders::_1 > >
  110. >
  111. > //pair
  112. >::type source_state_ids;
  113. typedef typename ::boost::mpl::first<source_state_ids>::type source_state_map;
  114. typedef typename ::boost::mpl::second<source_state_ids>::type highest_state_id;
  115. typedef typename
  116. ::boost::mpl::fold<
  117. stt,::boost::mpl::pair<source_state_map,highest_state_id >,
  118. ::boost::mpl::pair<
  119. ::boost::mpl::if_<
  120. ::boost::mpl::has_key< ::boost::mpl::first< ::boost::mpl::placeholders::_1>,
  121. transition_target_type< ::boost::mpl::placeholders::_2> >,
  122. ::boost::mpl::first< ::boost::mpl::placeholders::_1>,
  123. ::boost::mpl::insert< ::boost::mpl::first< ::boost::mpl::placeholders::_1>,
  124. make_pair_target_state_id< ::boost::mpl::second< ::boost::mpl::placeholders::_1 >,
  125. ::boost::mpl::placeholders::_2> >
  126. >,
  127. ::boost::mpl::if_<
  128. ::boost::mpl::has_key< ::boost::mpl::first< ::boost::mpl::placeholders::_1>,
  129. transition_target_type< ::boost::mpl::placeholders::_2> >,
  130. ::boost::mpl::second< ::boost::mpl::placeholders::_1 >,
  131. ::boost::mpl::next< ::boost::mpl::second< ::boost::mpl::placeholders::_1 > >
  132. >
  133. > //pair
  134. >::type all_state_ids;
  135. typedef typename ::boost::mpl::first<all_state_ids>::type type;
  136. };
  137. template <class Fsm>
  138. struct get_active_state_switch_policy_helper
  139. {
  140. typedef typename Fsm::active_state_switch_policy type;
  141. };
  142. template <class Iter>
  143. struct get_active_state_switch_policy_helper2
  144. {
  145. typedef typename boost::mpl::deref<Iter>::type Fsm;
  146. typedef typename Fsm::active_state_switch_policy type;
  147. };
  148. // returns the active state switching policy
  149. template <class Fsm>
  150. struct get_active_state_switch_policy
  151. {
  152. typedef typename ::boost::mpl::find_if<
  153. typename Fsm::configuration,
  154. has_active_state_switch_policy< ::boost::mpl::placeholders::_1 > >::type iter;
  155. typedef typename ::boost::mpl::eval_if<
  156. typename ::boost::is_same<
  157. iter,
  158. typename ::boost::mpl::end<typename Fsm::configuration>::type
  159. >::type,
  160. get_active_state_switch_policy_helper<Fsm>,
  161. get_active_state_switch_policy_helper2< iter >
  162. >::type type;
  163. };
  164. // returns the id of a given state
  165. template <class stt,class State>
  166. struct get_state_id
  167. {
  168. typedef typename ::boost::mpl::at<typename generate_state_ids<stt>::type,State>::type type;
  169. enum {value = type::value};
  170. };
  171. // returns a mpl::vector containing the init states of a state machine
  172. template <class States>
  173. struct get_initial_states
  174. {
  175. typedef typename ::boost::mpl::if_<
  176. ::boost::mpl::is_sequence<States>,
  177. States,
  178. typename ::boost::mpl::push_back< ::boost::mpl::vector0<>,States>::type >::type type;
  179. };
  180. // returns a mpl::int_ containing the size of a region. If the argument is not a sequence, returns 1
  181. template <class region>
  182. struct get_number_of_regions
  183. {
  184. typedef typename mpl::if_<
  185. ::boost::mpl::is_sequence<region>,
  186. ::boost::mpl::size<region>,
  187. ::boost::mpl::int_<1> >::type type;
  188. };
  189. // builds a mpl::vector of initial states
  190. //TODO remove duplicate from get_initial_states
  191. template <class region>
  192. struct get_regions_as_sequence
  193. {
  194. typedef typename ::boost::mpl::if_<
  195. ::boost::mpl::is_sequence<region>,
  196. region,
  197. typename ::boost::mpl::push_back< ::boost::mpl::vector0<>,region>::type >::type type;
  198. };
  199. template <class ToCreateSeq>
  200. struct get_explicit_creation_as_sequence
  201. {
  202. typedef typename ::boost::mpl::if_<
  203. ::boost::mpl::is_sequence<ToCreateSeq>,
  204. ToCreateSeq,
  205. typename ::boost::mpl::push_back< ::boost::mpl::vector0<>,ToCreateSeq>::type >::type type;
  206. };
  207. // returns true if 2 transitions have the same source (used to remove duplicates in search of composite states)
  208. template <class stt,class Transition1,class Transition2>
  209. struct have_same_source
  210. {
  211. enum {current_state1 = get_state_id<stt,typename Transition1::current_state_type >::type::value};
  212. enum {current_state2 = get_state_id<stt,typename Transition2::current_state_type >::type::value};
  213. enum {value = ((int)current_state1 == (int)current_state2) };
  214. };
  215. // A metafunction that returns the Event associated with a transition.
  216. template <class Transition>
  217. struct transition_event
  218. {
  219. typedef typename Transition::transition_event type;
  220. };
  221. // returns true for composite states
  222. template <class State>
  223. struct is_composite_state
  224. {
  225. enum {value = has_composite_tag<State>::type::value};
  226. typedef typename has_composite_tag<State>::type type;
  227. };
  228. // transform a transition table in a container of source states
  229. template <class stt>
  230. struct keep_source_names
  231. {
  232. // instead of the rows we want only the names of the states (from source)
  233. typedef typename
  234. ::boost::mpl::transform<
  235. stt,transition_source_type< ::boost::mpl::placeholders::_1> >::type type;
  236. };
  237. // transform a transition table in a container of target states
  238. template <class stt>
  239. struct keep_target_names
  240. {
  241. // instead of the rows we want only the names of the states (from source)
  242. typedef typename
  243. ::boost::mpl::transform<
  244. stt,transition_target_type< ::boost::mpl::placeholders::_1> >::type type;
  245. };
  246. template <class stt>
  247. struct generate_state_set
  248. {
  249. // keep in the original transition table only the source/target state types
  250. typedef typename keep_source_names<stt>::type sources;
  251. typedef typename keep_target_names<stt>::type targets;
  252. typedef typename
  253. ::boost::mpl::fold<
  254. sources, ::boost::mpl::set<>,
  255. ::boost::mpl::insert< ::boost::mpl::placeholders::_1, ::boost::mpl::placeholders::_2>
  256. >::type source_set;
  257. typedef typename
  258. ::boost::mpl::fold<
  259. targets,source_set,
  260. ::boost::mpl::insert< ::boost::mpl::placeholders::_1, ::boost::mpl::placeholders::_2>
  261. >::type type;
  262. };
  263. // iterates through the transition table and generate a mpl::set<> containing all the events
  264. template <class stt>
  265. struct generate_event_set
  266. {
  267. typedef typename
  268. ::boost::mpl::fold<
  269. stt, ::boost::mpl::set<>,
  270. ::boost::mpl::if_<
  271. ::boost::mpl::has_key< ::boost::mpl::placeholders::_1,
  272. transition_event< ::boost::mpl::placeholders::_2> >,
  273. ::boost::mpl::placeholders::_1,
  274. ::boost::mpl::insert< ::boost::mpl::placeholders::_1,
  275. transition_event< ::boost::mpl::placeholders::_2> > >
  276. >::type type;
  277. };
  278. // returns a mpl::bool_<true> if State has Event as deferred event
  279. template <class State, class Event>
  280. struct has_state_delayed_event
  281. {
  282. typedef typename ::boost::mpl::find<typename State::deferred_events,Event>::type found;
  283. typedef typename ::boost::mpl::if_<
  284. ::boost::is_same<found,typename ::boost::mpl::end<typename State::deferred_events>::type >,
  285. ::boost::mpl::bool_<false>,
  286. ::boost::mpl::bool_<true> >::type type;
  287. };
  288. // returns a mpl::bool_<true> if State has any deferred event
  289. template <class State>
  290. struct has_state_delayed_events
  291. {
  292. typedef typename ::boost::mpl::if_<
  293. ::boost::mpl::empty<typename State::deferred_events>,
  294. ::boost::mpl::bool_<false>,
  295. ::boost::mpl::bool_<true> >::type type;
  296. };
  297. // Template used to create dummy entries for initial states not found in the stt.
  298. template< typename T1 >
  299. struct not_a_row
  300. {
  301. typedef int not_real_row_tag;
  302. struct dummy_event
  303. {
  304. };
  305. typedef T1 current_state_type;
  306. typedef T1 next_state_type;
  307. typedef dummy_event transition_event;
  308. };
  309. // metafunctions used to find out if a state is entry, exit or something else
  310. template <class State>
  311. struct is_pseudo_entry
  312. {
  313. typedef typename ::boost::mpl::if_< typename has_pseudo_entry<State>::type,
  314. ::boost::mpl::bool_<true>,::boost::mpl::bool_<false>
  315. >::type type;
  316. };
  317. // says if a state is an exit pseudo state
  318. template <class State>
  319. struct is_pseudo_exit
  320. {
  321. typedef typename ::boost::mpl::if_< typename has_pseudo_exit<State>::type,
  322. ::boost::mpl::bool_<true>, ::boost::mpl::bool_<false>
  323. >::type type;
  324. };
  325. // says if a state is an entry pseudo state or an explicit entry
  326. template <class State>
  327. struct is_direct_entry
  328. {
  329. typedef typename ::boost::mpl::if_< typename has_explicit_entry_state<State>::type,
  330. ::boost::mpl::bool_<true>, ::boost::mpl::bool_<false>
  331. >::type type;
  332. };
  333. //converts a "fake" (simulated in a state_machine_ description )state into one which will really get created
  334. template <class StateType,class CompositeType>
  335. struct convert_fake_state
  336. {
  337. // converts a state (explicit entry) into the state we really are going to create (explicit<>)
  338. typedef typename ::boost::mpl::if_<
  339. typename is_direct_entry<StateType>::type,
  340. typename CompositeType::template direct<StateType>,
  341. typename ::boost::mpl::identity<StateType>::type
  342. >::type type;
  343. };
  344. template <class StateType>
  345. struct get_explicit_creation
  346. {
  347. typedef typename StateType::explicit_creation type;
  348. };
  349. template <class StateType>
  350. struct get_wrapped_entry
  351. {
  352. typedef typename StateType::wrapped_entry type;
  353. };
  354. // used for states created with explicit_creation
  355. // if the state is an explicit entry, we reach for the wrapped state
  356. // otherwise, this returns the state itself
  357. template <class StateType>
  358. struct get_wrapped_state
  359. {
  360. typedef typename ::boost::mpl::eval_if<
  361. typename has_wrapped_entry<StateType>::type,
  362. get_wrapped_entry<StateType>,
  363. ::boost::mpl::identity<StateType> >::type type;
  364. };
  365. template <class Derived>
  366. struct create_stt
  367. {
  368. //typedef typename Derived::transition_table stt;
  369. typedef typename Derived::real_transition_table Stt;
  370. // get the state set
  371. typedef typename generate_state_set<Stt>::type states;
  372. // transform the initial region(s) in a sequence
  373. typedef typename get_regions_as_sequence<typename Derived::initial_state>::type init_states;
  374. // iterate through the initial states and add them in the stt if not already there
  375. typedef typename
  376. ::boost::mpl::fold<
  377. init_states,Stt,
  378. ::boost::mpl::if_<
  379. ::boost::mpl::has_key<states, ::boost::mpl::placeholders::_2>,
  380. ::boost::mpl::placeholders::_1,
  381. ::boost::mpl::insert< ::boost::mpl::placeholders::_1, ::boost::mpl::end< ::boost::mpl::placeholders::_1>,
  382. not_a_row< get_wrapped_state< ::boost::mpl::placeholders::_2> > >
  383. >
  384. >::type with_init;
  385. // do the same for states marked as explicitly created
  386. typedef typename get_explicit_creation_as_sequence<
  387. typename ::boost::mpl::eval_if<
  388. typename has_explicit_creation<Derived>::type,
  389. get_explicit_creation<Derived>,
  390. ::boost::mpl::vector0<> >::type
  391. >::type fake_explicit_created;
  392. typedef typename
  393. ::boost::mpl::transform<
  394. fake_explicit_created,convert_fake_state< ::boost::mpl::placeholders::_1,Derived> >::type explicit_created;
  395. typedef typename
  396. ::boost::mpl::fold<
  397. explicit_created,with_init,
  398. ::boost::mpl::if_<
  399. ::boost::mpl::has_key<states, ::boost::mpl::placeholders::_2>,
  400. ::boost::mpl::placeholders::_1,
  401. ::boost::mpl::insert< ::boost::mpl::placeholders::_1, ::boost::mpl::end<mpl::placeholders::_1>,
  402. not_a_row< get_wrapped_state< ::boost::mpl::placeholders::_2> > >
  403. >
  404. >::type type;
  405. };
  406. // returns the transition table of a Composite state
  407. template <class Composite>
  408. struct get_transition_table
  409. {
  410. typedef typename create_stt<Composite>::type type;
  411. };
  412. // recursively builds an internal table including those of substates, sub-substates etc.
  413. // variant for submachines
  414. template <class StateType,class IsComposite>
  415. struct recursive_get_internal_transition_table
  416. {
  417. // get the composite's internal table
  418. typedef typename StateType::internal_transition_table composite_table;
  419. // and for every substate (state of submachine), recursively get the internal transition table
  420. typedef typename generate_state_set<typename StateType::stt>::type composite_states;
  421. typedef typename ::boost::mpl::fold<
  422. composite_states, composite_table,
  423. ::boost::mpl::insert_range< ::boost::mpl::placeholders::_1, ::boost::mpl::end< ::boost::mpl::placeholders::_1>,
  424. recursive_get_internal_transition_table< ::boost::mpl::placeholders::_2, is_composite_state< ::boost::mpl::placeholders::_2> >
  425. >
  426. >::type type;
  427. };
  428. // stop iterating on leafs (simple states)
  429. template <class StateType>
  430. struct recursive_get_internal_transition_table<StateType, ::boost::mpl::false_ >
  431. {
  432. typedef typename StateType::internal_transition_table type;
  433. };
  434. // recursively get a transition table for a given composite state.
  435. // returns the transition table for this state + the tables of all composite sub states recursively
  436. template <class Composite>
  437. struct recursive_get_transition_table
  438. {
  439. // get the transition table of the state if it's a state machine
  440. typedef typename ::boost::mpl::eval_if<typename is_composite_state<Composite>::type,
  441. get_transition_table<Composite>,
  442. ::boost::mpl::vector0<>
  443. >::type org_table;
  444. typedef typename generate_state_set<org_table>::type states;
  445. // and for every substate, recursively get the transition table if it's a state machine
  446. typedef typename ::boost::mpl::fold<
  447. states,org_table,
  448. ::boost::mpl::insert_range< ::boost::mpl::placeholders::_1, ::boost::mpl::end<mpl::placeholders::_1>,
  449. recursive_get_transition_table< ::boost::mpl::placeholders::_2 > >
  450. >::type type;
  451. };
  452. // metafunction used to say if a SM has pseudo exit states
  453. template <class Derived>
  454. struct has_fsm_deferred_events
  455. {
  456. typedef typename create_stt<Derived>::type Stt;
  457. typedef typename generate_state_set<Stt>::type state_list;
  458. typedef typename ::boost::mpl::or_<
  459. typename has_activate_deferred_events<Derived>::type,
  460. ::boost::mpl::bool_< ::boost::mpl::count_if<
  461. typename Derived::configuration,
  462. has_activate_deferred_events< ::boost::mpl::placeholders::_1 > >::value != 0>
  463. >::type found_in_fsm;
  464. typedef typename ::boost::mpl::or_<
  465. found_in_fsm,
  466. ::boost::mpl::bool_< ::boost::mpl::count_if<
  467. state_list,has_state_delayed_events<
  468. ::boost::mpl::placeholders::_1 > >::value != 0>
  469. >::type type;
  470. };
  471. // returns a mpl::bool_<true> if State has any delayed event
  472. template <class Event>
  473. struct is_completion_event
  474. {
  475. typedef typename ::boost::mpl::if_<
  476. has_completion_event<Event>,
  477. ::boost::mpl::bool_<true>,
  478. ::boost::mpl::bool_<false> >::type type;
  479. };
  480. // metafunction used to say if a SM has eventless transitions
  481. template <class Derived>
  482. struct has_fsm_eventless_transition
  483. {
  484. typedef typename create_stt<Derived>::type Stt;
  485. typedef typename generate_event_set<Stt>::type event_list;
  486. typedef ::boost::mpl::bool_< ::boost::mpl::count_if<
  487. event_list,is_completion_event< ::boost::mpl::placeholders::_1 > >::value != 0> type;
  488. };
  489. template <class Derived>
  490. struct find_completion_events
  491. {
  492. typedef typename create_stt<Derived>::type Stt;
  493. typedef typename generate_event_set<Stt>::type event_list;
  494. typedef typename ::boost::mpl::fold<
  495. event_list, ::boost::mpl::set<>,
  496. ::boost::mpl::if_<
  497. is_completion_event< ::boost::mpl::placeholders::_2>,
  498. ::boost::mpl::insert< ::boost::mpl::placeholders::_1, ::boost::mpl::placeholders::_2 >,
  499. ::boost::mpl::placeholders::_1 >
  500. >::type type;
  501. };
  502. template <class Transition>
  503. struct make_vector
  504. {
  505. typedef ::boost::mpl::vector<Transition> type;
  506. };
  507. template< typename Entry >
  508. struct get_first_element_pair_second
  509. {
  510. typedef typename ::boost::mpl::front<typename Entry::second>::type type;
  511. };
  512. //returns the owner of an explicit_entry state
  513. //which is the containing SM if the transition originates from outside the containing SM
  514. //or else the explicit_entry state itself
  515. template <class State,class ContainingSM>
  516. struct get_owner
  517. {
  518. typedef typename ::boost::mpl::if_<
  519. typename ::boost::mpl::not_<typename ::boost::is_same<typename State::owner,
  520. ContainingSM >::type>::type,
  521. typename State::owner,
  522. State >::type type;
  523. };
  524. template <class Sequence,class ContainingSM>
  525. struct get_fork_owner
  526. {
  527. typedef typename ::boost::mpl::front<Sequence>::type seq_front;
  528. typedef typename ::boost::mpl::if_<
  529. typename ::boost::mpl::not_<
  530. typename ::boost::is_same<typename seq_front::owner,ContainingSM>::type>::type,
  531. typename seq_front::owner,
  532. seq_front >::type type;
  533. };
  534. template <class StateType,class ContainingSM>
  535. struct make_exit
  536. {
  537. typedef typename ::boost::mpl::if_<
  538. typename is_pseudo_exit<StateType>::type ,
  539. typename ContainingSM::template exit_pt<StateType>,
  540. typename ::boost::mpl::identity<StateType>::type
  541. >::type type;
  542. };
  543. template <class StateType,class ContainingSM>
  544. struct make_entry
  545. {
  546. typedef typename ::boost::mpl::if_<
  547. typename is_pseudo_entry<StateType>::type ,
  548. typename ContainingSM::template entry_pt<StateType>,
  549. typename ::boost::mpl::if_<
  550. typename is_direct_entry<StateType>::type,
  551. typename ContainingSM::template direct<StateType>,
  552. typename ::boost::mpl::identity<StateType>::type
  553. >::type
  554. >::type type;
  555. };
  556. // metafunction used to say if a SM has pseudo exit states
  557. template <class StateType>
  558. struct has_exit_pseudo_states_helper
  559. {
  560. typedef typename StateType::stt Stt;
  561. typedef typename generate_state_set<Stt>::type state_list;
  562. typedef ::boost::mpl::bool_< ::boost::mpl::count_if<
  563. state_list,is_pseudo_exit< ::boost::mpl::placeholders::_1> >::value != 0> type;
  564. };
  565. template <class StateType>
  566. struct has_exit_pseudo_states
  567. {
  568. typedef typename ::boost::mpl::eval_if<typename is_composite_state<StateType>::type,
  569. has_exit_pseudo_states_helper<StateType>,
  570. ::boost::mpl::bool_<false> >::type type;
  571. };
  572. // builds flags (add internal_flag_list and flag_list). internal_flag_list is used for terminate/interrupt states
  573. //template <class StateType>
  574. //struct get_flag_list
  575. //{
  576. // typedef typename ::boost::mpl::insert_range<
  577. // typename StateType::flag_list,
  578. // typename ::boost::mpl::end< typename StateType::flag_list >::type,
  579. // typename StateType::internal_flag_list
  580. // >::type type;
  581. //};
  582. template <class StateType>
  583. struct get_flag_list
  584. {
  585. typedef typename ::boost::fusion::result_of::as_vector<
  586. typename ::boost::fusion::result_of::insert_range<
  587. typename StateType::flag_list,
  588. typename ::boost::fusion::result_of::end< typename StateType::flag_list >::type,
  589. typename StateType::internal_flag_list
  590. >::type
  591. >::type type;
  592. };
  593. template <class StateType>
  594. struct is_state_blocking
  595. {
  596. typedef typename ::boost::mpl::fold<
  597. typename get_flag_list<StateType>::type, ::boost::mpl::set<>,
  598. ::boost::mpl::if_<
  599. has_event_blocking_flag< ::boost::mpl::placeholders::_2>,
  600. ::boost::mpl::insert< ::boost::mpl::placeholders::_1, ::boost::mpl::placeholders::_2 >,
  601. ::boost::mpl::placeholders::_1 >
  602. >::type blocking_flags;
  603. typedef typename ::boost::mpl::if_<
  604. ::boost::mpl::empty<blocking_flags>,
  605. ::boost::mpl::bool_<false>,
  606. ::boost::mpl::bool_<true> >::type type;
  607. };
  608. // returns a mpl::bool_<true> if fsm has an event blocking flag in one of its substates
  609. template <class StateType>
  610. struct has_fsm_blocking_states
  611. {
  612. typedef typename create_stt<StateType>::type Stt;
  613. typedef typename generate_state_set<Stt>::type state_list;
  614. typedef typename ::boost::mpl::fold<
  615. state_list, ::boost::mpl::set<>,
  616. ::boost::mpl::if_<
  617. is_state_blocking< ::boost::mpl::placeholders::_2>,
  618. ::boost::mpl::insert< ::boost::mpl::placeholders::_1, ::boost::mpl::placeholders::_2 >,
  619. ::boost::mpl::placeholders::_1 >
  620. >::type blocking_states;
  621. typedef typename ::boost::mpl::if_<
  622. ::boost::mpl::empty<blocking_states>,
  623. ::boost::mpl::bool_<false>,
  624. ::boost::mpl::bool_<true> >::type type;
  625. };
  626. template <class StateType>
  627. struct is_no_exception_thrown
  628. {
  629. typedef ::boost::mpl::bool_< ::boost::mpl::count_if<
  630. typename StateType::configuration,
  631. has_no_exception_thrown< ::boost::mpl::placeholders::_1 > >::value != 0> found;
  632. typedef typename ::boost::mpl::or_<
  633. typename has_no_exception_thrown<StateType>::type,
  634. found
  635. >::type type;
  636. };
  637. template <class StateType>
  638. struct is_no_message_queue
  639. {
  640. typedef ::boost::mpl::bool_< ::boost::mpl::count_if<
  641. typename StateType::configuration,
  642. has_no_message_queue< ::boost::mpl::placeholders::_1 > >::value != 0> found;
  643. typedef typename ::boost::mpl::or_<
  644. typename has_no_message_queue<StateType>::type,
  645. found
  646. >::type type;
  647. };
  648. template <class StateType>
  649. struct is_active_state_switch_policy
  650. {
  651. typedef ::boost::mpl::bool_< ::boost::mpl::count_if<
  652. typename StateType::configuration,
  653. has_active_state_switch_policy< ::boost::mpl::placeholders::_1 > >::value != 0> found;
  654. typedef typename ::boost::mpl::or_<
  655. typename has_active_state_switch_policy<StateType>::type,
  656. found
  657. >::type type;
  658. };
  659. template <class StateType>
  660. struct get_initial_event
  661. {
  662. typedef typename StateType::initial_event type;
  663. };
  664. template <class StateType>
  665. struct get_final_event
  666. {
  667. typedef typename StateType::final_event type;
  668. };
  669. template <class TransitionTable, class InitState>
  670. struct build_one_orthogonal_region
  671. {
  672. template<typename Row>
  673. struct row_to_incidence :
  674. ::boost::mpl::vector<
  675. ::boost::mpl::pair<
  676. typename Row::next_state_type,
  677. typename Row::transition_event>,
  678. typename Row::current_state_type,
  679. typename Row::next_state_type
  680. > {};
  681. template <class Seq, class Elt>
  682. struct transition_incidence_list_helper
  683. {
  684. typedef typename ::boost::mpl::push_back< Seq, row_to_incidence< Elt > >::type type;
  685. };
  686. typedef typename ::boost::mpl::fold<
  687. TransitionTable,
  688. ::boost::mpl::vector<>,
  689. transition_incidence_list_helper< ::boost::mpl::placeholders::_1, ::boost::mpl::placeholders::_2>
  690. >::type transition_incidence_list;
  691. typedef ::boost::msm::mpl_graph::incidence_list_graph<transition_incidence_list>
  692. transition_graph;
  693. struct preordering_dfs_visitor :
  694. ::boost::msm::mpl_graph::dfs_default_visitor_operations
  695. {
  696. template<typename Node, typename Graph, typename State>
  697. struct discover_vertex :
  698. ::boost::mpl::insert<State, Node>
  699. {};
  700. };
  701. typedef typename mpl::first<
  702. typename ::boost::msm::mpl_graph::depth_first_search<
  703. transition_graph,
  704. preordering_dfs_visitor,
  705. ::boost::mpl::set<>,
  706. InitState
  707. >::type
  708. >::type type;
  709. };
  710. template <class Fsm>
  711. struct find_entry_states
  712. {
  713. typedef typename ::boost::mpl::copy<
  714. typename Fsm::substate_list,
  715. ::boost::mpl::inserter<
  716. ::boost::mpl::set0<>,
  717. ::boost::mpl::if_<
  718. has_explicit_entry_state< ::boost::mpl::placeholders::_2 >,
  719. ::boost::mpl::insert< ::boost::mpl::placeholders::_1, ::boost::mpl::placeholders::_2>,
  720. ::boost::mpl::placeholders::_1
  721. >
  722. >
  723. >::type type;
  724. };
  725. template <class Set1, class Set2>
  726. struct is_common_element
  727. {
  728. typedef typename ::boost::mpl::fold<
  729. Set1, ::boost::mpl::false_,
  730. ::boost::mpl::if_<
  731. ::boost::mpl::has_key<
  732. Set2,
  733. ::boost::mpl::placeholders::_2
  734. >,
  735. ::boost::mpl::true_,
  736. ::boost::mpl::placeholders::_1
  737. >
  738. >::type type;
  739. };
  740. template <class EntryRegion, class AllRegions>
  741. struct add_entry_region
  742. {
  743. typedef typename ::boost::mpl::transform<
  744. AllRegions,
  745. ::boost::mpl::if_<
  746. is_common_element<EntryRegion, ::boost::mpl::placeholders::_1>,
  747. set_insert_range< ::boost::mpl::placeholders::_1, EntryRegion>,
  748. ::boost::mpl::placeholders::_1
  749. >
  750. >::type type;
  751. };
  752. // build a vector of regions states (as a set)
  753. // one set of states for every region
  754. template <class Fsm, class InitStates>
  755. struct build_orthogonal_regions
  756. {
  757. typedef typename
  758. ::boost::mpl::fold<
  759. InitStates, ::boost::mpl::vector0<>,
  760. ::boost::mpl::push_back<
  761. ::boost::mpl::placeholders::_1,
  762. build_one_orthogonal_region< typename Fsm::stt, ::boost::mpl::placeholders::_2 > >
  763. >::type without_entries;
  764. typedef typename
  765. ::boost::mpl::fold<
  766. typename find_entry_states<Fsm>::type, ::boost::mpl::vector0<>,
  767. ::boost::mpl::push_back<
  768. ::boost::mpl::placeholders::_1,
  769. build_one_orthogonal_region< typename Fsm::stt, ::boost::mpl::placeholders::_2 > >
  770. >::type only_entries;
  771. typedef typename ::boost::mpl::fold<
  772. only_entries , without_entries,
  773. add_entry_region< ::boost::mpl::placeholders::_2, ::boost::mpl::placeholders::_1>
  774. >::type type;
  775. };
  776. template <class GraphAsSeqOfSets, class StateType>
  777. struct find_region_index
  778. {
  779. typedef typename
  780. ::boost::mpl::fold<
  781. GraphAsSeqOfSets, ::boost::mpl::pair< ::boost::mpl::int_< -1 > /*res*/, ::boost::mpl::int_<0> /*counter*/ >,
  782. ::boost::mpl::if_<
  783. ::boost::mpl::has_key< ::boost::mpl::placeholders::_2, StateType >,
  784. ::boost::mpl::pair<
  785. ::boost::mpl::second< ::boost::mpl::placeholders::_1 >,
  786. ::boost::mpl::next< ::boost::mpl::second< ::boost::mpl::placeholders::_1 > >
  787. >,
  788. ::boost::mpl::pair<
  789. ::boost::mpl::first< ::boost::mpl::placeholders::_1 >,
  790. ::boost::mpl::next< ::boost::mpl::second< ::boost::mpl::placeholders::_1 > >
  791. >
  792. >
  793. >::type result_pair;
  794. typedef typename ::boost::mpl::first<result_pair>::type type;
  795. enum {value = type::value};
  796. };
  797. template <class Fsm>
  798. struct check_regions_orthogonality
  799. {
  800. typedef typename build_orthogonal_regions< Fsm,typename Fsm::initial_states>::type regions;
  801. typedef typename ::boost::mpl::fold<
  802. regions, ::boost::mpl::int_<0>,
  803. ::boost::mpl::plus< ::boost::mpl::placeholders::_1 , ::boost::mpl::size< ::boost::mpl::placeholders::_2> >
  804. >::type number_of_states_in_regions;
  805. typedef typename ::boost::mpl::fold<
  806. regions,mpl::set0<>,
  807. set_insert_range<
  808. ::boost::mpl::placeholders::_1,
  809. ::boost::mpl::placeholders::_2 >
  810. >::type one_big_states_set;
  811. enum {states_in_regions_raw = number_of_states_in_regions::value};
  812. enum {cumulated_states_in_regions_raw = ::boost::mpl::size<one_big_states_set>::value};
  813. };
  814. template <class Fsm>
  815. struct check_no_unreachable_state
  816. {
  817. typedef typename check_regions_orthogonality<Fsm>::one_big_states_set states_in_regions;
  818. typedef typename set_insert_range<
  819. states_in_regions,
  820. typename ::boost::mpl::eval_if<
  821. typename has_explicit_creation<Fsm>::type,
  822. get_explicit_creation<Fsm>,
  823. ::boost::mpl::vector0<>
  824. >::type
  825. >::type with_explicit_creation;
  826. enum {states_in_fsm = ::boost::mpl::size< typename Fsm::substate_list >::value};
  827. enum {cumulated_states_in_regions = ::boost::mpl::size< with_explicit_creation >::value};
  828. };
  829. // helper to find out if a SM has an active exit state and is therefore waiting for exiting
  830. template <class StateType,class OwnerFct,class FSM>
  831. inline
  832. typename ::boost::enable_if<typename ::boost::mpl::and_<typename is_composite_state<FSM>::type,
  833. typename is_pseudo_exit<StateType>::type>,bool >::type
  834. is_exit_state_active(FSM& fsm)
  835. {
  836. typedef typename OwnerFct::type Composite;
  837. //typedef typename create_stt<Composite>::type stt;
  838. typedef typename Composite::stt stt;
  839. int state_id = get_state_id<stt,StateType>::type::value;
  840. Composite& comp = fsm.template get_state<Composite&>();
  841. return (std::find(comp.current_state(),comp.current_state()+Composite::nr_regions::value,state_id)
  842. !=comp.current_state()+Composite::nr_regions::value);
  843. }
  844. template <class StateType,class OwnerFct,class FSM>
  845. inline
  846. typename ::boost::disable_if<typename ::boost::mpl::and_<typename is_composite_state<FSM>::type,
  847. typename is_pseudo_exit<StateType>::type>,bool >::type
  848. is_exit_state_active(FSM&)
  849. {
  850. return false;
  851. }
  852. // transformation metafunction to end interrupt flags
  853. template <class Event>
  854. struct transform_to_end_interrupt
  855. {
  856. typedef boost::msm::EndInterruptFlag<Event> type;
  857. };
  858. // transform a sequence of events into another one of EndInterruptFlag<Event>
  859. template <class Events>
  860. struct apply_end_interrupt_flag
  861. {
  862. typedef typename
  863. ::boost::mpl::transform<
  864. Events,transform_to_end_interrupt< ::boost::mpl::placeholders::_1> >::type type;
  865. };
  866. // returns a mpl vector containing all end interrupt events if sequence, otherwise the same event
  867. template <class Event>
  868. struct get_interrupt_events
  869. {
  870. typedef typename ::boost::mpl::eval_if<
  871. ::boost::mpl::is_sequence<Event>,
  872. boost::msm::back::apply_end_interrupt_flag<Event>,
  873. boost::mpl::vector1<boost::msm::EndInterruptFlag<Event> > >::type type;
  874. };
  875. template <class Events>
  876. struct build_interrupt_state_flag_list
  877. {
  878. typedef ::boost::mpl::vector<boost::msm::InterruptedFlag> first_part;
  879. typedef typename ::boost::mpl::insert_range<
  880. first_part,
  881. typename ::boost::mpl::end< first_part >::type,
  882. Events
  883. >::type type;
  884. };
  885. } } }//boost::msm::back
  886. #endif // BOOST_MSM_BACK_METAFUNCTIONS_H