21 #ifndef G_STATE_MACHINE_H
22 #define G_STATE_MACHINE_H
108 template <
typename T,
typename State,
typename Event,
typename Arg = std::
string>
115 StateMachine( State s_start , State s_end , State s_same , State s_any ) ;
127 State
apply( T & t , Event event ,
const Arg & arg ) ;
143 State
state()
const ;
146 State
reset( State new_state ) ;
157 Transition(State s1,State s2,
Action a,State s3) :
158 from(s1) , to(s2) , alt(s3) , action(a) {}
160 typedef std::multimap<Event,Transition> Map ;
161 typedef typename Map::value_type Map_value_type ;
169 template <
typename T,
typename State,
typename Event,
typename Arg>
178 template <
typename T,
typename State,
typename Event,
typename Arg>
181 addTransition( event , from , to , action , to ) ;
184 template <
typename T,
typename State,
typename Event,
typename Arg>
187 if( to == m_any || alt == m_any )
188 throw Error(
"\"to any\" is invalid" ) ;
191 throw Error(
"\"from same\" is invalid" ) ;
193 if( to == m_end && alt != to )
194 throw Error(
"predicates on end-state transitions are invalid" ) ;
196 if( alt == m_end && to != m_end )
197 throw Error(
"false predicates cannot take you to the end state" ) ;
199 m_map.insert( Map_value_type( event , Transition(from,to,action,alt) ) ) ;
202 template <
typename T,
typename State,
typename Event,
typename Arg>
205 State old_state = m_state ;
206 m_state = new_state ;
210 template <
typename T,
typename State,
typename Event,
typename Arg>
216 template <
typename T,
typename State,
typename Event,
typename Arg>
219 State state = m_state ;
220 typename Map::iterator p = m_map.find(event) ;
221 for( ; p != m_map.end() && (*p).first == event ; ++p )
223 if( (*p).second.from == m_any || (*p).second.from == m_state )
225 State old_state = m_state ;
226 if( (*p).second.to != m_same )
227 state = m_state = (*p).second.to ;
231 bool predicate = true ;
232 (t.*((*p).second.action))( arg , predicate ) ;
234 if( state != end && !predicate )
236 State alt_state = (*p).second.alt ;
237 state = m_state = alt_state == m_same ? old_state : alt_state ;
State apply(T &t, Event event, const Arg &arg)
Applies an event.
StateMachine(State s_start, State s_end, State s_same, State s_any)
Constructor.
A class which holds a represention of the argc/argv command line array, and supports simple command-l...
void addTransition(Event event, State from, State to, Action action)
Adds a transition.
void(T::* Action)(const Arg &, bool &)
State reset(State new_state)
Sets the current state. Returns the old state.
State state() const
Returns the current state.
A finite state machine class template.
#define G_EXCEPTION_CLASS(class_name, description)