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