00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef PLANCK_DATATYPES_H
00037 #define PLANCK_DATATYPES_H
00038
00039 #include <string>
00040 #include "message_error.h"
00041
00042
00043
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
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