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