]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/type.h
Add facility for classes to specify what type they should be loaded as
[libs/datafile.git] / source / type.h
index 5a87a10222a84794341d9a167c94786ae022927f..72d148a0e16631b2440747fd2c20a1556597b728 100644 (file)
@@ -1,10 +1,3 @@
-/* $Id$
-
-This file is part of libmspdatafile
-Copyright © 2010  Mikko Rasa, Mikkosoft Productions
-Distributed under the LGPL
-*/
-
 #ifndef MSP_DATAFILE_TYPE_H_
 #define MSP_DATAFILE_TYPE_H_
 
@@ -17,7 +10,8 @@ struct Symbol
 {
        std::string name;
 
-       Symbol(const std::string &n): name(n) { }
+       template<typename T>
+       Symbol(const T &n): name(lexical_cast(n)) { }
 
        template<typename T> operator T() const { return lexical_cast<T>(name); }
 };
@@ -25,25 +19,33 @@ struct Symbol
 struct IntType
 {
        static const char signature = 'i';
+#ifdef MSVC
+       typedef __int64 Store;
+#else
        typedef long long int Store;
+#endif
+       typedef Store Load;
 };
 
 struct FloatType
 {
        static const char signature = 'f';
        typedef double Store;
+       typedef Store Load;
 };
 
 struct BoolType
 {
        static const char signature = 'b';
        typedef bool Store;
+       typedef Store Load;
 };
 
 struct StringType
 {
        static const char signature = 's';
        typedef std::string Store;
+       typedef Store Load;
 };
 
 struct SymbolType
@@ -54,49 +56,79 @@ struct SymbolType
 };
 
 template<typename T>
-struct TypeInfo: SymbolType { };
+struct HasLoadType
+{
+       struct Yes { char c[2]; };
+       struct No { char c; };
+
+       template<typename U>
+       static Yes f(typename U::LoadType *);
+       template<typename U>
+       static No f(...);
+
+       enum { value = (sizeof(f<T>(0))==sizeof(Yes)) };
+};
+
+template<typename T, bool lt = HasLoadType<T>::value>
+struct TypeInfo;
 
 template<>
-struct TypeInfo<short int>: IntType { };
+struct TypeInfo<short int, false>: IntType { };
 
 template<>
-struct TypeInfo<unsigned short int>: IntType { };
+struct TypeInfo<unsigned short int, false>: IntType { };
 
 template<>
-struct TypeInfo<int>: IntType { };
+struct TypeInfo<int, false>: IntType { };
 
 template<>
-struct TypeInfo<unsigned int>: IntType { };
+struct TypeInfo<unsigned int, false>: IntType { };
 
 template<>
-struct TypeInfo<long int>: IntType { };
+struct TypeInfo<long int, false>: IntType { };
 
 template<>
-struct TypeInfo<unsigned long int>: IntType { };
+struct TypeInfo<unsigned long int, false>: IntType { };
 
+#ifdef MSVC
 template<>
-struct TypeInfo<long long int>: IntType { };
+struct TypeInfo<__int64, false>: IntType { };
 
 template<>
-struct TypeInfo<unsigned long long int>: IntType { };
+struct TypeInfo<unsigned __int64, false>: IntType { };
+#else
+template<>
+struct TypeInfo<long long int, false>: IntType { };
+
+template<>
+struct TypeInfo<unsigned long long int, false>: IntType { };
+#endif
 
 template<>
-struct TypeInfo<float>: FloatType { };
+struct TypeInfo<float, false>: FloatType { };
 
 template<>
-struct TypeInfo<double>: FloatType { };
+struct TypeInfo<double, false>: FloatType { };
 
 template<>
-struct TypeInfo<bool>: BoolType { };
+struct TypeInfo<bool, false>: BoolType { };
 
 template<>
-struct TypeInfo<std::string>: StringType { };
+struct TypeInfo<std::string, false>: StringType { };
+
+template<typename T>
+struct TypeInfo<const T, false>: TypeInfo<T> { };
+
+template<typename T>
+struct TypeInfo<T &, false>: TypeInfo<T> { };
 
 template<typename T>
-struct TypeInfo<const T>: TypeInfo<T> { };
+struct TypeInfo<T, true>: TypeInfo<typename T::LoadType>
+{ typedef typename T::LoadType Load; };
 
 template<typename T>
-struct TypeInfo<T &>: TypeInfo<T> { };
+struct TypeInfo<T, false>: SymbolType
+{ typedef T Load; };
 
 } // namespace DataFile
 } // namespace Msp