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