]> git.tdb.fi Git - libs/datafile.git/blobdiff - source/type.h
Rewrite the type system
[libs/datafile.git] / source / type.h
diff --git a/source/type.h b/source/type.h
new file mode 100644 (file)
index 0000000..5a87a10
--- /dev/null
@@ -0,0 +1,104 @@
+/* $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_
+
+#include <msp/strings/lexicalcast.h>
+
+namespace Msp {
+namespace DataFile {
+
+struct Symbol
+{
+       std::string name;
+
+       Symbol(const std::string &n): name(n) { }
+
+       template<typename T> operator T() const { return lexical_cast<T>(name); }
+};
+
+struct IntType
+{
+       static const char signature = 'i';
+       typedef long long int Store;
+};
+
+struct FloatType
+{
+       static const char signature = 'f';
+       typedef double Store;
+};
+
+struct BoolType
+{
+       static const char signature = 'b';
+       typedef bool Store;
+};
+
+struct StringType
+{
+       static const char signature = 's';
+       typedef std::string Store;
+};
+
+struct SymbolType
+{
+       // For backward compatibility
+       static const char signature = 'e';
+       typedef Symbol Store;
+};
+
+template<typename T>
+struct TypeInfo: SymbolType { };
+
+template<>
+struct TypeInfo<short int>: IntType { };
+
+template<>
+struct TypeInfo<unsigned short int>: IntType { };
+
+template<>
+struct TypeInfo<int>: IntType { };
+
+template<>
+struct TypeInfo<unsigned int>: IntType { };
+
+template<>
+struct TypeInfo<long int>: IntType { };
+
+template<>
+struct TypeInfo<unsigned long int>: IntType { };
+
+template<>
+struct TypeInfo<long long int>: IntType { };
+
+template<>
+struct TypeInfo<unsigned long long int>: IntType { };
+
+template<>
+struct TypeInfo<float>: FloatType { };
+
+template<>
+struct TypeInfo<double>: FloatType { };
+
+template<>
+struct TypeInfo<bool>: BoolType { };
+
+template<>
+struct TypeInfo<std::string>: StringType { };
+
+template<typename T>
+struct TypeInfo<const T>: TypeInfo<T> { };
+
+template<typename T>
+struct TypeInfo<T &>: TypeInfo<T> { };
+
+} // namespace DataFile
+} // namespace Msp
+
+#endif