-
Notifications
You must be signed in to change notification settings - Fork 0
/
type.hpp
378 lines (310 loc) · 7.69 KB
/
type.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//@ {"targets":[{"name":"type.hpp","type":"include"}]}
#ifndef TEMPLE_TYPE_HPP
#define TEMPLE_TYPE_HPP
#include "error.hpp"
#include <cassert>
#include <cstdlib>
#include <type_traits>
#include <memory>
namespace Temple
{
enum class Type:int
{
I8=0,I8_ARRAY
,I16,I16_ARRAY
,I32,I32_ARRAY
,I64,I64_ARRAY
,U8,U8_ARRAY
,U16,U16_ARRAY
,U32,U32_ARRAY
,U64,U64_ARRAY
,FLOAT,FLOAT_ARRAY
,DOUBLE,DOUBLE_ARRAY
,STRING,STRING_ARRAY
,COMPOUND,COMPOUND_ARRAY
};
inline constexpr Type arraySet(Type t)
{return static_cast<Type>(static_cast<int>(t) | 1);}
inline constexpr Type arrayUnset(Type t)
{return static_cast<Type>(static_cast<int>(t)&( ~1));}
inline constexpr Type next(Type t)
{return static_cast<Type>(static_cast<int>(t) + 1);}
inline constexpr Type step(Type t,int x)
{return static_cast<Type>(static_cast<int>(t) + x);}
inline constexpr Type previous(Type t)
{return static_cast<Type>(static_cast<int>(t) - 1);}
template<class StorageModel>
class ItemBase;
template<Type t,class StorageModel>
struct TypeGet
{
typedef typename TypeGet<arrayUnset(t),StorageModel>::type BaseType;
static constexpr auto id=t;
typedef typename StorageModel::template ArrayType<BaseType> type;
};
template<class T,class StorageModel>
struct IdGet
{
static constexpr auto id=arraySet(IdGet<typename T::value_type,StorageModel>::id);
typedef T type;
};
template<class StorageModel>
struct TypeGet<Type::I8,StorageModel>
{
static constexpr auto id=Type::I8;
typedef int8_t type;
};
template<class StorageModel>
struct IdGet<int8_t,StorageModel>
{
static constexpr auto id=Type::I8;
typedef int8_t type;
};
template<class StorageModel>
struct TypeGet<Type::I16,StorageModel>
{
static constexpr auto id=Type::I16;
typedef int16_t type;
};
template<class StorageModel>
struct IdGet<int16_t,StorageModel>
{
static constexpr auto id=Type::I16;
typedef int16_t type;
};
template<class StorageModel>
struct TypeGet<Type::I32,StorageModel>
{
static constexpr auto id=Type::I32;
typedef int32_t type;
};
template<class StorageModel>
struct IdGet<int32_t,StorageModel>
{
static constexpr auto id=Type::I32;
typedef int32_t type;
};
template<class StorageModel>
struct TypeGet<Type::I64,StorageModel>
{
static constexpr auto id=Type::I64;
typedef int64_t type;
};
template<class StorageModel>
struct IdGet<int64_t,StorageModel>
{
static constexpr auto id=Type::I64;
typedef int64_t type;
};
template<class StorageModel>
struct TypeGet<Type::U8,StorageModel>
{
static constexpr auto id=Type::U8;
typedef uint8_t type;
};
template<class StorageModel>
struct IdGet<uint8_t,StorageModel>
{
static constexpr auto id=Type::U8;
typedef uint8_t type;
};
template<class StorageModel>
struct TypeGet<Type::U16,StorageModel>
{
static constexpr auto id=Type::U16;
typedef uint16_t type;
};
template<class StorageModel>
struct IdGet<uint16_t,StorageModel>
{
static constexpr auto id=Type::U16;
typedef uint16_t type;
};
template<class StorageModel>
struct TypeGet<Type::U32,StorageModel>
{
static constexpr auto id=Type::U32;
typedef uint32_t type;
};
template<class StorageModel>
struct IdGet<uint32_t,StorageModel>
{
static constexpr auto id=Type::U32;
typedef uint32_t type;
};
template<class StorageModel>
struct TypeGet<Type::U64,StorageModel>
{
static constexpr auto id=Type::U64;
typedef uint64_t type;
};
template<class StorageModel>
struct IdGet<uint64_t,StorageModel>
{
static constexpr auto id=Type::U64;
typedef uint64_t type;
};
template<class StorageModel>
struct TypeGet<Type::FLOAT,StorageModel>
{
static constexpr auto id=Type::FLOAT;
typedef float type;
};
template<class StorageModel>
struct IdGet<float,StorageModel>
{
static constexpr auto id=Type::FLOAT;
typedef float type;
};
template<class StorageModel>
struct TypeGet<Type::DOUBLE,StorageModel>
{
static constexpr auto id=Type::DOUBLE;
typedef double type;
};
template<class StorageModel>
struct IdGet<double,StorageModel>
{
static constexpr auto id=Type::DOUBLE;
typedef double type;
};
template<class StorageModel>
struct TypeGet<Type::STRING,StorageModel>
{
static constexpr auto id=Type::STRING;
typedef typename StorageModel::StringType type;
};
template<class StorageModel>
struct IdGet<typename StorageModel::StringType,StorageModel>
{
static constexpr auto id=Type::STRING;
typedef typename StorageModel::StringType type;
};
template<class StorageModel>
struct TypeGet<Type::COMPOUND,StorageModel>
{
static constexpr auto id=Type::COMPOUND;
typedef typename StorageModel::template MapType<std::unique_ptr< ItemBase<StorageModel> >> type;
};
template<class StorageModel>
struct IdGet<typename StorageModel::template MapType<std::unique_ptr<ItemBase<StorageModel>>>,StorageModel>
{
static constexpr auto id=Type::COMPOUND;
typedef typename StorageModel::template MapType<std::unique_ptr< ItemBase<StorageModel> >> type;
};
template<Type t,int x,Type t_end,class StorageModel,bool cont>
struct TypeProcess
{
template<class Callback>
static void doIt(Type type,Callback& cb)
{
if(t==type)
{cb(TypeGet<t,StorageModel>{});}
else
{
static constexpr auto t_next=step(t,x);
static constexpr bool cont_next=static_cast<int>(t_next) <= static_cast<int>(t_end);
TypeProcess<t_next,x,t_end,StorageModel,cont_next>::doIt(type,cb);
}
}
};
template<Type t,int x,Type t_end,class StorageModel>
struct TypeProcess<t,x,t_end,StorageModel,0>
{
template<class Callback>
static void doIt(Type type,Callback& cb)
{assert(0!=0);}
};
template<class StorageModel,Type start,int x,Type end,class Callback>
inline void for_type(Type type,Callback&& cb)
{TypeProcess<start,x,end,StorageModel,1>::doIt(type,cb);}
template<class StringType,class ExceptionHandler>
inline Type type(const StringType& str,ExceptionHandler& eh)
{
if(str=="s")
{return Type::STRING;}
if(str=="i8")
{return Type::I8;}
if(str=="i16")
{return Type::I16;}
if(str=="i32")
{return Type::I32;}
if(str=="i64")
{return Type::I64;}
if(str=="u8")
{return Type::U8;}
if(str=="u16")
{return Type::U16;}
if(str=="u32")
{return Type::U32;}
if(str=="u64")
{return Type::U64;}
if(str=="f32")
{return Type::FLOAT;}
if(str=="f64")
{return Type::DOUBLE;}
if(str=="comp")
{return Type::COMPOUND;}
raise(Error("The type identifier «",str.c_str(),"» does not correspond to a known type."),eh);
}
inline const char* type(Type type)
{
switch(type)
{
case Type::I8:
return "i8";
case Type::I8_ARRAY:
return "i8[]";
case Type::I16:
return "i16";
case Type::I16_ARRAY:
return "i16[]";
case Type::I32:
return "i32";
case Type::I32_ARRAY:
return "i32[]";
case Type::I64:
return "i64";
case Type::I64_ARRAY:
return "i64[]";
case Type::U8:
return "u8";
case Type::U8_ARRAY:
return "u8[]";
case Type::U16:
return "u16";
case Type::U16_ARRAY:
return "u16[]";
case Type::U32:
return "u32";
case Type::U32_ARRAY:
return "u32[]";
case Type::U64:
return "u64";
case Type::U64_ARRAY:
return "u64[]";
case Type::FLOAT:
return "f32";
case Type::FLOAT_ARRAY:
return "f32[]";
case Type::DOUBLE:
return "f64";
case Type::DOUBLE_ARRAY:
return "f64[]";
case Type::STRING:
return "s";
case Type::STRING_ARRAY:
return "s[]";
case Type::COMPOUND:
return "comp";
case Type::COMPOUND_ARRAY:
return "comp[]";
default:
assert(1!=1 && "Internal error");
return nullptr;
}
assert(1!=1 && "Internal error");
return nullptr;
}
}
#endif