VTK: Common/Core/vtkVariantCast.h Source File

| VTK 9.3.1

vtkVariantCast.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-FileCopyrightText: Copyright 2008 Sandia Corporation
3 // SPDX-License-Identifier: LicenseRef-BSD-3-Clause-Sandia-USGov
4
22 #ifndef vtkVariantCast_h
23 #define vtkVariantCast_h
24
25 #include "vtkVariant.h"
26 #include <typeinfo> // for warnings
27
28 VTK_ABI_NAMESPACE_BEGIN
29 template <typename T>
30 T vtkVariantCast(const vtkVariant& value, bool* valid = nullptr)
31 {
32 vtkGenericWarningMacro(<< "Cannot convert vtkVariant containing [" << value.GetTypeAsString()
33 << "] "
34 << "to unsupported type [" << typeid(T).name() << "]. "
35 << "Create a vtkVariantCast<> specialization to eliminate this warning.");
36
37 if (valid)
38 *valid = false;
39
40 static T dummy;
41 return dummy;
42 }
43
44 template <>
45 inline char vtkVariantCast<char>(const vtkVariant& value, bool* valid)
46 {
47 return value.ToChar(valid);
48 }
49
50 template <>
51 inline signed char vtkVariantCast<signed char>(const vtkVariant& value, bool* valid)
52 {
53 return value.ToSignedChar(valid);
54 }
55
56 template <>
57 inline unsigned char vtkVariantCast<unsigned char>(const vtkVariant& value, bool* valid)
58 {
59 return value.ToUnsignedChar(valid);
60 }
61
62 template <>
63 inline short vtkVariantCast<short>(const vtkVariant& value, bool* valid)
64 {
65 return value.ToShort(valid);
66 }
67
68 template <>
69 inline unsigned short vtkVariantCast<unsigned short>(const vtkVariant& value, bool* valid)
70 {
71 return value.ToUnsignedShort(valid);
72 }
73
74 template <>
75 inline int vtkVariantCast<int>(const vtkVariant& value, bool* valid)
76 {
77 return value.ToInt(valid);
78 }
79
80 template <>
81 inline unsigned int vtkVariantCast<unsigned int>(const vtkVariant& value, bool* valid)
82 {
83 return value.ToUnsignedInt(valid);
84 }
85
86 template <>
87 inline long vtkVariantCast<long>(const vtkVariant& value, bool* valid)
88 {
89 return value.ToLong(valid);
90 }
91
92 template <>
93 inline unsigned long vtkVariantCast<unsigned long>(const vtkVariant& value, bool* valid)
94 {
95 return value.ToUnsignedLong(valid);
96 }
97
98 template <>
99 inline long long vtkVariantCast<long long>(const vtkVariant& value, bool* valid)
100 {
101 return value.ToLongLong(valid);
102 }
103
104 template <>
105 inline unsigned long long vtkVariantCast<unsigned long long>(const vtkVariant& value, bool* valid)
106 {
107 return value.ToUnsignedLongLong(valid);
108 }
109
110 template <>
111 inline float vtkVariantCast<float>(const vtkVariant& value, bool* valid)
112 {
113 return value.ToFloat(valid);
114 }
115
116 template <>
117 inline double vtkVariantCast<double>(const vtkVariant& value, bool* valid)
118 {
119 return value.ToDouble(valid);
120 }
121
122 template <>
123 inline vtkStdString vtkVariantCast<vtkStdString>(const vtkVariant& value, bool* valid)
124 {
125 if (valid)
126 *valid = true;
127
128 return value.ToString();
129 }
130
131 template <>
132 inline vtkVariant vtkVariantCast<vtkVariant>(const vtkVariant& value, bool* valid)
133 {
134 if (valid)
135 *valid = true;
136
137 return value;
138 }
139
140 VTK_ABI_NAMESPACE_END
141 #endif
142
143 // VTK-HeaderTest-Exclude: vtkVariantCast.h
vtkStdString
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:38
vtkVariant
A type representing the union of many types.
Definition: vtkVariant.h:62
vtkX3D::value
@ value
Definition: vtkX3D.h:220
vtkVariantCast< vtkStdString >
vtkStdString vtkVariantCast< vtkStdString >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:123
vtkVariantCast< unsigned int >
unsigned int vtkVariantCast< unsigned int >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:81
vtkVariantCast< vtkVariant >
vtkVariant vtkVariantCast< vtkVariant >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:132
vtkVariantCast< int >
int vtkVariantCast< int >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:75
vtkVariantCast< long >
long vtkVariantCast< long >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:87
vtkVariantCast< unsigned short >
unsigned short vtkVariantCast< unsigned short >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:69
vtkVariantCast
T vtkVariantCast(const vtkVariant &value, bool *valid=nullptr)
Definition: vtkVariantCast.h:30
vtkVariantCast< double >
double vtkVariantCast< double >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:117
vtkVariantCast< char >
char vtkVariantCast< char >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:45
vtkVariantCast< unsigned char >
unsigned char vtkVariantCast< unsigned char >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:57
vtkVariantCast< short >
short vtkVariantCast< short >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:63
vtkVariantCast< unsigned long >
unsigned long vtkVariantCast< unsigned long >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:93
vtkVariantCast< long long >
long long vtkVariantCast< long long >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:99
vtkVariantCast< unsigned long long >
unsigned long long vtkVariantCast< unsigned long long >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:105
vtkVariantCast< signed char >
signed char vtkVariantCast< signed char >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:51
vtkVariantCast< float >
float vtkVariantCast< float >(const vtkVariant &value, bool *valid)
Definition: vtkVariantCast.h:111
vtkVariant.h