OMNeT++ API 6.1
Discrete Event Simulation Library
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 
24 namespace omnetpp {
25 
26 class cExpression;
27 class cXMLElement;
28 class cProperties;
29 class cComponent;
30 class cValue;
31 
32 namespace internal { class cParImpl; }
33 
70 class 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:
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:
131  void operator=(const cPar& other);
132 
136  virtual const char *getName() const override;
137 
141  virtual std::string str() const override;
142 
148  virtual cObject *getOwner() const override; // note: cannot return cComponent* (covariant return type) due to declaration order
149 
154  virtual void forEachChild(cVisitor *v) override;
156 
162  Type getType() const;
163 
167  static const char *getTypeName(Type t);
168 
172  bool isNumeric() const;
173 
178  bool isVolatile() const;
179 
184  bool isMutable() const;
185 
191  bool isExpression() const;
192 
198  bool isShared() const;
199 
205  bool isSet() const;
206 
212  bool containsValue() const;
213 
218  cProperties *getProperties() const;
219 
223  cComponent *getOwnerComponent() const { return ownerComponent;}
224 
229  virtual const char *getBaseDirectory() const;
230 
235  virtual std::string getSourceLocation() const;
237 
240 
244  cPar& setBoolValue(bool b);
245 
249  cPar& setIntValue(intval_t l);
250 
254  cPar& setDoubleValue(double d);
255 
261  cPar& setStringValue(const char *s);
262 
266  cPar& setStringValue(const std::string& s) {setStringValue(s.c_str()); return *this;}
267 
271  cPar& setObjectValue(cObject *object);
272 
276  cPar& setXMLValue(cXMLElement *node);
277 
284  cPar& setValue(const cValue& value);
285 
300  cPar& setExpression(cExpression *e, cComponent *evalcontext=nullptr);
301 
308  void setEvaluationContext(cComponent *ctx) {evalContext = ctx;}
310 
313 
317  bool boolValue() const;
318 
323  intval_t intValue() const;
324 
329  double doubleValue() const;
330 
336  double doubleValueInUnit(const char *targetUnit) const;
337 
343  const char *getUnit() const;
344 
353  const char *stringValue() const;
354 
358  std::string stdstringValue() const;
359 
364  bool isEmptyString() const {return stdstringValue().empty();}
365 
395  cObject *objectValue() const;
396 
411  cXMLElement *xmlValue() const;
412 
418  cValue getValue() const;
419 
423  cExpression *getExpression() const;
424 
435  cComponent *getEvaluationContext() const {return evalContext;}
437 
444  void convertToConst() {doConvertToConst(false);}
445 
454  void parse(const char *text, const char *baseDirectory=nullptr, FileLine loc=FileLine(), bool resetEvalContext=true);
456 
459 
463  cPar& operator=(bool b) {return setBoolValue(b);}
464 
468  cPar& operator=(char c) {return setIntValue(c);}
469 
473  cPar& operator=(unsigned char c) {return setIntValue(c);}
474 
478  cPar& operator=(int i) {return setIntValue(i);}
479 
483  cPar& operator=(unsigned int i) {return setIntValue(i);}
484 
488  cPar& operator=(short i) {return setIntValue(i);}
489 
493  cPar& operator=(unsigned short i) {return setIntValue(i);}
494 
498  cPar& operator=(long i) {return setIntValue(i);}
499 
503  cPar& operator=(unsigned long i) {return setIntValue(checked_int_cast<intval_t>(i, this));}
504 
508  cPar& operator=(long long i) {return setIntValue(checked_int_cast<intval_t>(i, this));}
509 
513  cPar& operator=(unsigned long long i) {return setIntValue(checked_int_cast<intval_t>(i, this));}
514 
518  cPar& operator=(double d) {return setDoubleValue(d);}
519 
523  cPar& operator=(long double d) {return setDoubleValue((double)d);}
524 
528  cPar& operator=(const char *s) {return setStringValue(s);}
529 
533  cPar& operator=(const std::string& s) {return setStringValue(s);}
534 
538  cPar& operator=(cObject *object) {return setObjectValue(object);}
539 
543  cPar& operator=(cXMLElement *node) {return setXMLValue(node);}
544 
548  operator bool() const {return boolValue();}
549 
554  operator char() const {return checked_int_cast<char>(intValue(), this);}
555 
560  operator unsigned char() const {return checked_int_cast<unsigned char>(intValue(), this);}
561 
566  operator int() const {return checked_int_cast<int>(intValue(), this);}
567 
572  operator unsigned int() const {return checked_int_cast<unsigned int>(intValue(), this);}
573 
578  operator short() const {return checked_int_cast<short>(intValue(), this);}
579 
584  operator unsigned short() const {return checked_int_cast<unsigned short>(intValue(), this);}
585 
590  operator long() const {return checked_int_cast<long>(intValue(), this);}
591 
596  operator unsigned long() const {return checked_int_cast<unsigned long>(intValue(), this);}
597 
602  operator long long() const {return checked_int_cast<long long>(intValue(), this);}
603 
608  operator unsigned long long() const {return checked_int_cast<unsigned long long>(intValue(), this);}
609 
613  operator double() const {return doubleValue();}
614 
618  operator long double() const {return doubleValue();}
619 
623  operator const char *() const {return stringValue();}
624 
628  operator std::string() const {return stdstringValue();}
629 
633  operator cObject *() const {return objectValue();}
634 
639  operator cXMLElement *() const {return xmlValue();}
641 };
642 
643 } // namespace omnetpp
644 
645 
646 #endif
647 
648 
649 
650 
omnetpp::cPar::operator=
cPar & operator=(long double d)
Definition: cpar.h:523
omnetpp::cPar::operator=
cPar & operator=(unsigned char c)
Definition: cpar.h:473
omnetpp::cPar::operator=
cPar & operator=(const std::string &s)
Definition: cpar.h:533
omnetpp::cObject
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition: cobject.h:92
omnetpp::cValue
A variant-like value class used during evaluating NED expressions.
Definition: cvalue.h:47
omnetpp::cPar::operator=
cPar & operator=(char c)
Definition: cpar.h:468
omnetpp::cPar::setStringValue
cPar & setStringValue(const std::string &s)
Definition: cpar.h:266
omnetpp::cPar::operator=
cPar & operator=(const char *s)
Definition: cpar.h:528
omnetpp::cExpression
Abstract base class for expression evaluators.
Definition: cexpression.h:33
omnetpp::cPar::operator=
cPar & operator=(unsigned int i)
Definition: cpar.h:483
omnetpp::cPar::operator=
cPar & operator=(cXMLElement *node)
Definition: cpar.h:543
omnetpp::cPar::convertToConst
void convertToConst()
Definition: cpar.h:444
omnetpp::cVisitor
Enables traversing the tree of (cObject-rooted) simulation objects.
Definition: cvisitor.h:56
omnetpp::cPar::operator=
cPar & operator=(short i)
Definition: cpar.h:488
omnetpp::cPar::operator=
cPar & operator=(unsigned short i)
Definition: cpar.h:493
omnetpp::cPar::getOwnerComponent
cComponent * getOwnerComponent() const
Definition: cpar.h:223
omnetpp::cPar::operator=
cPar & operator=(unsigned long long i)
Definition: cpar.h:513
omnetpp::cPar::operator=
cPar & operator=(cObject *object)
Definition: cpar.h:538
omnetpp::FileLine
Definition: fileline.h:27
omnetpp::cPar::setEvaluationContext
void setEvaluationContext(cComponent *ctx)
Definition: cpar.h:308
omnetpp::cProperties
A collection of properties (cProperty).
Definition: cproperties.h:34
omnetpp::cPar
Represents a module or channel parameter.
Definition: cpar.h:70
omnetpp::cPar::operator=
cPar & operator=(bool b)
Definition: cpar.h:463
omnetpp::cPar::getEvaluationContext
cComponent * getEvaluationContext() const
Definition: cpar.h:435
omnetpp::cPar::operator=
cPar & operator=(long long i)
Definition: cpar.h:508
omnetpp::cPar::operator=
cPar & operator=(long i)
Definition: cpar.h:498
omnetpp::intval_t
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
omnetpp::internal::cParImpl
Internal class that stores values for cPar objects.
Definition: cparimpl.h:46
omnetpp::cPar::operator=
cPar & operator=(int i)
Definition: cpar.h:478
omnetpp::cXMLElement
Represents an XML element in an XML configuration file.
Definition: cxmlelement.h:75
omnetpp::cComponent
Common base for module and channel classes.
Definition: ccomponent.h:49
omnetpp::cPar::isEmptyString
bool isEmptyString() const
Definition: cpar.h:364
omnetpp::cPar::operator=
cPar & operator=(double d)
Definition: cpar.h:518
omnetpp::cPar::operator=
cPar & operator=(unsigned long i)
Definition: cpar.h:503