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 signed char int8;
00073 typedef unsigned char uint8;
00074
00075 typedef sizeChooser<2, short, int>::TYPE
00076 int16;
00077 typedef sizeChooser<2, unsigned short, unsigned int>::TYPE
00078 uint16;
00079
00080 typedef sizeChooser<4, int, long, short>::TYPE
00081 int32;
00082 typedef sizeChooser<4, unsigned int, unsigned long, unsigned short>::TYPE
00083 uint32;
00084
00085 typedef sizeChooser<8, long, long long>::TYPE
00086 int64;
00087 typedef sizeChooser<8, unsigned long, unsigned long long>::TYPE
00088 uint64;
00089
00090 typedef sizeChooser<4, float, double>::TYPE
00091 float32;
00092 typedef sizeChooser<8, double, long double>::TYPE
00093 float64;
00094
00095
00096 enum { PLANCK_INT8 = 0,
00097 PLANCK_UINT8 = 1,
00098 PLANCK_INT16 = 2,
00099 PLANCK_UINT16 = 3,
00100 PLANCK_INT32 = 4,
00101 PLANCK_UINT32 = 5,
00102 PLANCK_INT64 = 6,
00103 PLANCK_UINT64 = 7,
00104 PLANCK_FLOAT32 = 8,
00105 PLANCK_FLOAT64 = 9,
00106 PLANCK_BOOL = 10,
00107 PLANCK_STRING = 11 };
00108
00109 template<typename T> struct typehelper {};
00110
00111 template<> struct typehelper<int8>
00112 { enum { id=PLANCK_INT8 }; };
00113 template<> struct typehelper<uint8>
00114 { enum { id=PLANCK_UINT8 }; };
00115 template<> struct typehelper<int16>
00116 { enum { id=PLANCK_INT16 }; };
00117 template<> struct typehelper<uint16>
00118 { enum { id=PLANCK_UINT16 }; };
00119 template<> struct typehelper<int32>
00120 { enum { id=PLANCK_INT32 }; };
00121 template<> struct typehelper<uint32>
00122 { enum { id=PLANCK_UINT32 }; };
00123 template<> struct typehelper<int64>
00124 { enum { id=PLANCK_INT64 }; };
00125 template<> struct typehelper<uint64>
00126 { enum { id=PLANCK_UINT64 }; };
00127 template<> struct typehelper<float32>
00128 { enum { id=PLANCK_FLOAT32 }; };
00129 template<> struct typehelper<float64>
00130 { enum { id=PLANCK_FLOAT64 }; };
00131 template<> struct typehelper<bool>
00132 { enum { id=PLANCK_BOOL }; };
00133 template<> struct typehelper<std::string>
00134 { enum { id=PLANCK_STRING }; };
00135
00136 inline int type2size (int type)
00137 {
00138 switch (type)
00139 {
00140 case PLANCK_INT8 : return 1;
00141 case PLANCK_UINT8 : return 1;
00142 case PLANCK_INT16 : return 2;
00143 case PLANCK_UINT16 : return 2;
00144 case PLANCK_INT32 : return 4;
00145 case PLANCK_UINT32 : return 4;
00146 case PLANCK_INT64 : return 8;
00147 case PLANCK_UINT64 : return 8;
00148 case PLANCK_FLOAT32: return 4;
00149 case PLANCK_FLOAT64: return 8;
00150 case PLANCK_BOOL : return 1;
00151 case PLANCK_STRING : return 1;
00152 default: throw Message_error ("unsupported data type");
00153 }
00154 }
00155
00156 inline int string2type(const std::string &type)
00157 {
00158 if (type=="FLOAT64") return PLANCK_FLOAT64;
00159 if (type=="FLOAT32") return PLANCK_FLOAT32;
00160 if (type=="INT8") return PLANCK_INT8;
00161 if (type=="UINT8") return PLANCK_UINT8;
00162 if (type=="INT16") return PLANCK_INT16;
00163 if (type=="UINT16") return PLANCK_UINT16;
00164 if (type=="INT32") return PLANCK_INT32;
00165 if (type=="UINT32") return PLANCK_UINT32;
00166 if (type=="INT64") return PLANCK_INT64;
00167 if (type=="UINT64") return PLANCK_UINT64;
00168 if (type=="BOOL") return PLANCK_BOOL;
00169 if (type=="STRING") return PLANCK_STRING;
00170 throw Message_error ("unknown data type "+type);
00171 }
00172
00173 inline const char *type2string (int type)
00174 {
00175 switch (type)
00176 {
00177 case PLANCK_INT8 : return "INT8";
00178 case PLANCK_UINT8 : return "UINT8";
00179 case PLANCK_INT16 : return "INT16";
00180 case PLANCK_UINT16 : return "UINT16";
00181 case PLANCK_INT32 : return "INT32";
00182 case PLANCK_UINT32 : return "UINT32";
00183 case PLANCK_INT64 : return "INT64";
00184 case PLANCK_UINT64 : return "UINT64";
00185 case PLANCK_FLOAT32: return "FLOAT32";
00186 case PLANCK_FLOAT64: return "FLOAT64";
00187 case PLANCK_BOOL : return "BOOL";
00188 case PLANCK_STRING : return "STRING";
00189 default: throw Message_error ("unsupported data type");
00190 }
00191 }
00192
00193 template<typename T> inline const char *type2typename ()
00194 { return "unknown type"; }
00195 template<> inline const char *type2typename<signed char> ()
00196 { return "signed char"; }
00197 template<> inline const char *type2typename<unsigned char> ()
00198 { return "unsigned char"; }
00199 template<> inline const char *type2typename<short> ()
00200 { return "short"; }
00201 template<> inline const char *type2typename<unsigned short> ()
00202 { return "unsigned short"; }
00203 template<> inline const char *type2typename<int> ()
00204 { return "int"; }
00205 template<> inline const char *type2typename<unsigned int> ()
00206 { return "unsigned int"; }
00207 template<> inline const char *type2typename<long> ()
00208 { return "long"; }
00209 template<> inline const char *type2typename<unsigned long> ()
00210 { return "unsigned long"; }
00211 template<> inline const char *type2typename<long long> ()
00212 { return "long long"; }
00213 template<> inline const char *type2typename<unsigned long long> ()
00214 { return "unsigned long long"; }
00215 template<> inline const char *type2typename<float> ()
00216 { return "float"; }
00217 template<> inline const char *type2typename<double> ()
00218 { return "double"; }
00219 template<> inline const char *type2typename<bool> ()
00220 { return "bool"; }
00221 template<> inline const char *type2typename<std::string> ()
00222 { return "std::string"; }
00223
00224 #endif