]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/msm/doc/PDF/examples/iPodSearchEuml.cpp
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)
11 // same as iPodSearch.cpp but using eUML
12 // requires boost >= v1.40 because using mpl::string
17 #include <boost/msm/back/state_machine.hpp>
18 #include <boost/msm/front/euml/euml.hpp>
19 #include <boost/msm/front/euml/stl.hpp>
22 using namespace boost::msm::front::euml
;
23 namespace msm
= boost::msm
;
24 namespace mpl
= boost::mpl
;
26 // how long the timer will ring when countdown elapsed.
27 #define RINGING_TIME 5
29 namespace // Concrete FSM implementation
32 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(std::string
,m_song
)
33 BOOST_MSM_EUML_ATTRIBUTES((attributes_
<< m_song
), OneSongDef
)
34 struct OneSong_impl
: euml_event
<OneSong_impl
>,OneSongDef
37 OneSong_impl(const string
& asong
)
39 get_attribute(m_song
)=asong
;
41 OneSong_impl(const char* asong
)
43 get_attribute(m_song
)=asong
;
45 OneSong_impl(const OneSong_impl
& asong
)
47 get_attribute(m_song
)=asong
.get_attribute(m_song
);
49 const string
& get_data() const {return get_attribute(m_song
);}
51 OneSong_impl
const OneSong
;
53 // attribute definitions
54 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(vector
<OneSong_impl
>,m_src_container
)
55 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(vector
<OneSong_impl
>,m_tgt_container
)
56 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(std::string
,m_letters
)
57 BOOST_MSM_EUML_DECLARE_ATTRIBUTE(vector
<OneSong_impl
>::iterator
,m_src_it
)
59 // the same attribute name can be reused
60 BOOST_MSM_EUML_ATTRIBUTES((attributes_
<< m_song
), NotFoundDef
)
61 BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(NotFound
,NotFoundDef
)
63 BOOST_MSM_EUML_ATTRIBUTES((attributes_
<< m_song
), FoundDef
)
64 BOOST_MSM_EUML_EVENT_WITH_ATTRIBUTES(Found
,FoundDef
)
66 BOOST_MSM_EUML_EVENT(Done
)
68 // Concrete FSM implementation
70 // The list of FSM states
71 BOOST_MSM_EUML_STATE(( (push_back_(fsm_(m_tgt_container
),event_(m_song
))
75 BOOST_MSM_EUML_STATE(( if_then_else_( string_find_(event_(m_song
),state_(m_letters
)) != Npos_
<string
>() ,
76 process2_(Found
,event_(m_song
)),
77 process2_(NotFound
,event_(m_song
)) ) ,
79 attributes_
<< m_letters
),StringFind
)
81 BOOST_MSM_EUML_STATE(( if_then_( state_(m_src_it
) != end_(fsm_(m_src_container
)),
82 process2_(OneSong
,*(state_(m_src_it
)++)) ),
84 attributes_
<< m_src_it
),Foreach
)
86 // replaces the old transition table
87 BOOST_MSM_EUML_TRANSITION_TABLE((
88 StringFind
== Foreach
+ OneSong
,
89 Insert
== StringFind
+ Found
,
90 Foreach
== StringFind
+ NotFound
,
91 Foreach
== Insert
+ Done
92 // +------------------------------------------------------------------------------+
95 BOOST_MSM_EUML_ACTION(Log_No_Transition
)
97 template <class FSM
,class Event
>
98 void operator()(Event
const& e
,FSM
&,int state
)
100 std::cout
<< "no transition from state " << state
101 << " on event " << typeid(e
).name() << std::endl
;
104 // create a state machine "on the fly"
105 BOOST_MSM_EUML_DECLARE_STATE_MACHINE(( transition_table
, //STT
106 init_
<< Foreach
, // Init
108 clear_(fsm_(m_src_container
)), //clear source
109 clear_(fsm_(m_tgt_container
)), //clear results
110 push_back_(fsm_(m_src_container
),
111 String_
<mpl::string
<'Let ','it ','be'> >()),//add a song
112 push_back_(fsm_(m_src_container
),
113 String_
<mpl::string
<'Yell','ow s','ubma','rine'> >()),//add a song
114 push_back_(fsm_(m_src_container
),
115 String_
<mpl::string
<'Twis','t an','d Sh','out'> >()),//add a song
116 push_back_(fsm_(m_src_container
),
117 String_
<mpl::string
<'She ','love','s yo','u'> >()),//add a song
118 attribute_(substate_(Foreach()),m_src_it
)
119 = begin_(fsm_(m_src_container
)) //set the search begin
122 attributes_
<< m_src_container
// song list
123 << m_tgt_container
, // result
124 configure_
<< no_configure_
,
127 iPodSearch_
) //fsm name
130 // choice of back-end
131 typedef msm::back::state_machine
<iPodSearch_
> iPodSearch
;
137 // look for "She Loves You" using the first letters
138 search
.get_state
<BOOST_MSM_EUML_STATE_NAME(StringFind
)&>().get_attribute(m_letters
)="Sh";// will find 2 songs
140 // needed to start the highest-level SM. This will call on_entry and mark the start of the SM
142 // display all the songs
143 for (vector
<OneSong_impl
>::const_iterator it
= search
.get_attribute(m_tgt_container
).begin();
144 it
!= search
.get_attribute(m_tgt_container
).end();++it
)
146 cout
<< "candidate song:" << (*it
).get_attribute(m_song
) << endl
;
149 cout
<< "search using more letters" << endl
;
150 // look for "She Loves You" using more letters
151 search
.get_state
<BOOST_MSM_EUML_STATE_NAME(StringFind
)&>().get_attribute(m_letters
)="She";// will find 1 song
153 // display all the songs
154 for (vector
<OneSong_impl
>::const_iterator it
= search
.get_attribute(m_tgt_container
).begin();
155 it
!= search
.get_attribute(m_tgt_container
).end();++it
)
157 cout
<< "candidate song:" << (*it
).get_attribute(m_song
) << endl
;