00001 00002 //useful for simple things where usability is more important than effeciency 00003 //Luke Lenhart (2005) 00004 //See /docs/License.txt for details on how this code may be used. 00005 00006 //Uninitialized type is a Vary::STRING of length 0. 00007 //Converting a string that doesn't contain a number to a number, is just 0. 00008 //A vectors of char's can be used as an input source, and is treated as a string. 00009 //When converting from real to integral types, number is rounded 00010 00011 #pragma once 00012 00013 #include <string> 00014 #include <vector> 00015 #include "Types.h" 00016 00017 namespace MPMA 00018 { 00020 class Vary 00021 { 00022 public: 00024 enum Type 00025 { 00026 INTEGER, 00027 REAL, 00028 STRING 00029 }; 00030 00031 //ctors and dtor 00032 Vary(); 00033 Vary(const Vary &o); 00034 inline Vary(const int &ival) {*this=ival;} 00035 inline Vary(const uint &ival) {*this=ival;} 00036 inline Vary(const float &rval) {*this=rval;} 00037 inline Vary(const std::string &sval) {*this=sval;} 00038 inline Vary(const char *sval) {*this=sval;} 00039 inline Vary(const std::vector<char> &sval) {*this=sval;} 00040 ~Vary(); 00041 00043 inline const Type GetType() const {return type;} 00044 00046 void Clear(); 00047 00048 //assignment 00049 const Vary& operator=(const Vary &vval); 00050 const Vary& operator=(const int &ival); 00051 inline const Vary& operator=(const uint &ival) {*this=(int)ival; return *this;} 00052 const Vary& operator=(const float &rval); 00053 const Vary& operator=(const std::string &sval); 00054 const Vary& operator=(const char *sval); 00055 const Vary& operator=(const std::vector<char> &sval); 00056 00057 //conversion (non-altering) 00058 operator const int() const; 00059 operator const std::string&() const; 00060 operator const float() const; 00061 inline const int AsInt() const {return (int)*this;} 00062 inline const std::string& AsString() const {return (const std::string&)*this;} 00063 inline const float AsFloat() const {return (float)*this;} 00064 inline const char* c_str() const { return ((const std::string&)*this).c_str(); } 00065 00066 //forced conversion of this instance into another type 00067 inline void MakeInt() {*this=(int)*this;} 00068 inline void MakeReal() {*this=(float)*this;} 00069 inline void MakeString() {*this=(const std::string&)*this;} 00070 00072 const Vary& operator+=(const Vary &var); 00073 const Vary& operator+=(const std::string &str); 00074 const Vary& operator+=(const char *str); 00075 inline friend const std::string& operator+=(std::string &str, const Vary &v) { return str+=(const std::string&)v; } 00076 //...letting implicit conversions take care of the rest 00077 00079 const Vary operator+(const Vary &var) const; 00080 inline const Vary operator+(const std::string &str) const { return *this+Vary(str); } 00081 inline const Vary operator+(const char *str) const { return *this+Vary(str); } 00082 inline friend Vary operator+(const char* str, const Vary &v) { return v+str; } 00083 inline friend Vary operator+(const std::string &str, const Vary &v) { return v+str; } 00084 //...letting implicit conversions take care of the rest 00085 00087 Vary& operator++(); //pre 00088 Vary operator++(int); //post 00090 Vary& operator--(); //pre 00091 Vary operator--(int); //post 00092 00093 //C++0x features: move semantics 00094 #ifdef USE_CPP_0X 00095 inline Vary(Vary &&o) 00096 { 00097 *this=o; 00098 } 00099 00100 inline const Vary& operator=(Vary &&o) 00101 { 00102 type=o.type; 00103 validBits=o.validBits; 00104 curInt=o.curInt; 00105 curReal=o.curReal; 00106 curStr=(std::string&&)o.curStr; 00107 00108 return *this; 00109 } 00110 #endif 00111 00112 private: 00113 Type type; //current type of us 00114 00115 //contents of us (current type has the actual value, the rest are just cache then) 00116 mutable std::string curStr; 00117 mutable int curInt; 00118 mutable float curReal; 00119 00120 mutable uint validBits; //which members are up to date 00121 }; 00122 00123 } //namespace MISC