Variant.h++

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002    commonc++ - A C++ Common Class Library
00003    Copyright (C) 2005-2012  Mark A Lindner
00004 
00005    This file is part of commonc++.
00006 
00007    This library is free software; you can redistribute it and/or
00008    modify it under the terms of the GNU Library General Public
00009    License as published by the Free Software Foundation; either
00010    version 2 of the License, or (at your option) any later version.
00011 
00012    This library is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015    Library General Public License for more details.
00016 
00017    You should have received a copy of the GNU Library General Public
00018    License along with this library; if not, write to the Free
00019    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020    ---------------------------------------------------------------------------
00021 */
00022 
00023 #ifndef __ccxx_Variant_hxx
00024 #define __ccxx_Variant_hxx
00025 
00026 #include <commonc++/Common.h++>
00027 #include <commonc++/Blob.h++>
00028 #include <commonc++/String.h++>
00029 
00030 #include <sys/types.h>
00031 #include <iostream>
00032 
00033 namespace ccxx {
00034 
00046 class COMMONCPP_API Variant
00047 {
00048   public:
00049 
00051   enum Type { TypeNull = 0, TypeBool, TypeChar, TypeWChar, TypeInt, TypeUInt,
00052               TypeFloat, TypeDouble, TypeInt64, TypeUInt64, TypeString,
00053               TypeWString, TypeBlob, TypeOpaque, TypeList, TypeMap };
00054 
00056   Variant();
00057 
00059   Variant(const Variant &other);
00060 
00065   explicit Variant(bool val);
00066 
00071   explicit Variant(char val);
00072 
00077   explicit Variant(wchar_t val);
00078 
00083   explicit Variant(int32_t val);
00084 
00089   explicit Variant(uint32_t val);
00090 
00095   explicit Variant(float val);
00096 
00101   explicit Variant(const double &val);
00102 
00107   explicit Variant(const int64_t &val);
00108 
00113   explicit Variant(const uint64_t &val);
00114 
00119   explicit Variant(const String &val);
00120 
00125   explicit Variant(const WString &val);
00126 
00131   explicit Variant(const char *val);
00132 
00137   explicit Variant(const wchar_t *val);
00138 
00143   explicit Variant(const Blob &val);
00144 
00149   explicit Variant(void *val);
00150 
00152   virtual ~Variant();
00153 
00155   inline Type getType() const
00156   { return(_type); }
00157 
00163   void setType(Type type);
00164 
00166   inline bool isNull() const
00167   { return(_type == TypeNull); }
00168 
00170   inline bool isBool() const
00171   { return(_type == TypeBool); }
00172 
00174   inline bool isChar() const
00175   { return(_type == TypeChar); }
00176 
00178   inline bool isWChar() const
00179   { return(_type == TypeWChar); }
00180 
00182   inline bool isInt() const
00183   { return(_type == TypeInt); }
00184 
00186   inline bool isUInt() const
00187   { return(_type == TypeUInt); }
00188 
00190   inline bool isFloat() const
00191   { return(_type == TypeFloat); }
00192 
00194   inline bool isDouble() const
00195   { return(_type == TypeDouble); }
00196 
00198   inline bool isInt64() const
00199   { return(_type == TypeInt64); }
00200 
00202   inline bool isUInt64() const
00203   { return(_type == TypeUInt64); }
00204 
00206   inline bool isString() const
00207   { return(_type == TypeString); }
00208 
00210   inline bool isWString() const
00211   { return(_type == TypeWString); }
00212 
00214   inline bool isBlob() const
00215   { return(_type == TypeBlob); }
00216 
00218   inline bool isOpaque() const
00219   { return(_type == TypeOpaque); }
00220 
00222   inline bool isList() const
00223   { return(_type == TypeList); }
00224 
00226   inline bool isMap() const
00227   { return(_type == TypeMap); }
00228 
00233   bool toBool() const;
00234 
00239   char toChar() const;
00240 
00245   wchar_t toWChar() const;
00246 
00251   int32_t toInt() const;
00252 
00257   uint32_t toUInt() const;
00258 
00263   float toFloat() const;
00264 
00269   double toDouble() const;
00270 
00275   int64_t toInt64() const;
00276 
00281   uint64_t toUInt64() const;
00282 
00286   void * toOpaque() const;
00287 
00292   String toString() const;
00293 
00298   WString toWString() const;
00299 
00304   Blob toBlob() const;
00305 
00307   inline operator bool() const
00308   { return(toBool()); }
00309 
00311   inline operator char() const
00312   { return(toChar()); }
00313 
00315   inline operator wchar_t() const
00316   { return(toWChar()); }
00317 
00319   inline operator int32_t() const
00320   { return(toInt()); }
00321 
00323   inline operator uint32_t() const
00324   { return(toUInt()); }
00325 
00327   inline operator float() const
00328   { return(toFloat()); }
00329 
00331   inline operator double() const
00332   { return(toDouble()); }
00333 
00335   inline operator String() const
00336   { return(toString()); }
00337 
00339   inline operator WString() const
00340   { return(toWString()); }
00341 
00343   inline operator Blob() const
00344   { return(toBlob()); }
00345 
00347   inline operator int64_t() const
00348   { return(toInt64()); }
00349 
00351   inline operator uint64_t() const
00352   { return(toUInt64()); }
00353 
00357   inline operator const void *() const throw()
00358   { return(isNull() ? NULL : this); }
00359 
00361   inline bool operator!() const throw()
00362   { return(isNull()); }
00363 
00368   size_t length() const;
00369 
00373   void clear();
00374 
00376   void setNull();
00377 
00379   Variant& operator=(bool val);
00380 
00382   Variant& operator=(char val);
00383 
00385   Variant& operator=(wchar_t val);
00386 
00388   Variant& operator=(int32_t val);
00389 
00391   Variant& operator=(uint32_t val);
00392 
00394   Variant& operator=(float val);
00395 
00397   Variant& operator=(const double &val);
00398 
00400   Variant& operator=(const int64_t &val);
00401 
00403   Variant& operator=(const uint64_t &val);
00404 
00406   Variant& operator=(const String &val);
00407 
00409   Variant& operator=(const WString &val);
00410 
00412   inline Variant& operator=(const char *val)
00413   { return(operator=(String(val))); }
00414 
00416   inline Variant& operator=(const wchar_t *val)
00417   { return(operator=(WString(val))); }
00418 
00420   Variant& operator=(const Blob &val);
00421 
00423   Variant& operator=(void *val);
00424 
00426   Variant& operator=(const Variant& other);
00427 
00437   Variant& operator[](const String &name);
00438 
00448   inline Variant& operator[](const char *name)
00449   { return(operator[](String(name))); }
00450 
00458   const Variant &get(const String &name) const;
00459 
00467   inline const Variant &operator[](const String &name) const
00468   { return(get(name)); }
00469 
00477   inline const Variant &operator[](const char *name) const
00478   { return(get(String(name))); }
00479 
00491   Variant& operator[](int index) throw(OutOfBoundsException);
00492 
00501   const Variant &get(int index) const throw(OutOfBoundsException);
00502 
00511   inline const Variant &operator[](int index) const
00512     throw(OutOfBoundsException)
00513   { return(get(index)); }
00514 
00521   bool getKeys(StringVec &vec) const;
00522 
00529   bool hasKey(const String &key) const;
00530 
00532   bool operator==(const Variant &other) const;
00533 
00535   inline bool operator!=(const Variant &other) const
00536   { return(! operator==(other)); }
00537 
00542   void write(std::ostream &stream) const;
00543 
00545   static const Variant null;
00546 
00547   private:
00548 
00549   class VariantList; // fwd decl
00550   class VariantMap; // fwd decl
00551 
00552   void _copy(const Variant& other);
00553   void _deleteValue();
00554   void _initValue();
00555 
00556   Type _type;
00557 
00558   union {
00559     bool boolVal;
00560     char charVal;
00561     wchar_t wcharVal;
00562     int32_t intVal;
00563     uint32_t uintVal;
00564     float floatVal;
00565     double doubleVal;
00566     int64_t int64Val;
00567     uint64_t uint64Val;
00568     String *stringVal;
00569     WString *wstringVal;
00570     Blob *blobVal;
00571     VariantList *listVal;
00572     VariantMap *mapVal;
00573     void *opaqueVal;
00574   } _value;
00575 
00576 };
00577 
00584 inline std::ostream& operator<<(std::ostream& stream, const Variant& v) {
00585   v.write(stream);
00586   return(stream);
00587 }
00588 
00589 }; // namespace ccxx
00590 
00591 #endif // __ccxx_Variant_hxx
00592 
00593 /* end of header file */
Generated on Sat Nov 26 16:49:07 2011 for libcommonc++ by  doxygen 1.6.3