guit  0.1
 All Classes Functions Variables Typedefs Enumerations Friends
gexpr.hpp
1 //
2 // Textual and numeric Expressions.
3 // Guit GUI Toolkit
4 // Copyright © 2019/2020 Eric Lecolinet. All rights reserved.
5 // http://www.telecom-paris.fr/~elc
6 //
7 
8 #ifndef Guit_Expr_hpp
9 #define Guit_Expr_hpp
10 #include <vector>
11 #include <gnumber.hpp>
12 #include <gwrapper.hpp>
13 namespace guit {
14 
16 class GExprBool {
17 public:
18  GExprBool(bool);
19  GExprBool(GBool&);
20  GExprBool(GPropRef_<GBool> const&);
22  GExprBool(std::function<bool()> const&);
23  GProp& prop;
24 };
25 
26 
28 class GExprText {
29 public:
30  GExprText(const char*);
31  GExprText(GString const&);
32  GExprText(GPropRef const&);
34  GProp const& prop;
35 };
36 
37 
39 class GExprNumber {
40 public:
41  GExprNumber(int);
42  GExprNumber(float);
43  GExprNumber(double);
44  GExprNumber(GNumPropRef const&);
47  GProp const& prop;
48 };
49 
50 
92 class GExpr : public GProp {
93 public:
94  virtual bool isBoolExpr() const {return false;}
95  virtual bool isNumExpr() const {return false;}
96  virtual bool isTextExpr() const {return false;}
97  GExpr* toExpr() override {return this;}
98 
99  bool boolValue() const override = 0;
100  float floatValue() const override = 0;
101  GString stringValue() const override = 0;
102 
104  GExpr* clone(bool) const override {return nullptr;}
105 
106  static GPropType& Type();
107  GPropType& type() const override {return Type();}
108 
110  template <class T>
111  class VectArg {
112  std::vector<T>& vect_;
113  public:
114  VectArg(std::vector<T>& v) : vect_(v) {}
115  auto operator[](GNumPropRef index) {
116  return GPropRef(*new GVectorWrapper<T>(index.prop_, vect_));
117  }
118  };
119 
121  template <class T>
122  class NumVectArg {
123  std::vector<T>& vect_;
124  public:
125  NumVectArg(std::vector<T>& v) : vect_(v) {}
126  auto operator[](GNumPropRef index) {
127  return GNumPropRef(*new GNumVectorWrapper<T>(index.prop_, vect_));
128  }
129  };
130 
131  enum Oper {
132  Equal, NotEqual, Greater, GreaterEqual, Less, LessEqual,
133  And, Or, Not, Identity, Add, Substract, Multiply, Divide,
134  StrStart, StrContain, StrRegex
135  };
136 
137 protected:
138  friend class GExprWrapper;
139  friend class GBoolExpr;
140  gptr<GProp> left_, right_;
141  Oper oper_;
142 
143  GExpr(GProp const* left, Oper oper, GProp const* right);
144  GExpr(const GExpr& from);
145 
146  bool isEquivalent(const GProp& other) const override {return false;} // !!
147  void bindImpl(class GExprWrapper&);
148  void linkImpl(class GExprTrigger&, class GBoolExpr&, bool trigger_or_guard);
149 };
150 
151 
157 template <class T, std::enable_if_t<std::is_arithmetic<T>::value,int> = 0>
158 inline GExpr::NumVectArg<T> operator~(std::vector<T>& vect) {
159  return GExpr::NumVectArg<T>(vect);
160 }
161 
162 template <class T, std::enable_if_t<std::is_compound<T>::value,int> = 0>
163 inline GExpr::VectArg<T> operator~(std::vector<T>& vect) {
164  return GExpr::VectArg<T>(vect);
165 }
166 
167 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
168 
169 // negation
170 GBoolExpr& operator!(GExprBool const& a);
171 //GBoolExpr& operator!(BoolExpr const& a);
172 
174 GBoolExpr& operator&&(GExprBool const& a, GExprBool const& b);
175 GBoolExpr& operator&&(GExprBool const& a, std::function<bool()> const& fun);
176 
178 GBoolExpr& operator||(GExprBool const& a, GExprBool const& b);
179 GBoolExpr& operator||(GExprBool const& a, std::function<bool()> const& fun);
180 
181 GBoolExpr& operator==(GExprBool const& a, GExprBool const& b);
182 GBoolExpr& operator!=(GExprBool const& a, GExprBool const& b);
183 
184 GBoolExpr& operator==(GExprNumber const& a, GExprNumber const& b);
185 GBoolExpr& operator!=(GExprNumber const& a, GExprNumber const& b);
186 GBoolExpr& operator> (GExprNumber const& a, GExprNumber const& b);
187 GBoolExpr& operator>=(GExprNumber const& a, GExprNumber const& b);
188 GBoolExpr& operator< (GExprNumber const& a, GExprNumber const& b);
189 GBoolExpr& operator<=(GExprNumber const& a, GExprNumber const& b);
190 
191 GNumExpr& operator+(GExprNumber const& a, GExprNumber const& b);
192 GNumExpr& operator-(GExprNumber const& a, GExprNumber const& b);
193 GNumExpr& operator*(GExprNumber const& a, GExprNumber const& b);
194 GNumExpr& operator/(GExprNumber const& a, GExprNumber const& b);
195 
197 GBoolExpr& operator==(GExprText const& a, GExprText const& b);
198 
200 GBoolExpr& operator^=(GExprText const& a, GExprText const& begin);
201 
203 GBoolExpr& operator*=(GExprText const& a, GExprText const& substr);
204 
206 GBoolExpr& operator%=(GExprText const& s, GExprText const& regex);
207 
208 GBoolExpr& operator!=(GExprText const& a, GExprText const& b);
209 GBoolExpr& operator> (GExprText const& a, GExprText const& b);
210 GBoolExpr& operator>=(GExprText const& a, GExprText const& b);
211 GBoolExpr& operator< (GExprText const& a, GExprText const& b);
212 GBoolExpr& operator<=(GExprText const& a, GExprText const& b);
213 
214 GTextExpr& operator+(GExprText const& a, GExprText const& b);
215 GTextExpr& operator+(GExprText const& a, GExprNumber const& b);
216 GTextExpr& operator+(GExprNumber const& a, GExprText const& b);
217 
218 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
219 
222 class GBoolExpr : public GExpr {
223 public:
224  enum Type {TBool, TNum, TText};
225  GBoolExpr(GProp const* left, Oper oper, GProp const* right, Type);
226  GBoolExpr(GBoolExpr const& from);
227 
228  bool isBoolExpr() const override {return true;}
229  bool isNumber() const override {return true;}
230  GBoolExpr* toBoolExpr() override {return this;}
231 
232  bool boolValue() const override;
233  float floatValue() const override {return float(boolValue());}
234  GString stringValue() const override;
235 
236  void linkWithTrigger(class GExprTrigger&);
237 protected:
238  Type type_;
239 };
240 
241 
244 class GTextExpr : public GExpr {
245 public:
246  GTextExpr(GProp const* left, Oper oper, GProp const* right) : GExpr(left,oper,right) {}
247  GTextExpr(GTextExpr const& from) : GExpr(from) {}
248 
249  bool isTextExpr() const override {return true;}
250  bool isString() const override {return true;}
251 
252  bool boolValue() const override;
253  float floatValue() const override;
254  GString stringValue() const override;
255 };
256 
257 
260 class GNumExpr : public GExpr {
261 public:
262  GNumExpr(GProp const* left, Oper oper, GProp const* right) : GExpr(left,oper,right) {}
263  GNumExpr(const GNumExpr& from) : GExpr(from) {}
264 
265  bool isNumExpr() const override {return true;}
266  bool isNumber() const override {return true;}
267 
268  bool boolValue() const override {return bool(floatValue());}
269  float floatValue() const override;
270  GString stringValue() const override;
271 };
272 
273 }
274 #endif