VTK  9.1.20211019
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 
35 #ifndef vtkVariantExtract_h
36 #define vtkVariantExtract_h
37 
38 #include "vtkVariant.h"
39 #include <typeinfo> // for typeid
40 
41 template <typename T>
42 T vtkVariantExtract(const vtkVariant& value, bool& valid)
43 {
44  vtkGenericWarningMacro(
45  << "Cannot convert vtkVariant containing [" << value.GetTypeAsString() << "] "
46  << "to unsupported type [" << typeid(T).name() << "]. "
47  << "Create a vtkVariantExtract<> specialization to eliminate this warning.");
48 
49  valid = false;
50 
51  static T dummy;
52  return dummy;
53 }
54 
55 template <>
56 inline char vtkVariantExtract<char>(const vtkVariant& value, bool& valid)
57 {
58  valid = value.IsChar();
59  return valid ? value.ToChar() : 0;
60 }
61 
62 template <>
63 inline unsigned char vtkVariantExtract<unsigned char>(const vtkVariant& value, bool& valid)
64 {
65  valid = value.IsUnsignedChar();
66  return valid ? value.ToUnsignedChar() : 0;
67 }
68 
69 template <>
70 inline short vtkVariantExtract<short>(const vtkVariant& value, bool& valid)
71 {
72  valid = value.IsShort();
73  return valid ? value.ToShort() : 0;
74 }
75 
76 template <>
77 inline unsigned short vtkVariantExtract<unsigned short>(const vtkVariant& value, bool& valid)
78 {
79  valid = value.IsUnsignedShort();
80  return valid ? value.ToUnsignedShort() : 0;
81 }
82 
83 template <>
84 inline int vtkVariantExtract<int>(const vtkVariant& value, bool& valid)
85 {
86  valid = value.IsInt();
87  return valid ? value.ToInt() : 0;
88 }
89 
90 template <>
91 inline unsigned int vtkVariantExtract<unsigned int>(const vtkVariant& value, bool& valid)
92 {
93  valid = value.IsUnsignedInt();
94  return valid ? value.ToUnsignedInt() : 0;
95 }
96 
97 template <>
98 inline long vtkVariantExtract<long>(const vtkVariant& value, bool& valid)
99 {
100  valid = value.IsLong();
101  return valid ? value.ToLong() : 0;
102 }
103 
104 template <>
105 inline unsigned long vtkVariantExtract<unsigned long>(const vtkVariant& value, bool& valid)
106 {
107  valid = value.IsUnsignedLong();
108  return valid ? value.ToUnsignedLong() : 0;
109 }
110 
111 template <>
112 inline long long vtkVariantExtract<long long>(const vtkVariant& value, bool& valid)
113 {
114  valid = value.IsLongLong();
115  return valid ? value.ToLongLong() : 0;
116 }
117 
118 template <>
119 inline unsigned long long vtkVariantExtract<unsigned long long>(
120  const vtkVariant& value, bool& valid)
121 {
122  valid = value.IsUnsignedLongLong();
123  return valid ? value.ToUnsignedLongLong() : 0;
124 }
125 
126 template <>
127 inline float vtkVariantExtract<float>(const vtkVariant& value, bool& valid)
128 {
129  valid = value.IsFloat();
130  return valid ? value.ToFloat() : 0.0f;
131 }
132 
133 template <>
134 inline double vtkVariantExtract<double>(const vtkVariant& value, bool& valid)
135 {
136  valid = value.IsDouble();
137  return valid ? value.ToDouble() : 0.0;
138 }
139 
140 template <>
142 {
143  valid = value.IsString();
144  return valid ? value.ToString() : vtkStdString();
145 }
146 
147 template <>
149  "Use vtkStdString vtkVariantExtract<vtkStdString>(const vtkVariant& value, bool& valid)")
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:105
String class that stores Unicode text.
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:155
@ value
Definition: vtkX3D.h:226
#define VTK_DEPRECATED_IN_9_1_0(reason)
vtkVariant vtkVariantExtract< vtkVariant >(const vtkVariant &value, bool &valid)
long long vtkVariantExtract< long long >(const vtkVariant &value, bool &valid)
double vtkVariantExtract< double >(const vtkVariant &value, bool &valid)
unsigned long long vtkVariantExtract< unsigned long long >(const vtkVariant &value, bool &valid)
unsigned long vtkVariantExtract< unsigned long >(const vtkVariant &value, bool &valid)
int vtkVariantExtract< int >(const vtkVariant &value, bool &valid)
T vtkVariantExtract(const vtkVariant &value, bool &valid)
unsigned char vtkVariantExtract< unsigned char >(const vtkVariant &value, bool &valid)
vtkStdString vtkVariantExtract< vtkStdString >(const vtkVariant &value, bool &valid)
long vtkVariantExtract< long >(const vtkVariant &value, bool &valid)
float vtkVariantExtract< float >(const vtkVariant &value, bool &valid)
short vtkVariantExtract< short >(const vtkVariant &value, bool &valid)
unsigned short vtkVariantExtract< unsigned short >(const vtkVariant &value, bool &valid)
char vtkVariantExtract< char >(const vtkVariant &value, bool &valid)
unsigned int vtkVariantExtract< unsigned int >(const vtkVariant &value, bool &valid)