]> git.tdb.fi Git - libs/datafile.git/blob - source/type.h
Cosmetic changes
[libs/datafile.git] / source / type.h
1 #ifndef MSP_DATAFILE_TYPE_H_
2 #define MSP_DATAFILE_TYPE_H_
3
4 #include <msp/core/inttypes.h>
5 #include <msp/core/meta.h>
6 #include <msp/strings/lexicalcast.h>
7
8 namespace Msp {
9 namespace DataFile {
10
11 struct Symbol
12 {
13         std::string name;
14
15         Symbol() { }
16
17         template<typename T>
18         Symbol(const T &n): name(lexical_cast<std::string>(n)) { }
19
20         template<typename T> operator T() const { return lexical_cast<T>(name); }
21 };
22
23 struct IntType
24 {
25         static const char signature = 'i';
26         typedef Int64 Store;
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 const char valid_signatures[] =
59 {
60         IntType::signature,
61         FloatType::signature,
62         BoolType::signature,
63         StringType::signature,
64         SymbolType::signature,
65         0
66 };
67
68 struct CheckLoadType: Sfinae
69 {
70         template<typename T>
71         static Yes f(typename T::LoadType *);
72         using Sfinae::f;
73 };
74
75 template<typename T>
76 struct HasLoadType: Sfinae::Evaluate<CheckLoadType, T> { };
77
78 template<typename T, bool lt = HasLoadType<T>::value>
79 struct TypeInfo;
80
81 template<>
82 struct TypeInfo<short int, false>: IntType { };
83
84 template<>
85 struct TypeInfo<unsigned short int, false>: IntType { };
86
87 template<>
88 struct TypeInfo<int, false>: IntType { };
89
90 template<>
91 struct TypeInfo<unsigned int, false>: IntType { };
92
93 template<>
94 struct TypeInfo<long int, false>: IntType { };
95
96 template<>
97 struct TypeInfo<unsigned long int, false>: IntType { };
98
99 #if defined(_MSC_VER)
100 template<>
101 struct TypeInfo<__int64, false>: IntType { };
102
103 template<>
104 struct TypeInfo<unsigned __int64, false>: IntType { };
105 #elif defined(__GNUC__)
106 template<>
107 struct TypeInfo<long long int, false>: IntType { };
108
109 template<>
110 struct TypeInfo<unsigned long long int, false>: IntType { };
111 #endif
112
113 template<>
114 struct TypeInfo<float, false>: FloatType { };
115
116 template<>
117 struct TypeInfo<double, false>: FloatType { };
118
119 template<>
120 struct TypeInfo<bool, false>: BoolType { };
121
122 template<>
123 struct TypeInfo<std::string, false>: StringType { };
124
125 template<>
126 struct TypeInfo<char *, false>: StringType { };
127
128 template<>
129 struct TypeInfo<const char *, false>: StringType { };
130
131 template<typename T>
132 struct TypeInfo<const T, false>: TypeInfo<T> { };
133
134 template<typename T>
135 struct TypeInfo<T &, false>: TypeInfo<T> { };
136
137 template<typename T>
138 struct TypeInfo<T, true>: TypeInfo<typename T::LoadType>
139 { typedef typename T::LoadType Load; };
140
141 template<typename T>
142 struct TypeInfo<T, false>: SymbolType
143 { typedef T Load; };
144
145 } // namespace DataFile
146 } // namespace Msp
147
148 #endif