16 #ifndef __OMNETPP_CVALUE_H
17 #define __OMNETPP_CVALUE_H
20 #include "simkerneldefs.h"
21 #include "cexception.h"
22 #include "opp_string.h"
23 #include "opp_pooledstring.h"
31 class cDynamicExpression;
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
76 static const char *OVERFLOW_MSG;
80 void assertType(Type)
const {}
82 void assertType(Type t)
const {
if (type!=t) cannotCastError(t);}
84 [[noreturn]]
void cannotCastError(Type t)
const;
87 static cObject *getContainedObject(
const cValue *p);
92 cValue() {type=UNDEF;}
93 cValue(
bool b) {set(b);}
95 cValue(
int l,
const char *unit) {set((
intval_t)l, unit);}
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;
106 cValue(
const cValue&) =
default;
112 void operator=(
const cValue& other);
129 static const char *getTypeName(Type t);
134 bool isNumeric()
const {
return type==DOUBLE || type==INT;}
139 bool isSet()
const {
return type!=UNDEF;}
144 std::string str()
const;
151 bool operator==(
const cValue& other);
159 static double convertUnit(
double d,
const char *unit,
const char *targetUnit);
166 static double parseQuantity(
const char *str,
const char *expectedUnit=
nullptr);
178 static double parseQuantity(
const char *str, std::string& outActualUnit);
187 void set(
bool b) {type=BOOL; bl=b;}
194 void set(
intval_t l,
const char *unit=
nullptr) {type=INT; intv=l; this->unit=unit;}
209 void set(
double d,
const char *unit=
nullptr) {type=DOUBLE; dbl=d; this->unit=unit;}
229 void setUnit(
const char *unit);
240 void convertTo(
const char *unit);
246 void convertToDouble();
252 void set(
const char *s) {type=STRING; this->s=s?s:
"";}
257 void set(
const std::string& s) {type=STRING; this->s=s;}
282 void set(
const void *) =
delete;
311 intval_t intValueInUnit(
const char *targetUnit)
const;
319 double doubleValue()
const;
326 double doubleValueRaw()
const;
333 double doubleValueInUnit(
const char *targetUnit)
const;
339 const char *
getUnit()
const {
return (type==DOUBLE || type==INT) ? unit.c_str() :
nullptr;}
344 const char *
stringValue()
const {assertType(STRING);
return s.c_str();}
359 bool isNullptr()
const {
return type==cValue::POINTER && ptr ==
nullptr;}
364 bool containsObject()
const;
369 bool containsXML()
const;
430 cValue&
operator=(
unsigned long l) {set(checked_int_cast<intval_t>(l, OVERFLOW_MSG));
return *
this;}
435 cValue&
operator=(
long long l) {set(checked_int_cast<intval_t>(l, OVERFLOW_MSG));
return *
this;}
440 cValue&
operator=(
unsigned long long l) {set(checked_int_cast<intval_t>(l, OVERFLOW_MSG));
return *
this;}
475 operator bool()
const {
return boolValue();}
481 operator char()
const {
return checked_int_cast<char>(intValue(), OVERFLOW_MSG);}
487 operator unsigned char()
const {
return checked_int_cast<unsigned char>(intValue(), OVERFLOW_MSG);}
493 operator int()
const {
return checked_int_cast<int>(intValue(), OVERFLOW_MSG);}
499 operator unsigned int()
const {
return checked_int_cast<unsigned int>(intValue(), OVERFLOW_MSG);}
505 operator short()
const {
return checked_int_cast<short>(intValue(), OVERFLOW_MSG);}
511 operator unsigned short()
const {
return checked_int_cast<unsigned short>(intValue(), OVERFLOW_MSG);}
517 operator long()
const {
return checked_int_cast<long>(intValue(), OVERFLOW_MSG);}
523 operator unsigned long()
const {
return checked_int_cast<unsigned long>(intValue(), OVERFLOW_MSG);}
529 operator long long()
const {
return checked_int_cast<long long>(intValue(), OVERFLOW_MSG);}
535 operator unsigned long long()
const {
return checked_int_cast<unsigned long long>(intValue(), OVERFLOW_MSG);}
540 operator double()
const {
return doubleValue();}
546 operator long double()
const {
return doubleValue();}
551 operator const char *()
const {
return stringValue();}
556 operator std::string()
const {
return stdstringValue();}
561 operator any_ptr()
const {
return pointerValue();}
566 operator cObject *()
const {
return objectValue();}
577 typedef cValue cNedValue;
578 typedef cValue cNEDValue;