guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gstatechart.hpp
1 //
2 // Statecharts
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef GuitState_hpp
9 #define GuitState_hpp
10 #include <list>
11 #include <gbool.hpp>
12 #include <gcond.hpp>
13 namespace guit {
14 
15 class GState;
16 class GSuperState;
17 class GStateChart;
18 class GStateTransition;
19 
37 class GState : public GBool {
38 public:
39  ~GState();
40 
46  GState(GSuperState& parent, GString const& name = "");
47  GState(GString const& name = "");
49 
51  GState* clone(bool) const override {return nullptr;}
52 
63  auto operator~() {return GPropRef_<GState>(*this);}
64 
67  bool entered() const;
68  float floatValue() const override {return entered();}
69  GString stringValue() const override;
71 
73  GString const name() const;
74 
77  GSuperState* parent() const {return parent_;}
78 
88 
89  using GBool::add;
90  virtual void add(GStateTransition*);
91  using GBool::remove;
92  virtual void remove(GStateTransition*);
93  virtual void removeTransitions();
94  virtual void enableGadgets(bool enable);
95  bool isEquivalent(const GProp&) const override {return false;}
96 
97  using GType = GPropType_<GState,GBool>;
98  static GType& Type();
99  GType& type() const override {return Type();}
100 
103  GSuperState* parent_{};
104  GString* name_{};
105  std::list<gptr<GStateTransition>> transitions_;
106  std::forward_list<Gadget*> gadgets_;
107  virtual void fireState(bool on);
109 };
110 
112 GState& State(GSuperState& parent, GString const& name = "");
113 
114 
118 GCond& operator/(GStateTransition&, GFunction const& fun);
119 GCond& operator/(GStateTransition&, GString const& text);
120 GCond& operator/(GStateTransition&, GProp& prop);
121 GCond& operator/(GStateTransition&, GProp* prop);
123 
127 GCond& operator/(GTrigger&, GStateTransition&);
128 GCond& operator/(GBoolExpr&, GStateTransition&);
129 GCond& operator/(GCond&, GStateTransition&);
131 
132 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
133 
135 class GSuperState : public GState {
136 public:
137  ~GSuperState();
138 
141  GSuperState(GSuperState& parent, GString const& name = "");
142  GSuperState(GString const& name = "");
144 
146  void setInitial(GState&);
147  void setInitial(GState*);
148 
150  GState* currentState() const {return current_;}
151 
155  GSuperState& operator<<(GState& substate);
156  GSuperState& operator<<(GState* substate);
158 
161  GState& addState(GString const& name = "");
162 
164  void removeStates();
165 
166  void enableGadgets(bool enable) override;
167 
168  using GType = GPropType_<GSuperState,GState>;
169  static GType& Type();
170  GType& type() const override {return Type();}
171 
172 protected:
173  friend class GStateTransition;
174  virtual void setCurrent(GState*);
175  void fireState(bool on) override;
176  std::vector<gptr<GState>> states_;
177  GState *current_{nullptr}, *initial_{nullptr};
178 };
179 
181 GSuperState& SuperState(GSuperState&, GString const& name = "");
182 
183 
184 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
185 
188 class GStateChart : public GSuperState {
189 public:
190  GStateChart(GString const& name = "");
191 
193  void start(GState& initial_state);
194 
198  void start() {run(true);}
199  void stop() {run(false);}
200  void run(bool yes);
202 
203  using GType = GPropType_<GStateChart,GSuperState>;
204  static GType& Type();
205  GType& type() const override {return Type();}
206 };
207 
209 GStateChart& StateChart(GString const& name = "");
210 
211 
212 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
213 
216 class GStateTransition : public GObject {
217 public:
218  using IsGadgetChild = bool;
219 
221  GStateTransition(GState& state1, GState& state2);
222  ~GStateTransition();
223 
224  GString typeName() const override {return "StateTransition";}
225 
226 protected:
227  friend class GState;
228  friend class GCondTrans;
229  GState *state1_, *state2_;
230  class GCondTrans* condtrans_;
231  virtual bool canFire() const;
232  virtual void doFire(GEvent&);
233 };
234 
235 }
236 #endif