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  template <> 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  /* The common C++ name for the type (e.g. float, unsigned int, etc).*/ \
60  static const char* Name() { return #type; } \
61  \
62  /* A type to use for printing or parsing values in strings. */ \
63  typedef print PrintType; \
64  \
65  /* A format for parsing values from strings. Use with PrintType. */ \
66  static const char* ParseFormat() { return format; } \
67  }
68 
69 // Define traits for floating-point types.
70 #define VTK_TYPE_NAME_FLOAT float
71 #define VTK_TYPE_NAME_DOUBLE double
72 #define VTK_TYPE_SIZED_FLOAT FLOAT32
73 #define VTK_TYPE_SIZED_DOUBLE FLOAT64
74 VTK_TYPE_TRAITS(float, FLOAT, 1, Float32, float, "%f");
75 VTK_TYPE_TRAITS(double, DOUBLE, 1, Float64, double, "%lf");
76 
77 // Define traits for char types.
78 // Note the print type is short because not all platforms support formating integers with char.
79 #define VTK_TYPE_NAME_CHAR char
80 #if VTK_TYPE_CHAR_IS_SIGNED
81 # define VTK_TYPE_SIZED_CHAR INT8
82 VTK_TYPE_TRAITS(char, CHAR, 1, Int8, short, "%hd");
83 #else
84 # define VTK_TYPE_SIZED_CHAR UINT8
85 VTK_TYPE_TRAITS(char, CHAR, 0, UInt8, unsigned short, "%hu");
86 #endif
87 #define VTK_TYPE_NAME_SIGNED_CHAR signed char
88 #define VTK_TYPE_NAME_UNSIGNED_CHAR unsigned char
89 #define VTK_TYPE_SIZED_SIGNED_CHAR INT8
90 #define VTK_TYPE_SIZED_UNSIGNED_CHAR UINT8
91 VTK_TYPE_TRAITS(signed char, SIGNED_CHAR, 1, Int8, short, "%hd");
92 VTK_TYPE_TRAITS(unsigned char, UNSIGNED_CHAR, 0, UInt8, unsigned short, "%hu");
93 
94 // Define traits for short types.
95 #define VTK_TYPE_NAME_SHORT short
96 #define VTK_TYPE_NAME_UNSIGNED_SHORT unsigned short
97 #define VTK_TYPE_SIZED_SHORT INT16
98 #define VTK_TYPE_SIZED_UNSIGNED_SHORT UINT16
99 VTK_TYPE_TRAITS(short, SHORT, 1, Int16, short, "%hd");
100 VTK_TYPE_TRAITS(unsigned short, UNSIGNED_SHORT, 0, UInt16, unsigned short,
101  "%hu");
102 
103 // Define traits for int types.
104 #define VTK_TYPE_NAME_INT int
105 #define VTK_TYPE_NAME_UNSIGNED_INT unsigned int
106 #define VTK_TYPE_SIZED_INT INT32
107 #define VTK_TYPE_SIZED_UNSIGNED_INT UINT32
108 VTK_TYPE_TRAITS(int, INT, 1, Int32, int, "%d");
109 VTK_TYPE_TRAITS(unsigned int, UNSIGNED_INT, 0, UInt32, unsigned int, "%u");
110 
111 // Define traits for long types.
112 #define VTK_TYPE_NAME_LONG long
113 #define VTK_TYPE_NAME_UNSIGNED_LONG unsigned long
114 #if VTK_SIZEOF_LONG == 4
115 # define VTK_TYPE_SIZED_LONG INT32
116 # define VTK_TYPE_SIZED_UNSIGNED_LONG UINT32
117 VTK_TYPE_TRAITS(long, LONG, 1, Int32, long, "%ld");
118 VTK_TYPE_TRAITS(unsigned long, UNSIGNED_LONG, 0, UInt32, unsigned long, "%lu");
119 #elif VTK_SIZEOF_LONG == 8
120 # define VTK_TYPE_SIZED_LONG INT64
121 # define VTK_TYPE_SIZED_UNSIGNED_LONG UINT64
122 VTK_TYPE_TRAITS(long, LONG, 1, Int64, long, "%ld");
123 VTK_TYPE_TRAITS(unsigned long, UNSIGNED_LONG, 0, UInt64, unsigned long, "%lu");
124 #else
125 # error "Type long is not 4 or 8 bytes in size."
126 #endif
127 
128 // Define traits for long long types if they are enabled.
129 #define VTK_TYPE_NAME_LONG_LONG long long
130 #define VTK_TYPE_NAME_UNSIGNED_LONG_LONG unsigned long long
131 #if VTK_SIZEOF_LONG_LONG == 8
132 # define VTK_TYPE_SIZED_LONG_LONG INT64
133 # define VTK_TYPE_SIZED_UNSIGNED_LONG_LONG UINT64
134 # define VTK_TYPE_LONG_LONG_FORMAT "%ll"
135 VTK_TYPE_TRAITS(long long, LONG_LONG, 1, Int64, long long,
136  VTK_TYPE_LONG_LONG_FORMAT "d");
137 VTK_TYPE_TRAITS(unsigned long long, UNSIGNED_LONG_LONG, 0, UInt64,
138  unsigned long long, VTK_TYPE_LONG_LONG_FORMAT "u");
139 # undef VTK_TYPE_LONG_LONG_FORMAT
140 #else
141 # error "Type long long is not 8 bytes in size."
142 #endif
143 
144 // Define traits for vtkIdType. The template specialization is
145 // already defined for the corresponding native type.
146 #define VTK_TYPE_NAME_ID_TYPE vtkIdType
147 #if defined(VTK_USE_64BIT_IDS)
148 # define VTK_TYPE_SIZED_ID_TYPE INT64
149 #else
150 # define VTK_TYPE_SIZED_ID_TYPE INT32
151 #endif
152 
153 #undef VTK_TYPE_TRAITS
154 
155 #endif
156 // 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