AnyType.h

Go to the documentation of this file.
00001 // $Id: AnyType.h 165 2006-05-29 16:39:53Z bingmann $
00002 
00003 #ifndef VGS_AnyType_H
00004 #define VGS_AnyType_H
00005 
00006 #include <string>
00007 #include <stdexcept>
00008 #include <functional>
00009 #include <assert.h>
00010 
00011 #include "GraphTypes.h"
00012 
00013 namespace VGServer {
00014 
00021 class AnyType
00022 {
00023 private:
00024     attrtype_t          type;
00025 
00026     union
00027     {
00028         int             _int;
00029         unsigned int    _uint;
00030         long long       _long;
00031         unsigned long long _ulong;
00032         float           _float;
00033         double          _double;
00034         std::string*    _string;
00035     };
00036     
00037 public:
00039     explicit inline AnyType(attrtype_t t)
00040     { 
00041         type = t;
00042         if (type == ATTRTYPE_STRING or type == ATTRTYPE_LONGSTRING) {
00043             _string = new std::string;
00044         }
00045     }
00046 
00047     // *** Constructors for the various types
00048 
00049     inline AnyType(bool b)
00050     {
00051         type = ATTRTYPE_BOOL;
00052         _int = b;
00053     }
00054      inline AnyType(char c)
00055     {
00056         type = ATTRTYPE_CHAR;
00057         _int = c;
00058     }
00059     inline AnyType(short s)
00060     {
00061         type = ATTRTYPE_SHORT;
00062         _int = s;
00063     }
00064     inline AnyType(int i)
00065     {
00066         type = ATTRTYPE_INTEGER;
00067         _int = i;
00068     }
00069     inline AnyType(long long l)
00070     {
00071         type = ATTRTYPE_LONG;
00072         _long = l;
00073     }
00074     inline AnyType(unsigned char c)
00075     {
00076         type = ATTRTYPE_BYTE;
00077         _uint = c;
00078     }
00079     inline AnyType(unsigned short s)
00080     {
00081         type = ATTRTYPE_WORD;
00082         _uint = s;
00083     }
00084     inline AnyType(unsigned int i)
00085     {
00086         type = ATTRTYPE_DWORD;
00087         _uint = i;
00088     }
00089     inline AnyType(unsigned long long l)
00090     {
00091         type = ATTRTYPE_QWORD;
00092         _ulong = l;
00093     }
00094     inline AnyType(float f)
00095     {
00096         type = ATTRTYPE_FLOAT;
00097         _float = f;
00098     }
00099     inline AnyType(double d)
00100     {
00101         type = ATTRTYPE_DOUBLE;
00102         _double = d;
00103     }
00104     inline AnyType(const char *s)
00105     {
00106         type = ATTRTYPE_STRING;
00107         _string = new std::string(s);
00108     }
00109     inline AnyType(const std::string &s)
00110     {
00111         type = ATTRTYPE_STRING;
00112         _string = new std::string(s);
00113     }
00114 
00116     inline ~AnyType()
00117     {
00118         if (type == ATTRTYPE_STRING or type == ATTRTYPE_LONGSTRING) {
00119             delete _string;
00120             _string = NULL;
00121         }
00122     }
00123     
00125     inline AnyType(const AnyType &a)
00126     {
00127         type = a.type;
00128 
00129         switch(type)
00130         {
00131         case ATTRTYPE_INVALID:
00132             break;
00133 
00134         case ATTRTYPE_BOOL:
00135         case ATTRTYPE_CHAR:
00136         case ATTRTYPE_SHORT:
00137         case ATTRTYPE_INTEGER:
00138             _int = a._int;
00139             break;
00140 
00141         case ATTRTYPE_BYTE:
00142         case ATTRTYPE_WORD:
00143         case ATTRTYPE_DWORD:
00144             _uint = a._uint;
00145             break;
00146 
00147         case ATTRTYPE_LONG:
00148             _long = a._long;
00149             break;
00150 
00151         case ATTRTYPE_QWORD:
00152             _ulong = a._ulong;
00153             break;
00154 
00155         case ATTRTYPE_FLOAT:
00156             _float = a._float;
00157             break;
00158 
00159         case ATTRTYPE_DOUBLE:
00160             _double = a._double;
00161             break;
00162 
00163         case ATTRTYPE_STRING:
00164         case ATTRTYPE_LONGSTRING:
00165             _string = new std::string(*a._string);
00166             break;
00167         }
00168     }
00169 
00171     inline AnyType& operator=(const AnyType &a)
00172     {
00173         if (type == ATTRTYPE_STRING or type == ATTRTYPE_LONGSTRING) {
00174             delete _string;
00175             _string = NULL;
00176         }
00177 
00178         type = a.type;
00179         
00180         switch(type)
00181         {
00182         case ATTRTYPE_INVALID: assert(0); break;
00183 
00184         case ATTRTYPE_BOOL:
00185         case ATTRTYPE_CHAR:
00186         case ATTRTYPE_SHORT:
00187         case ATTRTYPE_INTEGER:
00188             _int = a._int;
00189             break;
00190 
00191         case ATTRTYPE_BYTE:
00192         case ATTRTYPE_WORD:
00193         case ATTRTYPE_DWORD:
00194             _uint = a._uint;
00195             break;
00196 
00197         case ATTRTYPE_LONG:
00198             _long = a._long;
00199             break;
00200 
00201         case ATTRTYPE_QWORD:
00202             _ulong = a._ulong;
00203             break;
00204 
00205         case ATTRTYPE_FLOAT:
00206             _float = a._float;
00207             break;
00208 
00209         case ATTRTYPE_DOUBLE:
00210             _double = a._double;
00211             break;
00212 
00213         case ATTRTYPE_STRING:
00214         case ATTRTYPE_LONGSTRING:
00215             _string = new std::string(*a._string);
00216             break;
00217         }
00218         return *this;
00219     }
00220 
00222     bool operator==(const AnyType &a) const;
00223 
00225     inline bool operator!=(const AnyType &a) const
00226     { return not(*this == a); }
00227 
00229     inline attrtype_t   getType() const
00230     { return type; }
00231 
00234     bool        convertType(attrtype_t t);
00235 
00238     void        resetType(attrtype_t t);
00239 
00240     // *** attrtype_t to string and string to attrtype_t functions
00241 
00243     static bool isValidAttrtype(attrtype_t at);
00244     
00247     static attrtype_t stringToType(const std::string &s)
00248     { return stringToType(s.c_str()); }
00249 
00252     static attrtype_t stringToType(const char *s);
00253 
00255     static std::string getTypeString(attrtype_t at);
00256 
00258     std::string getTypeString() const
00259     { return getTypeString(type); }
00260 
00261     // *** Type and Value Length Functions
00262     
00264     inline int  getTypeLength() const
00265     { return getTypeLength(type); }
00266 
00268     static int  getTypeLength(attrtype_t t);
00269 
00271     static bool isFixedLength(attrtype_t t)
00272     { return getTypeLength(t) >= 0; }
00273 
00275     inline bool isFixedLength() const
00276     { return isFixedLength(type); }
00277 
00279     unsigned int getValueLength() const;
00280 
00283     bool        setInteger(int i);
00284 
00285     bool        setLong(long long l);
00286 
00287     bool        setDouble(double d);
00288 
00289     bool        setString(const std::string &s);
00290     
00291     bool        setStringQuoted(const std::string &s);
00292 
00296     bool        getBoolean() const;
00297 
00298     int         getInteger() const;
00299 
00300     unsigned int getUnsignedInteger() const;
00301 
00302     long long   getLong() const;
00303 
00304     unsigned long long getUnsignedLong() const;
00305 
00306     double      getDouble() const;
00307 
00308     std::string getString() const;
00309     
00310     // *** Unary Operators
00311     
00313     AnyType     operator-() const;
00314 
00315     // *** Binary Operators, these will convert the two operands to the largest
00316     // *** common type of the same field.
00317 
00318     template <template <typename Type> class Operator, char OpName>
00319     AnyType     binary_arith_op(const AnyType &b) const;
00320     
00321     inline AnyType      operator+(const AnyType &b) const
00322     { return binary_arith_op<std::plus, '+'>(b); }
00323 
00324     inline AnyType      operator-(const AnyType &b) const
00325     { return binary_arith_op<std::minus, '-'>(b); }
00326 
00327     inline AnyType      operator*(const AnyType &b) const
00328     { return binary_arith_op<std::multiplies, '*'>(b); }
00329 
00330     inline AnyType      operator/(const AnyType &b) const
00331     { return binary_arith_op<std::divides, '/'>(b); }
00332 
00333     template <template <typename Type> class Operator, int OpNum>
00334     bool        binary_comp_op(const AnyType &b) const;
00335 
00336     // dont use the operators themselves, because == is defined differently above.
00337     inline bool         op_equal_to(const AnyType &b) const
00338     { return binary_comp_op<std::equal_to, 0>(b); }
00339 
00340     inline bool         op_not_equal_to(const AnyType &b) const
00341     { return binary_comp_op<std::not_equal_to, 1>(b); }
00342 
00343     inline bool         op_less(const AnyType &b) const
00344     { return binary_comp_op<std::less, 2>(b); }
00345 
00346     inline bool         op_greater(const AnyType &b) const
00347     { return binary_comp_op<std::greater, 3>(b); }
00348 
00349     inline bool         op_less_equal(const AnyType &b) const
00350     { return binary_comp_op<std::less_equal, 4>(b); }
00351 
00352     inline bool         op_greater_equal(const AnyType &b) const
00353     { return binary_comp_op<std::greater_equal, 5>(b); }
00354 
00355 };
00356 
00360 class ConversionException : public GraphException
00361 {
00362 public:
00363     inline ConversionException(const std::string &s) throw()
00364         : GraphException(s)
00365     { }
00366 };
00367 
00368 } // namespace VGServer
00369 
00370 #endif // VGS_AnyType_H

Generated on Wed Sep 27 14:34:00 2006 for VGServer by  doxygen 1.4.7