OMNeT++ API 6.1
Discrete Event Simulation Library
cvalue.h
1 //==========================================================================
2 // CVALUE.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_CVALUE_H
17 #define __OMNETPP_CVALUE_H
18 
19 #include <string>
20 #include "simkerneldefs.h"
21 #include "cexception.h"
22 #include "opp_string.h"
23 #include "opp_pooledstring.h"
24 #include "any_ptr.h"
25 #include "simutil.h"
26 
27 namespace omnetpp {
28 
29 class cPar;
30 class cXMLElement;
31 class cDynamicExpression;
32 
47 class SIM_API cValue
48 {
49  friend class cDynamicExpression;
50  public:
54  // Note: char codes need to be present and be consistent with cNedFunction::getArgTypes()!
55  enum Type {
56  UNDEF = 0,
57  BOOL = 'B',
58  INT = 'L',
59  DOUBLE = 'D',
60  STRING = 'S',
61  POINTER = 'O',
62  OBJECT OPP_DEPRECATED_ENUMERATOR("and use cValue::containsObject()") = POINTER,
63  XML OPP_DEPRECATED_ENUMERATOR("and use cValue::containsXml()") = POINTER,
64  DBL OPP_DEPRECATED_ENUMERATOR("renamed to DOUBLE") = DOUBLE,
65  STR OPP_DEPRECATED_ENUMERATOR("renamed to STRING") = STRING
66  };
67  Type type;
68 
69  private:
70  bool bl;
71  intval_t intv;
72  double dbl;
73  opp_staticpooledstring unit = nullptr; // for INT/DOUBLE; may be nullptr
74  std::string s;
75  any_ptr ptr;
76  static const char *OVERFLOW_MSG;
77 
78  private:
79 #ifdef NDEBUG
80  void assertType(Type) const {}
81 #else
82  void assertType(Type t) const {if (type!=t) cannotCastError(t);}
83 #endif
84  [[noreturn]] void cannotCastError(Type t) const;
85  public:
86  // internal, for inspectors only:
87  static cObject *getContainedObject(const cValue *p);
88 
89  public:
92  cValue() {type=UNDEF;}
93  cValue(bool b) {set(b);}
94  cValue(int l) {set((intval_t)l);}
95  cValue(int l, const char *unit) {set((intval_t)l, unit);}
96  cValue(intval_t l) {set(l);}
97  cValue(intval_t l, const char *unit) {set(l, unit);}
98  cValue(double d) {set(d);}
99  cValue(double d, const char *unit) {set(d,unit);}
100  cValue(const char *s) {set(s);}
101  cValue(const std::string& s) {set(s);}
102  cValue(const opp_string& s) {set(s);}
103  cValue(any_ptr ptr) {set(ptr);}
104  cValue(cObject *obj) {set(obj);}
105  cValue(const void *) = delete; // prevent non-cObject pointers from silently being converted to bool
106  cValue(const cValue&) = default;
108 
112  void operator=(const cValue& other);
113 
119  Type getType() const {return type;}
120 
124  const char *getTypeName() const {return getTypeName(type);}
125 
129  static const char *getTypeName(Type t);
130 
134  bool isNumeric() const {return type==DOUBLE || type==INT;}
135 
139  bool isSet() const {return type!=UNDEF;}
140 
144  std::string str() const;
145 
151  bool operator==(const cValue& other);
152 
159  static double convertUnit(double d, const char *unit, const char *targetUnit);
160 
166  static double parseQuantity(const char *str, const char *expectedUnit=nullptr);
167 
178  static double parseQuantity(const char *str, std::string& outActualUnit);
180 
183 
187  void set(bool b) {type=BOOL; bl=b;}
188 
194  void set(intval_t l, const char *unit=nullptr) {type=INT; intv=l; this->unit=unit;}
195 
202  void set(int l, const char *unit=nullptr) {set((intval_t)l, unit);}
203 
209  void set(double d, const char *unit=nullptr) {type=DOUBLE; dbl=d; this->unit=unit;}
210 
215  void setPreservingUnit(intval_t l) {assertType(INT); intv=l;}
216 
221  void setPreservingUnit(double d) {assertType(DOUBLE); dbl=d;}
222 
229  void setUnit(const char *unit);
230 
240  void convertTo(const char *unit);
241 
246  void convertToDouble();
247 
252  void set(const char *s) {type=STRING; this->s=s?s:"";}
253 
257  void set(const std::string& s) {type=STRING; this->s=s;}
258 
262  void set(const opp_string& s) {type=STRING; this->s=s.c_str();}
263 
269  void set(any_ptr ptr) {type=POINTER; this->ptr=ptr;}
270 
277  void set(cObject *obj) {set(toAnyPtr(obj));}
278 
282  void set(const void *) = delete;
284 
287 
291  bool boolValue() const {assertType(BOOL); return bl;}
292 
298  intval_t intValue() const;
299 
304  intval_t intValueRaw() const;
305 
311  intval_t intValueInUnit(const char *targetUnit) const;
312 
319  double doubleValue() const;
320 
326  double doubleValueRaw() const;
327 
333  double doubleValueInUnit(const char *targetUnit) const;
334 
339  const char *getUnit() const {return (type==DOUBLE || type==INT) ? unit.c_str() : nullptr;}
340 
344  const char *stringValue() const {assertType(STRING); return s.c_str();}
345 
349  const std::string& stdstringValue() const {assertType(STRING); return s;}
350 
354  any_ptr pointerValue() const {assertType(POINTER); return ptr;}
355 
359  bool isNullptr() const {return type==cValue::POINTER && ptr == nullptr;}
360 
364  bool containsObject() const;
365 
369  bool containsXML() const;
370 
375  cObject *objectValue() const;
376 
381  cXMLElement *xmlValue() const;
383 
386 
390  cValue& operator=(bool b) {set(b); return *this;}
391 
395  cValue& operator=(char c) {set((intval_t)c); return *this;}
396 
400  cValue& operator=(unsigned char c) {set((intval_t)c); return *this;}
401 
405  cValue& operator=(short i) {set((intval_t)i); return *this;}
406 
410  cValue& operator=(unsigned short i) {set((intval_t)i); return *this;}
411 
415  cValue& operator=(int i) {set((intval_t)i); return *this;}
416 
420  cValue& operator=(unsigned int i) {set((intval_t)i); return *this;}
421 
425  cValue& operator=(long l) {set((intval_t)l); return *this;}
426 
430  cValue& operator=(unsigned long l) {set(checked_int_cast<intval_t>(l, OVERFLOW_MSG)); return *this;}
431 
435  cValue& operator=(long long l) {set(checked_int_cast<intval_t>(l, OVERFLOW_MSG)); return *this;}
436 
440  cValue& operator=(unsigned long long l) {set(checked_int_cast<intval_t>(l, OVERFLOW_MSG)); return *this;}
441 
445  cValue& operator=(double d) {set(d); return *this;}
446 
450  cValue& operator=(long double d) {set((double)d); return *this;}
451 
455  cValue& operator=(const char *s) {set(s); return *this;}
456 
460  cValue& operator=(const std::string& s) {set(s); return *this;}
461 
465  cValue& operator=(any_ptr ptr) {set(ptr); return *this;}
466 
470  cValue& operator=(cObject *obj) {set(obj); return *this;}
471 
475  operator bool() const {return boolValue();}
476 
481  operator char() const {return checked_int_cast<char>(intValue(), OVERFLOW_MSG);}
482 
487  operator unsigned char() const {return checked_int_cast<unsigned char>(intValue(), OVERFLOW_MSG);}
488 
493  operator int() const {return checked_int_cast<int>(intValue(), OVERFLOW_MSG);}
494 
499  operator unsigned int() const {return checked_int_cast<unsigned int>(intValue(), OVERFLOW_MSG);}
500 
505  operator short() const {return checked_int_cast<short>(intValue(), OVERFLOW_MSG);}
506 
511  operator unsigned short() const {return checked_int_cast<unsigned short>(intValue(), OVERFLOW_MSG);}
512 
517  operator long() const {return checked_int_cast<long>(intValue(), OVERFLOW_MSG);}
518 
523  operator unsigned long() const {return checked_int_cast<unsigned long>(intValue(), OVERFLOW_MSG);}
524 
529  operator long long() const {return checked_int_cast<long long>(intValue(), OVERFLOW_MSG);}
530 
535  operator unsigned long long() const {return checked_int_cast<unsigned long long>(intValue(), OVERFLOW_MSG);}
536 
540  operator double() const {return doubleValue();}
541 
546  operator long double() const {return doubleValue();}
547 
551  operator const char *() const {return stringValue();}
552 
556  operator std::string() const {return stdstringValue();}
557 
561  operator any_ptr() const {return pointerValue();}
562 
566  operator cObject *() const {return objectValue();}
567 
572  operator cXMLElement *() const {return xmlValue();}
574 };
575 
576 // cNedValue was renamed cValue in OMNeT++ 6.0; typedef added for compatibility
577 typedef cValue cNedValue;
578 typedef cValue cNEDValue;
579 
580 } // namespace omnetpp
581 
582 #endif
583 
584 
omnetpp::cValue::operator=
cValue & operator=(any_ptr ptr)
Definition: cvalue.h:465
omnetpp::cValue::set
void set(double d, const char *unit=nullptr)
Definition: cvalue.h:209
omnetpp::cValue::operator=
cValue & operator=(cObject *obj)
Definition: cvalue.h:470
omnetpp::cValue::operator=
cValue & operator=(double d)
Definition: cvalue.h:445
omnetpp::cValue::stdstringValue
const std::string & stdstringValue() const
Definition: cvalue.h:349
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::cValue::boolValue
bool boolValue() const
Definition: cvalue.h:291
omnetpp::cValue::operator=
cValue & operator=(bool b)
Definition: cvalue.h:390
omnetpp::cValue::set
void set(const std::string &s)
Definition: cvalue.h:257
omnetpp::cValue::setPreservingUnit
void setPreservingUnit(intval_t l)
Definition: cvalue.h:215
omnetpp::cValue::operator=
cValue & operator=(unsigned int i)
Definition: cvalue.h:420
omnetpp::any_ptr
A type-safe equivalent of the void* pointer.
Definition: any_ptr.h:61
omnetpp::opp_string
Lightweight string class, used internally in some parts of OMNeT++.
Definition: opp_string.h:39
omnetpp::cValue::set
void set(cObject *obj)
Definition: cvalue.h:277
omnetpp::cValue::operator=
cValue & operator=(unsigned long l)
Definition: cvalue.h:430
omnetpp::opp_string::c_str
const char * c_str() const
Definition: opp_string.h:97
omnetpp::cValue::isNullptr
bool isNullptr() const
Definition: cvalue.h:359
omnetpp::cValue::operator=
cValue & operator=(unsigned long long l)
Definition: cvalue.h:440
omnetpp::cDynamicExpression
A stack-based expression evaluator class, for dynamically created expressions.
Definition: cdynamicexpression.h:40
omnetpp::cValue::set
void set(const char *s)
Definition: cvalue.h:252
omnetpp::cValue::getType
Type getType() const
Definition: cvalue.h:119
omnetpp::cValue::operator=
cValue & operator=(long l)
Definition: cvalue.h:425
omnetpp::cValue::pointerValue
any_ptr pointerValue() const
Definition: cvalue.h:354
omnetpp::cValue::set
void set(intval_t l, const char *unit=nullptr)
Definition: cvalue.h:194
omnetpp::cValue::set
void set(any_ptr ptr)
Definition: cvalue.h:269
omnetpp::cValue::set
void set(bool b)
Definition: cvalue.h:187
omnetpp::cValue::operator=
cValue & operator=(const std::string &s)
Definition: cvalue.h:460
omnetpp::cValue::getTypeName
const char * getTypeName() const
Definition: cvalue.h:124
omnetpp::opp_staticpooledstring
Definition: opp_pooledstring.h:29
omnetpp::cValue::getUnit
const char * getUnit() const
Definition: cvalue.h:339
omnetpp::cValue::operator=
cValue & operator=(long long l)
Definition: cvalue.h:435
omnetpp::cValue::operator=
cValue & operator=(unsigned char c)
Definition: cvalue.h:400
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::cValue::operator=
cValue & operator=(unsigned short i)
Definition: cvalue.h:410
omnetpp::cValue::operator=
cValue & operator=(long double d)
Definition: cvalue.h:450
omnetpp::cValue::operator=
cValue & operator=(short i)
Definition: cvalue.h:405
omnetpp::cValue::operator=
cValue & operator=(const char *s)
Definition: cvalue.h:455
omnetpp::cXMLElement
Represents an XML element in an XML configuration file.
Definition: cxmlelement.h:75
omnetpp::cValue::set
void set(int l, const char *unit=nullptr)
Definition: cvalue.h:202
omnetpp::cValue::operator=
cValue & operator=(int i)
Definition: cvalue.h:415
omnetpp::cValue::operator=
cValue & operator=(char c)
Definition: cvalue.h:395
omnetpp::cValue::isNumeric
bool isNumeric() const
Definition: cvalue.h:134
omnetpp::cValue::stringValue
const char * stringValue() const
Definition: cvalue.h:344
omnetpp::cValue::isSet
bool isSet() const
Definition: cvalue.h:139
omnetpp::cValue::Type
Type
Definition: cvalue.h:55
omnetpp::cValue::setPreservingUnit
void setPreservingUnit(double d)
Definition: cvalue.h:221
omnetpp::cValue::set
void set(const opp_string &s)
Definition: cvalue.h:262