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