]> git.tdb.fi Git - libs/datafile.git/blob - source/type.h
Change remaining fixed-size integers to standard types
[libs/datafile.git] / source / type.h
1 #ifndef MSP_DATAFILE_TYPE_H_
2 #define MSP_DATAFILE_TYPE_H_
3
4 #include <cstdint>
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 std::int64_t 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 template<>
100 struct TypeInfo<long long int, false>: IntType { };
101
102 template<>
103 struct TypeInfo<unsigned long long int, false>: IntType { };
104
105 template<>
106 struct TypeInfo<float, false>: FloatType { };
107
108 template<>
109 struct TypeInfo<double, false>: FloatType { };
110
111 template<>
112 struct TypeInfo<bool, false>: BoolType { };
113
114 template<>
115 struct TypeInfo<std::string, false>: StringType { };
116
117 template<>
118 struct TypeInfo<char *, false>: StringType { };
119
120 template<>
121 struct TypeInfo<const char *, false>: StringType { };
122
123 template<typename T>
124 struct TypeInfo<const T, false>: TypeInfo<T> { };
125
126 template<typename T>
127 struct TypeInfo<T &, false>: TypeInfo<T> { };
128
129 template<typename T>
130 struct TypeInfo<T, true>: TypeInfo<typename T::LoadType>
131 { typedef typename T::LoadType Load; };
132
133 template<typename T>
134 struct TypeInfo<T, false>: SymbolType
135 { typedef T Load; };
136
137 } // namespace DataFile
138 } // namespace Msp
139
140 #endif