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