21 #include <tjutils/tjutils.h>
22 #include <tjutils/tjlabel.h>
23 #include <tjutils/tjlog.h>
32 class StateComponent {
34 static const char* get_compName();
40 template <
class T>
class State :
public Labeled {
43 typedef bool (T::* transition) ();
45 State(T* statemachine,
const char* statelabel, State* prerequired_state, transition tr)
46 :
Labeled(statelabel), machine(statemachine), pre_state(prerequired_state), trans(tr) {
55 if(machine->current_state==
this)
return true;
58 if(machine->direct_transition(
this))
return true;
62 if(!pre_state->obtain_state())
return false;
65 bool result=(machine->*trans)();
66 if(result) machine->current_state=
this;
79 struct DirectTransition {
81 DirectTransition(State<T>* init, State<T>* dest,
typename State<T>::transition tr) : init_state(init), dest_state(dest), trans(tr) {
85 bool operator == (
const DirectTransition<T>& dt)
const {
return init_state==dt.init_state && dest_state==dt.dest_state && trans==dt.trans;}
86 bool operator != (
const DirectTransition<T>& dt)
const {
return !(*
this==dt);}
87 bool operator < (
const DirectTransition<T>& dt)
const {
return init_state<dt.init_state && dest_state<dt.dest_state;}
92 typename State<T>::transition trans;
105 StateMachine(State<T>* first_state) : current_state(first_state) {
109 const STD_string& get_current_state_label()
const {
return current_state->get_label();}
111 void register_direct_trans(State<T>* init, State<T>* dest,
typename State<T>::transition tr) {
112 direct_trans.push_back( DirectTransition<T>(init,dest,tr) );
115 bool direct_transition(State<T>* dest) {
116 typename STD_list<DirectTransition<T> >::iterator it;
117 for(it=direct_trans.begin(); it!=direct_trans.end(); ++it) {
118 if( (it->init_state==current_state) && (it->dest_state==dest) ) {
119 typename State<T>::transition tt=it->trans;
120 bool result=((T*)(
this)->*tt)();
121 if(result) current_state=dest;
129 friend class State<T>;
131 STD_list<DirectTransition<T> > direct_trans;
133 State<T>* current_state;
bool operator!=(const TinyVector< T, N_rank > &t1, const TinyVector< T, N_rank > &t2)
bool operator==(const TinyVector< T, N_rank > &t1, const TinyVector< T, N_rank > &t2)
bool operator<(const STD_complex &c1, const STD_complex &c2)