Statistics
| Branch: | Revision:

root / include / cexpression.h @ 47c4b975

History | View | Annotate | Download (4.47 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//   CEXPRESSION.H  - part of
3
//                     OMNeT++/OMNEST
4
//            Discrete System Simulation in C++
5
//
6
//  Author: Andras Varga
7
//
8
//==========================================================================
9
10
/*--------------------------------------------------------------*
11
  Copyright (C) 1992-2008 Andras Varga
12
  Copyright (C) 2006-2008 OpenSim Ltd.
13

14
  This file is distributed WITHOUT ANY WARRANTY. See the file
15
  `license' for details on this and other legal matters.
16
*--------------------------------------------------------------*/
17
18
#ifndef __CEXPRESSION_H
19
#define __CEXPRESSION_H
20
21
#include "cobject.h"
22
#include "cexception.h"
23
24
NAMESPACE_BEGIN
25
26
class cXMLElement;
27
28
/**
29
 * Abstract base class for expression evaluators.
30
 *
31
 * @see cPar
32
 * @ingroup SimCore
33
 */
34
class SIM_API cExpression : public cObject
35
{
36
  public:
37
    /** @name Constructors, destructor, assignment. */
38
    //@{
39
40
    /**
41
     * Constructor.
42
     */
43
    explicit cExpression() {}
44
45
    /**
46
     * Copy constructor.
47
     */
48
    cExpression(const cExpression& other) {}
49
50
    /**
51
     * Destructor.
52
     */
53
    virtual ~cExpression() {}
54
55
    /**
56
     * Assignment operator.
57
     */
58
    cExpression& operator=(const cExpression& other) {return *this;}
59
    //@}
60
61
    /** @name Redefined cObject functions */
62
    //@{
63
    /**
64
     * Duplication not supported, this method is redefined to throw an error.
65
     */
66
    virtual cExpression *dup() const {copyNotSupported(); return NULL;}
67
68
    /**
69
     * Produces a one-line description of the object's contents.
70
     * See cObject for more details.
71
     */
72
    virtual std::string info() const {return "";}
73
74
    /**
75
     * Redefined to "de-inherit" it.
76
     */
77
    virtual void parsimPack(cCommBuffer *buffer)  {throw cRuntimeError(this, eCANTPACK);}
78
79
    /**
80
     * Redefined to "de-inherit" it.
81
     */
82
    virtual void parsimUnpack(cCommBuffer *buffer)  {throw cRuntimeError(this, eCANTPACK);}
83
    //@}
84
85
    /** @name Getter functions. Note that overloaded conversion operators also exist. */
86
    //@{
87
88
    /**
89
     * Evaluate the expression and convert the result to bool if possible;
90
     * throw an error if conversion from that type is not supported.
91
     */
92
    virtual bool boolValue(cComponent *context=NULL) = 0;
93
94
    /**
95
     * Evaluate the expression and convert the result to long if possible;
96
     * throw an error if conversion from that type is not supported.
97
     * Also throws an error if the actual unit does not match the expected unit.
98
     */
99
    virtual long longValue(cComponent *context=NULL, const char *expectedUnit=NULL) = 0;
100
101
    /**
102
     * Evaluate the expression and convert the result to double if possible;
103
     * throw an error if conversion from that type is not supported.
104
     * Also throws an error if the actual unit does not match the expected unit.
105
     */
106
    virtual double doubleValue(cComponent *context=NULL, const char *expectedUnit=NULL) = 0;
107
108
    /**
109
     * Evaluate the expression and convert the result to string if possible;
110
     * throw an error if conversion from that type is not supported.
111
     */
112
    virtual std::string stringValue(cComponent *context=NULL) = 0;
113
114
    /**
115
     * Evaluate the expression and convert the result to an XML tree if possible;
116
     * throw an error if conversion from that type is not supported.
117
     */
118
    virtual cXMLElement *xmlValue(cComponent *context=NULL) = 0;
119
    //@}
120
121
    /** @name Miscellaneous utility functions. */
122
    //@{
123
    /**
124
     * Converts the expression to string.
125
     */
126
    virtual std::string str() const = 0;
127
128
    /**
129
     * Interprets the string as an expression, and store it.
130
     * If the text cannot be parsed, an exception is thrown, which
131
     * can be caught as std::runtime_error& if necessary.
132
     * An exception is also thrown if the particular cExpression subclass
133
     * does not support parsing.
134
     */
135
    virtual void parse(const char *text) = 0;
136
137
    /**
138
     * Compares two expressions. Makes it possible to use cExpression
139
     * as (part of) a key in std::map or std::set.
140
     */
141
    virtual int compare(const cExpression *other) const = 0;
142
143
    /**
144
     * Returns true if this expression contains const subexpressions.
145
     */
146
    virtual bool containsConstSubexpressions() const = 0;
147
148
    /**
149
     * Evaluates const subexpressions, and replaces them with their values.
150
     * See cDynamicExpression::Elem::CONSTSUBEXPR.
151
     */
152
    virtual void evaluateConstSubexpressions(cComponent *context) = 0;
153
    //@}
154
};
155
156
NAMESPACE_END
157
158
159
#endif
160