VTK  9.7.1
vtkTypeTraits.h
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
2 // SPDX-FileCopyrightText: Copyright (c) Kitware, Inc.
3 // SPDX-License-Identifier: BSD-3-Clause
15 #ifndef vtkTypeTraits_h
16 #define vtkTypeTraits_h
17 
18 #include "vtkStdString.h"
19 #include "vtkSystemIncludes.h"
20 #include "vtkVariant.h"
21 
22 // Forward-declare template. There is no primary template.
23 VTK_ABI_NAMESPACE_BEGIN
24 template <class T>
26 
27 // Define a macro to simplify trait definitions.
28 #define VTK_TYPE_TRAITS(type, macro, isSigned, name, print, format) \
29  template <> \
30  struct vtkTypeTraits<type> \
31  { \
32  /* The type itself. */ \
33  using ValueType = type; \
34  \
35  /* the value defined for this type in vtkType */ \
36  enum \
37  { \
38  VTK_TYPE_ID = VTK_##macro \
39  }; \
40  static int VTKTypeID() \
41  { \
42  return VTK_##macro; \
43  } \
44  \
45  /* The smallest possible value represented by the type. */ \
46  static type Min() \
47  { \
48  return VTK_##macro##_MIN; \
49  } \
50  \
51  /* The largest possible value represented by the type. */ \
52  static type Max() \
53  { \
54  return VTK_##macro##_MAX; \
55  } \
56  \
57  /* Whether the type is signed. */ \
58  static int IsSigned() \
59  { \
60  return isSigned; \
61  } \
62  \
63  /* An "alias" type that is the same size and signedness. */ \
64  typedef vtkType##name SizedType; \
65  \
66  /* A name for the type indicating its size and signedness. */ \
67  static const char* SizedName() \
68  { \
69  return #name; \
70  } \
71  \
72  /* The common C++ name for the type (e.g. float, unsigned int, etc).*/ \
73  static const char* Name() \
74  { \
75  return #type; \
76  } \
77  \
78  /* A type to use for printing or parsing values in strings. */ \
79  using PrintType = print; \
80  \
81  /* A format for parsing values from strings. Use with PrintType. */ \
82  static const char* ParseFormat() \
83  { \
84  return format; \
85  } \
86  }
87 
88 // Define a macro to simplify trait definitions.
89 #define VTK_NON_NUMERIC_TYPE_TRAITS(type, macro) \
90  template <> \
91  struct vtkTypeTraits<type> \
92  { \
93  /* The type itself. */ \
94  using ValueType = type; \
95  \
96  /* the value defined for this type in vtkType */ \
97  enum \
98  { \
99  VTK_TYPE_ID = VTK_##macro \
100  }; \
101  static int VTKTypeID() \
102  { \
103  return VTK_##macro; \
104  } \
105  \
106  /* The common C++ name for the type (e.g. float, unsigned int, etc).*/ \
107  static const char* Name() \
108  { \
109  return #type; \
110  } \
111  }
112 
113 // Define traits for floating-point types.
114 #define VTK_TYPE_NAME_FLOAT float
115 #define VTK_TYPE_NAME_DOUBLE double
116 #define VTK_TYPE_SIZED_FLOAT FLOAT32
117 #define VTK_TYPE_SIZED_DOUBLE FLOAT64
118 VTK_TYPE_TRAITS(float, FLOAT, 1, Float32, float, "%f");
119 VTK_TYPE_TRAITS(double, DOUBLE, 1, Float64, double, "%lf");
120 
121 // Define traits for char types.
122 // Note the print type is short because not all platforms support formatting integers with char.
123 #define VTK_TYPE_NAME_CHAR char
124 #if VTK_TYPE_CHAR_IS_SIGNED
125 #define VTK_TYPE_SIZED_CHAR INT8
126 VTK_TYPE_TRAITS(char, CHAR, 1, Int8, short, "%hd");
127 #else
128 #define VTK_TYPE_SIZED_CHAR UINT8
129 VTK_TYPE_TRAITS(char, CHAR, 0, UInt8, unsigned short, "%hu");
130 #endif
131 #define VTK_TYPE_NAME_SIGNED_CHAR signed char
132 #define VTK_TYPE_NAME_UNSIGNED_CHAR unsigned char
133 #define VTK_TYPE_SIZED_SIGNED_CHAR INT8
134 #define VTK_TYPE_SIZED_UNSIGNED_CHAR UINT8
135 VTK_TYPE_TRAITS(signed char, SIGNED_CHAR, 1, Int8, short, "%hd");
136 VTK_TYPE_TRAITS(unsigned char, UNSIGNED_CHAR, 0, UInt8, unsigned short, "%hu");
137 
138 // Define traits for short types.
139 #define VTK_TYPE_NAME_SHORT short
140 #define VTK_TYPE_NAME_UNSIGNED_SHORT unsigned short
141 #define VTK_TYPE_SIZED_SHORT INT16
142 #define VTK_TYPE_SIZED_UNSIGNED_SHORT UINT16
143 VTK_TYPE_TRAITS(short, SHORT, 1, Int16, short, "%hd");
144 VTK_TYPE_TRAITS(unsigned short, UNSIGNED_SHORT, 0, UInt16, unsigned short, "%hu");
145 
146 // Define traits for int types.
147 #define VTK_TYPE_NAME_INT int
148 #define VTK_TYPE_NAME_UNSIGNED_INT unsigned int
149 #define VTK_TYPE_SIZED_INT INT32
150 #define VTK_TYPE_SIZED_UNSIGNED_INT UINT32
151 VTK_TYPE_TRAITS(int, INT, 1, Int32, int, "%d");
152 VTK_TYPE_TRAITS(unsigned int, UNSIGNED_INT, 0, UInt32, unsigned int, "%u");
153 
154 // Define traits for long types.
155 #define VTK_TYPE_NAME_LONG long
156 #define VTK_TYPE_NAME_UNSIGNED_LONG unsigned long
157 #if VTK_SIZEOF_LONG == 4
158 #define VTK_TYPE_SIZED_LONG INT32
159 #define VTK_TYPE_SIZED_UNSIGNED_LONG UINT32
160 VTK_TYPE_TRAITS(long, LONG, 1, Int32, long, "%ld");
161 VTK_TYPE_TRAITS(unsigned long, UNSIGNED_LONG, 0, UInt32, unsigned long, "%lu");
162 #elif VTK_SIZEOF_LONG == 8
163 #define VTK_TYPE_SIZED_LONG INT64
164 #define VTK_TYPE_SIZED_UNSIGNED_LONG UINT64
165 VTK_TYPE_TRAITS(long, LONG, 1, Int64, long, "%ld");
166 VTK_TYPE_TRAITS(unsigned long, UNSIGNED_LONG, 0, UInt64, unsigned long, "%lu");
167 #else
168 #error "Type long is not 4 or 8 bytes in size."
169 #endif
170 
171 // Define traits for long long types if they are enabled.
172 #define VTK_TYPE_NAME_LONG_LONG long long
173 #define VTK_TYPE_NAME_UNSIGNED_LONG_LONG unsigned long long
174 #if VTK_SIZEOF_LONG_LONG == 8
175 #define VTK_TYPE_SIZED_LONG_LONG INT64
176 #define VTK_TYPE_SIZED_UNSIGNED_LONG_LONG UINT64
177 #define VTK_TYPE_LONG_LONG_FORMAT "%ll"
178 VTK_TYPE_TRAITS(long long, LONG_LONG, 1, Int64, long long, VTK_TYPE_LONG_LONG_FORMAT "d");
179 VTK_TYPE_TRAITS(unsigned long long, UNSIGNED_LONG_LONG, 0, UInt64, unsigned long long,
180  VTK_TYPE_LONG_LONG_FORMAT "u");
181 #undef VTK_TYPE_LONG_LONG_FORMAT
182 #else
183 #error "Type long long is not 8 bytes in size."
184 #endif
185 
186 // Define traits for vtkIdType. The template specialization is
187 // already defined for the corresponding native type.
188 #define VTK_TYPE_NAME_ID_TYPE vtkIdType
189 #if defined(VTK_USE_64BIT_IDS)
190 #define VTK_TYPE_SIZED_ID_TYPE INT64
191 #else
192 #define VTK_TYPE_SIZED_ID_TYPE INT32
193 #endif
194 
195 // Define traits for vtkStdString
197 
198 // Define traits for vtkVariant
200 
201 #undef VTK_TYPE_TRAITS
202 #undef VTK_NON_NUMERIC_TYPE_TRAITS
203 
204 VTK_ABI_NAMESPACE_END
205 #endif
206 // VTK-HeaderTest-Exclude: vtkTypeTraits.h
Wrapper around std::string to keep symbols short.
Definition: vtkStdString.h:27
A type representing the union of many types.
Definition: vtkVariant.h:52
#define VTK_TYPE_TRAITS(type, macro, isSigned, name, print, format)
Definition: vtkTypeTraits.h:28
#define VTK_NON_NUMERIC_TYPE_TRAITS(type, macro)
Definition: vtkTypeTraits.h:89
Template defining traits of native types used by VTK.
Definition: vtkTypeTraits.h:25