Main Page | Modules | Alphabetical List | Class List | File List | Class Members | File Members

datatypes.h

00001 /*
00002  *  This file is part of Healpix_cxx.
00003  *
00004  *  Healpix_cxx 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  *  Healpix_cxx 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 Healpix_cxx; if not, write to the Free Software
00016  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00017  *
00018  *  For more information about HEALPix, see http://healpix.jpl.nasa.gov
00019  */
00020 
00021 /*
00022  *  Healpix_cxx is being developed at the Max-Planck-Institut fuer Astrophysik
00023  *  and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
00024  *  (DLR).
00025  */
00026 
00027 /*
00028  *  This file defines various platform-independent data types.
00029  *  If any of the requested types is not available, compilation aborts
00030  *  with an error (unfortunately a rather obscure one).
00031  *
00032  *  Copyright (C) 2004 Max-Planck-Society
00033  *  Author: Martin Reinecke
00034  */
00035 
00036 #ifndef PLANCK_DATATYPES_H
00037 #define PLANCK_DATATYPES_H
00038 
00039 #include <string>
00040 #include "message_error.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 typedef sizeChooser<4, int, long, short>::TYPE
00073   int32;
00074 typedef sizeChooser<4, unsigned int, unsigned long, unsigned short>::TYPE
00075   uint32;
00076 
00077 typedef sizeChooser<8, long, long long>::TYPE
00078   int64;
00079 typedef sizeChooser<8, unsigned long, unsigned long long>::TYPE
00080   uint64;
00081 
00082 typedef sizeChooser<4, float, double>::TYPE
00083   float32;
00084 typedef sizeChooser<8, double, long double>::TYPE
00085   float64;
00086 
00087 // mapping of types to integer constants
00088 enum { PLANCK_INT32   = 0,
00089        PLANCK_UINT32  = 1,
00090        PLANCK_INT64   = 2,
00091        PLANCK_UINT64  = 3,
00092        PLANCK_FLOAT32 = 4,
00093        PLANCK_FLOAT64 = 5,
00094        PLANCK_BOOL    = 6,
00095        PLANCK_STRING  = 7 };
00096 
00097 template<typename T> struct typehelper {};
00098 
00099 template<> struct typehelper<int32>
00100   { enum { id=PLANCK_INT32 }; };
00101 template<> struct typehelper<uint32>
00102   { enum { id=PLANCK_UINT32 }; };
00103 template<> struct typehelper<int64>
00104   { enum { id=PLANCK_INT64 }; };
00105 template<> struct typehelper<uint64>
00106   { enum { id=PLANCK_UINT64 }; };
00107 template<> struct typehelper<float32>
00108   { enum { id=PLANCK_FLOAT32 }; };
00109 template<> struct typehelper<float64>
00110   { enum { id=PLANCK_FLOAT64 }; };
00111 template<> struct typehelper<bool>
00112   { enum { id=PLANCK_BOOL }; };
00113 template<> struct typehelper<std::string>
00114   { enum { id=PLANCK_STRING }; };
00115 
00116 inline int type2size (int type)
00117   {
00118   switch (type)
00119     {
00120     case PLANCK_INT32  : return 4;
00121     case PLANCK_UINT32 : return 4;
00122     case PLANCK_INT64  : return 8;
00123     case PLANCK_UINT64 : return 8;
00124     case PLANCK_FLOAT32: return 4;
00125     case PLANCK_FLOAT64: return 8;
00126     case PLANCK_BOOL   : return 1;
00127     case PLANCK_STRING : return 1;
00128     default: throw Message_error ("unsupported data type");
00129     }
00130   }
00131 
00132 inline int string2type(const std::string &type)
00133   {
00134   if (type=="FLOAT64") return PLANCK_FLOAT64;
00135   if (type=="FLOAT32") return PLANCK_FLOAT32;
00136   if (type=="INT32")   return PLANCK_INT32;
00137   if (type=="UINT32")  return PLANCK_UINT32;
00138   if (type=="INT64")   return PLANCK_INT64;
00139   if (type=="UINT64")  return PLANCK_UINT64;
00140   if (type=="BOOL")    return PLANCK_BOOL;
00141   if (type=="STRING")  return PLANCK_STRING;
00142   throw Message_error ("unknown data type "+type);
00143   }
00144 
00145 #endif

Generated on Fri Jul 8 09:37:14 2005 for LevelS C++ support library