OMNeT++ Simulation Library  5.6.1
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 "simutil.h"
23 
24 namespace omnetpp {
25 
26 class cParImpl;
27 class cExpression;
28 class cXMLElement;
29 class cProperties;
30 class cComponent;
31 
68 class SIM_API cPar : public cObject
69 {
70  friend class cComponent;
71  public:
72  enum Type {
73  BOOL = 'B',
74  DOUBLE = 'D',
75  INT = 'L',
76  STRING = 'S',
77  XML = 'X',
78  LONG = INT // for backward compatibility
79  };
80 
81  private:
82  cComponent *ownerComponent;
83  cParImpl *p;
84  cComponent *evalContext;
85 
86  private:
87  // private constructor and destructor -- only cComponent is allowed to create parameters
88  cPar() {ownerComponent = evalContext = nullptr; p = nullptr;}
89  virtual ~cPar();
90  // internal, called from cComponent
91  void init(cComponent *ownercomponent, cParImpl *p);
92  // internal
93  void moveto(cPar& other);
94  // internal: called each time before the value of this object changes.
95  void beforeChange();
96  // internal: called each time after the value of this object changes.
97  void afterChange();
98 
99  public:
100  // internal, used by cComponent::finalizeParameters()
101  void read();
102  // internal, used by cComponent::finalizeParameters()
103  void finalize();
104  // internal: applies the default value if there is one
105  void acceptDefault();
106  // internal
107  void setImpl(cParImpl *p);
108  // internal
109  cParImpl *impl() const {return p;}
110  // internal
111  cParImpl *copyIfShared();
112 
113 #ifdef SIMFRONTEND_SUPPORT
114  // internal
115  virtual bool hasChangedSince(int64_t lastRefreshSerial);
116 #endif
117 
118  public:
124  void operator=(const cPar& other);
125 
129  virtual const char *getName() const override;
130 
134  virtual std::string str() const override;
135 
141  virtual cObject *getOwner() const override; // note: cannot return cComponent* (covariant return type) due to declaration order
142 
147  virtual void forEachChild(cVisitor *v) override;
149 
155  Type getType() const;
156 
160  static const char *getTypeName(Type t);
161 
165  bool isNumeric() const;
166 
171  bool isVolatile() const;
172 
178  bool isExpression() const;
179 
185  bool isShared() const;
186 
192  bool isSet() const;
193 
199  bool containsValue() const;
200 
205  cProperties *getProperties() const;
207 
210 
214  cPar& setBoolValue(bool b);
215 
219  cPar& setIntValue(intpar_t l);
220 
224  _OPPDEPRECATED cPar& setLongValue(intpar_t l) {return setIntValue(l);}
225 
229  cPar& setDoubleValue(double d);
230 
236  cPar& setStringValue(const char *s);
237 
241  cPar& setStringValue(const std::string& s) {setStringValue(s.c_str()); return *this;}
242 
246  cPar& setXMLValue(cXMLElement *node);
247 
262  cPar& setExpression(cExpression *e, cComponent *evalcontext=nullptr);
263 
270  void setEvaluationContext(cComponent *ctx) {evalContext = ctx;}
272 
275 
279  bool boolValue() const;
280 
285  intpar_t intValue() const;
286 
290  _OPPDEPRECATED intpar_t longValue() const {return intValue();}
291 
296  double doubleValue() const;
297 
303  double doubleValueInUnit(const char *targetUnit) const;
304 
310  const char *getUnit() const;
311 
320  const char *stringValue() const;
321 
325  std::string stdstringValue() const;
326 
331  bool isEmptyString() const {return stdstringValue().empty();}
332 
347  cXMLElement *xmlValue() const;
348 
352  cExpression *getExpression() const;
353 
364  cComponent *getEvaluationContext() const {return evalContext;}
366 
373  void convertToConst();
374 
383  void parse(const char *text);
385 
388 
392  cPar& operator=(bool b) {return setBoolValue(b);}
393 
397  cPar& operator=(char c) {return setIntValue(c);}
398 
402  cPar& operator=(unsigned char c) {return setIntValue(c);}
403 
407  cPar& operator=(int i) {return setIntValue(i);}
408 
412  cPar& operator=(unsigned int i) {return setIntValue(i);}
413 
417  cPar& operator=(short i) {return setIntValue(i);}
418 
422  cPar& operator=(unsigned short i) {return setIntValue(i);}
423 
427  cPar& operator=(long i) {return setIntValue(i);}
428 
432  cPar& operator=(unsigned long i) {return setIntValue(checked_int_cast<intpar_t>(i, this));}
433 
437  cPar& operator=(long long i) {return setIntValue(checked_int_cast<intpar_t>(i, this));}
438 
442  cPar& operator=(unsigned long long i) {return setIntValue(checked_int_cast<intpar_t>(i, this));}
443 
447  cPar& operator=(double d) {return setDoubleValue(d);}
448 
452  cPar& operator=(long double d) {return setDoubleValue((double)d);}
453 
457  cPar& operator=(const char *s) {return setStringValue(s);}
458 
462  cPar& operator=(const std::string& s) {return setStringValue(s);}
463 
467  cPar& operator=(cXMLElement *node) {return setXMLValue(node);}
468 
472  operator bool() const {return boolValue();}
473 
478  operator char() const {return checked_int_cast<char>(intValue(), this);}
479 
484  operator unsigned char() const {return checked_int_cast<unsigned char>(intValue(), this);}
485 
490  operator int() const {return checked_int_cast<int>(intValue(), this);}
491 
496  operator unsigned int() const {return checked_int_cast<unsigned int>(intValue(), this);}
497 
502  operator short() const {return checked_int_cast<short>(intValue(), this);}
503 
508  operator unsigned short() const {return checked_int_cast<unsigned short>(intValue(), this);}
509 
514  operator long() const {return checked_int_cast<long>(intValue(), this);}
515 
520  operator unsigned long() const {return checked_int_cast<unsigned long>(intValue(), this);}
521 
526  operator long long() const {return checked_int_cast<long long>(intValue(), this);}
527 
532  operator unsigned long long() const {return checked_int_cast<unsigned long long>(intValue(), this);}
533 
537  operator double() const {return doubleValue();}
538 
542  operator long double() const {return doubleValue();}
543 
547  operator const char *() const {return stringValue();}
548 
552  operator std::string() const {return stdstringValue();}
553 
558  operator cXMLElement *() const {return xmlValue();}
560 };
561 
562 } // namespace omnetpp
563 
564 
565 #endif
566 
567 
568 
569 
Common base for module and channel classes.
Definition: ccomponent.h:48
cPar & operator=(unsigned char c)
Definition: cpar.h:402
Root of the OMNeT++ class hierarchy. cObject is a lightweight class without any data members...
Definition: cobject.h:58
cPar & operator=(long long i)
Definition: cpar.h:437
void setEvaluationContext(cComponent *ctx)
Definition: cpar.h:270
Represents an XML element in an XML configuration file.
Definition: cxmlelement.h:73
Represents a module or channel parameter.
Definition: cpar.h:68
cPar & operator=(const std::string &s)
Definition: cpar.h:462
int64_t intpar_t
Type for NED parameter values that store integers. It is guaranteed to be signed and at least as wide...
Definition: simkerneldefs.h:86
cPar & operator=(long i)
Definition: cpar.h:427
cPar & operator=(unsigned long long i)
Definition: cpar.h:442
cPar & operator=(unsigned long i)
Definition: cpar.h:432
_OPPDEPRECATED cPar & setLongValue(intpar_t l)
Definition: cpar.h:224
cPar & operator=(unsigned int i)
Definition: cpar.h:412
cPar & operator=(short i)
Definition: cpar.h:417
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
cComponent * getEvaluationContext() const
Definition: cpar.h:364
cPar & operator=(long double d)
Definition: cpar.h:452
Definition: cabstracthistogram.h:21
cPar & operator=(int i)
Definition: cpar.h:407
A collection of properties (cProperty).
Definition: cproperties.h:34
cPar & setStringValue(const std::string &s)
Definition: cpar.h:241
bool isEmptyString() const
Definition: cpar.h:331
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:54
cPar & operator=(char c)
Definition: cpar.h:397
Abstract base class for expression evaluators.
Definition: cexpression.h:33
cPar & operator=(unsigned short i)
Definition: cpar.h:422
_OPPDEPRECATED intpar_t longValue() const
Definition: cpar.h:290
cPar & operator=(cXMLElement *node)
Definition: cpar.h:467
cPar & operator=(double d)
Definition: cpar.h:447
Internal class that stores values for cPar objects.
Definition: cparimpl.h:44
cPar & operator=(bool b)
Definition: cpar.h:392
cPar & operator=(const char *s)
Definition: cpar.h:457