VTK  9.1.0
vtkVariantCast.h
Go to the documentation of this file.
1/*=========================================================================
2
3 Program: Visualization Toolkit
4 Module: vtkVariantCast.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
36#ifndef vtkVariantCast_h
37#define vtkVariantCast_h
38
39#include "vtkVariant.h"
40#include <typeinfo> // for warnings
41
42template <typename T>
43T vtkVariantCast(const vtkVariant& value, bool* valid = nullptr)
44{
45 vtkGenericWarningMacro(<< "Cannot convert vtkVariant containing [" << value.GetTypeAsString()
46 << "] "
47 << "to unsupported type [" << typeid(T).name() << "]. "
48 << "Create a vtkVariantCast<> specialization to eliminate this warning.");
49
50 if (valid)
51 *valid = false;
52
53 static T dummy;
54 return dummy;
55}
56
57template <>
58inline char vtkVariantCast<char>(const vtkVariant& value, bool* valid)
59{
60 return value.ToChar(valid);
61}
62
63template <>
64inline signed char vtkVariantCast<signed char>(const vtkVariant& value, bool* valid)
65{
66 return value.ToSignedChar(valid);
67}
68
69template <>
70inline unsigned char vtkVariantCast<unsigned char>(const vtkVariant& value, bool* valid)
71{
72 return value.ToUnsignedChar(valid);
73}
74
75template <>
76inline short vtkVariantCast<short>(const vtkVariant& value, bool* valid)
77{
78 return value.ToShort(valid);
79}
80
81template <>
82inline unsigned short vtkVariantCast<unsigned short>(const vtkVariant& value, bool* valid)
83{
84 return value.ToUnsignedShort(valid);
85}
86
87template <>
88inline int vtkVariantCast<int>(const vtkVariant& value, bool* valid)
89{
90 return value.ToInt(valid);
91}
92
93template <>
94inline unsigned int vtkVariantCast<unsigned int>(const vtkVariant& value, bool* valid)
95{
96 return value.ToUnsignedInt(valid);
97}
98
99template <>
100inline long vtkVariantCast<long>(const vtkVariant& value, bool* valid)
101{
102 return value.ToLong(valid);
103}
104
105template <>
106inline unsigned long vtkVariantCast<unsigned long>(const vtkVariant& value, bool* valid)
107{
108 return value.ToUnsignedLong(valid);
109}
110
111template <>
112inline long long vtkVariantCast<long long>(const vtkVariant& value, bool* valid)
113{
114 return value.ToLongLong(valid);
115}
116
117template <>
118inline unsigned long long vtkVariantCast<unsigned long long>(const vtkVariant& value, bool* valid)
119{
120 return value.ToUnsignedLongLong(valid);
121}
122
123template <>
124inline float vtkVariantCast<float>(const vtkVariant& value, bool* valid)
125{
126 return value.ToFloat(valid);
127}
128
129template <>
130inline double vtkVariantCast<double>(const vtkVariant& value, bool* valid)
131{
132 return value.ToDouble(valid);
133}
134
135template <>
136inline vtkStdString vtkVariantCast<vtkStdString>(const vtkVariant& value, bool* valid)
137{
138 if (valid)
139 *valid = true;
140
141 return value.ToString();
142}
143
144template <>
145VTKCOMMONCORE_EXPORT VTK_DEPRECATED_IN_9_1_0(
146 "Use vtkStdString vtkVariantCast<vtkStdString>(const vtkVariant& value, bool* valid)")
148
149template <>
150inline vtkVariant vtkVariantCast<vtkVariant>(const vtkVariant& value, bool* valid)
151{
152 if (valid)
153 *valid = true;
154
155 return value;
156}
157
158#endif
159
160// VTK-HeaderTest-Exclude: vtkVariantCast.h
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:105
String class that stores Unicode text.
Converts a vtkVariant to some other type.
A atomic type representing the union of many types.
Definition: vtkVariant.h:145
#define VTK_DEPRECATED_IN_9_1_0(reason)
vtkStdString vtkVariantCast< vtkStdString >(const vtkVariant &value, bool *valid)
unsigned int vtkVariantCast< unsigned int >(const vtkVariant &value, bool *valid)
int vtkVariantCast< int >(const vtkVariant &value, bool *valid)
long vtkVariantCast< long >(const vtkVariant &value, bool *valid)
unsigned short vtkVariantCast< unsigned short >(const vtkVariant &value, bool *valid)
double vtkVariantCast< double >(const vtkVariant &value, bool *valid)
char vtkVariantCast< char >(const vtkVariant &value, bool *valid)
unsigned char vtkVariantCast< unsigned char >(const vtkVariant &value, bool *valid)
short vtkVariantCast< short >(const vtkVariant &value, bool *valid)
unsigned long vtkVariantCast< unsigned long >(const vtkVariant &value, bool *valid)
long long vtkVariantCast< long long >(const vtkVariant &value, bool *valid)
unsigned long long vtkVariantCast< unsigned long long >(const vtkVariant &value, bool *valid)
signed char vtkVariantCast< signed char >(const vtkVariant &value, bool *valid)
float vtkVariantCast< float >(const vtkVariant &value, bool *valid)