]> git.tdb.fi Git - libs/datafile.git/blob - source/type.h
72d148a0e16631b2440747fd2c20a1556597b728
[libs/datafile.git] / source / type.h
1 #ifndef MSP_DATAFILE_TYPE_H_
2 #define MSP_DATAFILE_TYPE_H_
3
4 #include <msp/strings/lexicalcast.h>
5
6 namespace Msp {
7 namespace DataFile {
8
9 struct Symbol
10 {
11         std::string name;
12
13         template<typename T>
14         Symbol(const T &n): name(lexical_cast(n)) { }
15
16         template<typename T> operator T() const { return lexical_cast<T>(name); }
17 };
18
19 struct IntType
20 {
21         static const char signature = 'i';
22 #ifdef MSVC
23         typedef __int64 Store;
24 #else
25         typedef long long int Store;
26 #endif
27         typedef Store Load;
28 };
29
30 struct FloatType
31 {
32         static const char signature = 'f';
33         typedef double Store;
34         typedef Store Load;
35 };
36
37 struct BoolType
38 {
39         static const char signature = 'b';
40         typedef bool Store;
41         typedef Store Load;
42 };
43
44 struct StringType
45 {
46         static const char signature = 's';
47         typedef std::string Store;
48         typedef Store Load;
49 };
50
51 struct SymbolType
52 {
53         // For backward compatibility
54         static const char signature = 'e';
55         typedef Symbol Store;
56 };
57
58 template<typename T>
59 struct HasLoadType
60 {
61         struct Yes { char c[2]; };
62         struct No { char c; };
63
64         template<typename U>
65         static Yes f(typename U::LoadType *);
66         template<typename U>
67         static No f(...);
68
69         enum { value = (sizeof(f<T>(0))==sizeof(Yes)) };
70 };
71
72 template<typename T, bool lt = HasLoadType<T>::value>
73 struct TypeInfo;
74
75 template<>
76 struct TypeInfo<short int, false>: IntType { };
77
78 template<>
79 struct TypeInfo<unsigned short int, false>: IntType { };
80
81 template<>
82 struct TypeInfo<int, false>: IntType { };
83
84 template<>
85 struct TypeInfo<unsigned int, false>: IntType { };
86
87 template<>
88 struct TypeInfo<long int, false>: IntType { };
89
90 template<>
91 struct TypeInfo<unsigned long int, false>: IntType { };
92
93 #ifdef MSVC
94 template<>
95 struct TypeInfo<__int64, false>: IntType { };
96
97 template<>
98 struct TypeInfo<unsigned __int64, false>: IntType { };
99 #else
100 template<>
101 struct TypeInfo<long long int, false>: IntType { };
102
103 template<>
104 struct TypeInfo<unsigned long long int, false>: IntType { };
105 #endif
106
107 template<>
108 struct TypeInfo<float, false>: FloatType { };
109
110 template<>
111 struct TypeInfo<double, false>: FloatType { };
112
113 template<>
114 struct TypeInfo<bool, false>: BoolType { };
115
116 template<>
117 struct TypeInfo<std::string, false>: StringType { };
118
119 template<typename T>
120 struct TypeInfo<const T, false>: TypeInfo<T> { };
121
122 template<typename T>
123 struct TypeInfo<T &, false>: TypeInfo<T> { };
124
125 template<typename T>
126 struct TypeInfo<T, true>: TypeInfo<typename T::LoadType>
127 { typedef typename T::LoadType Load; };
128
129 template<typename T>
130 struct TypeInfo<T, false>: SymbolType
131 { typedef T Load; };
132
133 } // namespace DataFile
134 } // namespace Msp
135
136 #endif