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