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)
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     
00065         //forced conversion of this instance into another type
00066         inline void MakeInt() {*this=(int)*this;} 
00067         inline void MakeReal() {*this=(float)*this;} 
00068         inline void MakeString() {*this=(const std::string&)*this;} 
00069     
00071         const Vary& operator+=(const Vary &var);
00072         const Vary& operator+=(const std::string &str);
00073         const Vary& operator+=(const char *str);
00074         inline friend const std::string& operator+=(std::string &str, const Vary &v) { return str+=(const std::string&)v; }
00075         //...letting implicit conversions take care of the rest
00076     
00078         const Vary operator+(const Vary &var) const;
00079         inline const Vary operator+(const std::string &str) const { return *this+Vary(str); }
00080         inline const Vary operator+(const char *str) const { return *this+Vary(str); }
00081         inline friend Vary operator+(const char* str, const Vary &v) { return v+str; }
00082         inline friend Vary operator+(const std::string &str, const Vary &v) { return v+str; }
00083         //...letting implicit conversions take care of the rest
00084     
00086         Vary& operator++(); //pre
00087         Vary operator++(int); //post
00089         Vary& operator--(); //pre
00090         Vary operator--(int); //post
00091         
00092     private:
00093         Type type; //current type of us
00094         
00095         //contents of us (current type has the actual value, the rest are just cache then)
00096         mutable std::string curStr;
00097         mutable int curInt;
00098         mutable float curReal;
00099     
00100         mutable uint validBits; //which members are up to date
00101     };
00102 
00103 } //namespace MISC

Generated on Wed Feb 13 20:57:04 2008 for MPMA Framework by  doxygen 1.5.4