]> git.tdb.fi Git - libs/datafile.git/blob - source/type.h
Rewrite the type system
[libs/datafile.git] / source / type.h
1 /* $Id$
2
3 This file is part of libmspdatafile
4 Copyright © 2010  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #ifndef MSP_DATAFILE_TYPE_H_
9 #define MSP_DATAFILE_TYPE_H_
10
11 #include <msp/strings/lexicalcast.h>
12
13 namespace Msp {
14 namespace DataFile {
15
16 struct Symbol
17 {
18         std::string name;
19
20         Symbol(const std::string &n): name(n) { }
21
22         template<typename T> operator T() const { return lexical_cast<T>(name); }
23 };
24
25 struct IntType
26 {
27         static const char signature = 'i';
28         typedef long long int Store;
29 };
30
31 struct FloatType
32 {
33         static const char signature = 'f';
34         typedef double Store;
35 };
36
37 struct BoolType
38 {
39         static const char signature = 'b';
40         typedef bool Store;
41 };
42
43 struct StringType
44 {
45         static const char signature = 's';
46         typedef std::string Store;
47 };
48
49 struct SymbolType
50 {
51         // For backward compatibility
52         static const char signature = 'e';
53         typedef Symbol Store;
54 };
55
56 template<typename T>
57 struct TypeInfo: SymbolType { };
58
59 template<>
60 struct TypeInfo<short int>: IntType { };
61
62 template<>
63 struct TypeInfo<unsigned short int>: IntType { };
64
65 template<>
66 struct TypeInfo<int>: IntType { };
67
68 template<>
69 struct TypeInfo<unsigned int>: IntType { };
70
71 template<>
72 struct TypeInfo<long int>: IntType { };
73
74 template<>
75 struct TypeInfo<unsigned long int>: IntType { };
76
77 template<>
78 struct TypeInfo<long long int>: IntType { };
79
80 template<>
81 struct TypeInfo<unsigned long long int>: IntType { };
82
83 template<>
84 struct TypeInfo<float>: FloatType { };
85
86 template<>
87 struct TypeInfo<double>: FloatType { };
88
89 template<>
90 struct TypeInfo<bool>: BoolType { };
91
92 template<>
93 struct TypeInfo<std::string>: StringType { };
94
95 template<typename T>
96 struct TypeInfo<const T>: TypeInfo<T> { };
97
98 template<typename T>
99 struct TypeInfo<T &>: TypeInfo<T> { };
100
101 } // namespace DataFile
102 } // namespace Msp
103
104 #endif