OMNeT++ Simulation Library  5.6.1
cdynamicexpression.h
1 //==========================================================================
2 // CDYNAMICEXPRESSION.H - part of
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CDYNAMICEXPRESSION_H
17 #define __OMNETPP_CDYNAMICEXPRESSION_H
18 
19 #include "cnedvalue.h"
20 #include "cexpression.h"
21 #include "cstringpool.h"
22 
23 namespace omnetpp {
24 
25 class cXMLElement;
26 class cPar;
27 class cNedMathFunction;
28 class cNedFunction;
29 
36 class SIM_API cDynamicExpression : public cExpression
37 {
38  public:
49  enum OpType {
50  ADD, SUB, MUL, DIV, MOD, POW, NEG,
51  EQ, NE, GT, GE, LT, LE, IIF, AND, OR, XOR, NOT,
52  BIN_AND, BIN_OR, BIN_XOR, BIN_NOT, LSHIFT, RSHIFT
53  };
54 
55  class Functor; // forward decl
56 
60  class SIM_API Elem
61  {
62  friend class cDynamicExpression;
63  public:
64  // Types:
65  // - bool
66  // - intpar_t
67  // - double
68  // - string
69  // - pointer to an "external" cXMLElement
70  // - cNedMathFunction: function with 0/1/2/3/4 double arguments
71  // - cNedFunction: function taking/returning cNedValue (NedFunction)
72  // - functor
73  // - math operator (+-*/%^...)
74  // - constant subexpression
75  //
76  enum Type {UNDEF, BOOL, INT, DBL, STR, XML, MATHFUNC, NEDFUNC, FUNCTOR, OP, CONSTSUBEXPR};
77  private:
78  static cStringPool stringPool;
79  Type type;
80  union {
81  bool b;
82  struct {intpar_t i; const char *unit;} i;
83  struct {double d; const char *unit;} d;
84  const char *s; // points into stringPool
85  cXMLElement *x;
87  struct {cNedFunction *f; int argc;} nf;
88  Functor *fu;
89  OpType op;
90  cExpression *constExpr;
91  };
92 
93  private:
94  void copy(const Elem& other);
95  void deleteOld();
96 
97  public:
98  Elem() {type=UNDEF;}
99  Elem(const Elem& other) {type=UNDEF; copy(other);}
100  ~Elem();
101 
105  void operator=(const Elem& other);
106 
111  void operator=(bool b);
112 
117  void operator=(intpar_t i);
118 
123  void operator=(double d);
124 
129  void setUnit(const char *s);
130 
135  void operator=(const char *s);
136 
141  void operator=(cXMLElement *x);
142 
147  void operator=(cNedMathFunction *f);
148 
153  void set(cNedFunction *f, int argc);
154 
159  void operator=(Functor *f);
160 
164  void operator=(OpType op);
165 
169  void operator=(cExpression* expr);
170 
172  Type getType() const {return type;}
173 
175  bool isNumericConstant() const {return type==INT || type==DBL;}
176 
178  void negate();
179 
181  bool getBoolConstant() const {ASSERT(type==BOOL); return b;}
182 
184  intpar_t getIntConstant() const {ASSERT(type==INT); return i.i;}
185 
187  double getDoubleConstant() const {ASSERT(type==DBL); return d.d;}
188 
190  const char *getUnit() const {return (type==INT) ? i.unit : (type==DBL) ? d.unit : nullptr;}
191 
193  const char *getStringConstant() const {ASSERT(type==STR); return s;}
194 
196  cXMLElement *getXMLElement() const {ASSERT(type==XML); return x;}
197 
199  cNedMathFunction *getMathFunction() const {ASSERT(type==MATHFUNC); return f;}
200 
202  cNedFunction *getNedFunction() const {ASSERT(type==NEDFUNC); return nf.f;}
203 
205  int getNedFunctionNumArgs() const {ASSERT(type==NEDFUNC); return nf.argc;}
206 
208  Functor *getFunctor() const {ASSERT(type==FUNCTOR); return fu;}
209 
211  OpType getOperation() const {ASSERT(type==OP); return op;}
212 
214  cExpression *getConstSubexpression() const {ASSERT(type==CONSTSUBEXPR); return constExpr;}
215 
219  int compare(const Elem& other) const;
220 
224  static const char *getOpName(OpType op);
225 
229  std::string str() const;
230  };
231 
237  class SIM_API Functor : public cObject
238  {
239  public:
240  virtual const char *getArgTypes() const = 0;
241  virtual int getNumArgs() const {return strlen(getArgTypes());}
242  virtual char getReturnType() const = 0;
243  virtual cNedValue evaluate(Context *context, cNedValue args[], int numargs) = 0;
244  virtual std::string str(std::string args[], int numargs) = 0;
245  using cObject::str;
247  };
248 
249 
250  protected:
251  Elem *elems;
252  int size;
253 
254  private:
255  void copy(const cDynamicExpression& other);
256  void bringToCommonTypeAndUnit(cNedValue& a, cNedValue& b) const;
257  static void ensureNoLogarithmicUnit(const cNedValue& v);
258 
259  public:
262 
266  explicit cDynamicExpression();
267 
271  cDynamicExpression(const cDynamicExpression& other) : cExpression(other) {elems=nullptr; copy(other);}
272 
276  virtual ~cDynamicExpression();
277 
281  cDynamicExpression& operator=(const cDynamicExpression& other);
283 
286 
290  virtual cDynamicExpression *dup() const override {return new cDynamicExpression(*this);}
291 
296  virtual std::string str() const override;
297 
298  // Note: parsimPack()/parsimUnpack() de-inherited in cExpression.
300 
308  virtual void setExpression(Elem e[], int size);
309 
315  virtual cNedValue evaluate(Context *context) const override;
316 
321  virtual bool boolValue(Context *context) const override;
322 
328  virtual intpar_t intValue(Context *context, const char *expectedUnit=nullptr) const override;
329 
334  virtual double doubleValue(Context *context, const char *expectedUnit=nullptr) const override;
335 
340  virtual std::string stringValue(Context *context) const override;
341 
346  virtual cXMLElement *xmlValue(Context *context) const override;
347 
348  using cExpression::evaluate;
350  using cExpression::intValue;
353  using cExpression::xmlValue;
355 
361  virtual void parse(const char *text) override;
362 
366  virtual int compare(const cExpression *other) const override;
367 
372  virtual bool isAConstant() const;
373 
377  virtual bool containsConstSubexpressions() const override;
378 
383  virtual void evaluateConstSubexpressions(Context *context) override;
384 
389  static double convertUnit(double d, const char *unit, const char *targetUnit);
390 
392 };
393 
394 
395 } // namespace omnetpp
396 
397 
398 #endif
399 
400 
const char * getStringConstant() const
Definition: cdynamicexpression.h:193
Type getType() const
Definition: cdynamicexpression.h:172
Registration class for extending NED with arbitrary new functions.
Definition: cnedfunction.h:48
bool isNumericConstant() const
Definition: cdynamicexpression.h:175
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
virtual cXMLElement * xmlValue(Context *context) const =0
Functor * getFunctor() const
Definition: cdynamicexpression.h:208
double getDoubleConstant() const
Definition: cdynamicexpression.h:187
Represents an XML element in an XML configuration file.
Definition: cxmlelement.h:73
intpar_t getIntConstant() const
Definition: cdynamicexpression.h:184
Function object base class. We use function objects to handle NED parameter references, "index" and "sizeof" operators, and references to NED "for" loop variables.
Definition: cdynamicexpression.h:237
int64_t intpar_t
Type for NED parameter values that store integers. It is guaranteed to be signed and at least as wide...
Definition: simkerneldefs.h:86
OpType
Definition: cdynamicexpression.h:49
bool getBoolConstant() const
Definition: cdynamicexpression.h:181
Value used during evaluating NED expressions.
Definition: cnedvalue.h:50
int getNedFunctionNumArgs() const
Definition: cdynamicexpression.h:205
cDynamicExpression(const cDynamicExpression &other)
Definition: cdynamicexpression.h:271
One element in a (reverse Polish) expression.
Definition: cdynamicexpression.h:60
cNedFunction * getNedFunction() const
Definition: cdynamicexpression.h:202
A stack-based expression evaluator class, for dynamically created expressions.
Definition: cdynamicexpression.h:36
virtual bool boolValue(Context *context) const =0
virtual std::string str() const
virtual double doubleValue(Context *context, const char *expectedUnit=nullptr) const =0
Definition: cabstracthistogram.h:21
virtual cNedValue evaluate(Context *context) const =0
cExpression * getConstSubexpression() const
Definition: cdynamicexpression.h:214
cNedMathFunction * getMathFunction() const
Definition: cdynamicexpression.h:199
cXMLElement * getXMLElement() const
Definition: cdynamicexpression.h:196
virtual cDynamicExpression * dup() const override
Definition: cdynamicexpression.h:290
OpType getOperation() const
Definition: cdynamicexpression.h:211
virtual std::string stringValue(Context *context) const =0
Contextual information for evaluating the expression.
Definition: cexpression.h:39
Registration class for extending NED with new mathematical functions.
Definition: cnedmathfunction.h:85
const char * getUnit() const
Definition: cdynamicexpression.h:190
Abstract base class for expression evaluators.
Definition: cexpression.h:33
virtual intpar_t intValue(Context *context, const char *expectedUnit=nullptr) const =0
Reference-counted storage for strings.
Definition: cstringpool.h:36