-
Notifications
You must be signed in to change notification settings - Fork 98
/
object.hpp
410 lines (353 loc) · 12.6 KB
/
object.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// Copyright (c) 2010-2023 niXman (github dot nixman at pm dot me). All
// rights reserved.
//
// This file is part of YAS(https://github.com/niXman/yas) project.
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
//
//
// Boost Software License - Version 1.0 - August 17th, 2003
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#ifndef __yas__object_hpp
#define __yas__object_hpp
#include <yas/detail/preprocessor/preprocessor.hpp>
#include <yas/detail/type_traits/type_traits.hpp>
#include <yas/detail/tools/fnv1a.hpp>
#include <yas/detail/tools/ctsort.hpp>
#include <yas/detail/tools/ctmap.hpp>
#include <tuple>
#include <cstring>
namespace yas {
/***************************************************************************/
template<typename T>
struct value {
template<typename VT>
struct real_value_type {
using type = typename std::conditional<
std::is_array<typename std::remove_reference<VT>::type>::value
,typename std::remove_cv<VT>::type
,typename std::conditional<
std::is_lvalue_reference<VT>::value
,VT
,typename std::decay<VT>::type
>::type
>::type;
};
using key_type = const char*;
using value_type = typename real_value_type<T>::type;
value(const value &) = delete;
value& operator=(const value &) = delete;
constexpr value(const char *k, std::size_t klen, T &&v) noexcept
:key(k)
,klen(klen)
,val(std::forward<T>(v))
{}
constexpr value(value &&r) noexcept
:key(r.key)
,klen(r.klen)
,val(std::forward<value_type>(r.val))
{}
key_type key;
const std::size_t klen;
value_type val;
};
template<std::size_t N, typename T>
constexpr value<T>
make_val(const char (&key)[N], T &&val) {
return {key, N-1, std::forward<T>(val)};
}
template<typename ConstCharPtr, typename T>
constexpr typename std::enable_if<
std::is_same<ConstCharPtr, const char*>::value
,value<T>
>::type
make_val(ConstCharPtr key, T &&val) {
return {key, std::strlen(key), std::forward<T>(val)};
}
/***************************************************************************/
template<typename KVI, typename... Pairs>
struct object {
using tuple = std::tuple<Pairs...>;
object(const object &) = delete;
object& operator=(const object &) = delete;
constexpr object(const char *k, std::size_t klen, Pairs&&... pairs) noexcept
:key(k)
,klen(klen)
,pairs(std::forward<Pairs>(pairs)...)
{}
constexpr object(object &&r) noexcept
:key(r.key)
,klen(r.klen)
,pairs(std::move(r.pairs))
{}
const char *key;
const std::size_t klen;
tuple pairs;
static constexpr detail::ctmap<KVI> map{};
};
template<typename KVI, typename... Pairs>
constexpr detail::ctmap<KVI> object<KVI, Pairs...>::map;
template<typename KVI, std::size_t N, typename... Pairs>
constexpr object<KVI, Pairs...>
make_object(const char (&key)[N], Pairs &&... pairs) {
return {key, N-1, std::forward<Pairs>(pairs)...};
}
template<typename KVI, typename... Pairs>
constexpr object<KVI, Pairs...>
make_object(std::nullptr_t, Pairs &&... pairs) {
return {nullptr, 0, std::forward<Pairs>(pairs)...};
}
/**************************************************************************/
#if defined(YAS_SERIALIZE_BOOST_TYPES)
# include <boost/version.hpp>
# if BOOST_VERSION >= 106000
# define __YAS_CAN_USE_BOOST_VMD
# endif // BOOST_VERSION >= 106000
#endif // defined(YAS_SERIALIZE_BOOST_TYPES)
#ifdef __YAS_CAN_USE_BOOST_VMD
# include <boost/vmd/is_empty.hpp>
# define __YAS_PP_TUPLE_IS_EMPTY(...) BOOST_VMD_IS_EMPTY(__VA_ARGS__)
#else
# define __YAS_PP_ARG50( \
_0 , _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 \
, ...) _49
# define __YAS_PP_HAS_COMMA(...) \
__YAS_PP_ARG50( \
__VA_ARGS__, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, \
1, 1, 1, 1, 1, 1, 1, 1, 0 \
)
# define __YAS_PP_TRIGGER_PARENTHESIS_(...) ,
# define __YAS_PP_PASTE5(_0, _1, _2, _3, _4) _0 ## _1 ## _2 ## _3 ## _4
# define __YAS_PP_IS_EMPTY_CASE_0001 ,
# define __YAS_PP_IS_EMPTY(_0, _1, _2, _3) __YAS_PP_HAS_COMMA(__YAS_PP_PASTE5(__YAS_PP_IS_EMPTY_CASE_, _0, _1, _2, _3))
# define __YAS_PP_TUPLE_IS_EMPTY_IMPL(...) \
__YAS_PP_IS_EMPTY( \
__YAS_PP_HAS_COMMA(__VA_ARGS__), \
__YAS_PP_HAS_COMMA(__YAS_PP_TRIGGER_PARENTHESIS_ __VA_ARGS__), \
__YAS_PP_HAS_COMMA(__VA_ARGS__ (/*empty*/)), \
__YAS_PP_HAS_COMMA(__YAS_PP_TRIGGER_PARENTHESIS_ __VA_ARGS__ (/*empty*/)) \
)
# define __YAS_PP_TUPLE_IS_EMPTY(...) __YAS_PP_TUPLE_IS_EMPTY_IMPL(__VA_ARGS__)
#endif // __YAS_CAN_USE_BOOST_VMD
/**************************************************************************/
#define __YAS_OBJECT_GEN_PAIRS_IMPL(forstruct, sname, idx, elem) \
YAS_PP_COMMA_IF(idx) \
::yas::make_val( \
YAS_PP_STRINGIZE(elem) \
,YAS_PP_IF(forstruct, sname., /*empty*/) elem \
)
#define __YAS_OBJECT_GEN_PAIRS(unused0, data, idx, elem) \
__YAS_OBJECT_GEN_PAIRS_IMPL( \
YAS_PP_TUPLE_ELEM(0, data) \
,YAS_PP_TUPLE_ELEM(1, data) \
,idx \
,elem \
)
#define __YAS_OBJECT_IMPL(forstruct, sname, seq) \
YAS_PP_SEQ_FOR_EACH_I( \
__YAS_OBJECT_GEN_PAIRS \
,(forstruct, sname) \
,seq \
)
#define __YAS_OBJECT_NONEMPTY_GEN_KVI_CB(unised0, unised1, idx, elem) \
YAS_PP_COMMA_IF(idx) \
std::pair< \
std::integral_constant< \
std::uint32_t \
,::yas::detail::fnv1a(YAS_PP_STRINGIZE(elem)) \
> \
,std::integral_constant<std::uint8_t, idx> \
>
#define __YAS_OBJECT_NONEMPTY_GEN_KVI(seq) \
typename ::yas::detail::mergesort< \
::yas::detail::predic_less \
,std::tuple< \
YAS_PP_SEQ_FOR_EACH_I( \
__YAS_OBJECT_NONEMPTY_GEN_KVI_CB \
,~ \
,seq \
) \
> \
>::type
#define __YAS_OBJECT_EMPTY(forstruct, oname, sname, ...) \
::yas::make_object<std::tuple<>>(oname)
#define __YAS_OBJECT_NONEMPTY(forstruct, oname, sname, seq) \
::yas::make_object< \
__YAS_OBJECT_NONEMPTY_GEN_KVI(seq) \
>( \
oname \
,__YAS_OBJECT_IMPL(forstruct, sname, seq) \
)
#define __YAS_OBJECT_AUX(forstruct, oname, sname, ...) \
YAS_PP_IF( \
__YAS_PP_TUPLE_IS_EMPTY(__VA_ARGS__) \
,__YAS_OBJECT_EMPTY \
,__YAS_OBJECT_NONEMPTY \
)(forstruct, oname, sname, YAS_PP_TUPLE_TO_SEQ((__VA_ARGS__)))
#define YAS_OBJECT(oname, ...) \
__YAS_OBJECT_AUX( \
0 \
,oname \
,~ \
,__VA_ARGS__ \
)
#define YAS_OBJECT_STRUCT(oname, sname, ...) \
__YAS_OBJECT_AUX( \
1 \
,oname \
,sname \
,__VA_ARGS__ \
)
/***************************************************************************/
#define __YAS_OBJECT_NVP_GEN_PAIRS_IMPL(forstruct, sname, idx, elem) \
YAS_PP_COMMA_IF(idx) \
::yas::make_val( \
YAS_PP_TUPLE_ELEM(0, elem) \
,YAS_PP_IF(forstruct, sname., /*empty*/) YAS_PP_TUPLE_ELEM(1, elem) \
)
#define __YAS_OBJECT_NVP_GEN_PAIRS(unused0, data, idx, elem) \
__YAS_OBJECT_NVP_GEN_PAIRS_IMPL( \
YAS_PP_TUPLE_ELEM(0, data) \
,YAS_PP_TUPLE_ELEM(1, data) \
,idx \
,elem \
)
#define __YAS_OBJECT_NVP_IMPL(forstruct, sname, seq) \
YAS_PP_SEQ_FOR_EACH_I( \
__YAS_OBJECT_NVP_GEN_PAIRS \
,(forstruct, sname) \
,seq \
)
#define __YAS_OBJECT_NVP_NONEMPTY_GEN_KVI_CB(unised0, unised1, idx, elem) \
YAS_PP_COMMA_IF(idx) \
std::pair< \
std::integral_constant< \
std::uint32_t \
,::yas::detail::fnv1a(YAS_PP_TUPLE_ELEM(0, elem)) \
> \
,std::integral_constant<std::uint8_t, idx> \
>
#define __YAS_OBJECT_NVP_NONEMPTY_GEN_KVI(seq) \
typename ::yas::detail::mergesort< \
::yas::detail::predic_less \
,std::tuple< \
YAS_PP_SEQ_FOR_EACH_I( \
__YAS_OBJECT_NVP_NONEMPTY_GEN_KVI_CB \
,~ \
,seq \
) \
> \
>::type
#define __YAS_OBJECT_NVP_EMPTY(forstruct, oname, sname, ...) \
::yas::make_object<std::tuple<>>(oname)
#define __YAS_OBJECT_NVP_NONEMPTY(forstruct, oname, sname, seq) \
::yas::make_object< \
__YAS_OBJECT_NVP_NONEMPTY_GEN_KVI(seq) \
>( \
oname \
,__YAS_OBJECT_NVP_IMPL(forstruct, sname, seq) \
)
#define __YAS_OBJECT_NVP_AUX(forstruct, oname, sname, ...) \
YAS_PP_IF( \
__YAS_PP_TUPLE_IS_EMPTY(__VA_ARGS__) \
,__YAS_OBJECT_NVP_EMPTY \
,__YAS_OBJECT_NVP_NONEMPTY \
)(forstruct, oname, sname, YAS_PP_TUPLE_TO_SEQ((__VA_ARGS__)))
#define YAS_OBJECT_NVP(oname, ...) \
__YAS_OBJECT_NVP_AUX( \
0 \
,oname \
,~ \
,__VA_ARGS__ \
)
#define YAS_OBJECT_STRUCT_NVP(oname, sname, ...) \
__YAS_OBJECT_NVP_AUX( \
1 \
,oname \
,sname \
,__VA_ARGS__ \
)
/***************************************************************************/
#define __YAS_DEFINE_STRUCT_SERIALIZE_AUX(nvp, oname, ...) \
template<typename Archive> \
void serialize(Archive &ar) const { \
auto o = YAS_PP_IF(nvp, YAS_OBJECT_NVP, YAS_OBJECT) \
( \
oname \
,__VA_ARGS__ \
); \
ar & o; \
} \
template<typename Archive> \
void serialize(Archive &ar) { \
auto o = YAS_PP_IF(nvp, YAS_OBJECT_NVP, YAS_OBJECT) \
( \
oname \
,__VA_ARGS__ \
); \
ar & o; \
}
#define YAS_DEFINE_STRUCT_SERIALIZE(oname, ...) \
__YAS_DEFINE_STRUCT_SERIALIZE_AUX(0, oname, __VA_ARGS__)
#define YAS_DEFINE_STRUCT_SERIALIZE_NVP(oname, ...) \
__YAS_DEFINE_STRUCT_SERIALIZE_AUX(1, oname, __VA_ARGS__)
/***************************************************************************/
#define __YAS_DEFINE_INTRUSIVE_SERIALIZE_AUX(nvp, oname, tname, ...) \
template<typename Archive> \
void serialize(Archive &ar, const tname &t) { \
auto o = YAS_PP_IF(nvp, YAS_OBJECT_STRUCT_NVP, YAS_OBJECT_STRUCT) \
( \
oname \
,t \
,__VA_ARGS__ \
); \
ar & o; \
} \
template<typename Archive> \
void serialize(Archive &ar, tname &t) { \
auto o = YAS_PP_IF(nvp, YAS_OBJECT_STRUCT_NVP, YAS_OBJECT_STRUCT) \
( \
oname \
,t \
,__VA_ARGS__ \
); \
ar & o; \
}
#define YAS_DEFINE_INTRUSIVE_SERIALIZE(oname, tname, ...) \
__YAS_DEFINE_INTRUSIVE_SERIALIZE_AUX(0, oname, tname, __VA_ARGS__)
#define YAS_DEFINE_INTRUSIVE_SERIALIZE_NVP(oname, tname, ...) \
__YAS_DEFINE_INTRUSIVE_SERIALIZE_AUX(1, oname, tname, __VA_ARGS__)
/***************************************************************************/
} // ns yas
#endif // __yas__object_hpp