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