cpar.h Source File¶

API: cpar.h Source File
API
cpar.h
1//==========================================================================
2// CPAR.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_CPAR_H
17#define __OMNETPP_CPAR_H
18
19#include "cownedobject.h"
20#include "cexpression.h"
21#include "cexception.h"
22#include "fileline.h"
23
24namespace omnetpp {
25
26class cExpression;
27class cXMLElement;
28class cProperties;
29class cComponent;
30class cValue;
31
32namespace internal { class cParImpl; }
33
70class SIM_API cPar : public cObject
71{
72 friend class cComponent;
73 public:
74 enum Type {
75 BOOL = 'B',
76 DOUBLE = 'D',
77 INT = 'L',
78 STRING = 'S',
79 OBJECT = 'O',
80 XML = 'X',
81 LONG = INT // for backward compatibility
82 };
83
84 private:
85 typedef internal::cParImpl cParImpl;
86 cComponent *ownerComponent;
87 cParImpl *p;
88 cComponent *evalContext;
89
90 private:
91 // private constructor and destructor -- only cComponent is allowed to create parameters
92 cPar() {ownerComponent = evalContext = nullptr; p = nullptr;}
93 virtual ~cPar();
94 // internal, called from cComponent
95 void init(cComponent *ownercomponent, cParImpl *p);
96 // internal
97 void moveto(cPar& other);
98 // internal: called each time before the value of this object changes.
99 void beforeChange(bool isInternalChange=false);
100 // internal: called each time after the value of this object changes.
101 void afterChange();
102 // internal: replace expression with the value it evaluates to
103 void doConvertToConst(bool isInternalChange=true);
104
105
106 public:
107 // internal, used by cComponent::finalizeParameters()
108 void read();
109 // internal, used by cComponent::finalizeParameters()
110 void finalize();
111 // internal: applies the default value if there is one
112 void acceptDefault();
113 // internal
114 void setImpl(cParImpl *p);
115 // internal
116 cParImpl *impl() const {return p;}
117 // internal
118 cParImpl *copyIfShared();
119
120#ifdef SIMFRONTEND_SUPPORT
121 // internal
122 virtual bool hasChangedSince(int64_t lastRefreshSerial);
123#endif
124
125 public:
128
131 void operator=(const cPar& other);
132
136 virtual const char *getName() const override;
137
143 virtual std::string str() const override;
144
149 virtual std::string unparse() const;
150
156 virtual cObject *getOwner() const override; // note: cannot return cComponent* (covariant return type) due to declaration order
157
162 virtual void forEachChild(cVisitor *v) override;
164
167
170 Type getType() const;
171
175 static const char *getTypeName(Type t);
176
180 bool isNumeric() const;
181
186 bool isVolatile() const;
187
192 bool isMutable() const;
193
199 bool isExpression() const;
200
206 bool isShared() const;
207
213 bool isSet() const;
214
220 bool containsValue() const;
221
227
231 cComponent *getOwnerComponent() const { return ownerComponent;}
232
237 virtual const char *getBaseDirectory() const;
238
243 virtual std::string getSourceLocation() const;
245
248
252 cPar& setBoolValue(bool b);
253
258
262 cPar& setDoubleValue(double d);
263
269 cPar& setStringValue(const char *s);
270
274 cPar& setStringValue(const std::string& s) {setStringValue(s.c_str()); return *this;}
275
279 cPar& setObjectValue(cObject *object);
280
285
292 cPar& setValue(const cValue& value);
293
308 cPar& setExpression(cExpression *e, cComponent *evalcontext=nullptr);
309
316 void setEvaluationContext(cComponent *ctx) {evalContext = ctx;}
318
321
325 bool boolValue() const;
326
332
337 double doubleValue() const;
338
344 double doubleValueInUnit(const char *targetUnit) const;
345
351 const char *getUnit() const;
352
361 const char *stringValue() const;
362
366 std::string stdstringValue() const;
367
372 bool isEmptyString() const {return stdstringValue().empty();}
373
404
420
427
432
443 cComponent *getEvaluationContext() const {return evalContext;}
445
448
452 void convertToConst() {doConvertToConst(false);}
453
462 void parse(const char *text, const char *baseDirectory=nullptr, FileLine loc=FileLine(), bool resetEvalContext=true);
464
467
471 cPar& operator=(bool b) {return setBoolValue(b);}
472
476 cPar& operator=(char c) {return setIntValue(c);}
477
481 cPar& operator=(unsigned char c) {return setIntValue(c);}
482
486 cPar& operator=(int i) {return setIntValue(i);}
487
491 cPar& operator=(unsigned int i) {return setIntValue(i);}
492
496 cPar& operator=(short i) {return setIntValue(i);}
497
501 cPar& operator=(unsigned short i) {return setIntValue(i);}
502
506 cPar& operator=(long i) {return setIntValue(i);}
507
511 cPar& operator=(unsigned long i) {return setIntValue(checked_int_cast<intval_t>(i, this));}
512
516 cPar& operator=(long long i) {return setIntValue(checked_int_cast<intval_t>(i, this));}
517
521 cPar& operator=(unsigned long long i) {return setIntValue(checked_int_cast<intval_t>(i, this));}
522
526 cPar& operator=(double d) {return setDoubleValue(d);}
527
531 cPar& operator=(long double d) {return setDoubleValue((double)d);}
532
536 cPar& operator=(const char *s) {return setStringValue(s);}
537
541 cPar& operator=(const std::string& s) {return setStringValue(s);}
542
546 cPar& operator=(cObject *object) {return setObjectValue(object);}
547
551 cPar& operator=(cXMLElement *node) {return setXMLValue(node);}
552
556 operator bool() const {return boolValue();}
557
562 operator char() const {return checked_int_cast<char>(intValue(), this);}
563
568 operator unsigned char() const {return checked_int_cast<unsigned char>(intValue(), this);}
569
574 operator int() const {return checked_int_cast<int>(intValue(), this);}
575
580 operator unsigned int() const {return checked_int_cast<unsigned int>(intValue(), this);}
581
586 operator short() const {return checked_int_cast<short>(intValue(), this);}
587
592 operator unsigned short() const {return checked_int_cast<unsigned short>(intValue(), this);}
593
598 operator long() const {return checked_int_cast<long>(intValue(), this);}
599
604 operator unsigned long() const {return checked_int_cast<unsigned long>(intValue(), this);}
605
610 operator long long() const {return checked_int_cast<long long>(intValue(), this);}
611
616 operator unsigned long long() const {return checked_int_cast<unsigned long long>(intValue(), this);}
617
621 operator double() const {return doubleValue();}
622
626 operator long double() const {return doubleValue();}
627
631 operator const char *() const {return stringValue();}
632
636 operator std::string() const {return stdstringValue();}
637
641 operator cObject *() const {return objectValue();}
642
649 operator cXMLElement *() const {return xmlValue();}
651};
652
653} // namespace omnetpp
654
655
656#endif
657
658
659
660
Definition fileline.h:28
Common base for module and channel classes.
Definition ccomponent.h:51
Abstract base class for expression evaluators.
Definition cexpression.h:34
cObject()
Definition cobject.h:124
cPar & setStringValue(const char *s)
cPar & operator=(int i)
Definition cpar.h:486
double doubleValueInUnit(const char *targetUnit) const
cPar & operator=(char c)
Definition cpar.h:476
void operator=(const cPar &other)
cPar & operator=(unsigned long i)
Definition cpar.h:511
cPar & setDoubleValue(double d)
virtual std::string unparse() const
cExpression * getExpression() const
cPar & setExpression(cExpression *e, cComponent *evalcontext=nullptr)
const char * getUnit() const
cPar & setXMLValue(cXMLElement *node)
bool isNumeric() const
cPar & setIntValue(intval_t l)
virtual cObject * getOwner() const override
cObject * objectValue() const
cPar & setValue(const cValue &value)
bool isSet() const
cPar & setBoolValue(bool b)
cPar & operator=(unsigned long long i)
Definition cpar.h:521
cComponent * getEvaluationContext() const
Definition cpar.h:443
bool containsValue() const
cPar & operator=(long long i)
Definition cpar.h:516
bool isExpression() const
intval_t intValue() const
cPar & setObjectValue(cObject *object)
virtual std::string getSourceLocation() const
bool isMutable() const
const char * stringValue() const
virtual const char * getBaseDirectory() const
cProperties * getProperties() const
cPar & operator=(cXMLElement *node)
Definition cpar.h:551
void convertToConst()
Definition cpar.h:452
cValue getValue() const
Type getType() const
cPar & operator=(unsigned char c)
Definition cpar.h:481
cXMLElement * xmlValue() const
bool isVolatile() const
virtual const char * getName() const override
cPar & operator=(short i)
Definition cpar.h:496
cPar & setStringValue(const std::string &s)
Definition cpar.h:274
cPar & operator=(const std::string &s)
Definition cpar.h:541
static const char * getTypeName(Type t)
cPar & operator=(bool b)
Definition cpar.h:471
void parse(const char *text, const char *baseDirectory=nullptr, FileLine loc=FileLine(), bool resetEvalContext=true)
cPar & operator=(cObject *object)
Definition cpar.h:546
virtual void forEachChild(cVisitor *v) override
bool isEmptyString() const
Definition cpar.h:372
virtual std::string str() const override
bool isShared() const
void setEvaluationContext(cComponent *ctx)
Definition cpar.h:316
double doubleValue() const
cPar & operator=(unsigned int i)
Definition cpar.h:491
cPar & operator=(long double d)
Definition cpar.h:531
cPar & operator=(double d)
Definition cpar.h:526
bool boolValue() const
cPar & operator=(const char *s)
Definition cpar.h:536
cComponent * getOwnerComponent() const
Definition cpar.h:231
cPar & operator=(long i)
Definition cpar.h:506
std::string stdstringValue() const
cPar & operator=(unsigned short i)
Definition cpar.h:501
A collection of properties (cProperty).
Definition cproperties.h:35
A variant-like value class used during evaluating NED expressions.
Definition cvalue.h:48
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition cvisitor.h:57
Represents an XML element in an XML configuration file.
Definition cxmlelement.h:76
Internal class that stores values for cPar objects.
Definition cparimpl.h:47
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
ToInt checked_int_cast(FromInt x, const char *errmsg=nullptr)
Safe integer cast: it throws an exception if in case of an overflow, i.e. when if the target type can...
Definition simutil.h:46