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