iPodSearch.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. // Copyright 2010 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. #include <set>
  11. #include <string>
  12. #include <iostream>
  13. #include <boost/msm/back/state_machine.hpp>
  14. #include <boost/msm/front/state_machine_def.hpp>
  15. using namespace std;
  16. namespace msm = boost::msm;
  17. namespace // Concrete FSM implementation
  18. {
  19. // events
  20. struct OneSong
  21. {
  22. OneSong(string const& asong):m_Song(asong){}
  23. const string& get_data() const {return m_Song;}
  24. private:
  25. string m_Song;
  26. };
  27. template <class DATA>
  28. struct NotFound
  29. {
  30. DATA get_data() const {return m_Data;}
  31. DATA m_Data;
  32. };
  33. template <class DATA>
  34. struct Found
  35. {
  36. DATA get_data() const {return m_Data;}
  37. DATA m_Data;
  38. };
  39. struct Done {};
  40. template <class Container,class BASE_TYPE,class FSMType>
  41. struct Insert : public boost::msm::front::state<BASE_TYPE,boost::msm::front::sm_ptr>
  42. {
  43. template <class Event,class FSM>
  44. void on_entry(Event const& evt,FSM& )
  45. {
  46. //TODO other containers
  47. if (m_Cont)
  48. {
  49. m_Cont->insert(evt.get_data());
  50. }
  51. m_fsm->process_event(Done());
  52. }
  53. void set_sm_ptr(FSMType* fsm){m_fsm=fsm;}
  54. void set_container(Container* cont){m_Cont=cont;}
  55. Container* m_Cont;
  56. private:
  57. FSMType* m_fsm;
  58. };
  59. template <class BASE_TYPE,class FSMType>
  60. struct StringFind : public boost::msm::front::state<BASE_TYPE,boost::msm::front::sm_ptr>
  61. {
  62. template <class Event,class FSM>
  63. void on_entry(Event const& evt,FSM& )
  64. {
  65. //TODO other containers
  66. // if the element in the event is found
  67. if (evt.get_data().find(m_Cont) != std::string::npos )
  68. {
  69. Found<std::string> res;
  70. res.m_Data = evt.get_data();
  71. m_fsm->process_event(res);
  72. }
  73. // data not found
  74. else
  75. {
  76. NotFound<std::string> res;
  77. res.m_Data = evt.get_data();
  78. m_fsm->process_event(res);
  79. }
  80. }
  81. void set_sm_ptr(FSMType* fsm){m_fsm=fsm;}
  82. void set_container(const char* cont){m_Cont=cont;}
  83. private:
  84. std::string m_Cont;
  85. FSMType* m_fsm;
  86. };
  87. template <class EventType,class Container,class BASE_TYPE,class FSMType>
  88. struct Foreach : public boost::msm::front::state<BASE_TYPE,boost::msm::front::sm_ptr>
  89. {
  90. template <class Event,class FSM>
  91. void on_entry(Event const& evt,FSM& )
  92. {
  93. //TODO other containers
  94. if (!m_Cont.empty())
  95. {
  96. typename Container::value_type next_event = *m_Cont.begin();
  97. m_Cont.erase(m_Cont.begin());
  98. m_fsm->process_event(EventType(next_event));
  99. }
  100. }
  101. void set_sm_ptr(FSMType* fsm){m_fsm=fsm;}
  102. void set_container(Container* cont){m_Cont=*cont;}
  103. private:
  104. Container m_Cont;
  105. FSMType* m_fsm;
  106. };
  107. // Concrete FSM implementation
  108. struct iPodSearch_ : public msm::front::state_machine_def<iPodSearch_>
  109. {
  110. typedef msm::back::state_machine<iPodSearch_> iPodSearch;
  111. // The list of FSM states
  112. typedef std::set<std::string> Songset;
  113. typedef Insert<Songset,boost::msm::front::default_base_state,iPodSearch> MyInsert;
  114. typedef StringFind<boost::msm::front::default_base_state,iPodSearch> MyFind;
  115. typedef Foreach<OneSong,Songset,boost::msm::front::default_base_state,iPodSearch> MyForeach;
  116. // the initial state of the player SM. Must be defined
  117. typedef MyForeach initial_state;
  118. // transition actions
  119. // guard conditions
  120. typedef iPodSearch_ fsm; // makes transition table cleaner
  121. // Transition table for player
  122. struct transition_table : mpl::vector4<
  123. // Start Event Next Action Guard
  124. // +-----------+------------------+------------+---------------------+----------------------+
  125. _row < MyForeach , OneSong , MyFind >,
  126. _row < MyFind , NotFound<string> , MyForeach >,
  127. _row < MyFind , Found<string> , MyInsert >,
  128. _row < MyInsert , Done , MyForeach >
  129. // +-----------+------------------+------------+---------------------+----------------------+
  130. > {};
  131. iPodSearch_():m_AllSongs(),m_ResultSearch()
  132. {
  133. // add a few songs for testing
  134. m_AllSongs.insert("Let it be");
  135. m_AllSongs.insert("Yellow submarine");
  136. m_AllSongs.insert("Twist and Shout");
  137. m_AllSongs.insert("She Loves You");
  138. }
  139. template <class Event,class FSM>
  140. void on_entry(Event const&,FSM& fsm)
  141. {
  142. fsm.template get_state<MyForeach&>().set_container(&m_AllSongs);
  143. fsm.template get_state<MyInsert&>().set_container(&m_ResultSearch);
  144. }
  145. const Songset& get_result(){return m_ResultSearch;}
  146. void reset_search(){m_ResultSearch.clear();}
  147. // Replaces the default no-transition response.
  148. template <class FSM,class Event>
  149. void no_transition(Event const& e, FSM&,int state)
  150. {
  151. std::cout << "no transition from state " << state
  152. << " on event " << typeid(e).name() << std::endl;
  153. }
  154. private:
  155. Songset m_AllSongs;
  156. Songset m_ResultSearch;
  157. };
  158. typedef msm::back::state_machine<iPodSearch_> iPodSearch;
  159. void test()
  160. {
  161. iPodSearch search;
  162. // look for "She Loves You" using the first letters
  163. search.get_state<iPodSearch::MyFind*>()->set_container("Sh");// will find 2 songs
  164. // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
  165. search.start();
  166. // display all the songs
  167. const iPodSearch::Songset& res = search.get_result();
  168. for (iPodSearch::Songset::const_iterator it = res.begin();it != res.end();++it)
  169. {
  170. cout << "candidate song:" << *it << endl;
  171. }
  172. cout << "search using more letters" << endl;
  173. // look for "She Loves You" using more letters
  174. search.reset_search();
  175. search.get_state<iPodSearch::MyFind*>()->set_container("She");// will find 1 song
  176. search.start();
  177. const iPodSearch::Songset& res2 = search.get_result();
  178. for (iPodSearch::Songset::const_iterator it = res2.begin();it != res2.end();++it)
  179. {
  180. cout << "candidate song:" << *it << endl;
  181. }
  182. }
  183. }
  184. int main()
  185. {
  186. test();
  187. return 0;
  188. }