• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

dox/Common/vtkVariantCast.h

Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Visualization Toolkit
00004   Module:    $RCSfile: vtkVariantCast.h,v $
00005   
00006 -------------------------------------------------------------------------
00007   Copyright 2008 Sandia Corporation.
00008   Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00009   the U.S. Government retains certain rights in this software.
00010 -------------------------------------------------------------------------
00011 
00012   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
00013   All rights reserved.
00014   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
00015 
00016      This software is distributed WITHOUT ANY WARRANTY; without even
00017      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00018      PURPOSE.  See the above copyright notice for more information.
00019 
00020 =========================================================================*/
00021 
00022 #ifndef __vtkVariantCast_h
00023 #define __vtkVariantCast_h
00024 
00025 
00026 // .SECTION Thanks
00027 // Developed by Timothy M. Shead (tshead@sandia.gov) at Sandia National Laboratories.
00028 
00029 // Description:
00030 // Converts a vtkVariant to some other type.  Wherever possible, implicit conversions are
00031 // performed, so this method can be used to convert from nearly any type to a string, or
00032 // from a string to nearly any type.  Note that some conversions may fail at runtime, such
00033 // as a conversion from the string "abc" to a numeric type.
00034 //
00035 // The optional 'valid' flag can be used by callers to verify whether conversion succeeded.
00036 template<typename T>
00037 T vtkVariantCast(const vtkVariant& value, bool* valid = 0)
00038 {
00039   vtkGenericWarningMacro(<< "cannot cast vtkVariant containing " << value.GetTypeAsString() << " to unsupported type.");
00040 
00041   if(valid)
00042     *valid = false;
00043   
00044   static T dummy;
00045   return dummy;
00046 }
00047 
00048 template<>
00049 inline char vtkVariantCast<char>(const vtkVariant& value, bool* valid)
00050 {
00051   return value.ToChar(valid);
00052 }
00053 
00054 template<>
00055 inline unsigned char vtkVariantCast<unsigned char>(const vtkVariant& value, bool* valid)
00056 {
00057   return value.ToUnsignedChar(valid);
00058 }
00059 
00060 template<>
00061 inline short vtkVariantCast<short>(const vtkVariant& value, bool* valid)
00062 {
00063   return value.ToShort(valid);
00064 }
00065 
00066 template<>
00067 inline unsigned short vtkVariantCast<unsigned short>(const vtkVariant& value, bool* valid)
00068 {
00069   return value.ToUnsignedShort(valid);
00070 }
00071 
00072 template<>
00073 inline int vtkVariantCast<int>(const vtkVariant& value, bool* valid)
00074 {
00075   return value.ToInt(valid);
00076 }
00077 
00078 template<>
00079 inline unsigned int vtkVariantCast<unsigned int>(const vtkVariant& value, bool* valid)
00080 {
00081   return value.ToUnsignedInt(valid);
00082 }
00083 
00084 template<>
00085 inline long vtkVariantCast<long>(const vtkVariant& value, bool* valid)
00086 {
00087   return value.ToLong(valid);
00088 }
00089 
00090 template<>
00091 inline unsigned long vtkVariantCast<unsigned long>(const vtkVariant& value, bool* valid)
00092 {
00093   return value.ToUnsignedLong(valid);
00094 }
00095 
00096 #ifdef VTK_TYPE_USE___INT64
00097 
00098 template<>
00099 inline __int64 vtkVariantCast<__int64>(const vtkVariant& value, bool* valid)
00100 {
00101   return value.To__Int64(valid);
00102 }
00103 
00104 template<>
00105 inline unsigned __int64 vtkVariantCast<unsigned __int64>(const vtkVariant& value, bool* valid)
00106 {
00107   return value.ToUnsigned__Int64(valid);
00108 }
00109 
00110 #endif
00111 
00112 
00113 #ifdef VTK_TYPE_USE_LONG_LONG
00114 
00115 template<>
00116 inline long long vtkVariantCast<long long>(const vtkVariant& value, bool* valid)
00117 {
00118   return value.ToLongLong(valid);
00119 }
00120 
00121 template<>
00122 inline unsigned long long vtkVariantCast<unsigned long long>(const vtkVariant& value, bool* valid)
00123 {
00124   return value.ToUnsignedLongLong(valid);
00125 }
00126 
00127 #endif
00128 
00129 template<>
00130 inline float vtkVariantCast<float>(const vtkVariant& value, bool* valid)
00131 {
00132   return value.ToFloat(valid);
00133 }
00134 
00135 template<>
00136 inline double vtkVariantCast<double>(const vtkVariant& value, bool* valid)
00137 {
00138   return value.ToDouble(valid);
00139 }
00140 
00141 template<>
00142 inline vtkStdString vtkVariantCast<vtkStdString>(const vtkVariant& value, bool* valid)
00143 {
00144   if(valid)
00145     *valid = true;
00146 
00147   return value.ToString();
00148 }
00149 
00150 template<>
00151 inline vtkVariant vtkVariantCast<vtkVariant>(const vtkVariant& value, bool* valid)
00152 {
00153   if(valid)
00154     *valid = true;
00155 
00156   return value;
00157 }
00158 
00159 #endif
00160 

Generated by  doxygen 1.7.1