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-2015 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 "xcomplex.h"
00040 #include "error_handling.h"
00041 
00042 // Template magic to select the proper data types. These templates
00043 // should not be used outside this file.
00044 
00045 template <typename T, bool equalSize> struct sizeChooserHelper__
00046   { typedef void TYPE; };
00047 
00048 template <typename T> struct sizeChooserHelper__<T,true>
00049   { typedef T TYPE; };
00050 
00051 template <typename T1, typename T2, typename T3> struct sizeChooserHelper2__
00052   { typedef T1 TYPE; };
00053 
00054 template <typename T2, typename T3> struct sizeChooserHelper2__ <void, T2, T3>
00055   { typedef T2 TYPE; };
00056 
00057 template <typename T3> struct sizeChooserHelper2__ <void, void, T3>
00058   { typedef T3 TYPE; };
00059 
00060 template <> struct sizeChooserHelper2__ <void, void, void>
00061   { };
00062 
00063 template <int sz, typename T1, typename T2=char, typename T3=char>
00064   struct sizeChooser__
00065   {
00066   typedef typename sizeChooserHelper2__
00067     <typename sizeChooserHelper__<T1,sizeof(T1)==sz>::TYPE,
00068      typename sizeChooserHelper__<T2,sizeof(T2)==sz>::TYPE,
00069      typename sizeChooserHelper__<T3,sizeof(T3)==sz>::TYPE >::TYPE TYPE;
00070   };
00071 
00072 #if (__cplusplus>=201103L)
00073 
00074 #include <cstdint>
00075 
00076 typedef int8_t int8;
00077 typedef uint8_t uint8;
00078 
00079 typedef int16_t int16;
00080 typedef uint16_t uint16;
00081 
00082 typedef int32_t int32;
00083 typedef uint32_t uint32;
00084 
00085 typedef int64_t int64;
00086 typedef uint64_t uint64;
00087 
00088 #else
00089 
00090 typedef signed char int8;
00091 typedef unsigned char uint8;
00092 
00093 typedef sizeChooser__<2, short, int>::TYPE
00094   int16;
00095 typedef sizeChooser__<2, unsigned short, unsigned int>::TYPE
00096   uint16;
00097 
00098 typedef sizeChooser__<4, int, long, short>::TYPE
00099   int32;
00100 typedef sizeChooser__<4, unsigned int, unsigned long, unsigned short>::TYPE
00101   uint32;
00102 
00103 typedef sizeChooser__<8, long, long long>::TYPE
00104   int64;
00105 typedef sizeChooser__<8, unsigned long, unsigned long long>::TYPE
00106   uint64;
00107 
00108 #endif
00109 
00110 typedef sizeChooser__<4, float, double>::TYPE
00111   float32;
00112 typedef sizeChooser__<8, double, long double>::TYPE
00113   float64;
00114 
00115 /*! unsigned integer type which should be used for array sizes */
00116 typedef std::size_t tsize;
00117 /*! signed integer type which should be used for relative array indices */
00118 typedef std::ptrdiff_t tdiff;
00119 
00120 /*! mapping of Planck data types to integer constants */
00121 enum PDT {
00122        PLANCK_INT8    =  0,
00123        PLANCK_UINT8   =  1,
00124        PLANCK_INT16   =  2,
00125        PLANCK_UINT16  =  3,
00126        PLANCK_INT32   =  4,
00127        PLANCK_UINT32  =  5,
00128        PLANCK_INT64   =  6,
00129        PLANCK_UINT64  =  7,
00130        PLANCK_FLOAT32 =  8,
00131        PLANCK_FLOAT64 =  9,
00132        PLANCK_BOOL    = 10,
00133        PLANCK_STRING  = 11,
00134        PLANCK_INVALID = -1 };
00135 
00136 /*! Returns the \a PDT constant associated with \a T. */
00137 template<typename T> inline PDT planckType()
00138   { planck_fail(T::UNSUPPORTED_DATA_TYPE); }
00139 template<> inline PDT planckType<int8>       () { return PLANCK_INT8;   }
00140 template<> inline PDT planckType<uint8>      () { return PLANCK_UINT8;  }
00141 template<> inline PDT planckType<int16>      () { return PLANCK_INT16;  }
00142 template<> inline PDT planckType<uint16>     () { return PLANCK_UINT16; }
00143 template<> inline PDT planckType<int32>      () { return PLANCK_INT32;  }
00144 template<> inline PDT planckType<uint32>     () { return PLANCK_UINT32; }
00145 template<> inline PDT planckType<int64>      () { return PLANCK_INT64;  }
00146 template<> inline PDT planckType<uint64>     () { return PLANCK_UINT64; }
00147 template<> inline PDT planckType<float32>    () { return PLANCK_FLOAT32;}
00148 template<> inline PDT planckType<float64>    () { return PLANCK_FLOAT64;}
00149 template<> inline PDT planckType<bool>       () { return PLANCK_BOOL;   }
00150 template<> inline PDT planckType<std::string>() { return PLANCK_STRING; }
00151 
00152 /*! Returns the size (in bytes) of the Planck data type \a type. */
00153 inline int type2size (PDT type)
00154   {
00155   switch (type)
00156     {
00157     case PLANCK_INT8   :
00158     case PLANCK_UINT8  :
00159     case PLANCK_BOOL   :
00160     case PLANCK_STRING : return 1;
00161     case PLANCK_INT16  :
00162     case PLANCK_UINT16 : return 2;
00163     case PLANCK_INT32  :
00164     case PLANCK_UINT32 :
00165     case PLANCK_FLOAT32: return 4;
00166     case PLANCK_INT64  :
00167     case PLANCK_UINT64 :
00168     case PLANCK_FLOAT64: return 8;
00169     default:
00170       planck_fail ("type2size: unsupported data type");
00171     }
00172   }
00173 
00174 /*! Converts the string \a type to a \a PDT. */
00175 inline PDT string2type(const std::string &type)
00176   {
00177   if (type=="FLOAT64") return PLANCK_FLOAT64;
00178   if (type=="FLOAT32") return PLANCK_FLOAT32;
00179   if (type=="INT8")    return PLANCK_INT8;
00180   if (type=="UINT8")   return PLANCK_UINT8;
00181   if (type=="INT16")   return PLANCK_INT16;
00182   if (type=="UINT16")  return PLANCK_UINT16;
00183   if (type=="INT32")   return PLANCK_INT32;
00184   if (type=="UINT32")  return PLANCK_UINT32;
00185   if (type=="INT64")   return PLANCK_INT64;
00186   if (type=="UINT64")  return PLANCK_UINT64;
00187   if (type=="BOOL")    return PLANCK_BOOL;
00188   if (type=="STRING")  return PLANCK_STRING;
00189   planck_fail ("string2type: unknown data type '"+type+"'");
00190   }
00191 
00192 /*! Converts the Planck data type \a type to a C string. */
00193 inline const char *type2string (PDT type)
00194   {
00195   switch (type)
00196     {
00197     case PLANCK_INT8   : return "INT8";
00198     case PLANCK_UINT8  : return "UINT8";
00199     case PLANCK_INT16  : return "INT16";
00200     case PLANCK_UINT16 : return "UINT16";
00201     case PLANCK_INT32  : return "INT32";
00202     case PLANCK_UINT32 : return "UINT32";
00203     case PLANCK_INT64  : return "INT64";
00204     case PLANCK_UINT64 : return "UINT64";
00205     case PLANCK_FLOAT32: return "FLOAT32";
00206     case PLANCK_FLOAT64: return "FLOAT64";
00207     case PLANCK_BOOL   : return "BOOL";
00208     case PLANCK_STRING : return "STRING";
00209     default:
00210       planck_fail ("type2string: unsupported data type");
00211     }
00212   }
00213 
00214 /*! Returns a C string describing the data type \a T. */
00215 template<typename T> inline const char *type2typename ()
00216   { planck_fail(T::UNSUPPORTED_DATA_TYPE); }
00217 template<> inline const char *type2typename<signed char> ()
00218   { return "signed char"; }
00219 template<> inline const char *type2typename<unsigned char> ()
00220   { return "unsigned char"; }
00221 template<> inline const char *type2typename<short> ()
00222   { return "short"; }
00223 template<> inline const char *type2typename<unsigned short> ()
00224   { return "unsigned short"; }
00225 template<> inline const char *type2typename<int> ()
00226   { return "int"; }
00227 template<> inline const char *type2typename<unsigned int> ()
00228   { return "unsigned int"; }
00229 template<> inline const char *type2typename<long> ()
00230   { return "long"; }
00231 template<> inline const char *type2typename<unsigned long> ()
00232   { return "unsigned long"; }
00233 template<> inline const char *type2typename<long long> ()
00234   { return "long long"; }
00235 template<> inline const char *type2typename<unsigned long long> ()
00236   { return "unsigned long long"; }
00237 template<> inline const char *type2typename<float> ()
00238   { return "float"; }
00239 template<> inline const char *type2typename<double> ()
00240   { return "double"; }
00241 template<> inline const char *type2typename<long double> ()
00242   { return "long double"; }
00243 template<> inline const char *type2typename<bool> ()
00244   { return "bool"; }
00245 template<> inline const char *type2typename<std::string> ()
00246   { return "std::string"; }
00247 
00248 /*! mapping of "native" data types to integer constants */
00249 enum NDT {
00250        NAT_CHAR,
00251        NAT_SCHAR,
00252        NAT_UCHAR,
00253        NAT_SHORT,
00254        NAT_USHORT,
00255        NAT_INT,
00256        NAT_UINT,
00257        NAT_LONG,
00258        NAT_ULONG,
00259        NAT_LONGLONG,
00260        NAT_ULONGLONG,
00261        NAT_FLOAT,
00262        NAT_DOUBLE,
00263        NAT_LONGDOUBLE,
00264        NAT_FCMPLX,
00265        NAT_DCMPLX,
00266        NAT_BOOL,
00267        NAT_STRING };
00268 
00269 /*! Returns the \a NDT constant associated with \a T. */
00270 template<typename T> inline NDT nativeType()
00271   { planck_fail(T::UNSUPPORTED_DATA_TYPE); }
00272 template<> inline NDT nativeType<char>              () { return NAT_CHAR;      }
00273 template<> inline NDT nativeType<signed char>       () { return NAT_SCHAR;     }
00274 template<> inline NDT nativeType<unsigned char>     () { return NAT_UCHAR;     }
00275 template<> inline NDT nativeType<short>             () { return NAT_SHORT;     }
00276 template<> inline NDT nativeType<unsigned short>    () { return NAT_USHORT;    }
00277 template<> inline NDT nativeType<int>               () { return NAT_INT;       }
00278 template<> inline NDT nativeType<unsigned int>      () { return NAT_UINT;      }
00279 template<> inline NDT nativeType<long>              () { return NAT_LONG;      }
00280 template<> inline NDT nativeType<unsigned long>     () { return NAT_ULONG;     }
00281 template<> inline NDT nativeType<long long>         () { return NAT_LONGLONG;  }
00282 template<> inline NDT nativeType<unsigned long long>() { return NAT_ULONGLONG; }
00283 template<> inline NDT nativeType<float>             () { return NAT_FLOAT;     }
00284 template<> inline NDT nativeType<double>            () { return NAT_DOUBLE;    }
00285 template<> inline NDT nativeType<long double>       () { return NAT_LONGDOUBLE;}
00286 template<> inline NDT nativeType<fcomplex>          () { return NAT_FCMPLX;    }
00287 template<> inline NDT nativeType<dcomplex>          () { return NAT_DCMPLX;    }
00288 template<> inline NDT nativeType<bool>              () { return NAT_BOOL;      }
00289 template<> inline NDT nativeType<std::string>       () { return NAT_STRING;    }
00290 
00291 /*! Returns the size (in bytes) of the native data type \a type. */
00292 inline int ndt2size (NDT type)
00293   {
00294   switch (type)
00295     {
00296     case NAT_CHAR      :
00297     case NAT_SCHAR     :
00298     case NAT_UCHAR     : return sizeof(char);
00299     case NAT_SHORT     :
00300     case NAT_USHORT    : return sizeof(short);
00301     case NAT_INT       :
00302     case NAT_UINT      : return sizeof(int);
00303     case NAT_LONG      :
00304     case NAT_ULONG     : return sizeof(long);
00305     case NAT_LONGLONG  :
00306     case NAT_ULONGLONG : return sizeof(long long);
00307     case NAT_FLOAT     : return sizeof(float);
00308     case NAT_DOUBLE    : return sizeof(double);
00309     case NAT_LONGDOUBLE: return sizeof(long double);
00310     case NAT_FCMPLX    : return sizeof(fcomplex);
00311     case NAT_DCMPLX    : return sizeof(dcomplex);
00312     case NAT_BOOL      : return sizeof(bool);
00313     default:
00314       planck_fail ("ndt2size: unsupported data type");
00315     }
00316   }
00317 
00318 #endif

Generated on Thu Oct 8 14:48:51 2015 for LevelS C++ support library