MPMA Framework 0.4
|
00001 00002 //useful for simple things where usability is more important than effeciency 00003 //Luke Lenhart (2005-2011) 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 sint64 &ival) {*this=ival;} 00035 inline Vary(const uint64 &ival) {*this=ival;} 00036 inline Vary(const sint32 &ival) {*this=(sint64)ival;} 00037 inline Vary(const uint32 &ival) {*this=(uint64)ival;} 00038 inline Vary(const sint16 &ival) {*this=(sint64)ival;} 00039 inline Vary(const uint16 &ival) {*this=(uint64)ival;} 00040 inline Vary(const sint8 &ival) {*this=(sint64)ival;} 00041 inline Vary(const uint8 &ival) {*this=(uint64)ival;} 00042 inline Vary(const float &rval) {*this=rval;} 00043 inline Vary(const std::string &sval) {*this=sval;} 00044 inline Vary(const char *sval) {*this=sval;} 00045 inline Vary(const std::vector<char> &sval) {*this=sval;} 00046 00047 #if defined(linux) && defined(__amd64) //need a size_t version also for some reason 00048 inline Vary(const size_t &ival) {*this=(sint64)ival;} 00049 #endif 00050 00051 ~Vary(); 00052 00054 inline const Type GetType() const {return type;} 00055 00057 void Clear(); 00058 00059 //assignment 00060 const Vary& operator=(const Vary &vval); 00061 const Vary& operator=(const sint64 &ival); 00062 inline const Vary& operator=(const uint64 &ival) {*this=(sint64)ival; return *this;} 00063 const Vary& operator=(const float &rval); 00064 const Vary& operator=(const std::string &sval); 00065 const Vary& operator=(const char *sval); 00066 const Vary& operator=(const std::vector<char> &sval); 00067 00068 static Vary FromHexString(const std::string &s); 00069 00070 //conversion (non-altering) 00071 operator const sint64() const; 00072 inline operator const uint64() const {return (uint64)(sint64)*this;} 00073 inline operator const sint32() const {return (sint32)(sint64)*this;} 00074 inline operator const uint32() const {return (uint32)(sint64)*this;} 00075 inline operator const sint16() const {return (sint16)(sint64)*this;} 00076 inline operator const uint16() const {return (uint16)(sint64)*this;} 00077 inline operator const sint8() const {return (sint8)(sint64)*this;} 00078 inline operator const uint8() const {return (uint8)(sint64)*this;} 00079 operator const std::string&() const; 00080 operator const float() const; 00081 00082 #if defined(linux) && defined(__amd64) //need a size_t version also for some reason 00083 inline operator const size_t() const {return (size_t)(sint64)*this;} 00084 #endif 00085 00086 inline const sint64 AsInt() const {return (sint64)*this;} 00087 inline const std::string& AsString() const {return (const std::string&)*this;} 00088 std::string AsHexString() const; 00089 inline const float AsFloat() const {return (float)*this;} 00090 inline const char* c_str() const { return ((const std::string&)*this).c_str(); } 00091 00092 //forced conversion of this instance into another type 00093 inline void MakeInt() {*this=(sint64)*this;} 00094 inline void MakeReal() {*this=(float)*this;} 00095 inline void MakeString() {*this=(const std::string&)*this;} 00096 00098 const Vary& operator+=(const Vary &var); 00099 const Vary& operator+=(const std::string &str); 00100 const Vary& operator+=(const char *str); 00101 inline friend const std::string& operator+=(std::string &str, const Vary &v) { return str+=(const std::string&)v; } 00102 //...letting implicit conversions take care of the rest 00103 00105 const Vary operator+(const Vary &var) const; 00106 inline const Vary operator+(const std::string &str) const { return *this+Vary(str); } 00107 inline const Vary operator+(const char *str) const { return *this+Vary(str); } 00108 inline friend Vary operator+(const char* str, const Vary &v) { return Vary(str)+v; } 00109 inline friend Vary operator+(const std::string &str, const Vary &v) { return Vary(str)+v; } 00110 //...letting implicit conversions take care of the rest 00111 00113 Vary& operator++(); //pre 00114 Vary operator++(int); //post 00116 Vary& operator--(); //pre 00117 Vary operator--(int); //post 00118 00119 inline Vary(Vary &&o) 00120 { 00121 *this=o; 00122 } 00123 00124 inline const Vary& operator=(Vary &&o) 00125 { 00126 type=o.type; 00127 validBits=o.validBits; 00128 curInt=o.curInt; 00129 curReal=o.curReal; 00130 curStr=(std::string&&)o.curStr; 00131 00132 return *this; 00133 } 00134 00135 protected: 00136 Type type; //current type of us 00137 00138 //contents of us (current type has the actual value, the rest are just cache then) 00139 mutable std::string curStr; 00140 mutable sint64 curInt; 00141 mutable float curReal; 00142 00143 mutable nuint validBits; //which members are up to date 00144 }; 00145 00146 00148 class VaryString: public Vary 00149 { 00150 public: 00151 //ctors and dtor 00152 VaryString(); 00153 VaryString(const Vary &o); 00154 inline VaryString(const sint64 &ival) {*this=ival;} 00155 inline VaryString(const uint64 &ival) {*this=ival;} 00156 inline VaryString(const sint32 &ival) {*this=(sint64)ival;} 00157 inline VaryString(const uint32 &ival) {*this=(uint64)ival;} 00158 inline VaryString(const sint16 &ival) {*this=(sint64)ival;} 00159 inline VaryString(const uint16 &ival) {*this=(uint64)ival;} 00160 inline VaryString(const sint8 &ival) {*this=(sint64)ival;} 00161 inline VaryString(const uint8 &ival) {*this=(uint64)ival;} 00162 inline VaryString(const float &rval) {*this=rval;} 00163 inline VaryString(const std::string &sval) {*this=sval;} 00164 inline VaryString(const char *sval) {*this=sval;} 00165 inline VaryString(const std::vector<char> &sval) {*this=sval;} 00166 00167 #if defined(linux) && defined(__amd64) //need a size_t version also for some reason 00168 inline VaryString(const size_t &ival) {*this=(sint64)ival;} 00169 #endif 00170 00171 ~VaryString(); 00172 00173 //assignment 00174 const VaryString& operator=(const Vary &vval); 00175 const VaryString& operator=(const sint64 &ival); 00176 inline const VaryString& operator=(const uint64 &ival) {*this=(sint64)ival; return *this;} 00177 const VaryString& operator=(const float &rval); 00178 const VaryString& operator=(const std::string &sval); 00179 const VaryString& operator=(const char *sval); 00180 const VaryString& operator=(const std::vector<char> &sval); 00181 00182 //hide stuff that doesn't make sense for this type 00183 private: 00184 void MakeInt(); 00185 void MakeReal(); 00186 00187 Vary& operator++(); 00188 Vary operator++(int); 00189 }; 00190 00191 } //namespace MPMA