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