cnedvalue.h
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __CNEDVALUE_H
00019 #define __CNEDVALUE_H
00020
00021 #include <string>
00022 #include "simkerneldefs.h"
00023 #include "cexception.h"
00024
00025 NAMESPACE_BEGIN
00026
00027 class cPar;
00028 class cXMLElement;
00029 class cDynamicExpression;
00030
00062 class SIM_API cNEDValue
00063 {
00064 friend class cDynamicExpression;
00065 public:
00069
00070 enum Type {UNDEF=0, BOOL='B', DBL='D', STR='S', XML='X'} type;
00071
00072 private:
00073 bool bl;
00074 double dbl;
00075 const char *dblunit;
00076 std::string s;
00077 cXMLElement *xml;
00078
00079 private:
00080 #ifdef NDEBUG
00081 void assertType(Type) const {}
00082 #else
00083 void assertType(Type t) const {if (type!=t) cannotCastError(t);}
00084 #endif
00085 void cannotCastError(Type t) const;
00086
00087 public:
00090 cNEDValue() {type=UNDEF;}
00091 cNEDValue(bool b) {set(b);}
00092 cNEDValue(long l) {set(l);}
00093 cNEDValue(double d) {set(d);}
00094 cNEDValue(double d, const char *unit) {set(d,unit);}
00095 cNEDValue(const char *s) {set(s);}
00096 cNEDValue(const std::string& s) {set(s);}
00097 cNEDValue(cXMLElement *x) {set(x);}
00098 cNEDValue(const cPar& par) {set(par);}
00100
00104 void operator=(const cNEDValue& other);
00105
00111 Type getType() const {return type;}
00112
00116 static const char *getTypeName(Type t);
00117
00121 bool isNumeric() const {return type==DBL;}
00122
00126 bool isSet() const {return type!=UNDEF;}
00127
00131 std::string str() const;
00132
00139 static double convertUnit(double d, const char *unit, const char *targetUnit);
00140
00146 static double parseQuantity(const char *str, const char *expectedUnit=NULL);
00147
00158 static double parseQuantity(const char *str, std::string& outActualUnit);
00159
00168 static const char *getPooled(const char *s);
00170
00173
00177 void set(bool b) {type=BOOL; bl=b;}
00178
00182 void set(long l) {type=DBL; dbl=l; dblunit=NULL;}
00183
00189 void set(double d, const char *unit=NULL) {type=DBL; dbl=d; dblunit=unit;}
00190
00195 void setPreservingUnit(double d) {assertType(DBL); dbl=d;}
00196
00203 void setUnit(const char *unit) {assertType(DBL); dblunit=unit;}
00204
00214 void convertTo(const char *unit);
00215
00220 void set(const char *s) {type=STR; this->s=s?s:"";}
00221
00225 void set(const std::string& s) {type=STR; this->s=s;}
00226
00230 void set(cXMLElement *x) {type=XML; xml=x;}
00231
00235 void set(const cPar& par);
00237
00240
00244 bool boolValue() const {assertType(BOOL); return bl;}
00245
00249 long longValue() const {assertType(DBL); return (long)dbl;}
00250
00254 double doubleValue() const {assertType(DBL); return dbl;}
00255
00261 double doubleValueInUnit(const char *unit) const {return convertUnit(dbl, dblunit, unit);}
00262
00267 const char *getUnit() const {assertType(DBL); return dblunit;}
00268
00272 const char *stringValue() const {assertType(STR); return s.c_str();}
00273
00277 const std::string& stdstringValue() const {assertType(STR); return s;}
00278
00282 cXMLElement *xmlValue() const {assertType(XML); return xml;}
00284
00287
00291 cNEDValue& operator=(bool b) {set(b); return *this;}
00292
00296 cNEDValue& operator=(char c) {set((long)c); return *this;}
00297
00301 cNEDValue& operator=(unsigned char c) {set((long)c); return *this;}
00302
00306 cNEDValue& operator=(int i) {set((long)i); return *this;}
00307
00311 cNEDValue& operator=(unsigned int i) {set((long)i); return *this;}
00312
00316 cNEDValue& operator=(short i) {set((long)i); return *this;}
00317
00321 cNEDValue& operator=(unsigned short i) {set((long)i); return *this;}
00322
00326 cNEDValue& operator=(long l) {set(l); return *this;}
00327
00331 cNEDValue& operator=(unsigned long l) {set((long)l); return *this;}
00332
00336 cNEDValue& operator=(double d) {set(d); return *this;}
00337
00341 cNEDValue& operator=(long double d) {set((double)d); return *this;}
00342
00346 cNEDValue& operator=(const char *s) {set(s); return *this;}
00347
00351 cNEDValue& operator=(const std::string& s) {set(s); return *this;}
00352
00356 cNEDValue& operator=(cXMLElement *node) {set(node); return *this;}
00357
00361 cNEDValue& operator=(const cPar& par) {set(par); return *this;}
00362
00366 operator bool() const {return boolValue();}
00367
00372 operator char() const {return (char)longValue();}
00373
00378 operator unsigned char() const {return (unsigned char)longValue();}
00379
00384 operator int() const {return (int)longValue();}
00385
00390 operator unsigned int() const {return (unsigned int)longValue();}
00391
00396 operator short() const {return (short)longValue();}
00397
00402 operator unsigned short() const {return (unsigned short)longValue();}
00403
00407 operator long() const {return longValue();}
00408
00413 operator unsigned long() const {return longValue();}
00414
00418 operator double() const {return doubleValue();}
00419
00424 operator long double() const {return doubleValue();}
00425
00429 operator const char *() const {return stringValue();}
00430
00434 operator std::string() const {return stdstringValue();}
00435
00440 operator cXMLElement *() const {return xmlValue();}
00442 };
00443
00444 NAMESPACE_END
00445
00446 #endif
00447
00448