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