VTK
dox/Common/Core/vtkTypeTraits.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   ParaView
00004   Module:    vtkTypeTraits.h
00005 
00006   Copyright (c) Kitware, Inc.
00007   All rights reserved.
00008   See Copyright.txt or http://www.paraview.org/HTML/Copyright.html for details.
00009 
00010      This software is distributed WITHOUT ANY WARRANTY; without even
00011      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00012      PURPOSE.  See the above copyright notice for more information.
00013 
00014 =========================================================================*/
00025 #ifndef __vtkTypeTraits_h
00026 #define __vtkTypeTraits_h
00027 
00028 #include "vtkSystemIncludes.h"
00029 
00030 // Forward-declare template.  There is no primary template.
00031 template <class T> struct vtkTypeTraits;
00032 
00033 // Define a macro to simplify trait definitions.
00034 #define VTK_TYPE_TRAITS(type, macro, isSigned, name, print, format)           \
00035   VTK_TEMPLATE_SPECIALIZE struct vtkTypeTraits< type >                        \
00036   {                                                                           \
00037     /* The type itself.  */                                                   \
00038     typedef type ValueType;                                                   \
00039                                                                               \
00040     /* the value defined for this type in vtkType */                          \
00041     static int VTKTypeID() { return VTK_##macro; }                            \
00042                                                                               \
00043     /* The smallest possible value represented by the type.  */               \
00044     static type Min() { return VTK_##macro##_MIN; }                           \
00045                                                                               \
00046     /* The largest possible value represented by the type.  */                \
00047     static type Max() { return VTK_##macro##_MAX; }                           \
00048                                                                               \
00049     /* Whether the type is signed.  */                                        \
00050     static int IsSigned() { return isSigned; }                                \
00051                                                                               \
00052     /* An "alias" type that is the same size and signedness.  */              \
00053     typedef vtkType##name SizedType;                                          \
00054                                                                               \
00055     /* A name for the type indicating its size and signedness.  */            \
00056     static const char* SizedName() { return #name; }                          \
00057                                                                               \
00058     /* A type to use for printing or parsing values in strings.  */           \
00059     typedef print PrintType;                                                  \
00060                                                                               \
00061     /* A format for parsing values from strings.  Use with PrintType.  */     \
00062     static const char* ParseFormat() { return format; }                       \
00063   }
00064 
00065 // Define traits for floating-point types.
00066 #define VTK_TYPE_NAME_FLOAT float
00067 #define VTK_TYPE_NAME_DOUBLE double
00068 #define VTK_TYPE_SIZED_FLOAT FLOAT32
00069 #define VTK_TYPE_SIZED_DOUBLE FLOAT64
00070 VTK_TYPE_TRAITS(float, FLOAT, 1, Float32, float, "%f");
00071 VTK_TYPE_TRAITS(double, DOUBLE, 1, Float64, double, "%lf");
00072 
00073 // Define traits for char types.
00074 // Note the print type is short because not all platforms support formating integers with char.
00075 #define VTK_TYPE_NAME_CHAR char
00076 #if VTK_TYPE_CHAR_IS_SIGNED
00077 # define VTK_TYPE_SIZED_CHAR INT8
00078 VTK_TYPE_TRAITS(char, CHAR, 1, Int8, short, "%hd");
00079 #else
00080 # define VTK_TYPE_SIZED_CHAR UINT8
00081 VTK_TYPE_TRAITS(char, CHAR, 0, UInt8, unsigned short, "%hu");
00082 #endif
00083 #define VTK_TYPE_NAME_SIGNED_CHAR signed char
00084 #define VTK_TYPE_NAME_UNSIGNED_CHAR unsigned char
00085 #define VTK_TYPE_SIZED_SIGNED_CHAR INT8
00086 #define VTK_TYPE_SIZED_UNSIGNED_CHAR UINT8
00087 VTK_TYPE_TRAITS(signed char, SIGNED_CHAR, 1, Int8, short, "%hd");
00088 VTK_TYPE_TRAITS(unsigned char, UNSIGNED_CHAR, 0, UInt8, unsigned short, "%hu");
00089 
00090 // Define traits for short types.
00091 #define VTK_TYPE_NAME_SHORT short
00092 #define VTK_TYPE_NAME_UNSIGNED_SHORT unsigned short
00093 #define VTK_TYPE_SIZED_SHORT INT16
00094 #define VTK_TYPE_SIZED_UNSIGNED_SHORT UINT16
00095 VTK_TYPE_TRAITS(short, SHORT, 1, Int16, short, "%hd");
00096 VTK_TYPE_TRAITS(unsigned short, UNSIGNED_SHORT, 0, UInt16, unsigned short,
00097                 "%hu");
00098 
00099 // Define traits for int types.
00100 #define VTK_TYPE_NAME_INT int
00101 #define VTK_TYPE_NAME_UNSIGNED_INT unsigned int
00102 #define VTK_TYPE_SIZED_INT INT32
00103 #define VTK_TYPE_SIZED_UNSIGNED_INT UINT32
00104 VTK_TYPE_TRAITS(int, INT, 1, Int32, int, "%d");
00105 VTK_TYPE_TRAITS(unsigned int, UNSIGNED_INT, 0, UInt32, unsigned int, "%u");
00106 
00107 // Define traits for long types.
00108 #define VTK_TYPE_NAME_LONG long
00109 #define VTK_TYPE_NAME_UNSIGNED_LONG unsigned long
00110 #if VTK_SIZEOF_LONG == 4
00111 # define VTK_TYPE_SIZED_LONG INT32
00112 # define VTK_TYPE_SIZED_UNSIGNED_LONG UINT32
00113 VTK_TYPE_TRAITS(long, LONG, 1, Int32, long, "%ld");
00114 VTK_TYPE_TRAITS(unsigned long, UNSIGNED_LONG, 0, UInt32, unsigned long, "%lu");
00115 #elif VTK_SIZEOF_LONG == 8
00116 # define VTK_TYPE_SIZED_LONG INT64
00117 # define VTK_TYPE_SIZED_UNSIGNED_LONG UINT64
00118 VTK_TYPE_TRAITS(long, LONG, 1, Int64, long, "%ld");
00119 VTK_TYPE_TRAITS(unsigned long, UNSIGNED_LONG, 0, UInt64, unsigned long, "%lu");
00120 #else
00121 # error "Type long is not 4 or 8 bytes in size."
00122 #endif
00123 
00124 // Define traits for long long types if they are enabled.
00125 #if defined(VTK_TYPE_USE_LONG_LONG)
00126 # define VTK_TYPE_NAME_LONG_LONG long long
00127 # define VTK_TYPE_NAME_UNSIGNED_LONG_LONG unsigned long long
00128 # if VTK_SIZEOF_LONG_LONG == 8
00129 #  define VTK_TYPE_SIZED_LONG_LONG INT64
00130 #  define VTK_TYPE_SIZED_UNSIGNED_LONG_LONG UINT64
00131 #  if defined(_MSC_VER) && _MSC_VER < 1400
00132 #   define VTK_TYPE_LONG_LONG_FORMAT "%I64"
00133 #  else
00134 #   define VTK_TYPE_LONG_LONG_FORMAT "%ll"
00135 #  endif
00136 VTK_TYPE_TRAITS(long long, LONG_LONG, 1, Int64, long long,
00137                 VTK_TYPE_LONG_LONG_FORMAT "d");
00138 VTK_TYPE_TRAITS(unsigned long long, UNSIGNED_LONG_LONG, 0, UInt64,
00139                 unsigned long long, VTK_TYPE_LONG_LONG_FORMAT "u");
00140 #  undef VTK_TYPE_LONG_LONG_FORMAT
00141 # else
00142 #  error "Type long long is not 8 bytes in size."
00143 # endif
00144 #endif
00145 
00146 // Define traits for __int64 types if they are enabled.
00147 #if defined(VTK_TYPE_USE___INT64)
00148 # define VTK_TYPE_NAME___INT64 __int64
00149 # define VTK_TYPE_NAME_UNSIGNED___INT64 unsigned __int64
00150 # if VTK_SIZEOF___INT64 == 8
00151 #  define VTK_TYPE_SIZED___INT64 INT64
00152 #  define VTK_TYPE_SIZED_UNSIGNED___INT64 UINT64
00153 VTK_TYPE_TRAITS(__int64, __INT64, 1, Int64, __int64, "%I64d");
00154 VTK_TYPE_TRAITS(unsigned __int64, UNSIGNED___INT64, 0, UInt64,
00155                 unsigned __int64, "%I64u");
00156 # else
00157 #  error "Type __int64 is not 8 bytes in size."
00158 # endif
00159 #endif
00160 
00161 // Define traits for vtkIdType.  The template specialization is
00162 // already defined for the corresponding native type.
00163 #define VTK_TYPE_NAME_ID_TYPE vtkIdType
00164 #if defined(VTK_USE_64BIT_IDS)
00165 # define VTK_TYPE_SIZED_ID_TYPE INT64
00166 #else
00167 # define VTK_TYPE_SIZED_ID_TYPE INT32
00168 #endif
00169 
00170 #undef VTK_TYPE_TRAITS
00171 
00172 #endif
00173 // VTK-HeaderTest-Exclude: vtkTypeTraits.h