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 template<>
114 inline long long vtkVariantExtract<long long>(const vtkVariant& value, bool& valid)
115 {
116  valid = value.IsLongLong();
117  return valid ? value.ToLongLong() : 0;
118 }
119 
120 template<>
121 inline unsigned long long vtkVariantExtract<unsigned long long>(const vtkVariant& value, bool& valid)
122 {
123  valid = value.IsUnsignedLongLong();
124  return valid ? value.ToUnsignedLongLong() : 0;
125 }
126 
127 template<>
128 inline float vtkVariantExtract<float>(const vtkVariant& value, bool& valid)
129 {
130  valid = value.IsFloat();
131  return valid ? value.ToFloat() : 0.0f;
132 }
133 
134 template<>
135 inline double vtkVariantExtract<double>(const vtkVariant& value, bool& valid)
136 {
137  valid = value.IsDouble();
138  return valid ? value.ToDouble() : 0.0;
139 }
140 
141 template<>
143 {
144  valid = value.IsString();
145  return valid ? value.ToString() : vtkStdString();
146 }
147 
148 template<>
150 {
151  valid = value.IsUnicodeString();
152  return valid ? value.ToUnicodeString() : vtkUnicodeString();
153 }
154 
155 template<>
157 {
158  valid = true;
159  return value;
160 }
161 
162 #endif
163 
164 // 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.