cDynamicExpression Class Reference¶

API: cDynamicExpression Class Reference
API
cDynamicExpression Class Reference
Inheritance diagram for cDynamicExpression:
[legend]

Classes

class  IResolver
class  SymbolTable

Public Member Functions

Constructors, destructor, assignment.
 cDynamicExpression ()
 cDynamicExpression (const cDynamicExpression &other)
virtual ~cDynamicExpression ()
cDynamicExpressionoperator= (const cDynamicExpression &other)
virtual cDynamicExpressiondup () const override
virtual std::string str () const override
Setter and evaluator methods.
virtual void parse (const char *text) override
virtual void parse (const char *text, IResolver *resolver)
virtual void parse (const char *text, const std::map< std::string, cValue > &symbolTable)
virtual void setResolver (IResolver *resolver)
virtual IResolvergetResolver () const
virtual void parseNedExpr (const char *text)
virtual cValue evaluate (Context *context) const override
virtual bool boolValue (Context *context) const override
virtual intval_t intValue (Context *context, const char *expectedUnit=nullptr) const override
virtual double doubleValue (Context *context, const char *expectedUnit=nullptr) const override
virtual std::string stringValue (Context *context) const override
virtual cXMLElementxmlValue (Context *context) const override
virtual cValue evaluate (cComponent *contextComponent=nullptr) const
virtual bool boolValue (cComponent *contextComponent=nullptr) const
virtual intval_t intValue (cComponent *contextComponent=nullptr, const char *expectedUnit=nullptr) const
virtual double doubleValue (cComponent *contextComponent=nullptr, const char *expectedUnit=nullptr) const
virtual std::string stringValue (cComponent *contextComponent=nullptr) const
virtual cXMLElementxmlValue (cComponent *contextComponent=nullptr) const
Public Member Functions inherited from cExpression
 cExpression ()
 cExpression (const cExpression &other)=default
virtual ~cExpression ()
cExpressionoperator= (const cExpression &other)

Miscellaneous utility functions.

virtual int compare (const cExpression *other) const override
virtual bool isAConstant () const override
void setSourceLocation (FileLine loc)
virtual std::string getSourceLocation () const override
static double convertUnit (double d, const char *unit, const char *targetUnit)

Description

A stack-based expression evaluator class, for dynamically created expressions.

Constructor & Destructor Documentation

◆ cDynamicExpression() [1/2]

◆ cDynamicExpression() [2/2]

cDynamicExpression ( const cDynamicExpression & other)
inline

Copy constructor.

References cDynamicExpression(), and cExpression::cExpression().

◆ ~cDynamicExpression()

virtual ~cDynamicExpression ( )
virtual

Destructor.

Member Function Documentation

◆ operator=()

cDynamicExpression & operator= ( const cDynamicExpression & other)

Assignment operator.

References cDynamicExpression().

◆ dup()

virtual cDynamicExpression * dup ( ) const
inlineoverridevirtual

Creates and returns an exact copy of this object.

Implements cExpression.

Reimplemented in cOwnedDynamicExpression.

References cDynamicExpression().

◆ str()

virtual std::string str ( ) const
overridevirtual

Converts the expression to string. See cObject for more details.

Reimplemented from cExpression.

Reimplemented in cOwnedDynamicExpression.

Referenced by cOwnedDynamicExpression::str().

◆ parse() [1/3]

virtual void parse ( const char * text)
overridevirtual

Interprets the string as a generic expression, and stores it.

Implements cExpression.

◆ parse() [2/3]

virtual void parse ( const char * text,
IResolver * resolver )
inlinevirtual

Interprets the string as a generic expression, and stores it. The resolver object allows variables, member accesses, function calls and method calls to be handled in a custom way. The expression object will take ownership of the resolver object.

References parse(), and setResolver().

Referenced by parse().

◆ parse() [3/3]

virtual void parse ( const char * text,
const std::map< std::string, cValue > & symbolTable )
inlinevirtual

Interprets the string as a generic expression, and stores it. Values for variables used in the expression can be passed in the symbolTable argument. This method is simply delegates to parse(const char *text, IResolver *resolver) while using cDynamicExpression::SymbolTable as resolver.

References parse(), and setResolver().

Referenced by parse().

◆ setResolver()

virtual void setResolver ( IResolver * resolver)
virtual

Allows changing the resolver after a parse() call.

Referenced by parse(), and parse().

◆ getResolver()

virtual IResolver * getResolver ( ) const
inlinevirtual

Returns the resolver.

◆ parseNedExpr()

virtual void parseNedExpr ( const char * text)
virtual

Interprets the string as a NED expression, and stores it.

◆ evaluate() [1/2]

virtual cValue evaluate ( Context * context) const
overridevirtual

Evaluate the expression, and return the results as a cValue. Evaluation errors result in exceptions.

Implements cExpression.

◆ boolValue() [1/2]

virtual bool boolValue ( Context * context) const
overridevirtual

Evaluate the expression and convert the result to bool if possible; throw an error if conversion from that type is not supported.

Implements cExpression.

◆ intValue() [1/2]

virtual intval_t intValue ( Context * context,
const char * expectedUnit = nullptr ) const
overridevirtual

Evaluate the expression and convert the result to intval_t if possible; throw an error if conversion from that type is not supported, or the value of out of the range of intval_t.

Implements cExpression.

◆ doubleValue() [1/2]

virtual double doubleValue ( Context * context,
const char * expectedUnit = nullptr ) const
overridevirtual

Evaluate the expression and convert the result to double if possible; throw an error if conversion from that type is not supported.

Implements cExpression.

◆ stringValue() [1/2]

virtual std::string stringValue ( Context * context) const
overridevirtual

Evaluate the expression and convert the result to string if possible; throw an error if conversion from that type is not supported.

Implements cExpression.

◆ xmlValue() [1/2]

virtual cXMLElement * xmlValue ( Context * context) const
overridevirtual

Evaluate the expression and convert the result to an XML tree if possible; throw an error if conversion from that type is not supported.

Implements cExpression.

References cExpression::boolValue(), cExpression::doubleValue(), cExpression::evaluate(), cExpression::intValue(), cExpression::stringValue(), and cExpression::xmlValue().

◆ compare()

virtual int compare ( const cExpression * other) const
overridevirtual

Compares two expressions.

Implements cExpression.

References cExpression::cExpression().

◆ isAConstant()

virtual bool isAConstant ( ) const
overridevirtual

Returns true if the expression is just a literal (or equivalent to one, like "2+2"). This can be used for optimization.

Reimplemented from cExpression.

◆ setSourceLocation()

void setSourceLocation ( FileLine loc)
inline

Returns the file:line info where this expression was parsed from. Returns empty string if such info is not available.

◆ getSourceLocation()

virtual std::string getSourceLocation ( ) const
inlineoverridevirtual

Returns the file:line info where this expression was parsed from. Returns empty string if such info is not available.

Implements cExpression.

◆ convertUnit()

double convertUnit ( double d,
const char * unit,
const char * targetUnit )
static

Convert the given number into the target unit (e.g. milliwatt to watt). Throws an exception if conversion is not possible (unknown/unrelated units).

◆ evaluate() [2/2]

virtual cValue evaluate ( cComponent * contextComponent = nullptr) const
virtual

Evaluate the expression and return the result in a cValue. This method creates a Context from contextComponent, and delegates to evaluate(Context*).

Reimplemented from cExpression.

◆ boolValue() [2/2]

virtual bool boolValue ( cComponent * contextComponent = nullptr) const
virtual

Evaluate the expression and convert the result to bool if possible; throw an error if conversion from that type is not supported. This method creates a Context from contextComponent, and delegates to boolValue(Context*).

Reimplemented from cExpression.

◆ intValue() [2/2]

virtual intval_t intValue ( cComponent * contextComponent = nullptr,
const char * expectedUnit = nullptr ) const
virtual

Evaluate the expression and convert the result to intval_t if possible; throw an error if conversion from that type is not supported. Also throws an error if the actual unit does not match the expected unit. This method creates a Context from contextComponent, and delegates to intValue(Context*,const char*).

Reimplemented from cExpression.

◆ doubleValue() [2/2]

virtual double doubleValue ( cComponent * contextComponent = nullptr,
const char * expectedUnit = nullptr ) const
virtual

Evaluate the expression and convert the result to double if possible; throw an error if conversion from that type is not supported. Also throws an error if the actual unit does not match the expected unit. This method creates a Context from contextComponent, and delegates to doubleValue(Context*,const char*).

Reimplemented from cExpression.

◆ stringValue() [2/2]

virtual std::string stringValue ( cComponent * contextComponent = nullptr) const
virtual

Evaluate the expression and convert the result to string if possible; throw an error if conversion from that type is not supported. This method creates a Context from contextComponent, and delegates to stringValue(Context*).

Reimplemented from cExpression.

◆ xmlValue() [2/2]

virtual cXMLElement * xmlValue ( cComponent * contextComponent = nullptr) const
virtual

Evaluate the expression and convert the result to an XML tree if possible; throw an error if conversion from that type is not supported. This method creates a Context from contextComponent, and delegates to xmlValue(Context*).

Reimplemented from cExpression.