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  static int VTKTypeID() { return VTK_##macro; } \
42  \
43  /* The smallest possible value represented by the type. */ \
44  static type Min() { return VTK_##macro##_MIN; } \
45  \
46  /* The largest possible value represented by the type. */ \
47  static type Max() { return VTK_##macro##_MAX; } \
48  \
49  /* Whether the type is signed. */ \
50  static int IsSigned() { return isSigned; } \
51  \
52  /* An "alias" type that is the same size and signedness. */ \
53  typedef vtkType##name SizedType; \
54  \
55  /* A name for the type indicating its size and signedness. */ \
56  static const char* SizedName() { return #name; } \
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 formating 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,
97  "%hu");
98 
99 // Define traits for int types.
100 #define VTK_TYPE_NAME_INT int
101 #define VTK_TYPE_NAME_UNSIGNED_INT unsigned int
102 #define VTK_TYPE_SIZED_INT INT32
103 #define VTK_TYPE_SIZED_UNSIGNED_INT UINT32
104 VTK_TYPE_TRAITS(int, INT, 1, Int32, int, "%d");
105 VTK_TYPE_TRAITS(unsigned int, UNSIGNED_INT, 0, UInt32, unsigned int, "%u");
106 
107 // Define traits for long types.
108 #define VTK_TYPE_NAME_LONG long
109 #define VTK_TYPE_NAME_UNSIGNED_LONG unsigned long
110 #if VTK_SIZEOF_LONG == 4
111 # define VTK_TYPE_SIZED_LONG INT32
112 # define VTK_TYPE_SIZED_UNSIGNED_LONG UINT32
113 VTK_TYPE_TRAITS(long, LONG, 1, Int32, long, "%ld");
114 VTK_TYPE_TRAITS(unsigned long, UNSIGNED_LONG, 0, UInt32, unsigned long, "%lu");
115 #elif VTK_SIZEOF_LONG == 8
116 # define VTK_TYPE_SIZED_LONG INT64
117 # define VTK_TYPE_SIZED_UNSIGNED_LONG UINT64
118 VTK_TYPE_TRAITS(long, LONG, 1, Int64, long, "%ld");
119 VTK_TYPE_TRAITS(unsigned long, UNSIGNED_LONG, 0, UInt64, unsigned long, "%lu");
120 #else
121 # error "Type long is not 4 or 8 bytes in size."
122 #endif
123 
124 // Define traits for long long types if they are enabled.
125 #if defined(VTK_TYPE_USE_LONG_LONG)
126 # define VTK_TYPE_NAME_LONG_LONG long long
127 # define VTK_TYPE_NAME_UNSIGNED_LONG_LONG unsigned long long
128 # if VTK_SIZEOF_LONG_LONG == 8
129 # define VTK_TYPE_SIZED_LONG_LONG INT64
130 # define VTK_TYPE_SIZED_UNSIGNED_LONG_LONG UINT64
131 # if defined(_MSC_VER) && _MSC_VER < 1400
132 # define VTK_TYPE_LONG_LONG_FORMAT "%I64"
133 # else
134 # define VTK_TYPE_LONG_LONG_FORMAT "%ll"
135 # endif
136 VTK_TYPE_TRAITS(long long, LONG_LONG, 1, Int64, long long,
137  VTK_TYPE_LONG_LONG_FORMAT "d");
138 VTK_TYPE_TRAITS(unsigned long long, UNSIGNED_LONG_LONG, 0, UInt64,
139  unsigned long long, 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 #endif
145 
146 // Define traits for __int64 types if they are enabled.
147 #if defined(VTK_TYPE_USE___INT64)
148 # define VTK_TYPE_NAME___INT64 __int64
149 # define VTK_TYPE_NAME_UNSIGNED___INT64 unsigned __int64
150 # if VTK_SIZEOF___INT64 == 8
151 # define VTK_TYPE_SIZED___INT64 INT64
152 # define VTK_TYPE_SIZED_UNSIGNED___INT64 UINT64
153 VTK_TYPE_TRAITS(__int64, __INT64, 1, Int64, __int64, "%I64d");
154 VTK_TYPE_TRAITS(unsigned __int64, UNSIGNED___INT64, 0, UInt64,
155  unsigned __int64, "%I64u");
156 # else
157 # error "Type __int64 is not 8 bytes in size."
158 # endif
159 #endif
160 
161 // Define traits for vtkIdType. The template specialization is
162 // already defined for the corresponding native type.
163 #define VTK_TYPE_NAME_ID_TYPE vtkIdType
164 #if defined(VTK_USE_64BIT_IDS)
165 # define VTK_TYPE_SIZED_ID_TYPE INT64
166 #else
167 # define VTK_TYPE_SIZED_ID_TYPE INT32
168 #endif
169 
170 #undef VTK_TYPE_TRAITS
171 
172 #endif
#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