Vary.h

Go to the documentation of this file.
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         //conversion (non-altering)
00069         operator const sint64() const; 
00070         inline operator const uint64() const {return (uint64)(sint64)*this;}
00071         inline operator const sint32() const {return (sint32)(sint64)*this;}
00072         inline operator const uint32() const {return (uint32)(sint64)*this;}
00073         inline operator const sint16() const {return (sint16)(sint64)*this;}
00074         inline operator const uint16() const {return (uint16)(sint64)*this;}
00075         inline operator const sint8() const {return (sint8)(sint64)*this;}
00076         inline operator const uint8() const {return (uint8)(sint64)*this;}
00077         operator const std::string&() const; 
00078         operator const float() const; 
00079 
00080 #if defined(linux) && defined(__amd64) //need a size_t version also for some reason
00081         inline operator const size_t() const {return (size_t)(sint64)*this;}
00082 #endif
00083 
00084         inline const sint64 AsInt() const {return (sint64)*this;} 
00085         inline const std::string& AsString() const {return (const std::string&)*this;} 
00086         inline const float AsFloat() const {return (float)*this;} 
00087         inline const char* c_str() const { return ((const std::string&)*this).c_str(); } 
00088 
00089         //forced conversion of this instance into another type
00090         inline void MakeInt() {*this=(sint64)*this;} 
00091         inline void MakeReal() {*this=(float)*this;} 
00092         inline void MakeString() {*this=(const std::string&)*this;} 
00093 
00095         const Vary& operator+=(const Vary &var);
00096         const Vary& operator+=(const std::string &str);
00097         const Vary& operator+=(const char *str);
00098         inline friend const std::string& operator+=(std::string &str, const Vary &v) { return str+=(const std::string&)v; }
00099         //...letting implicit conversions take care of the rest
00100 
00102         const Vary operator+(const Vary &var) const;
00103         inline const Vary operator+(const std::string &str) const { return *this+Vary(str); }
00104         inline const Vary operator+(const char *str) const { return *this+Vary(str); }
00105         inline friend Vary operator+(const char* str, const Vary &v) { return Vary(str)+v; }
00106         inline friend Vary operator+(const std::string &str, const Vary &v) { return Vary(str)+v; }
00107         //...letting implicit conversions take care of the rest
00108 
00110         Vary& operator++(); //pre
00111         Vary operator++(int); //post
00113         Vary& operator--(); //pre
00114         Vary operator--(int); //post
00115 
00116         inline Vary(Vary &&o) 
00117         {
00118             *this=o;
00119         }
00120 
00121         inline const Vary& operator=(Vary &&o) 
00122         {
00123             type=o.type;
00124             validBits=o.validBits;
00125             curInt=o.curInt;
00126             curReal=o.curReal;
00127             curStr=(std::string&&)o.curStr;
00128 
00129             return *this;
00130         }
00131 
00132     protected:
00133         Type type; //current type of us
00134 
00135         //contents of us (current type has the actual value, the rest are just cache then)
00136         mutable std::string curStr;
00137         mutable sint64 curInt;
00138         mutable float curReal;
00139 
00140         mutable nuint validBits; //which members are up to date
00141     };
00142 
00143 
00145     class VaryString: public Vary
00146     {
00147     public:
00148         //ctors and dtor
00149         VaryString(); 
00150         VaryString(const Vary &o); 
00151         inline VaryString(const sint64 &ival) {*this=ival;} 
00152         inline VaryString(const uint64 &ival) {*this=ival;} 
00153         inline VaryString(const sint32 &ival) {*this=(sint64)ival;} 
00154         inline VaryString(const uint32 &ival) {*this=(uint64)ival;} 
00155         inline VaryString(const sint16 &ival) {*this=(sint64)ival;} 
00156         inline VaryString(const uint16 &ival) {*this=(uint64)ival;} 
00157         inline VaryString(const sint8 &ival) {*this=(sint64)ival;} 
00158         inline VaryString(const uint8 &ival) {*this=(uint64)ival;} 
00159         inline VaryString(const float &rval) {*this=rval;} 
00160         inline VaryString(const std::string &sval) {*this=sval;} 
00161         inline VaryString(const char *sval) {*this=sval;} 
00162         inline VaryString(const std::vector<char> &sval) {*this=sval;} 
00163 
00164 #if defined(linux) && defined(__amd64) //need a size_t version also for some reason
00165         inline VaryString(const size_t &ival) {*this=(sint64)ival;} 
00166 #endif
00167 
00168         ~VaryString();
00169 
00170         //assignment
00171         const VaryString& operator=(const Vary &vval); 
00172         const VaryString& operator=(const sint64 &ival); 
00173         inline const VaryString& operator=(const uint64 &ival) {*this=(sint64)ival; return *this;} 
00174         const VaryString& operator=(const float &rval); 
00175         const VaryString& operator=(const std::string &sval); 
00176         const VaryString& operator=(const char *sval); 
00177         const VaryString& operator=(const std::vector<char> &sval); 
00178 
00179         //hide stuff that doesn't make sense for this type
00180     private:
00181         void MakeInt();
00182         void MakeReal();
00183 
00184         Vary& operator++();
00185         Vary operator++(int);
00186     };
00187 
00188 } //namespace MPMA
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends