ODIN
tjstate.h
1 /***************************************************************************
2  tjstate.h - description
3  -------------------
4  begin : Sun Sep 28 2003
5  copyright : (C) 2000-2021 by Thies H. Jochimsen
6  email : thies@jochimsen.de
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef TJSTATE_H
19 #define TJSTATE_H
20 
21 #include <tjutils/tjutils.h>
22 #include <tjutils/tjlabel.h>
23 #include <tjutils/tjlog.h>
24 
31 
32 class StateComponent {
33  public:
34  static const char* get_compName();
35 };
36 
38 
39 
40 template <class T> class State : public Labeled {
41 
42  public:
43  typedef bool (T::* transition) ();
44 
45  State(T* statemachine, const char* statelabel, State* prerequired_state, transition tr)
46  : Labeled(statelabel), machine(statemachine), pre_state(prerequired_state), trans(tr) {
47  Log<StateComponent> odinlog(this,"State()");
48  }
49 
50  bool obtain_state() {
51  Log<StateComponent> odinlog(this,"obtain_state");
52 
53 
54  // return if we are already in the desired state
55  if(machine->current_state==this) return true;
56 
57  // try a registered direct transition
58  if(machine->direct_transition(this)) return true;
59 
60 
61  if(pre_state) { // check whether a prerequisite state exists and obtain that state
62  if(!pre_state->obtain_state()) return false;
63  }
64 
65  bool result=(machine->*trans)(); // perform the transition
66  if(result) machine->current_state=this;
67  return result;
68  }
69 
70  private:
71  T* machine;
72  State* pre_state;
73  transition trans;
74 };
75 
77 
78 template <class T>
79 struct DirectTransition {
80 
81  DirectTransition(State<T>* init, State<T>* dest, typename State<T>::transition tr) : init_state(init), dest_state(dest), trans(tr) {
82  Log<StateComponent> odinlog("DirectTransition","DirectTransition()");
83  }
84 
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;}
88 
89  State<T>* init_state;
90  State<T>* dest_state;
91 
92  typename State<T>::transition trans;
93 
94 
95 };
96 
97 
99 
100 
101 template <class T>
102 class StateMachine {
103 
104  protected:
105  StateMachine(State<T>* first_state) : current_state(first_state) {
106  Log<StateComponent> odinlog("StateMachine","StateMachine()");
107  }
108 
109  const STD_string& get_current_state_label() const {return current_state->get_label();}
110 
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) );
113  }
114 
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;
122  return result;
123  }
124  }
125  return false;
126  }
127 
128  private:
129  friend class State<T>;
130 
131  STD_list<DirectTransition<T> > direct_trans;
132 
133  State<T>* current_state;
134 };
135 
136 
139 #endif
Definition: tjlog.h:218
bool operator!=(const TinyVector< T, N_rank > &t1, const TinyVector< T, N_rank > &t2)
Definition: utils.h:139
bool operator==(const TinyVector< T, N_rank > &t1, const TinyVector< T, N_rank > &t2)
Definition: utils.h:129
bool operator<(const STD_complex &c1, const STD_complex &c2)
Definition: tjcomplex.h:61