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