OMNeT++ Simulation Library  6.0.3
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;
220 
223 
227  cPar& setBoolValue(bool b);
228 
232  cPar& setIntValue(intval_t l);
233 
237  cPar& setDoubleValue(double d);
238 
244  cPar& setStringValue(const char *s);
245 
249  cPar& setStringValue(const std::string& s) {setStringValue(s.c_str()); return *this;}
250 
254  cPar& setObjectValue(cObject *object);
255 
259  cPar& setXMLValue(cXMLElement *node);
260 
267  cPar& setValue(const cValue& value);
268 
283  cPar& setExpression(cExpression *e, cComponent *evalcontext=nullptr);
284 
291  void setEvaluationContext(cComponent *ctx) {evalContext = ctx;}
293 
296 
300  bool boolValue() const;
301 
306  intval_t intValue() const;
307 
312  double doubleValue() const;
313 
319  double doubleValueInUnit(const char *targetUnit) const;
320 
326  const char *getUnit() const;
327 
336  const char *stringValue() const;
337 
341  std::string stdstringValue() const;
342 
347  bool isEmptyString() const {return stdstringValue().empty();}
348 
378  cObject *objectValue() const;
379 
394  cXMLElement *xmlValue() const;
395 
401  cValue getValue() const;
402 
406  cExpression *getExpression() const;
407 
418  cComponent *getEvaluationContext() const {return evalContext;}
420 
427  void convertToConst() {doConvertToConst(false);}
428 
437  void parse(const char *text, const char *baseDirectory=nullptr, FileLine loc=FileLine());
439 
442 
446  cPar& operator=(bool b) {return setBoolValue(b);}
447 
451  cPar& operator=(char c) {return setIntValue(c);}
452 
456  cPar& operator=(unsigned char c) {return setIntValue(c);}
457 
461  cPar& operator=(int i) {return setIntValue(i);}
462 
466  cPar& operator=(unsigned int i) {return setIntValue(i);}
467 
471  cPar& operator=(short i) {return setIntValue(i);}
472 
476  cPar& operator=(unsigned short i) {return setIntValue(i);}
477 
481  cPar& operator=(long i) {return setIntValue(i);}
482 
486  cPar& operator=(unsigned long i) {return setIntValue(checked_int_cast<intval_t>(i, this));}
487 
491  cPar& operator=(long long i) {return setIntValue(checked_int_cast<intval_t>(i, this));}
492 
496  cPar& operator=(unsigned long long i) {return setIntValue(checked_int_cast<intval_t>(i, this));}
497 
501  cPar& operator=(double d) {return setDoubleValue(d);}
502 
506  cPar& operator=(long double d) {return setDoubleValue((double)d);}
507 
511  cPar& operator=(const char *s) {return setStringValue(s);}
512 
516  cPar& operator=(const std::string& s) {return setStringValue(s);}
517 
521  cPar& operator=(cObject *object) {return setObjectValue(object);}
522 
526  cPar& operator=(cXMLElement *node) {return setXMLValue(node);}
527 
531  operator bool() const {return boolValue();}
532 
537  operator char() const {return checked_int_cast<char>(intValue(), this);}
538 
543  operator unsigned char() const {return checked_int_cast<unsigned char>(intValue(), this);}
544 
549  operator int() const {return checked_int_cast<int>(intValue(), this);}
550 
555  operator unsigned int() const {return checked_int_cast<unsigned int>(intValue(), this);}
556 
561  operator short() const {return checked_int_cast<short>(intValue(), this);}
562 
567  operator unsigned short() const {return checked_int_cast<unsigned short>(intValue(), this);}
568 
573  operator long() const {return checked_int_cast<long>(intValue(), this);}
574 
579  operator unsigned long() const {return checked_int_cast<unsigned long>(intValue(), this);}
580 
585  operator long long() const {return checked_int_cast<long long>(intValue(), this);}
586 
591  operator unsigned long long() const {return checked_int_cast<unsigned long long>(intValue(), this);}
592 
596  operator double() const {return doubleValue();}
597 
601  operator long double() const {return doubleValue();}
602 
606  operator const char *() const {return stringValue();}
607 
611  operator std::string() const {return stdstringValue();}
612 
616  operator cObject *() const {return objectValue();}
617 
622  operator cXMLElement *() const {return xmlValue();}
624 };
625 
626 } // namespace omnetpp
627 
628 
629 #endif
630 
631 
632 
633 
omnetpp::cPar::operator=
cPar & operator=(long double d)
Definition: cpar.h:506
omnetpp::cPar::operator=
cPar & operator=(unsigned char c)
Definition: cpar.h:456
omnetpp::cPar::operator=
cPar & operator=(const std::string &s)
Definition: cpar.h:516
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:451
omnetpp::cPar::setStringValue
cPar & setStringValue(const std::string &s)
Definition: cpar.h:249
omnetpp::cPar::operator=
cPar & operator=(const char *s)
Definition: cpar.h:511
omnetpp::cExpression
Abstract base class for expression evaluators.
Definition: cexpression.h:33
omnetpp::cPar::operator=
cPar & operator=(unsigned int i)
Definition: cpar.h:466
omnetpp::cPar::operator=
cPar & operator=(cXMLElement *node)
Definition: cpar.h:526
omnetpp::cPar::convertToConst
void convertToConst()
Definition: cpar.h:427
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:471
omnetpp::cPar::operator=
cPar & operator=(unsigned short i)
Definition: cpar.h:476
omnetpp::cPar::operator=
cPar & operator=(unsigned long long i)
Definition: cpar.h:496
omnetpp::cPar::operator=
cPar & operator=(cObject *object)
Definition: cpar.h:521
omnetpp::FileLine
Definition: fileline.h:27
omnetpp::cPar::setEvaluationContext
void setEvaluationContext(cComponent *ctx)
Definition: cpar.h:291
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:446
omnetpp::cPar::getEvaluationContext
cComponent * getEvaluationContext() const
Definition: cpar.h:418
omnetpp::cPar::operator=
cPar & operator=(long long i)
Definition: cpar.h:491
omnetpp::cPar::operator=
cPar & operator=(long i)
Definition: cpar.h:481
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:461
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:347
omnetpp::cPar::operator=
cPar & operator=(double d)
Definition: cpar.h:501
omnetpp::cPar::operator=
cPar & operator=(unsigned long i)
Definition: cpar.h:486