cvalue.h Source File¶

API: cvalue.h Source File
API
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
27namespace omnetpp {
28
29class cPar;
30class cXMLElement;
32
47class 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
116
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);
179
183 static const char *getBestUnit(double d, const char *unit);
184
195 static std::string formatQuantity(double d, const char *unit, int maxSignificantDigits=6, const char *groupSeparator="'");
196
204 static std::string formatQuantityInBestUnit(double d, const char *unit, int maxSignificantDigits=6, const char *groupSeparator="'");
206
209
213 void set(bool b) {type=BOOL; bl=b;}
214
220 void set(intval_t l, const char *unit=nullptr) {type=INT; intv=l; this->unit=unit;}
221
228 void set(int l, const char *unit=nullptr) {set((intval_t)l, unit);}
229
235 void set(double d, const char *unit=nullptr) {type=DOUBLE; dbl=d; this->unit=unit;}
236
241 void setPreservingUnit(intval_t l) {assertType(INT); intv=l;}
242
247 void setPreservingUnit(double d) {assertType(DOUBLE); dbl=d;}
248
255 void setUnit(const char *unit);
256
266 void convertTo(const char *unit);
267
273
278 void set(const char *s) {type=STRING; this->s=s?s:"";}
279
283 void set(const std::string& s) {type=STRING; this->s=s;}
284
288 void set(const opp_string& s) {type=STRING; this->s=s.c_str();}
289
295 void set(any_ptr ptr) {type=POINTER; this->ptr=ptr;}
296
303 void set(cObject *obj) {set(toAnyPtr(obj));}
304
308 void set(const void *) = delete;
310
313
317 bool boolValue() const {assertType(BOOL); return bl;}
318
325
331
337 intval_t intValueInUnit(const char *targetUnit) const;
338
345 double doubleValue() const;
346
352 double doubleValueRaw() const;
353
359 double doubleValueInUnit(const char *targetUnit) const;
360
365 const char *getUnit() const {return (type==DOUBLE || type==INT) ? unit.c_str() : nullptr;}
366
370 const char *stringValue() const {assertType(STRING); return s.c_str();}
371
375 const std::string& stdstringValue() const {assertType(STRING); return s;}
376
380 any_ptr pointerValue() const {assertType(POINTER); return ptr;}
381
385 bool isNullptr() const {return type==cValue::POINTER && ptr == nullptr;}
386
390 bool containsObject() const;
391
395 bool containsXML() const;
396
402
409
412
416 cValue& operator=(bool b) {set(b); return *this;}
417
421 cValue& operator=(char c) {set((intval_t)c); return *this;}
422
426 cValue& operator=(unsigned char c) {set((intval_t)c); return *this;}
427
431 cValue& operator=(short i) {set((intval_t)i); return *this;}
432
436 cValue& operator=(unsigned short i) {set((intval_t)i); return *this;}
437
441 cValue& operator=(int i) {set((intval_t)i); return *this;}
442
446 cValue& operator=(unsigned int i) {set((intval_t)i); return *this;}
447
451 cValue& operator=(long l) {set((intval_t)l); return *this;}
452
456 cValue& operator=(unsigned long l) {set(checked_int_cast<intval_t>(l, OVERFLOW_MSG)); return *this;}
457
461 cValue& operator=(long long l) {set(checked_int_cast<intval_t>(l, OVERFLOW_MSG)); return *this;}
462
466 cValue& operator=(unsigned long long l) {set(checked_int_cast<intval_t>(l, OVERFLOW_MSG)); return *this;}
467
471 cValue& operator=(double d) {set(d); return *this;}
472
476 cValue& operator=(long double d) {set((double)d); return *this;}
477
481 cValue& operator=(const char *s) {set(s); return *this;}
482
486 cValue& operator=(const std::string& s) {set(s); return *this;}
487
491 cValue& operator=(any_ptr ptr) {set(ptr); return *this;}
492
496 cValue& operator=(cObject *obj) {set(obj); return *this;}
497
501 operator bool() const {return boolValue();}
502
507 operator char() const {return checked_int_cast<char>(intValue(), OVERFLOW_MSG);}
508
513 operator unsigned char() const {return checked_int_cast<unsigned char>(intValue(), OVERFLOW_MSG);}
514
519 operator int() const {return checked_int_cast<int>(intValue(), OVERFLOW_MSG);}
520
525 operator unsigned int() const {return checked_int_cast<unsigned int>(intValue(), OVERFLOW_MSG);}
526
531 operator short() const {return checked_int_cast<short>(intValue(), OVERFLOW_MSG);}
532
537 operator unsigned short() const {return checked_int_cast<unsigned short>(intValue(), OVERFLOW_MSG);}
538
543 operator long() const {return checked_int_cast<long>(intValue(), OVERFLOW_MSG);}
544
549 operator unsigned long() const {return checked_int_cast<unsigned long>(intValue(), OVERFLOW_MSG);}
550
555 operator long long() const {return checked_int_cast<long long>(intValue(), OVERFLOW_MSG);}
556
561 operator unsigned long long() const {return checked_int_cast<unsigned long long>(intValue(), OVERFLOW_MSG);}
562
566 operator double() const {return doubleValue();}
567
572 operator long double() const {return doubleValue();}
573
577 operator const char *() const {return stringValue();}
578
582 operator std::string() const {return stdstringValue();}
583
587 operator any_ptr() const {return pointerValue();}
588
592 operator cObject *() const {return objectValue();}
593
600 operator cXMLElement *() const {return xmlValue();}
602};
603
604// cNedValue was renamed cValue in OMNeT++ 6.0; typedef added for compatibility
605typedef cValue cNedValue;
606typedef cValue cNEDValue;
607
608} // namespace omnetpp
609
610#endif
611
612
A type-safe equivalent of the void* pointer.
Definition any_ptr.h:63
A stack-based expression evaluator class, for dynamically created expressions.
Definition cdynamicexpression.h:41
cObject is a lightweight class which serves as the root of the OMNeT++ class hierarchy....
Definition cobject.h:93
Represents a module or channel parameter.
Definition cpar.h:71
cValue & operator=(long l)
Definition cvalue.h:451
void convertToDouble()
double doubleValueInUnit(const char *targetUnit) const
intval_t intValueInUnit(const char *targetUnit) const
Type
Definition cvalue.h:55
void set(const std::string &s)
Definition cvalue.h:283
const char * getUnit() const
Definition cvalue.h:365
void set(int l, const char *unit=nullptr)
Definition cvalue.h:228
void setUnit(const char *unit)
void operator=(const cValue &other)
bool isNumeric() const
Definition cvalue.h:134
cValue & operator=(short i)
Definition cvalue.h:431
void set(double d, const char *unit=nullptr)
Definition cvalue.h:235
bool containsXML() const
cValue & operator=(unsigned long long l)
Definition cvalue.h:466
cObject * objectValue() const
static double convertUnit(double d, const char *unit, const char *targetUnit)
bool isSet() const
Definition cvalue.h:139
const std::string & stdstringValue() const
Definition cvalue.h:375
static double parseQuantity(const char *str, std::string &outActualUnit)
cValue & operator=(any_ptr ptr)
Definition cvalue.h:491
void set(const opp_string &s)
Definition cvalue.h:288
cValue & operator=(long double d)
Definition cvalue.h:476
intval_t intValue() const
cValue & operator=(unsigned short i)
Definition cvalue.h:436
double doubleValueRaw() const
cValue & operator=(double d)
Definition cvalue.h:471
const char * stringValue() const
Definition cvalue.h:370
void setPreservingUnit(intval_t l)
Definition cvalue.h:241
intval_t intValueRaw() const
cValue & operator=(long long l)
Definition cvalue.h:461
static std::string formatQuantityInBestUnit(double d, const char *unit, int maxSignificantDigits=6, const char *groupSeparator="'")
cValue & operator=(int i)
Definition cvalue.h:441
cValue & operator=(const std::string &s)
Definition cvalue.h:486
void set(any_ptr ptr)
Definition cvalue.h:295
Type getType() const
Definition cvalue.h:119
void set(bool b)
Definition cvalue.h:213
any_ptr pointerValue() const
Definition cvalue.h:380
cXMLElement * xmlValue() const
void setPreservingUnit(double d)
Definition cvalue.h:247
cValue & operator=(const char *s)
Definition cvalue.h:481
void convertTo(const char *unit)
void set(intval_t l, const char *unit=nullptr)
Definition cvalue.h:220
cValue & operator=(cObject *obj)
Definition cvalue.h:496
bool containsObject() const
static const char * getTypeName(Type t)
static double parseQuantity(const char *str, const char *expectedUnit=nullptr)
cValue & operator=(unsigned long l)
Definition cvalue.h:456
bool isNullptr() const
Definition cvalue.h:385
cValue & operator=(char c)
Definition cvalue.h:421
double doubleValue() const
void set(const void *)=delete
cValue & operator=(unsigned char c)
Definition cvalue.h:426
bool operator==(const cValue &other)
void set(const char *s)
Definition cvalue.h:278
void set(cObject *obj)
Definition cvalue.h:303
bool boolValue() const
Definition cvalue.h:317
const char * getTypeName() const
Definition cvalue.h:124
cValue & operator=(bool b)
Definition cvalue.h:416
std::string str() const
cValue & operator=(unsigned int i)
Definition cvalue.h:446
static const char * getBestUnit(double d, const char *unit)
static std::string formatQuantity(double d, const char *unit, int maxSignificantDigits=6, const char *groupSeparator="'")
Represents an XML element in an XML configuration file.
Definition cxmlelement.h:76
Definition opp_pooledstring.h:30
Lightweight string class, used internally in some parts of OMNeT++.
Definition opp_string.h:40
const char * c_str() const
Definition opp_string.h:97
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