datatypes.h

Go to the documentation of this file.
00001 /*
00002  *  This file is part of libcxxsupport.
00003  *
00004  *  libcxxsupport is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  libcxxsupport is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with libcxxsupport; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  */
00018 
00019 /*
00020  *  libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
00021  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00022  *  (DLR).
00023  */
00024 
00025 /*! \file datatypes.h
00026  *  This file defines various platform-independent data types.
00027  *  If any of the requested types is not available, compilation aborts
00028  *  with an error (unfortunately a rather obscure one).
00029  *
00030  *  Copyright (C) 2004-2011 Max-Planck-Society
00031  *  \author Martin Reinecke
00032  */
00033 
00034 #ifndef PLANCK_DATATYPES_H
00035 #define PLANCK_DATATYPES_H
00036 
00037 #include <string>
00038 #include <cstddef>
00039 #include "error_handling.h"
00040 
00041 // Template magic to select the proper data types. These templates
00042 // should not be used outside this file.
00043 
00044 template <typename T, bool equalSize> struct sizeChooserHelper__
00045   { typedef void TYPE; };
00046 
00047 template <typename T> struct sizeChooserHelper__<T,true>
00048   { typedef T TYPE; };
00049 
00050 template <typename T1, typename T2, typename T3> struct sizeChooserHelper2__
00051   { typedef T1 TYPE; };
00052 
00053 template <typename T2, typename T3> struct sizeChooserHelper2__ <void, T2, T3>
00054   { typedef T2 TYPE; };
00055 
00056 template <typename T3> struct sizeChooserHelper2__ <void, void, T3>
00057   { typedef T3 TYPE; };
00058 
00059 template <> struct sizeChooserHelper2__ <void, void, void>
00060   { };
00061 
00062 template <int sz, typename T1, typename T2=char, typename T3=char>
00063   struct sizeChooser__
00064   {
00065   typedef typename sizeChooserHelper2__
00066     <typename sizeChooserHelper__<T1,sizeof(T1)==sz>::TYPE,
00067      typename sizeChooserHelper__<T2,sizeof(T2)==sz>::TYPE,
00068      typename sizeChooserHelper__<T3,sizeof(T3)==sz>::TYPE >::TYPE TYPE;
00069   };
00070 
00071 typedef signed char int8;
00072 typedef unsigned char uint8;
00073 
00074 typedef sizeChooser__<2, short, int>::TYPE
00075   int16;
00076 typedef sizeChooser__<2, unsigned short, unsigned int>::TYPE
00077   uint16;
00078 
00079 typedef sizeChooser__<4, int, long, short>::TYPE
00080   int32;
00081 typedef sizeChooser__<4, unsigned int, unsigned long, unsigned short>::TYPE
00082   uint32;
00083 
00084 typedef sizeChooser__<8, long, long long>::TYPE
00085   int64;
00086 typedef sizeChooser__<8, unsigned long, unsigned long long>::TYPE
00087   uint64;
00088 
00089 typedef sizeChooser__<4, float, double>::TYPE
00090   float32;
00091 typedef sizeChooser__<8, double, long double>::TYPE
00092   float64;
00093 
00094 /*! unsigned integer type which should be used for array sizes */
00095 typedef std::size_t tsize;
00096 /*! signed integer type which should be used for relative array indices */
00097 typedef std::ptrdiff_t tdiff;
00098 
00099 /*! mapping of Planck data types to integer constants */
00100 enum PDT {
00101        PLANCK_INT8    =  0,
00102        PLANCK_UINT8   =  1,
00103        PLANCK_INT16   =  2,
00104        PLANCK_UINT16  =  3,
00105        PLANCK_INT32   =  4,
00106        PLANCK_UINT32  =  5,
00107        PLANCK_INT64   =  6,
00108        PLANCK_UINT64  =  7,
00109        PLANCK_FLOAT32 =  8,
00110        PLANCK_FLOAT64 =  9,
00111        PLANCK_BOOL    = 10,
00112        PLANCK_STRING  = 11,
00113        PLANCK_INVALID = -1 };
00114 
00115 /*! Returns the \a PDT constant associated with \a T. */
00116 template<typename T> inline PDT planckType()
00117   { planck_fail(T::UNSUPPORTED_DATA_TYPE); }
00118 template<> inline PDT planckType<int8>       () { return PLANCK_INT8;   }
00119 template<> inline PDT planckType<uint8>      () { return PLANCK_UINT8;  }
00120 template<> inline PDT planckType<int16>      () { return PLANCK_INT16;  }
00121 template<> inline PDT planckType<uint16>     () { return PLANCK_UINT16; }
00122 template<> inline PDT planckType<int32>      () { return PLANCK_INT32;  }
00123 template<> inline PDT planckType<uint32>     () { return PLANCK_UINT32; }
00124 template<> inline PDT planckType<int64>      () { return PLANCK_INT64;  }
00125 template<> inline PDT planckType<uint64>     () { return PLANCK_UINT64; }
00126 template<> inline PDT planckType<float32>    () { return PLANCK_FLOAT32;}
00127 template<> inline PDT planckType<float64>    () { return PLANCK_FLOAT64;}
00128 template<> inline PDT planckType<bool>       () { return PLANCK_BOOL;   }
00129 template<> inline PDT planckType<std::string>() { return PLANCK_STRING; }
00130 
00131 /*! Returns the size (in bytes) of the Planck data type \a type. */
00132 inline int type2size (PDT type)
00133   {
00134   switch (type)
00135     {
00136     case PLANCK_INT8   :
00137     case PLANCK_UINT8  :
00138     case PLANCK_BOOL   :
00139     case PLANCK_STRING : return 1;
00140     case PLANCK_INT16  :
00141     case PLANCK_UINT16 : return 2;
00142     case PLANCK_INT32  :
00143     case PLANCK_UINT32 :
00144     case PLANCK_FLOAT32: return 4;
00145     case PLANCK_INT64  :
00146     case PLANCK_UINT64 :
00147     case PLANCK_FLOAT64: return 8;
00148     default:
00149       planck_fail ("type2size: unsupported data type");
00150     }
00151   }
00152 
00153 /*! Converts the string \a type to a \a PDT. */
00154 inline PDT string2type(const std::string &type)
00155   {
00156   if (type=="FLOAT64") return PLANCK_FLOAT64;
00157   if (type=="FLOAT32") return PLANCK_FLOAT32;
00158   if (type=="INT8")    return PLANCK_INT8;
00159   if (type=="UINT8")   return PLANCK_UINT8;
00160   if (type=="INT16")   return PLANCK_INT16;
00161   if (type=="UINT16")  return PLANCK_UINT16;
00162   if (type=="INT32")   return PLANCK_INT32;
00163   if (type=="UINT32")  return PLANCK_UINT32;
00164   if (type=="INT64")   return PLANCK_INT64;
00165   if (type=="UINT64")  return PLANCK_UINT64;
00166   if (type=="BOOL")    return PLANCK_BOOL;
00167   if (type=="STRING")  return PLANCK_STRING;
00168   planck_fail ("string2type: unknown data type '"+type+"'");
00169   }
00170 
00171 /*! Converts the Planck data type \a type to a C string. */
00172 inline const char *type2string (PDT type)
00173   {
00174   switch (type)
00175     {
00176     case PLANCK_INT8   : return "INT8";
00177     case PLANCK_UINT8  : return "UINT8";
00178     case PLANCK_INT16  : return "INT16";
00179     case PLANCK_UINT16 : return "UINT16";
00180     case PLANCK_INT32  : return "INT32";
00181     case PLANCK_UINT32 : return "UINT32";
00182     case PLANCK_INT64  : return "INT64";
00183     case PLANCK_UINT64 : return "UINT64";
00184     case PLANCK_FLOAT32: return "FLOAT32";
00185     case PLANCK_FLOAT64: return "FLOAT64";
00186     case PLANCK_BOOL   : return "BOOL";
00187     case PLANCK_STRING : return "STRING";
00188     default:
00189       planck_fail ("type2string: unsupported data type");
00190     }
00191   }
00192 
00193 /*! Returns a C string describing the data type \a T. */
00194 template<typename T> inline const char *type2typename ()
00195   { planck_fail(T::UNSUPPORTED_DATA_TYPE); }
00196 template<> inline const char *type2typename<signed char> ()
00197   { return "signed char"; }
00198 template<> inline const char *type2typename<unsigned char> ()
00199   { return "unsigned char"; }
00200 template<> inline const char *type2typename<short> ()
00201   { return "short"; }
00202 template<> inline const char *type2typename<unsigned short> ()
00203   { return "unsigned short"; }
00204 template<> inline const char *type2typename<int> ()
00205   { return "int"; }
00206 template<> inline const char *type2typename<unsigned int> ()
00207   { return "unsigned int"; }
00208 template<> inline const char *type2typename<long> ()
00209   { return "long"; }
00210 template<> inline const char *type2typename<unsigned long> ()
00211   { return "unsigned long"; }
00212 template<> inline const char *type2typename<long long> ()
00213   { return "long long"; }
00214 template<> inline const char *type2typename<unsigned long long> ()
00215   { return "unsigned long long"; }
00216 template<> inline const char *type2typename<float> ()
00217   { return "float"; }
00218 template<> inline const char *type2typename<double> ()
00219   { return "double"; }
00220 template<> inline const char *type2typename<long double> ()
00221   { return "long double"; }
00222 template<> inline const char *type2typename<bool> ()
00223   { return "bool"; }
00224 template<> inline const char *type2typename<std::string> ()
00225   { return "std::string"; }
00226 
00227 /*! mapping of "native" data types to integer constants */
00228 enum NDT {
00229        NAT_CHAR,
00230        NAT_SCHAR,
00231        NAT_UCHAR,
00232        NAT_SHORT,
00233        NAT_USHORT,
00234        NAT_INT,
00235        NAT_UINT,
00236        NAT_LONG,
00237        NAT_ULONG,
00238        NAT_LONGLONG,
00239        NAT_ULONGLONG,
00240        NAT_FLOAT,
00241        NAT_DOUBLE,
00242        NAT_LONGDOUBLE,
00243        NAT_BOOL,
00244        NAT_STRING };
00245 
00246 /*! Returns the \a NDT constant associated with \a T. */
00247 template<typename T> inline NDT nativeType()
00248   { planck_fail(T::UNSUPPORTED_DATA_TYPE); }
00249 template<> inline NDT nativeType<char>              () { return NAT_CHAR;      }
00250 template<> inline NDT nativeType<signed char>       () { return NAT_SCHAR;     }
00251 template<> inline NDT nativeType<unsigned char>     () { return NAT_UCHAR;     }
00252 template<> inline NDT nativeType<short>             () { return NAT_SHORT;     }
00253 template<> inline NDT nativeType<unsigned short>    () { return NAT_USHORT;    }
00254 template<> inline NDT nativeType<int>               () { return NAT_INT;       }
00255 template<> inline NDT nativeType<unsigned int>      () { return NAT_UINT;      }
00256 template<> inline NDT nativeType<long>              () { return NAT_LONG;      }
00257 template<> inline NDT nativeType<unsigned long>     () { return NAT_ULONG;     }
00258 template<> inline NDT nativeType<long long>         () { return NAT_LONGLONG;  }
00259 template<> inline NDT nativeType<unsigned long long>() { return NAT_ULONGLONG; }
00260 template<> inline NDT nativeType<float>             () { return NAT_FLOAT;     }
00261 template<> inline NDT nativeType<double>            () { return NAT_DOUBLE;    }
00262 template<> inline NDT nativeType<long double>       () { return NAT_LONGDOUBLE;}
00263 template<> inline NDT nativeType<bool>              () { return NAT_BOOL;      }
00264 template<> inline NDT nativeType<std::string>       () { return NAT_STRING;    }
00265 
00266 /*! Returns the size (in bytes) of the native data type \a type. */
00267 inline int ndt2size (NDT type)
00268   {
00269   switch (type)
00270     {
00271     case NAT_CHAR      :
00272     case NAT_SCHAR     :
00273     case NAT_UCHAR     : return sizeof(char);
00274     case NAT_SHORT     :
00275     case NAT_USHORT    : return sizeof(short);
00276     case NAT_INT       :
00277     case NAT_UINT      : return sizeof(int);
00278     case NAT_LONG      :
00279     case NAT_ULONG     : return sizeof(long);
00280     case NAT_LONGLONG  :
00281     case NAT_ULONGLONG : return sizeof(long long);
00282     case NAT_FLOAT     : return sizeof(float);
00283     case NAT_DOUBLE    : return sizeof(double);
00284     case NAT_LONGDOUBLE: return sizeof(long double);
00285     case NAT_BOOL      : return sizeof(bool);
00286     default:
00287       planck_fail ("ndt2size: unsupported data type");
00288     }
00289   }
00290 
00291 #endif

Generated on Wed Apr 24 11:31:17 2013 for LevelS C++ support library