00001 #ifndef VVELIB_STORAGE_TYPES_H
00002 #define VVELIB_STORAGE_TYPES_H
00003
00010 #include <config.h>
00011 #include <QString>
00012 #include <string>
00013
00014 namespace storage
00015 {
00016
00017 #define NB_STORAGE_TYPES 17
00018
00019 enum TYPES
00020 {
00021 T_INVALID = -1,
00022 T_BOOL = 0,
00023 T_WCHART,
00024 T_CHAR,
00025 T_SIGNED_CHAR,
00026 T_UNSIGNED_CHAR,
00027 T_SHORT,
00028 T_UNSIGNED_SHORT,
00029 T_INT,
00030 T_UNSIGNED_INT,
00031 T_LONG,
00032 T_UNSIGNED_LONG,
00033 T_LONG_LONG,
00034 T_UNSIGNED_LONG_LONG,
00035 T_FLOAT,
00036 T_DOUBLE,
00037 T_LONG_DOUBLE,
00038 T_STRING
00039 };
00040
00041 #ifndef _MSC_VER
00042 #define FOR_ALL_TYPES(macro) \
00043 macro(bool) \
00044 macro(wchar_t) \
00045 macro(char) \
00046 macro(signed char) \
00047 macro(unsigned char) \
00048 macro(short) \
00049 macro(unsigned short) \
00050 macro(int) \
00051 macro(unsigned int) \
00052 macro(long) \
00053 macro(unsigned long) \
00054 macro(long long) \
00055 macro(unsigned long long) \
00056 macro(float) \
00057 macro(double) \
00058 macro(long double) \
00059 macro(std::string) \
00060 macro(QString)
00061
00062 #define FOR_ALL_TYPES_NOSTRING(macro) \
00063 macro(bool) \
00064 macro(wchar_t) \
00065 macro(char) \
00066 macro(signed char) \
00067 macro(unsigned char) \
00068 macro(short) \
00069 macro(unsigned short) \
00070 macro(int) \
00071 macro(unsigned int) \
00072 macro(long) \
00073 macro(unsigned long) \
00074 macro(long long) \
00075 macro(unsigned long long) \
00076 macro(float) \
00077 macro(double) \
00078 macro(long double)
00079
00080 #define FOR_ALL_TYPEIDS(macro) \
00081 macro(T_BOOL,bool) \
00082 macro(T_WCHART,wchar_t) \
00083 macro(T_CHAR,char) \
00084 macro(T_SIGNED_CHAR,signed char) \
00085 macro(T_UNSIGNED_CHAR,unsigned char) \
00086 macro(T_SHORT,short) \
00087 macro(T_UNSIGNED_SHORT,unsigned short) \
00088 macro(T_INT,int) \
00089 macro(T_UNSIGNED_INT,unsigned int) \
00090 macro(T_LONG,long) \
00091 macro(T_UNSIGNED_LONG,unsigned long) \
00092 macro(T_LONG_LONG,long long) \
00093 macro(T_UNSIGNED_LONG_LONG,unsigned long long) \
00094 macro(T_FLOAT,float) \
00095 macro(T_DOUBLE,double) \
00096 macro(T_LONG_DOUBLE,long double) \
00097 macro(T_STRING,QString)
00098 #else // _MSC_VER
00099 #define FOR_ALL_TYPES(macro) \
00100 macro(bool) \
00101 macro(char) \
00102 macro(signed char) \
00103 macro(unsigned char) \
00104 macro(short) \
00105 macro(unsigned short) \
00106 macro(int) \
00107 macro(unsigned int) \
00108 macro(long) \
00109 macro(unsigned long) \
00110 macro(long long) \
00111 macro(unsigned long long) \
00112 macro(float) \
00113 macro(double) \
00114 macro(long double) \
00115 macro(std::string) \
00116 macro(QString)
00117
00118 #define FOR_ALL_TYPES_NOSTRING(macro) \
00119 macro(bool) \
00120 macro(char) \
00121 macro(signed char) \
00122 macro(unsigned char) \
00123 macro(short) \
00124 macro(unsigned short) \
00125 macro(int) \
00126 macro(unsigned int) \
00127 macro(long) \
00128 macro(unsigned long) \
00129 macro(long long) \
00130 macro(unsigned long long) \
00131 macro(float) \
00132 macro(double) \
00133 macro(long double)
00134
00135 #define FOR_ALL_TYPEIDS(macro) \
00136 macro(T_BOOL,bool) \
00137 macro(T_CHAR,char) \
00138 macro(T_SIGNED_CHAR,signed char) \
00139 macro(T_UNSIGNED_CHAR,unsigned char) \
00140 macro(T_SHORT,short) \
00141 macro(T_UNSIGNED_SHORT,unsigned short) \
00142 macro(T_INT,int) \
00143 macro(T_UNSIGNED_INT,unsigned int) \
00144 macro(T_LONG,long) \
00145 macro(T_UNSIGNED_LONG,unsigned long) \
00146 macro(T_LONG_LONG,long long) \
00147 macro(T_UNSIGNED_LONG_LONG,unsigned long long) \
00148 macro(T_FLOAT,float) \
00149 macro(T_DOUBLE,double) \
00150 macro(T_LONG_DOUBLE,long double) \
00151 macro(T_STRING,QString)
00152 #endif
00153
00157 template <typename From, typename To>
00158 struct ConvertType
00159 {
00163 bool operator()(const From& from, To& to) const
00164 {
00165 to = (To)from;
00166 return true;
00167 }
00168 };
00169
00170 template <typename T>
00171 struct ConvertType<T,T>
00172 {
00173 bool operator()(const T& from, T& to) const
00174 {
00175 to = from;
00176 return true;
00177 }
00178 };
00179
00180 #define CONVERT_TO_STRING(type) template <> struct ConvertType<type,QString> { bool operator()(const type& from, QString& to) const; };
00181 #define CONVERT_FROM_STRING(type) template <> struct ConvertType<QString,type> { bool operator()(const QString&, type&) const; };
00182 FOR_ALL_TYPES_NOSTRING(CONVERT_TO_STRING)
00183 CONVERT_TO_STRING(std::string)
00184 FOR_ALL_TYPES_NOSTRING(CONVERT_FROM_STRING)
00185 CONVERT_FROM_STRING(std::string)
00186 #undef CONVERT_TO_STRING
00187 #undef CONVERT_FROM_STRING
00188
00189 #define CONVERT_TO_STRING(type) template <> struct ConvertType<type,std::string> { bool operator()(const type& from, std::string& to) const; };
00190 #define CONVERT_FROM_STRING(type) template <> struct ConvertType<std::string,type> { bool operator()(const std::string&, type&) const; };
00191 FOR_ALL_TYPES_NOSTRING(CONVERT_TO_STRING)
00192 FOR_ALL_TYPES_NOSTRING(CONVERT_FROM_STRING)
00193 #undef CONVERT_TO_STRING
00194 #undef CONVERT_FROM_STRING
00195
00201 template <typename From, typename To>
00202 bool convert_type(const From& from, To& to)
00203 {
00204 static const ConvertType<From,To> converter = ConvertType<From,To>();
00205 return converter(from, to);
00206 }
00207
00208 enum NUMBER_CLASS
00209 {
00210 SIGNED_INTEGER,
00211 UNSIGNED_INTEGER,
00212 FLOATING_POINT,
00213 CHAR,
00214 NOT_A_NUMBER
00215 };
00216
00220 template <typename T>
00221 struct TypeTraits
00222 {
00228 static const QString name;
00230 static const TYPES id;
00234 static const NUMBER_CLASS type;
00235 };
00236
00237 template <> struct TypeTraits<bool>
00238 {
00239 static const QString name;
00240 static const TYPES id;
00241 static const NUMBER_CLASS type;
00242 };
00243
00244 #ifndef _MSC_VER
00245 template <> struct TypeTraits<wchar_t>
00246 {
00247 static const QString name;
00248 static const TYPES id;
00249 static const NUMBER_CLASS type;
00250 };
00251 #endif
00252
00253 template <> struct TypeTraits<char>
00254 {
00255 static const QString name;
00256 static const TYPES id;
00257 static const NUMBER_CLASS type;
00258 };
00259
00260 template <> struct TypeTraits<signed char>
00261 {
00262 static const QString name;
00263 static const TYPES id;
00264 static const NUMBER_CLASS type;
00265 };
00266
00267 template <> struct TypeTraits<unsigned char>
00268 {
00269 static const QString name;
00270 static const TYPES id;
00271 static const NUMBER_CLASS type;
00272 };
00273
00274 template <> struct TypeTraits<short>
00275 {
00276 static const QString name;
00277 static const TYPES id;
00278 static const NUMBER_CLASS type;
00279 };
00280
00281 template <> struct TypeTraits<unsigned short>
00282 {
00283 static const QString name;
00284 static const TYPES id;
00285 static const NUMBER_CLASS type;
00286 };
00287
00288 template <> struct TypeTraits<int>
00289 {
00290 static const QString name;
00291 static const TYPES id;
00292 static const NUMBER_CLASS type;
00293 };
00294
00295 template <> struct TypeTraits<unsigned int>
00296 {
00297 static const QString name;
00298 static const TYPES id;
00299 static const NUMBER_CLASS type;
00300 };
00301
00302 template <> struct TypeTraits<long>
00303 {
00304 static const QString name;
00305 static const TYPES id;
00306 static const NUMBER_CLASS type;
00307 };
00308
00309 template <> struct TypeTraits<unsigned long>
00310 {
00311 static const QString name;
00312 static const TYPES id;
00313 static const NUMBER_CLASS type;
00314 };
00315
00316 template <> struct TypeTraits<long long>
00317 {
00318 static const QString name;
00319 static const TYPES id;
00320 static const NUMBER_CLASS type;
00321 };
00322
00323 template <> struct TypeTraits<unsigned long long>
00324 {
00325 static const QString name;
00326 static const TYPES id;
00327 static const NUMBER_CLASS type;
00328 };
00329
00330 template <> struct TypeTraits<float>
00331 {
00332 static const QString name;
00333 static const TYPES id;
00334 static const NUMBER_CLASS type;
00335 };
00336
00337 template <> struct TypeTraits<double>
00338 {
00339 static const QString name;
00340 static const TYPES id;
00341 static const NUMBER_CLASS type;
00342 };
00343
00344 template <> struct TypeTraits<long double>
00345 {
00346 static const QString name;
00347 static const TYPES id;
00348 static const NUMBER_CLASS type;
00349 };
00350
00351 template <> struct TypeTraits<QString>
00352 {
00353 static const QString name;
00354 static const TYPES id;
00355 static const NUMBER_CLASS type;
00356 };
00357
00358 template <> struct TypeTraits<std::string>
00359 {
00360 static const QString name;
00361 static const TYPES id;
00362 static const NUMBER_CLASS type;
00363 };
00364
00365 template <typename T> const QString TypeTraits<T>::name = "invalid";
00366 template <typename T> const TYPES TypeTraits<T>::id = T_INVALID;
00367 template <typename T> const NUMBER_CLASS TypeTraits<T>::type = NOT_A_NUMBER;
00368
00373 bool isStrictConversion(TYPES from, TYPES to);
00374
00378 TYPES typename_to_id(const QString& name);
00379
00383 const QString& typeid_to_name(TYPES id);
00384
00385 template <typename T>
00386 TYPES type_id(const T& = T())
00387 {
00388 return TypeTraits<T>::id;
00389 }
00390
00391 template <typename T>
00392 const QString& type_name(const T& = T())
00393 {
00394 return TypeTraits<T>::name;
00395 }
00396
00402 TYPES find_type(NUMBER_CLASS klass, size_t bytes);
00403
00404 }
00405
00406 #endif // VVELIB_STORAGE_TYPES_H
00407