OMNeT++ Simulation Library  6.0.3
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 <map>
20 #include "cvalue.h"
21 #include "cexpression.h"
22 #include "cownedobject.h"
23 #include "fileline.h"
24 
25 namespace omnetpp {
26 
27 namespace common { class Expression; }
28 
29 class cXMLElement;
30 class cPar;
31 class cNedMathFunction;
32 class cNedFunction;
33 
40 class SIM_API cDynamicExpression : public cExpression
41 {
42  public:
48  class SIM_API IResolver {
49  public:
50  virtual ~IResolver() {}
54  virtual IResolver *dup() const = 0;
55 
59  virtual cValue readVariable(Context *context, const char *name) {return cValue();}
60 
62  virtual cValue readVariable(Context *context, const char *name, intval_t index) {return cValue();}
63 
65  virtual cValue readMember(Context *context, const cValue& object, const char *name) {return cValue();}
66 
68  virtual cValue readMember(Context *context, const cValue& object, const char *name, intval_t index) {return cValue();}
69 
71  virtual cValue callFunction(Context *context, const char *name, cValue argv[], int argc) {return cValue();}
72 
74  virtual cValue callMethod(Context *context, const cValue& object, const char *name, cValue argv[], int argc) {return cValue();}
76  };
77 
78  typedef IResolver ResolverBase;
79 
83  class SIM_API SymbolTable : public IResolver {
84  private:
85  std::map<std::string, cValue> variables;
86  std::map<std::string, std::vector<cValue>> arrays;
87  public:
88  SymbolTable() {}
89  SymbolTable(const std::map<std::string, cValue>& variables) : variables(variables) {}
90  SymbolTable(const std::map<std::string, cValue>& variables, const std::map<std::string, std::vector<cValue>>& arrays) : variables(variables), arrays(arrays) {}
91  virtual SymbolTable *dup() const override {return new SymbolTable(variables, arrays);}
92  virtual cValue readVariable(Context *context, const char *name) override;
93  virtual cValue readVariable(Context *context, const char *name, intval_t index) override;
94  };
95 
96  protected:
97  common::Expression *expression = nullptr;
98  IResolver *resolver = nullptr;
99  FileLine sourceLoc;
100 
101  private:
102  void copy(const cDynamicExpression& other);
103 
104  public:
107 
111  explicit cDynamicExpression();
112 
116  cDynamicExpression(const cDynamicExpression& other) : cExpression(other) {copy(other);}
117 
121  virtual ~cDynamicExpression();
122 
126  cDynamicExpression& operator=(const cDynamicExpression& other);
127 
131  virtual cDynamicExpression *dup() const override {return new cDynamicExpression(*this);}
132 
137  virtual std::string str() const override;
139 
145  virtual void parse(const char *text) override;
146 
153  virtual void parse(const char *text, IResolver *resolver) {parse(text); setResolver(resolver);}
154 
162  virtual void parse(const char *text, const std::map<std::string,cValue>& symbolTable) {parse(text); setResolver(new SymbolTable(symbolTable));}
163 
167  virtual void setResolver(IResolver *resolver);
168 
172  virtual IResolver *getResolver() const {return resolver;}
173 
177  virtual void parseNedExpr(const char *text);
178 
183  virtual cValue evaluate(Context *context) const override;
184 
189  virtual bool boolValue(Context *context) const override;
190 
196  virtual intval_t intValue(Context *context, const char *expectedUnit=nullptr) const override;
197 
202  virtual double doubleValue(Context *context, const char *expectedUnit=nullptr) const override;
203 
208  virtual std::string stringValue(Context *context) const override;
209 
214  virtual cXMLElement *xmlValue(Context *context) const override;
215 
216  using cExpression::evaluate;
218  using cExpression::intValue;
221  using cExpression::xmlValue;
223 
229  virtual int compare(const cExpression *other) const override;
230 
235  virtual bool isAConstant() const override;
236 
241  void setSourceLocation(FileLine loc) {sourceLoc = loc;}
242 
247  virtual std::string getSourceLocation() const override {return sourceLoc.str();}
248 
253  static double convertUnit(double d, const char *unit, const char *targetUnit);
255 };
256 
264 {
265  private:
266  virtual void parsimPack(cCommBuffer *) const override {throw cRuntimeError(this, E_CANTPACK);}
267  virtual void parsimUnpack(cCommBuffer *) override {throw cRuntimeError(this, E_CANTPACK);}
268 
269  public:
272 
276  explicit cOwnedDynamicExpression(const char *name=nullptr) : cOwnedObject(name) {}
277 
282 
291 
294 
298  virtual cOwnedDynamicExpression *dup() const override {return new cOwnedDynamicExpression(*this);}
299 
304  virtual std::string str() const override { return cDynamicExpression::str(); }
305 };
306 
307 } // namespace omnetpp
308 
309 
310 #endif
311 
312 
omnetpp::cExpression::stringValue
virtual std::string stringValue(Context *context) const =0
omnetpp::cOwnedDynamicExpression
An "owned" version cDynamicExpression to allow it to be assigned to module parameters of the type "ob...
Definition: cdynamicexpression.h:263
omnetpp::cOwnedDynamicExpression::str
virtual std::string str() const override
Definition: cdynamicexpression.h:304
omnetpp::cOwnedDynamicExpression::operator=
cOwnedDynamicExpression & operator=(const cOwnedDynamicExpression &other)=default
omnetpp::cValue
A variant-like value class used during evaluating NED expressions.
Definition: cvalue.h:47
omnetpp::cDynamicExpression::dup
virtual cDynamicExpression * dup() const override
Definition: cdynamicexpression.h:131
omnetpp::cExpression
Abstract base class for expression evaluators.
Definition: cexpression.h:33
omnetpp::cExpression::intValue
virtual intval_t intValue(Context *context, const char *expectedUnit=nullptr) const =0
omnetpp::cOwnedDynamicExpression::cOwnedDynamicExpression
cOwnedDynamicExpression(const char *name=nullptr)
Definition: cdynamicexpression.h:276
omnetpp::cExpression::boolValue
virtual bool boolValue(Context *context) const =0
omnetpp::cDynamicExpression::IResolver::readVariable
virtual cValue readVariable(Context *context, const char *name, intval_t index)
Definition: cdynamicexpression.h:62
omnetpp::cDynamicExpression::IResolver::readMember
virtual cValue readMember(Context *context, const cValue &object, const char *name, intval_t index)
Definition: cdynamicexpression.h:68
omnetpp::cDynamicExpression
A stack-based expression evaluator class, for dynamically created expressions.
Definition: cdynamicexpression.h:40
omnetpp::cDynamicExpression::cDynamicExpression
cDynamicExpression(const cDynamicExpression &other)
Definition: cdynamicexpression.h:116
omnetpp::cDynamicExpression::IResolver::readMember
virtual cValue readMember(Context *context, const cValue &object, const char *name)
Definition: cdynamicexpression.h:65
omnetpp::cDynamicExpression::IResolver::callFunction
virtual cValue callFunction(Context *context, const char *name, cValue argv[], int argc)
Definition: cdynamicexpression.h:71
omnetpp::cExpression::doubleValue
virtual double doubleValue(Context *context, const char *expectedUnit=nullptr) const =0
omnetpp::FileLine
Definition: fileline.h:27
omnetpp::cExpression::Context
Contextual information for evaluating the expression.
Definition: cexpression.h:39
omnetpp::cDynamicExpression::getSourceLocation
virtual std::string getSourceLocation() const override
Definition: cdynamicexpression.h:247
omnetpp::cOwnedDynamicExpression::cOwnedDynamicExpression
cOwnedDynamicExpression(const cOwnedDynamicExpression &other)
Definition: cdynamicexpression.h:281
omnetpp::cDynamicExpression::IResolver
Definition: cdynamicexpression.h:48
omnetpp::cDynamicExpression::IResolver::callMethod
virtual cValue callMethod(Context *context, const cValue &object, const char *name, cValue argv[], int argc)
Definition: cdynamicexpression.h:74
omnetpp::cDynamicExpression::IResolver::readVariable
virtual cValue readVariable(Context *context, const char *name)
Definition: cdynamicexpression.h:59
omnetpp::cExpression::xmlValue
virtual cXMLElement * xmlValue(Context *context) const =0
omnetpp::intval_t
int64_t intval_t
Signed integer type which is guaranteed to be at least 64 bits wide. It is used throughout the librar...
Definition: simkerneldefs.h:101
omnetpp::cDynamicExpression::parse
virtual void parse(const char *text, const std::map< std::string, cValue > &symbolTable)
Definition: cdynamicexpression.h:162
omnetpp::cDynamicExpression::SymbolTable
Definition: cdynamicexpression.h:83
omnetpp::cDynamicExpression::setSourceLocation
void setSourceLocation(FileLine loc)
Definition: cdynamicexpression.h:241
omnetpp::cXMLElement
Represents an XML element in an XML configuration file.
Definition: cxmlelement.h:75
omnetpp::cDynamicExpression::SymbolTable::dup
virtual SymbolTable * dup() const override
Definition: cdynamicexpression.h:91
omnetpp::cOwnedDynamicExpression::dup
virtual cOwnedDynamicExpression * dup() const override
Definition: cdynamicexpression.h:298
omnetpp::cDynamicExpression::getResolver
virtual IResolver * getResolver() const
Definition: cdynamicexpression.h:172
omnetpp::cRuntimeError
Thrown when the simulation kernel or other components detect a runtime error.
Definition: cexception.h:286
omnetpp::cExpression::evaluate
virtual cValue evaluate(Context *context) const =0
omnetpp::cDynamicExpression::parse
virtual void parse(const char *text, IResolver *resolver)
Definition: cdynamicexpression.h:153
omnetpp::cCommBuffer
Buffer for the communications layer of parallel simulation.
Definition: ccommbuffer.h:41
omnetpp::cDynamicExpression::str
virtual std::string str() const override
omnetpp::cOwnedObject
A cObject that keeps track of its owner. It serves as base class for many classes in the OMNeT++ libr...
Definition: cownedobject.h:105