VTK
|
00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkVariantExtract.h 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 00037 #ifndef __vtkVariantExtract_h 00038 #define __vtkVariantExtract_h 00039 00040 #include <typeinfo> // for typeid 00041 00042 template<typename T> 00043 T vtkVariantExtract(const vtkVariant& value, bool& valid) 00044 { 00045 vtkGenericWarningMacro( 00046 << "Cannot convert vtkVariant containing [" << value.GetTypeAsString() << "] " 00047 << "to unsupported type [" << typeid(T).name() << "]. " 00048 << "Create a vtkVariantExtract<> specialization to eliminate this warning." 00049 ); 00050 00051 valid = false; 00052 00053 static T dummy; 00054 return dummy; 00055 } 00056 00057 template<> 00058 inline char vtkVariantExtract<char>(const vtkVariant& value, bool& valid) 00059 { 00060 valid = value.IsChar(); 00061 return valid ? value.ToChar() : 0; 00062 } 00063 00064 template<> 00065 inline unsigned char vtkVariantExtract<unsigned char>(const vtkVariant& value, bool& valid) 00066 { 00067 valid = value.IsUnsignedChar(); 00068 return valid ? value.ToUnsignedChar() : 0; 00069 } 00070 00071 template<> 00072 inline short vtkVariantExtract<short>(const vtkVariant& value, bool& valid) 00073 { 00074 valid = value.IsShort(); 00075 return valid ? value.ToShort() : 0; 00076 } 00077 00078 template<> 00079 inline unsigned short vtkVariantExtract<unsigned short>(const vtkVariant& value, bool& valid) 00080 { 00081 valid = value.IsUnsignedShort(); 00082 return valid ? value.ToUnsignedShort() : 0; 00083 } 00084 00085 template<> 00086 inline int vtkVariantExtract<int>(const vtkVariant& value, bool& valid) 00087 { 00088 valid = value.IsInt(); 00089 return valid ? value.ToInt() : 0; 00090 } 00091 00092 template<> 00093 inline unsigned int vtkVariantExtract<unsigned int>(const vtkVariant& value, bool& valid) 00094 { 00095 valid = value.IsUnsignedInt(); 00096 return valid ? value.ToUnsignedInt() : 0; 00097 } 00098 00099 template<> 00100 inline long vtkVariantExtract<long>(const vtkVariant& value, bool& valid) 00101 { 00102 valid = value.IsLong(); 00103 return valid ? value.ToLong() : 0; 00104 } 00105 00106 template<> 00107 inline unsigned long vtkVariantExtract<unsigned long>(const vtkVariant& value, bool& valid) 00108 { 00109 valid = value.IsUnsignedLong(); 00110 return valid ? value.ToUnsignedLong() : 0; 00111 } 00112 00113 #ifdef VTK_TYPE_USE___INT64 00114 00115 template<> 00116 inline __int64 vtkVariantExtract<__int64>(const vtkVariant& value, bool& valid) 00117 { 00118 valid = value.Is__Int64(); 00119 return valid ? value.To__Int64() : 0; 00120 } 00121 00122 template<> 00123 inline unsigned __int64 vtkVariantExtract<unsigned __int64>(const vtkVariant& value, bool& valid) 00124 { 00125 valid = value.IsUnsigned__Int64(); 00126 return valid ? value.ToUnsigned__Int64() : 0; 00127 } 00128 00129 #endif 00130 00131 00132 #ifdef VTK_TYPE_USE_LONG_LONG 00133 00134 template<> 00135 inline long long vtkVariantExtract<long long>(const vtkVariant& value, bool& valid) 00136 { 00137 valid = value.IsLongLong(); 00138 return valid ? value.ToLongLong() : 0; 00139 } 00140 00141 template<> 00142 inline unsigned long long vtkVariantExtract<unsigned long long>(const vtkVariant& value, bool& valid) 00143 { 00144 valid = value.IsUnsignedLongLong(); 00145 return valid ? value.ToUnsignedLongLong() : 0; 00146 } 00147 00148 #endif 00149 00150 template<> 00151 inline float vtkVariantExtract<float>(const vtkVariant& value, bool& valid) 00152 { 00153 valid = value.IsFloat(); 00154 return valid ? value.ToFloat() : 0.0f; 00155 } 00156 00157 template<> 00158 inline double vtkVariantExtract<double>(const vtkVariant& value, bool& valid) 00159 { 00160 valid = value.IsDouble(); 00161 return valid ? value.ToDouble() : 0.0; 00162 } 00163 00164 template<> 00165 inline vtkStdString vtkVariantExtract<vtkStdString>(const vtkVariant& value, bool& valid) 00166 { 00167 valid = value.IsString(); 00168 return valid ? value.ToString() : vtkStdString(); 00169 } 00170 00171 template<> 00172 inline vtkUnicodeString vtkVariantExtract<vtkUnicodeString>(const vtkVariant& value, bool& valid) 00173 { 00174 valid = value.IsUnicodeString(); 00175 return valid ? value.ToUnicodeString() : vtkUnicodeString(); 00176 } 00177 00178 template<> 00179 inline vtkVariant vtkVariantExtract<vtkVariant>(const vtkVariant& value, bool& valid) 00180 { 00181 valid = true; 00182 return value; 00183 } 00184 00185 #endif 00186 00187 // VTK-HeaderTest-Exclude: vtkVariantExtract.h