VTK
vtkVariantExtract.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkVariantExtract.h
5 
6 -------------------------------------------------------------------------
7  Copyright 2008 Sandia Corporation.
8  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
9  the U.S. Government retains certain rights in this software.
10 -------------------------------------------------------------------------
11 
12  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
13  All rights reserved.
14  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
15 
16  This software is distributed WITHOUT ANY WARRANTY; without even
17  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18  PURPOSE. See the above copyright notice for more information.
19 
20 =========================================================================*/
21 
37 #ifndef vtkVariantExtract_h
38 #define vtkVariantExtract_h
39 
40 #include <typeinfo> // for typeid
41 
42 template<typename T>
43 T vtkVariantExtract(const vtkVariant& value, bool& valid)
44 {
45  vtkGenericWarningMacro(
46  << "Cannot convert vtkVariant containing [" << value.GetTypeAsString() << "] "
47  << "to unsupported type [" << typeid(T).name() << "]. "
48  << "Create a vtkVariantExtract<> specialization to eliminate this warning."
49  );
50 
51  valid = false;
52 
53  static T dummy;
54  return dummy;
55 }
56 
57 template<>
58 inline char vtkVariantExtract<char>(const vtkVariant& value, bool& valid)
59 {
60  valid = value.IsChar();
61  return valid ? value.ToChar() : 0;
62 }
63 
64 template<>
65 inline unsigned char vtkVariantExtract<unsigned char>(const vtkVariant& value, bool& valid)
66 {
67  valid = value.IsUnsignedChar();
68  return valid ? value.ToUnsignedChar() : 0;
69 }
70 
71 template<>
72 inline short vtkVariantExtract<short>(const vtkVariant& value, bool& valid)
73 {
74  valid = value.IsShort();
75  return valid ? value.ToShort() : 0;
76 }
77 
78 template<>
79 inline unsigned short vtkVariantExtract<unsigned short>(const vtkVariant& value, bool& valid)
80 {
81  valid = value.IsUnsignedShort();
82  return valid ? value.ToUnsignedShort() : 0;
83 }
84 
85 template<>
86 inline int vtkVariantExtract<int>(const vtkVariant& value, bool& valid)
87 {
88  valid = value.IsInt();
89  return valid ? value.ToInt() : 0;
90 }
91 
92 template<>
93 inline unsigned int vtkVariantExtract<unsigned int>(const vtkVariant& value, bool& valid)
94 {
95  valid = value.IsUnsignedInt();
96  return valid ? value.ToUnsignedInt() : 0;
97 }
98 
99 template<>
100 inline long vtkVariantExtract<long>(const vtkVariant& value, bool& valid)
101 {
102  valid = value.IsLong();
103  return valid ? value.ToLong() : 0;
104 }
105 
106 template<>
107 inline unsigned long vtkVariantExtract<unsigned long>(const vtkVariant& value, bool& valid)
108 {
109  valid = value.IsUnsignedLong();
110  return valid ? value.ToUnsignedLong() : 0;
111 }
112 
113 #ifdef VTK_TYPE_USE___INT64
114 
115 template<>
116 inline __int64 vtkVariantExtract<__int64>(const vtkVariant& value, bool& valid)
117 {
118  valid = value.Is__Int64();
119  return valid ? value.To__Int64() : 0;
120 }
121 
122 template<>
123 inline unsigned __int64 vtkVariantExtract<unsigned __int64>(const vtkVariant& value, bool& valid)
124 {
125  valid = value.IsUnsigned__Int64();
126  return valid ? value.ToUnsigned__Int64() : 0;
127 }
128 
129 #endif
130 
131 
132 #ifdef VTK_TYPE_USE_LONG_LONG
133 
134 template<>
135 inline long long vtkVariantExtract<long long>(const vtkVariant& value, bool& valid)
136 {
137  valid = value.IsLongLong();
138  return valid ? value.ToLongLong() : 0;
139 }
140 
141 template<>
142 inline unsigned long long vtkVariantExtract<unsigned long long>(const vtkVariant& value, bool& valid)
143 {
144  valid = value.IsUnsignedLongLong();
145  return valid ? value.ToUnsignedLongLong() : 0;
146 }
147 
148 #endif
149 
150 template<>
151 inline float vtkVariantExtract<float>(const vtkVariant& value, bool& valid)
152 {
153  valid = value.IsFloat();
154  return valid ? value.ToFloat() : 0.0f;
155 }
156 
157 template<>
158 inline double vtkVariantExtract<double>(const vtkVariant& value, bool& valid)
159 {
160  valid = value.IsDouble();
161  return valid ? value.ToDouble() : 0.0;
162 }
163 
164 template<>
166 {
167  valid = value.IsString();
168  return valid ? value.ToString() : vtkStdString();
169 }
170 
171 template<>
173 {
174  valid = value.IsUnicodeString();
175  return valid ? value.ToUnicodeString() : vtkUnicodeString();
176 }
177 
178 template<>
180 {
181  valid = true;
182  return value;
183 }
184 
185 #endif
186 
187 // VTK-HeaderTest-Exclude: vtkVariantExtract.h
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:46
A atomic type representing the union of many types.
Definition: vtkVariant.h:78
T vtkVariantExtract(const vtkVariant &value, bool &valid)
const char * GetTypeAsString() const
String class that stores Unicode text.