VTK  9.4.20241108
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
28VTK_ABI_NAMESPACE_BEGIN
29template <typename T>
30T 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
44template <>
45inline char vtkVariantCast<char>(const vtkVariant& value, bool* valid)
46{
47 return value.ToChar(valid);
48}
49
50template <>
51inline signed char vtkVariantCast<signed char>(const vtkVariant& value, bool* valid)
52{
53 return value.ToSignedChar(valid);
54}
55
56template <>
57inline unsigned char vtkVariantCast<unsigned char>(const vtkVariant& value, bool* valid)
58{
59 return value.ToUnsignedChar(valid);
60}
61
62template <>
63inline short vtkVariantCast<short>(const vtkVariant& value, bool* valid)
64{
65 return value.ToShort(valid);
66}
67
68template <>
69inline unsigned short vtkVariantCast<unsigned short>(const vtkVariant& value, bool* valid)
70{
71 return value.ToUnsignedShort(valid);
72}
73
74template <>
75inline int vtkVariantCast<int>(const vtkVariant& value, bool* valid)
76{
77 return value.ToInt(valid);
78}
79
80template <>
81inline unsigned int vtkVariantCast<unsigned int>(const vtkVariant& value, bool* valid)
82{
83 return value.ToUnsignedInt(valid);
84}
85
86template <>
87inline long vtkVariantCast<long>(const vtkVariant& value, bool* valid)
88{
89 return value.ToLong(valid);
90}
91
92template <>
93inline unsigned long vtkVariantCast<unsigned long>(const vtkVariant& value, bool* valid)
94{
95 return value.ToUnsignedLong(valid);
96}
97
98template <>
99inline long long vtkVariantCast<long long>(const vtkVariant& value, bool* valid)
100{
101 return value.ToLongLong(valid);
102}
103
104template <>
105inline unsigned long long vtkVariantCast<unsigned long long>(const vtkVariant& value, bool* valid)
106{
107 return value.ToUnsignedLongLong(valid);
108}
109
110template <>
111inline float vtkVariantCast<float>(const vtkVariant& value, bool* valid)
112{
113 return value.ToFloat(valid);
114}
115
116template <>
117inline double vtkVariantCast<double>(const vtkVariant& value, bool* valid)
118{
119 return value.ToDouble(valid);
120}
121
122template <>
123inline vtkStdString vtkVariantCast<vtkStdString>(const vtkVariant& value, bool* valid)
124{
125 if (valid)
126 *valid = true;
127
128 return value.ToString();
129}
130
131template <>
132inline vtkVariant vtkVariantCast<vtkVariant>(const vtkVariant& value, bool* valid)
133{
134 if (valid)
135 *valid = true;
136
137 return value;
138}
139
140VTK_ABI_NAMESPACE_END
141#endif
142
143// VTK-HeaderTest-Exclude: vtkVariantCast.h
Wrapper around std::string to keep symbols short.
Converts a vtkVariant to some other type.
A type representing the union of many types.
Definition vtkVariant.h:162
vtkStdString vtkVariantCast< vtkStdString >(const vtkVariant &value, bool *valid)
unsigned int vtkVariantCast< unsigned int >(const vtkVariant &value, bool *valid)
vtkVariant vtkVariantCast< vtkVariant >(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)