cdynamicexpression.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __CDYNAMICEXPRESSION_H
00019 #define __CDYNAMICEXPRESSION_H
00020
00021 #include "cnedvalue.h"
00022 #include "cexpression.h"
00023 #include "cstringpool.h"
00024
00025 NAMESPACE_BEGIN
00026
00027 class cXMLElement;
00028 class cPar;
00029 class cNEDMathFunction;
00030 class cNEDFunction;
00031
00040 class SIM_API cDynamicExpression : public cExpression
00041 {
00042 public:
00053 enum OpType {
00054 ADD, SUB, MUL, DIV, MOD, POW, NEG,
00055 EQ, NE, GT, GE, LT, LE, IIF, AND, OR, XOR, NOT,
00056 BIN_AND, BIN_OR, BIN_XOR, BIN_NOT, LSHIFT, RSHIFT
00057 };
00058
00059 class Functor;
00060
00064 class SIM_API Elem
00065 {
00066 friend class cDynamicExpression;
00067 private:
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079 enum Type {UNDEF, BOOL, DBL, STR, XML, MATHFUNC, NEDFUNC, FUNCTOR, OP, CONSTSUBEXPR} type;
00080 static cStringPool stringPool;
00081 union {
00082 bool b;
00083 struct {double d; const char *unit;} d;
00084 const char *s;
00085 cXMLElement *x;
00086 cNEDMathFunction *f;
00087 struct {cNEDFunction *f; int argc;} nf;
00088 Functor *fu;
00089 OpType op;
00090 cExpression *constExpr;
00091 };
00092
00093 private:
00094 void copy(const Elem& other);
00095
00096 void deleteOld();
00097
00098 public:
00099 Elem() {type=UNDEF;}
00100 Elem(const Elem& other) {type=UNDEF; copy(other);}
00101 ~Elem();
00102
00106 void operator=(const Elem& other);
00107
00112 void operator=(bool _b) {type=BOOL; b=_b;}
00113
00118 void operator=(int _i) {type=DBL; d.d=_i; d.unit=NULL;}
00119
00124 void operator=(short _i) {type=DBL; d.d=_i; d.unit=NULL;}
00125
00130 void operator=(long _l) {type=DBL; d.d=_l; d.unit=NULL;}
00131
00136 void operator=(double _d) {type=DBL; d.d=_d; d.unit=NULL;}
00137
00142 void setUnit(const char *s) {ASSERT(type==DBL); d.unit = stringPool.get(s);}
00143
00148 void operator=(const char *_s) {type=STR; s = stringPool.get(_s);}
00149
00154 void operator=(cXMLElement *_x) {type=XML; x=_x;}
00155
00160 void operator=(cNEDMathFunction *_f) {type=MATHFUNC; ASSERT(_f); f=_f;}
00161
00166 void set(cNEDFunction *f, int argc) {type=NEDFUNC; ASSERT(f); nf.f=f; nf.argc=argc;}
00167
00172 void operator=(Functor *f) {type=FUNCTOR; ASSERT(f); fu=f;}
00173
00177 void operator=(OpType _op) {type=OP; op=_op;}
00178
00182 void operator=(cExpression *_expr) {type=CONSTSUBEXPR; constExpr=_expr;}
00183
00187 int compare(const Elem& other) const;
00188 };
00189
00195 class SIM_API Functor : public cObject
00196 {
00197 public:
00198 virtual const char *getArgTypes() const = 0;
00199 virtual int getNumArgs() const {return strlen(getArgTypes());}
00200 virtual char getReturnType() const = 0;
00201 virtual cNEDValue evaluate(cComponent *context, cNEDValue args[], int numargs) = 0;
00202 virtual std::string str(std::string args[], int numargs) = 0;
00203 };
00204
00205 protected:
00206 Elem *elems;
00207 int size;
00208
00209 private:
00210 void copy(const cDynamicExpression& other);
00211
00212 public:
00215
00219 explicit cDynamicExpression();
00220
00224 cDynamicExpression(const cDynamicExpression& other) : cExpression(other) {elems=NULL; copy(other);}
00225
00229 virtual ~cDynamicExpression();
00230
00234 cDynamicExpression& operator=(const cDynamicExpression& other);
00236
00239
00243 virtual cDynamicExpression *dup() const {return new cDynamicExpression(*this);}
00244
00249 virtual std::string info() const;
00250
00251
00253
00261 virtual void setExpression(Elem e[], int size);
00262
00268 virtual cNEDValue evaluate(cComponent *context) const;
00269
00274 virtual bool boolValue(cComponent *context);
00275
00280 virtual long longValue(cComponent *context, const char *expectedUnit=NULL);
00281
00286 virtual double doubleValue(cComponent *context, const char *expectedUnit=NULL);
00287
00292 virtual std::string stringValue(cComponent *context);
00293
00298 virtual cXMLElement *xmlValue(cComponent *context);
00300
00306 virtual std::string str() const;
00307
00311 virtual void parse(const char *text);
00312
00316 virtual int compare(const cExpression *other) const;
00317
00322 virtual bool isAConstant() const;
00323
00327 virtual bool containsConstSubexpressions() const;
00328
00333 virtual void evaluateConstSubexpressions(cComponent *context);
00334
00339 static double convertUnit(double d, const char *unit, const char *targetUnit);
00340
00342 };
00343
00344 NAMESPACE_END
00345
00346
00347 #endif
00348
00349