OMNeT++ Simulation Library  5.6.1
cnedvalue.h
1 //==========================================================================
2 // CNEDVALUE.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_CNEDVALUE_H
17 #define __OMNETPP_CNEDVALUE_H
18 
19 #include <string>
20 #include "simkerneldefs.h"
21 #include "cexception.h"
22 #include "simutil.h"
23 
24 namespace omnetpp {
25 
26 class cPar;
27 class cXMLElement;
28 class cDynamicExpression;
29 
50 class SIM_API cNedValue
51 {
52  friend class cDynamicExpression;
53  public:
57  // Note: char codes need to be present and be consistent with cNedFunction::getArgTypes()!
58  enum Type {UNDEF=0, BOOL='B', INT='L', DOUBLE='D', STRING='S', XML='X', DBL=DOUBLE, STR=STRING} type;
59 
60  private:
61  bool bl;
62  intpar_t intv;
63  double dbl;
64  const char *unit; // for INT/DOUBLE; must point to string constant or pooled string; may be nullptr
65  std::string s;
66  cXMLElement *xml;
67  static const char *OVERFLOW_MSG;
68 
69  private:
70 #ifdef NDEBUG
71  void assertType(Type) const {}
72 #else
73  void assertType(Type t) const {if (type!=t) cannotCastError(t);}
74 #endif
75  [[noreturn]] void cannotCastError(Type t) const;
76 
77  public:
80  cNedValue() {type=UNDEF;}
81  cNedValue(bool b) {set(b);}
82  cNedValue(int l) {set((intpar_t)l);}
83  cNedValue(int l, const char *unit) {set((intpar_t)l, unit);}
84  cNedValue(intpar_t l) {set(l);}
85  cNedValue(intpar_t l, const char *unit) {set(l, unit);}
86  cNedValue(double d) {set(d);}
87  cNedValue(double d, const char *unit) {set(d,unit);}
88  cNedValue(const char *s) {set(s);}
89  cNedValue(const std::string& s) {set(s);}
90  cNedValue(cXMLElement *x) {set(x);}
91  cNedValue(const cPar& par) {set(par);}
93 
97  void operator=(const cNedValue& other);
98 
104  Type getType() const {return type;}
105 
109  static const char *getTypeName(Type t);
110 
114  bool isNumeric() const {return type==DOUBLE || type==INT;}
115 
119  bool isSet() const {return type!=UNDEF;}
120 
124  std::string str() const;
125 
132  static double convertUnit(double d, const char *unit, const char *targetUnit);
133 
139  static double parseQuantity(const char *str, const char *expectedUnit=nullptr);
140 
151  static double parseQuantity(const char *str, std::string& outActualUnit);
152 
161  static const char *getPooled(const char *s);
163 
166 
170  void set(bool b) {type=BOOL; bl=b;}
171 
177  void set(intpar_t l, const char *unit=nullptr) {type=INT; intv=l; this->unit=unit;}
178 
185  void set(int l, const char *unit=nullptr) {set((intpar_t)l, unit);}
186 
192  void set(double d, const char *unit=nullptr) {type=DOUBLE; dbl=d; this->unit=unit;}
193 
198  void setPreservingUnit(intpar_t l) {assertType(INT); intv=l;}
199 
204  void setPreservingUnit(double d) {assertType(DOUBLE); dbl=d;}
205 
212  void setUnit(const char* unit);
213 
223  void convertTo(const char *unit);
224 
229  void convertToDouble();
230 
235  void set(const char *s) {type=STRING; this->s=s?s:"";}
236 
240  void set(const std::string& s) {type=STRING; this->s=s;}
241 
245  void set(cXMLElement *x) {type=XML; xml=x;}
246 
250  void set(const cPar& par);
252 
255 
259  bool boolValue() const {assertType(BOOL); return bl;}
260 
264  intpar_t intValue() const;
265 
269  _OPPDEPRECATED intpar_t longValue() const {return intValue();}
270 
276  intpar_t intValueInUnit(const char *targetUnit) const;
277 
281  double doubleValue() const;
282 
288  double doubleValueInUnit(const char *targetUnit) const;
289 
294  const char *getUnit() const {return (type==DOUBLE || type==INT) ? unit : nullptr;}
295 
299  const char *stringValue() const {assertType(STRING); return s.c_str();}
300 
304  const std::string& stdstringValue() const {assertType(STRING); return s;}
305 
309  cXMLElement *xmlValue() const {assertType(XML); return xml;}
311 
314 
318  cNedValue& operator=(bool b) {set(b); return *this;}
319 
323  cNedValue& operator=(char c) {set((intpar_t)c); return *this;}
324 
328  cNedValue& operator=(unsigned char c) {set((intpar_t)c); return *this;}
329 
333  cNedValue& operator=(short i) {set((intpar_t)i); return *this;}
334 
338  cNedValue& operator=(unsigned short i) {set((intpar_t)i); return *this;}
339 
343  cNedValue& operator=(int i) {set((intpar_t)i); return *this;}
344 
348  cNedValue& operator=(unsigned int i) {set((intpar_t)i); return *this;}
349 
353  cNedValue& operator=(long l) {set((intpar_t)l); return *this;}
354 
358  cNedValue& operator=(unsigned long l) {set(checked_int_cast<intpar_t>(l, OVERFLOW_MSG)); return *this;}
359 
363  cNedValue& operator=(long long l) {set(checked_int_cast<intpar_t>(l, OVERFLOW_MSG)); return *this;}
364 
368  cNedValue& operator=(unsigned long long l) {set(checked_int_cast<intpar_t>(l, OVERFLOW_MSG)); return *this;}
369 
373  cNedValue& operator=(double d) {set(d); return *this;}
374 
378  cNedValue& operator=(long double d) {set((double)d); return *this;}
379 
383  cNedValue& operator=(const char *s) {set(s); return *this;}
384 
388  cNedValue& operator=(const std::string& s) {set(s); return *this;}
389 
393  cNedValue& operator=(cXMLElement *node) {set(node); return *this;}
394 
398  cNedValue& operator=(const cPar& par) {set(par); return *this;}
399 
403  operator bool() const {return boolValue();}
404 
409  operator char() const {return checked_int_cast<char>(intValue(), OVERFLOW_MSG);}
410 
415  operator unsigned char() const {return checked_int_cast<unsigned char>(intValue(), OVERFLOW_MSG);}
416 
421  operator int() const {return checked_int_cast<int>(intValue(), OVERFLOW_MSG);}
422 
427  operator unsigned int() const {return checked_int_cast<unsigned int>(intValue(), OVERFLOW_MSG);}
428 
433  operator short() const {return checked_int_cast<short>(intValue(), OVERFLOW_MSG);}
434 
439  operator unsigned short() const {return checked_int_cast<unsigned short>(intValue(), OVERFLOW_MSG);}
440 
445  operator long() const {return checked_int_cast<long>(intValue(), OVERFLOW_MSG);}
446 
451  operator unsigned long() const {return checked_int_cast<unsigned long>(intValue(), OVERFLOW_MSG);}
452 
457  operator long long() const {return checked_int_cast<long long>(intValue(), OVERFLOW_MSG);}
458 
463  operator unsigned long long() const {return checked_int_cast<unsigned long long>(intValue(), OVERFLOW_MSG);}
464 
468  operator double() const {return doubleValue();}
469 
474  operator long double() const {return doubleValue();}
475 
479  operator const char *() const {return stringValue();}
480 
484  operator std::string() const {return stdstringValue();}
485 
490  operator cXMLElement *() const {return xmlValue();}
492 };
493 
494 // cNEDValue was renamed cNedValue in OMNeT++ 5.3; typedef added for compatibility
495 typedef cNedValue cNEDValue;
496 
497 } // namespace omnetpp
498 
499 #endif
500 
501 
cNedValue & operator=(const cPar &par)
Definition: cnedvalue.h:398
const char * stringValue() const
Definition: cnedvalue.h:299
Type
Definition: cnedvalue.h:58
_OPPDEPRECATED intpar_t longValue() const
Definition: cnedvalue.h:269
cNedValue & operator=(bool b)
Definition: cnedvalue.h:318
cNedValue & operator=(double d)
Definition: cnedvalue.h:373
cNedValue & operator=(const char *s)
Definition: cnedvalue.h:383
cNedValue & operator=(unsigned char c)
Definition: cnedvalue.h:328
Represents an XML element in an XML configuration file.
Definition: cxmlelement.h:73
cNedValue & operator=(long l)
Definition: cnedvalue.h:353
Represents a module or channel parameter.
Definition: cpar.h:68
cXMLElement * xmlValue() const
Definition: cnedvalue.h:309
int64_t intpar_t
Type for NED parameter values that store integers. It is guaranteed to be signed and at least as wide...
Definition: simkerneldefs.h:86
cNedValue & operator=(long double d)
Definition: cnedvalue.h:378
const std::string & stdstringValue() const
Definition: cnedvalue.h:304
cNedValue & operator=(long long l)
Definition: cnedvalue.h:363
Value used during evaluating NED expressions.
Definition: cnedvalue.h:50
void setPreservingUnit(intpar_t l)
Definition: cnedvalue.h:198
void setPreservingUnit(double d)
Definition: cnedvalue.h:204
const char * getUnit() const
Definition: cnedvalue.h:294
cNedValue & operator=(char c)
Definition: cnedvalue.h:323
bool isSet() const
Definition: cnedvalue.h:119
cNedValue & operator=(const std::string &s)
Definition: cnedvalue.h:388
cNedValue & operator=(short i)
Definition: cnedvalue.h:333
A stack-based expression evaluator class, for dynamically created expressions.
Definition: cdynamicexpression.h:36
cNedValue & operator=(unsigned long long l)
Definition: cnedvalue.h:368
bool boolValue() const
Definition: cnedvalue.h:259
Definition: cabstracthistogram.h:21
cNedValue & operator=(unsigned long l)
Definition: cnedvalue.h:358
cNedValue & operator=(unsigned int i)
Definition: cnedvalue.h:348
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:54
cNedValue & operator=(int i)
Definition: cnedvalue.h:343
Type getType() const
Definition: cnedvalue.h:104
cNedValue & operator=(cXMLElement *node)
Definition: cnedvalue.h:393
bool isNumeric() const
Definition: cnedvalue.h:114
cNedValue & operator=(unsigned short i)
Definition: cnedvalue.h:338