Project

General

Profile

Statistics
| Branch: | Revision:

root / include / cparimpl.h @ 0e7ff5c8

History | View | Annotate | Download (10.2 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//   CPARIMPL.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 __CPARIMPL_H
19
#define __CPARIMPL_H
20
21
#include "cpar.h"
22
#include "cexpression.h"
23
#include "cdummystringpool.h"
24
#include "cexception.h"
25
26
NAMESPACE_BEGIN
27
28
class cExpression;
29
class cXMLElement;
30
class cProperties;
31
class cComponent;
32
33
34
/**
35
 * Internal class that stores parameter values. cPar delegates almost all
36
 * methods to cParImpl. Delegation was introduced to save memory by using
37
 * shared storage for module parameters of the same values.
38
 *
39
 * cParImpl is an abstract base class, which supports several data types via
40
 * subclasses: cLongParImpl, cDoubleParImpl, cBoolParImpl, cStringParImpl,
41
 * cXMLParImpl.
42
 *
43
 * @ingroup Internals
44
 */
45
class SIM_API cParImpl : public cNamedObject
46
{
47
  protected:
48
    // various flags, stored in cNamedObject::flags
49
    enum {
50
      FL_ISVOLATILE = 4,  // whether it was declared as "volatile" in NED
51
      FL_ISEXPR = 8,      // whether it stores a constant or an expression
52
      FL_ISSHARED = 16,   // used by cPar only: whether this object is shared among multiple cPars
53
      FL_CONTAINSVALUE = 32, // whether it has a value
54
      FL_ISSET = 64       // whether the contained value is just a default value (false) or the set value (true)
55
    };
56
57
  private:
58
    // unit (s, mW, GHz, baud, etc); optional
59
    const char *unitp; // stringpooled
60
61
    // global variables for statistics
62
    static long total_parimpl_objs;
63
    static long live_parimpl_objs;
64
    static cDummyStringPool unitStringPool;
65
66
  public:
67
    typedef cPar::Type Type;
68
69
  public:
70
    /** @name Constructors, destructor, assignment. */
71
    //@{
72
73
    /**
74
     * Constructor.
75
     */
76
    explicit cParImpl();
77
78
    /**
79
     * Copy constructor.
80
     */
81
    cParImpl(const cParImpl& other);
82
83
    /**
84
     * Destructor.
85
     */
86
    virtual ~cParImpl();
87
88
    /**
89
     * Assignment operator.
90
     */
91
    cParImpl& operator=(const cParImpl& otherpar);
92
    //@}
93
94
    /** @name Redefined cObject member functions */
95
    //@{
96
    /**
97
     * Returns a one-line description of the object.
98
     * @see detailedInfo()
99
     */
100
    virtual std::string info() const;
101
102
    /**
103
     * Returns a long description of the object.
104
     */
105
    virtual std::string detailedInfo() const;
106
107
    /**
108
     * Redefined change return type to cParImpl.
109
     */
110
    virtual cParImpl *dup() const;
111
112
    /**
113
     * Serializes the object into a buffer.
114
     */
115
    virtual void parsimPack(cCommBuffer *buffer);
116
117
    /**
118
     * Deserializes the object from a buffer.
119
     */
120
    virtual void parsimUnpack(cCommBuffer *buffer);
121
    //@}
122
123
    /** @name Owner component, type, flags. */
124
    //@{
125
    /**
126
     * Returns the parameter type.
127
     */
128
    virtual Type getType() const = 0;
129
130
    /**
131
     * Returns true if the stored value is of a numeric type.
132
     */
133
    virtual bool isNumeric() const = 0;
134
135
    /**
136
     * Returns true if this parameter is marked in the NED file as "volatile".
137
     */
138
    virtual bool isVolatile() const {return flags & FL_ISVOLATILE;}
139
140
    /**
141
     * Returns false if the stored value is a constant, and true if it is
142
     * an expression. (It is not examined whether the expression yields
143
     * a constant value.)
144
     */
145
    virtual bool isExpression() const {return flags & FL_ISEXPR;}
146
147
    /**
148
     * Used by cPar only: Returns true if this object is shared among multiple cPars.
149
     */
150
    virtual bool isShared() const {return flags & FL_ISSHARED;}
151
152
    /**
153
     * Returns true if the parameter contains a value. Note that isSet()
154
     * may still return true or false.
155
     */
156
    virtual bool containsValue() const  {return flags & FL_CONTAINSVALUE;}
157
158
    /**
159
     * Used by cPar only: Returns true if the parameter value is set,
160
     * false otherwise (i.e. if the object contains no value
161
     * or the current value is just a default).
162
     */
163
    virtual bool isSet() const {return flags & FL_ISSET;}
164
165
    /**
166
     * Sets the isVolatile flag. NOTE: It may be necessary to invoke
167
     * convertToConst(cComponent *context) as well.
168
     */
169
    virtual void setIsVolatile(bool f) {setFlag(FL_ISVOLATILE,f);}
170
171
    /**
172
     * Sets the isShared flag.
173
     */
174
    virtual void setIsShared(bool f) {setFlag(FL_ISSHARED,f);}
175
176
    /**
177
     * Sets the isSet flag.
178
     */
179
    virtual void setIsSet(bool f) {setFlag(FL_ISSET,f);}
180
181
    /**
182
     * Returns the parameter's unit ("s", "mW", "Hz", "bps", etc),
183
     * as declared in the \@unit property of the parameter in NED.
184
     * Unit is only meaningful with numeric parameters.
185
     */
186
    virtual const char *getUnit() const;
187
188
    /**
189
     * Initialize the parameter's unit (normally from the \@unit property).
190
     */
191
    virtual void setUnit(const char *s);
192
    //@}
193
194
    /** @name Setter functions. Note that overloaded assignment operators also exist. */
195
    //@{
196
197
    /**
198
     * Sets the value to the given bool value.
199
     */
200
    virtual void setBoolValue(bool b) = 0;
201
202
    /**
203
     * Sets the value to the given long value.
204
     */
205
    virtual void setLongValue(long l) = 0;
206
207
    /**
208
     * Sets the value to the given double value.
209
     */
210
    virtual void setDoubleValue(double d) = 0;
211
212
    /**
213
     * Sets the value to the given string value.
214
     * The cParImpl will make its own copy of the string. NULL is also accepted
215
     * and treated as an empty string.
216
     */
217
    virtual void setStringValue(const char *s) = 0;
218
219
    /**
220
     * Sets the value to the given string value.
221
     */
222
    virtual void setStringValue(const std::string& s)  {setStringValue(s.c_str());}
223
224
    /**
225
     * Sets the value to the given cXMLElement.
226
     */
227
    virtual void setXMLValue(cXMLElement *node) = 0;
228
229
    /**
230
     * Sets the value to the given expression. This object will assume
231
     * the responsibility to delete the expression object.
232
     *
233
     * Note: if the parameter is marked as non-volatile (isVolatile()==false),
234
     * one should not set an expression as value. This is not enforced
235
     * by cParImpl though.
236
     */
237
    virtual void setExpression(cExpression *e) = 0;
238
    //@}
239
240
    /** @name Getter functions. */
241
    //@{
242
243
    /**
244
     * Returns value as a boolean. The cParImpl type must be BOOL.
245
     */
246
    virtual bool boolValue(cComponent *context) const = 0;
247
248
    /**
249
     * Returns value as long. The cParImpl type must be LONG or DOUBLE.
250
     */
251
    virtual long longValue(cComponent *context) const = 0;
252
253
    /**
254
     * Returns value as long. The cParImpl type must be LONG or DOUBLE.
255
     */
256
    virtual double doubleValue(cComponent *context) const = 0;
257
258
    /**
259
     * Returns value as const char *. Only for STRING type.
260
     * This method may only be invoked when the parameter's value is a
261
     * string constant and not the result of expression evaluation, otherwise
262
     * an error is thrown. This practically means this method cannot be used
263
     * on parameters declared as "volatile string" in NED; they can only be
264
     * accessed using stdstringValue().
265
     */
266
    virtual const char *stringValue(cComponent *context) const = 0;
267
268
    /**
269
     * Returns value as string. Only for string (S) type.
270
     */
271
    virtual std::string stdstringValue(cComponent *context) const = 0;
272
273
    /**
274
     * Returns value as pointer to cXMLElement. The cParImpl type must be XML.
275
     */
276
    virtual cXMLElement *xmlValue(cComponent *context) const = 0;
277
278
    /**
279
     * Returns pointer to the expression stored by the object, or NULL.
280
     */
281
    virtual cExpression *getExpression() const = 0;
282
    //@}
283
284
    /** @name Miscellaneous utility functions. */
285
    //@{
286
287
    /**
288
     * Replaces for non-const values, replaces the stored expression with its
289
     * evaluation.
290
     */
291
    virtual void convertToConst(cComponent *context) = 0;
292
293
    /**
294
     * Returns true if this expression contains const subexpressions.
295
     */
296
    virtual bool containsConstSubexpressions() const;
297
298
    /**
299
     * Evaluates const subexpressions, and replaces them with their values.
300
     * See cDynamicExpression::Elem::CONSTSUBEXPR.
301
     */
302
    virtual void evaluateConstSubexpressions(cComponent *context);
303
304
    /**
305
     * Returns the value in text form.
306
     */
307
    virtual std::string str() const = 0;
308
309
    /**
310
     * Convert the value from string, and store the result.
311
     * If the text cannot be parsed, an exception is thrown, which
312
     * can be caught as std::runtime_error& if necessary.
313
     */
314
    virtual void parse(const char *text) = 0;
315
316
    /**
317
     * Factory method: creates a parameter object representing the given type.
318
     */
319
    static cParImpl *createWithType(Type type);
320
    //@}
321
322
    /** @name Compare functions */
323
    //@{
324
325
    /**
326
     * Compares two cParImpls, including name, type, flags, stored value or expression.
327
     * Makes it possible to use cParImpl as a key in std::map or std::set.
328
     */
329
    virtual int compare(const cParImpl *other) const;
330
    //@}
331
332
    /** @name Statistics. */
333
    //@{
334
    /**
335
     * Returns the total number of objects created since the start of the program
336
     * (or since the last reset). The counter is incremented by cOwnedObject constructor.
337
     * Counter is <tt>signed</tt> to make it easier to detect if it overflows
338
     * during very long simulation runs.
339
     * May be useful for profiling or debugging memory leaks.
340
     */
341
    static long getTotalParImplObjectCount() {return total_parimpl_objs;}
342
343
    /**
344
     * Returns the number of objects that currently exist in the program.
345
     * The counter is incremented by cOwnedObject constructor and decremented by
346
     * the destructor.
347
     * May be useful for profiling or debugging memory leaks.
348
     */
349
    static long getLiveParImplObjectCount() {return live_parimpl_objs;}
350
351
    /**
352
     * Reset counters used by getTotalObjectCount() and getLiveObjectCount().
353
     * (Note that getLiveObjectCount() may go negative after a reset call.)
354
     */
355
    static void resetParImplObjectCounters()  {total_parimpl_objs=live_parimpl_objs=0L;}
356
    //@}
357
};
358
359
NAMESPACE_END
360
361
362
#endif
363