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