VTK
dox/Common/vtkVariant.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    vtkVariant.h
00005 
00006   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00007   All rights reserved.
00008   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00015 /*-------------------------------------------------------------------------
00016   Copyright 2008 Sandia Corporation.
00017   Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00018   the U.S. Government retains certain rights in this software.
00019 -------------------------------------------------------------------------*/
00036 #ifndef __vtkVariant_h
00037 #define __vtkVariant_h
00038 
00039 #include "vtkType.h"           // To define type IDs and VTK_TYPE_USE_* flags
00040 #include "vtkSystemIncludes.h" // To define ostream
00041 #include "vtkSetGet.h"         // For vtkNotUsed macro
00042 #include "vtkStdString.h"
00043 #include "vtkUnicodeString.h"
00044 
00045 //
00046 // The following should be eventually placed in vtkSetGet.h
00047 //
00048 
00049 // This is same as extended template macro with an additional case for VTK_VARIANT
00050 #define vtkExtraExtendedTemplateMacro(call)                                 \
00051   vtkExtendedTemplateMacro(call);                                            \
00052   vtkTemplateMacroCase(VTK_VARIANT, vtkVariant, call)
00053 
00054 // This is same as Iterator Template macro with an additional case for VTK_VARIANT
00055 #define vtkExtendedArrayIteratorTemplateMacro(call)                                      \
00056   vtkArrayIteratorTemplateMacro(call);                                                   \
00057   vtkArrayIteratorTemplateMacroCase(VTK_VARIANT, vtkVariant, call);
00058 
00059 class vtkStdString;
00060 class vtkUnicodeString;
00061 class vtkObjectBase;
00062 class vtkAbstractArray;
00063 class vtkVariant;
00064 struct vtkVariantLessThan;
00065 
00066 VTK_COMMON_EXPORT ostream& operator << ( ostream& os, const vtkVariant& val );
00067 
00068 
00069 class VTK_COMMON_EXPORT vtkVariant
00070 {
00071 public:
00072 
00074   vtkVariant();
00075 
00077   ~vtkVariant();
00078 
00080   vtkVariant(const vtkVariant & other);
00081 
00083   vtkVariant(bool value);
00084 
00086   vtkVariant(char value);
00087 
00089   vtkVariant(unsigned char value);
00090 
00092   vtkVariant(signed char value);
00093 
00095   vtkVariant(short value);
00096 
00098   vtkVariant(unsigned short value);
00099 
00101   vtkVariant(int value);
00102 
00104   vtkVariant(unsigned int value);
00105 
00107   vtkVariant(long value);
00108 
00110   vtkVariant(unsigned long value);
00111 
00112 #if defined(VTK_TYPE_USE___INT64)
00113 
00114   vtkVariant(__int64 value);
00115   
00117   vtkVariant(unsigned __int64 value);
00118 #endif
00119 #if defined(VTK_TYPE_USE_LONG_LONG)
00120 
00121   vtkVariant(long long value);
00122 
00124   vtkVariant(unsigned long long value);
00125 #endif
00126 
00128   vtkVariant(float value);
00129 
00131   vtkVariant(double value);
00132 
00134   vtkVariant(const char* value);
00135 
00137   vtkVariant(vtkStdString value);
00138 
00140   vtkVariant(const vtkUnicodeString& value);
00141 
00143   vtkVariant(vtkObjectBase* value);
00144 
00146   const vtkVariant & operator= (const vtkVariant & other);
00147 
00149   bool IsValid() const;
00150 
00152   bool IsString() const;
00153 
00155   bool IsUnicodeString() const;
00156 
00158   bool IsNumeric() const;
00159 
00161   bool IsFloat() const;
00162 
00164   bool IsDouble() const;
00165 
00167   bool IsChar() const;
00168 
00170   bool IsUnsignedChar() const;
00171 
00173   bool IsSignedChar() const;
00174 
00176   bool IsShort() const;
00177 
00179   bool IsUnsignedShort() const;
00180 
00182   bool IsInt() const;
00183 
00185   bool IsUnsignedInt() const;
00186 
00188   bool IsLong() const;
00189 
00191   bool IsUnsignedLong() const;
00192 
00194   bool Is__Int64() const;
00195 
00197   bool IsUnsigned__Int64() const;
00198 
00200   bool IsLongLong() const;
00201 
00203   bool IsUnsignedLongLong() const;
00204 
00206   bool IsVTKObject() const;
00207 
00210   bool IsArray() const;
00211 
00213   unsigned int GetType() const;
00214 
00216   const char* GetTypeAsString() const;
00217 
00219   vtkStdString ToString() const;
00220 
00222   vtkUnicodeString ToUnicodeString() const;
00223 
00225 
00231   float ToFloat(bool* valid = 0) const;
00232   double ToDouble(bool* valid = 0) const;
00233   char ToChar(bool* valid = 0) const;
00234   unsigned char ToUnsignedChar(bool* valid = 0) const;
00235   signed char ToSignedChar(bool* valid = 0) const;
00236   short ToShort(bool* valid = 0) const;
00237   unsigned short ToUnsignedShort(bool* valid = 0) const;
00238   int ToInt(bool* valid = 0) const;
00239   unsigned int ToUnsignedInt(bool* valid = 0) const;
00240   long ToLong(bool* valid = 0) const;
00241   unsigned long ToUnsignedLong(bool* valid = 0) const;
00242 #if defined(VTK_TYPE_USE___INT64)
00243   __int64 To__Int64(bool* valid = 0) const;
00244   unsigned __int64 ToUnsigned__Int64(bool* valid = 0) const;
00246 #endif
00247 #if defined(VTK_TYPE_USE_LONG_LONG)
00248   long long ToLongLong(bool* valid = 0) const;
00249   unsigned long long ToUnsignedLongLong(bool* valid = 0) const;
00250 #endif
00251   vtkTypeInt64 ToTypeInt64(bool* valid = 0) const;
00252   vtkTypeUInt64 ToTypeUInt64(bool* valid = 0) const;
00253 
00255   vtkObjectBase* ToVTKObject() const;
00256 
00258   vtkAbstractArray* ToArray() const;
00259 
00260   template <typename T>
00261   T ToNumeric(bool* valid, T* vtkNotUsed(ignored)) const;
00262 
00269   bool IsEqual(const vtkVariant& other) const;
00270 
00272 
00292   bool operator==(const vtkVariant &other) const;
00293   bool operator!=(const vtkVariant &other) const;
00294   bool operator<(const vtkVariant &other) const;
00295   bool operator>(const vtkVariant &other) const;
00296   bool operator<=(const vtkVariant &other) const;
00297   bool operator>=(const vtkVariant &other) const;
00299 
00300   friend VTK_COMMON_EXPORT ostream& operator << ( ostream& os, const vtkVariant& val );
00301 
00302 
00303 private:
00304   union
00305   {
00306     vtkStdString* String;
00307     vtkUnicodeString* UnicodeString;
00308     float Float;
00309     double Double;
00310     char Char;
00311     unsigned char UnsignedChar;
00312     signed char SignedChar;
00313     short Short;
00314     unsigned short UnsignedShort;
00315     int Int;
00316     unsigned int UnsignedInt;
00317     long Long;
00318     unsigned long UnsignedLong;
00319 #if defined(VTK_TYPE_USE___INT64)
00320     __int64 __Int64;
00321     unsigned __int64 Unsigned__Int64;
00322 #endif
00323 #if defined(VTK_TYPE_USE_LONG_LONG)
00324     long long LongLong;
00325     unsigned long long UnsignedLongLong;
00326 #endif
00327     vtkObjectBase* VTKObject;
00328   } Data;
00329 
00330   unsigned char Valid;
00331   unsigned char Type;
00332 
00333   friend struct vtkVariantLessThan;
00334   friend struct vtkVariantEqual;
00335   friend struct vtkVariantStrictWeakOrder;
00336   friend struct vtkVariantStrictEquality;
00337 };
00338 
00339 #include "vtkVariantInlineOperators.h" // needed for operator== and company
00340 
00341 // A STL-style function object so you can compare two variants using
00342 // comp(s1,s2) where comp is an instance of vtkVariantStrictWeakOrder.
00343 // This is a faster version of operator< that makes no attempt to
00344 // compare values.  It satisfies the STL requirement for a comparison
00345 // function for ordered containers like map and set.
00346 
00347 struct VTK_COMMON_EXPORT vtkVariantLessThan
00348 {
00349 public:
00350   bool operator()(const vtkVariant &s1, const vtkVariant &s2) const;
00351 };
00352 
00353 struct VTK_COMMON_EXPORT vtkVariantEqual
00354 {
00355 public:
00356   bool operator()(const vtkVariant &s1, const vtkVariant &s2) const;
00357 };
00358 
00359 struct VTK_COMMON_EXPORT vtkVariantStrictWeakOrder
00360 {
00361 public:
00362   bool operator()(const vtkVariant& s1, const vtkVariant& s2) const;
00363 };
00364 
00365 // Similarly, this is a fast version of operator== that requires that
00366 // the types AND the values be equal in order to admit equality.
00367 
00368 struct VTK_COMMON_EXPORT vtkVariantStrictEquality
00369 {
00370 public:
00371   bool operator()(const vtkVariant &s1, const vtkVariant &s2) const;
00372 };
00373 
00374 #endif