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 
38 #ifndef vtkVariantExtract_h
39 #define vtkVariantExtract_h
40 
41 #include <typeinfo> // for typeid
42 
43 template<typename T>
44 T vtkVariantExtract(const vtkVariant& value, bool& valid)
45 {
46  vtkGenericWarningMacro(
47  << "Cannot convert vtkVariant containing [" << value.GetTypeAsString() << "] "
48  << "to unsupported type [" << typeid(T).name() << "]. "
49  << "Create a vtkVariantExtract<> specialization to eliminate this warning."
50  );
51 
52  valid = false;
53 
54  static T dummy;
55  return dummy;
56 }
57 
58 template<>
59 inline char vtkVariantExtract<char>(const vtkVariant& value, bool& valid)
60 {
61  valid = value.IsChar();
62  return valid ? value.ToChar() : 0;
63 }
64 
65 template<>
66 inline unsigned char vtkVariantExtract<unsigned char>(const vtkVariant& value, bool& valid)
67 {
68  valid = value.IsUnsignedChar();
69  return valid ? value.ToUnsignedChar() : 0;
70 }
71 
72 template<>
73 inline short vtkVariantExtract<short>(const vtkVariant& value, bool& valid)
74 {
75  valid = value.IsShort();
76  return valid ? value.ToShort() : 0;
77 }
78 
79 template<>
80 inline unsigned short vtkVariantExtract<unsigned short>(const vtkVariant& value, bool& valid)
81 {
82  valid = value.IsUnsignedShort();
83  return valid ? value.ToUnsignedShort() : 0;
84 }
85 
86 template<>
87 inline int vtkVariantExtract<int>(const vtkVariant& value, bool& valid)
88 {
89  valid = value.IsInt();
90  return valid ? value.ToInt() : 0;
91 }
92 
93 template<>
94 inline unsigned int vtkVariantExtract<unsigned int>(const vtkVariant& value, bool& valid)
95 {
96  valid = value.IsUnsignedInt();
97  return valid ? value.ToUnsignedInt() : 0;
98 }
99 
100 template<>
101 inline long vtkVariantExtract<long>(const vtkVariant& value, bool& valid)
102 {
103  valid = value.IsLong();
104  return valid ? value.ToLong() : 0;
105 }
106 
107 template<>
108 inline unsigned long vtkVariantExtract<unsigned long>(const vtkVariant& value, bool& valid)
109 {
110  valid = value.IsUnsignedLong();
111  return valid ? value.ToUnsignedLong() : 0;
112 }
113 
114 template<>
115 inline long long vtkVariantExtract<long long>(const vtkVariant& value, bool& valid)
116 {
117  valid = value.IsLongLong();
118  return valid ? value.ToLongLong() : 0;
119 }
120 
121 template<>
122 inline unsigned long long vtkVariantExtract<unsigned long long>(const vtkVariant& value, bool& valid)
123 {
124  valid = value.IsUnsignedLongLong();
125  return valid ? value.ToUnsignedLongLong() : 0;
126 }
127 
128 template<>
129 inline float vtkVariantExtract<float>(const vtkVariant& value, bool& valid)
130 {
131  valid = value.IsFloat();
132  return valid ? value.ToFloat() : 0.0f;
133 }
134 
135 template<>
136 inline double vtkVariantExtract<double>(const vtkVariant& value, bool& valid)
137 {
138  valid = value.IsDouble();
139  return valid ? value.ToDouble() : 0.0;
140 }
141 
142 template<>
144 {
145  valid = value.IsString();
146  return valid ? value.ToString() : vtkStdString();
147 }
148 
149 template<>
151 {
152  valid = value.IsUnicodeString();
153  return valid ? value.ToUnicodeString() : vtkUnicodeString();
154 }
155 
156 template<>
158 {
159  valid = true;
160  return value;
161 }
162 
163 #endif
164 
165 // VTK-HeaderTest-Exclude: vtkVariantExtract.h
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:47
Performs an explicit conversion from a vtkVariant to the type that it contains.
A atomic type representing the union of many types.
Definition: vtkVariant.h:75
T vtkVariantExtract(const vtkVariant &value, bool &valid)
const char * GetTypeAsString() const
Get the type of the variant as a string.
String class that stores Unicode text.