forked from krabicezpapundeklu/smart_enum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsmart_enum.hpp
493 lines (415 loc) · 12.6 KB
/
smart_enum.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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
// Copyright Jarda Flieger 2016.
// 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)
#ifndef SMART_ENUM_HEADER_INCLUDED
#define SMART_ENUM_HEADER_INCLUDED
#include <cstring>
#include <stdexcept>
#include <tuple>
#include <boost/iterator/iterator_facade.hpp>
#include <boost/preprocessor/arithmetic/dec.hpp>
#include <boost/preprocessor/logical/not.hpp>
#include <boost/preprocessor/punctuation/remove_parens.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/tuple/push_back.hpp>
#define SMART_ENUM(...) \
SMART_ENUM_IMPL(, __VA_ARGS__)
#define SMART_ENUM_CLASS(...) \
SMART_ENUM_IMPL(class, __VA_ARGS__)
#ifdef _MSC_VER
#define SMART_ENUM_IMPL(CLASS, ...) \
BOOST_PP_CAT \
( \
BOOST_PP_OVERLOAD(SMART_ENUM_IMPL_, __VA_ARGS__)(CLASS, __VA_ARGS__), \
BOOST_PP_EMPTY() \
)
#else
#define SMART_ENUM_IMPL(CLASS, ...) \
BOOST_PP_OVERLOAD(SMART_ENUM_IMPL_, __VA_ARGS__)(CLASS, __VA_ARGS__)
#endif
#define SMART_ENUM_IMPL_2(CLASS, NAME, MEMBERS) \
SMART_ENUM_IMPL_DEFINITION(CLASS, _, NAME, MEMBERS)
#define SMART_ENUM_IMPL_3(CLASS, NAMESPACES, NAME, MEMBERS) \
SMART_ENUM_IMPL_DEFINITION(CLASS, SMART_ENUM_IMPL_ARG_TO_TUPLE(NAMESPACES), NAME, MEMBERS)
#define SMART_ENUM_IMPL_DEFINITION(CLASS, NAMESPACES, NAME, MEMBERS) \
SMART_ENUM_IMPL_DEFINITION_1 \
( \
CLASS, NAMESPACES, SMART_ENUM_IMPL_ARG_TO_TUPLE(NAME), (SMART_ENUM_IMPL_ARGS_TO_TUPLES(MEMBERS)) \
)
#define SMART_ENUM_IMPL_DEFINITION_1(CLASS, NAMESPACES, NAME, MEMBERS) \
SMART_ENUM_IMPL_REPEAT(NAMESPACE_START, NAMESPACES) \
enum CLASS SMART_ENUM_IMPL_NAME_SIZE(NAME) \
{ \
SMART_ENUM_IMPL_MEMBER_DEFINITIONS(MEMBERS) \
}; \
SMART_ENUM_IMPL_REPEAT(NAMESPACE_END, NAMESPACES) \
\
SMART_ENUM_IMPL_TRAITS(NAMESPACES, BOOST_PP_TUPLE_ELEM(0, NAME), MEMBERS)
#define SMART_ENUM_IMPL_NAME_SIZE(NAME_SIZE) \
BOOST_PP_TUPLE_ELEM(0, NAME_SIZE) \
BOOST_PP_IF \
( \
BOOST_PP_DEC(BOOST_PP_TUPLE_SIZE(NAME_SIZE)), \
SMART_ENUM_IMPL_NAME_SIZE_1, BOOST_PP_TUPLE_EAT(1) \
) \
NAME_SIZE
#define SMART_ENUM_IMPL_NAME_SIZE_1(NAME, SIZE) \
: SIZE
// traits
#define SMART_ENUM_IMPL_TRAITS(NAMESPACES, NAME, MEMBERS) \
SMART_ENUM_IMPL_TRAITS_1 \
( \
SMART_ENUM_IMPL_FULL_NAME(NAMESPACES, NAME), \
SMART_ENUM_IMPL_FULL_NAME_STRING(NAMESPACES, NAME), \
NAME, \
MEMBERS \
)
#define SMART_ENUM_IMPL_TRAITS_1(FULL_NAME, FULL_NAME_STRING, NAME, MEMBERS) \
namespace smart_enum \
{ \
template<> struct enum_traits<FULL_NAME> : detail::enum_traits_base<FULL_NAME> \
{ \
using type = FULL_NAME; \
using data_type = SMART_ENUM_IMPL_DATA_TYPE(BOOST_PP_TUPLE_ELEM(0, MEMBERS)); \
\
static constexpr const char *name = BOOST_PP_STRINGIZE(NAME); \
static constexpr const char *full_name = FULL_NAME_STRING; \
\
static constexpr std::size_t count = BOOST_PP_TUPLE_SIZE(MEMBERS); \
\
SMART_ENUM_IMPL_DATA(FULL_NAME, MEMBERS) \
SMART_ENUM_IMPL_FROM_STRING(FULL_NAME, MEMBERS) \
SMART_ENUM_IMPL_INDEX_OF(FULL_NAME, MEMBERS) \
SMART_ENUM_IMPL_TO_STRING(FULL_NAME, MEMBERS) \
SMART_ENUM_IMPL_VALUE_OF(FULL_NAME, MEMBERS) \
}; \
}
// full name
#define SMART_ENUM_IMPL_FULL_NAME(NAMESPACES, NAME) \
SMART_ENUM_IMPL_REPEAT(FULL_NAME_1, NAMESPACES) NAME
#define SMART_ENUM_IMPL_FULL_NAME_1(NAMESPACE) \
NAMESPACE ::
#define SMART_ENUM_IMPL_FULL_NAME_STRING(NAMESPACES, NAME) \
SMART_ENUM_IMPL_REPEAT(FULL_NAME_STRING_1, NAMESPACES) BOOST_PP_STRINGIZE(NAME)
#define SMART_ENUM_IMPL_FULL_NAME_STRING_1(NAMESPACE) \
BOOST_PP_STRINGIZE(NAMESPACE) "::"
// data
#define SMART_ENUM_IMPL_DATA(PREFIX, MEMBERS) \
static data_type data(PREFIX value) \
{ \
switch(value) \
{ \
SMART_ENUM_IMPL_REPEAT_MEMBERS(PREFIX, MEMBERS, MEMBER_DATA) \
} \
\
throw std::invalid_argument("value"); \
}
#define SMART_ENUM_IMPL_MEMBER_DATA(PREFIX, NAME, MEMBER, INDEX) \
case PREFIX :: NAME: \
return std::make_tuple \
SMART_ENUM_IMPL_MEMBER_DATA_1 \
( \
SMART_ENUM_IMPL_TUPLE_LAST_ELEMENT(MEMBER) \
) \
;
#define SMART_ENUM_IMPL_MEMBER_DATA_1(DATA) \
BOOST_PP_IIF \
( \
BOOST_PP_IS_BEGIN_PARENS(DATA), DATA, () \
)
#define SMART_ENUM_IMPL_DATA_TYPE(MEMBER) \
decltype \
( \
std::make_tuple \
SMART_ENUM_IMPL_DATA_TYPE_1 \
( \
SMART_ENUM_IMPL_TUPLE_LAST_ELEMENT(MEMBER) \
) \
)
#define SMART_ENUM_IMPL_DATA_TYPE_1(DATA) \
BOOST_PP_IIF \
( \
BOOST_PP_IS_BEGIN_PARENS(DATA), DATA, () \
)
// member definitions
#define SMART_ENUM_IMPL_MEMBER_DEFINITIONS(MEMBERS) \
SMART_ENUM_IMPL_ENUM_MEMBERS(_, MEMBERS, MEMBER_DEFINITION)
#define SMART_ENUM_IMPL_MEMBER_DEFINITION(PREFIX, NAME, MEMBER, INDEX) \
NAME \
SMART_ENUM_IMPL_MEMBER_DEFINITION_1 \
( \
BOOST_PP_TUPLE_ELEM(1, BOOST_PP_TUPLE_PUSH_BACK(MEMBER, ())) \
)
#define SMART_ENUM_IMPL_MEMBER_DEFINITION_1(VALUE) \
BOOST_PP_EXPR_IIF \
( \
BOOST_PP_NOT(BOOST_PP_IS_BEGIN_PARENS(VALUE)), = VALUE \
)
// from_string
#define SMART_ENUM_IMPL_FROM_STRING(PREFIX, MEMBERS) \
static PREFIX from_string(const char *s) \
{ \
return SMART_ENUM_IMPL_REPEAT_MEMBERS(PREFIX, MEMBERS, MEMBER_FROM_STRING) throw std::invalid_argument("s"); \
}
#define SMART_ENUM_IMPL_MEMBER_FROM_STRING(PREFIX, NAME, MEMBER, INDEX) \
!std::strcmp(s, BOOST_PP_STRINGIZE(NAME)) ? PREFIX :: NAME :
// index_of
#define SMART_ENUM_IMPL_INDEX_OF(PREFIX, MEMBERS) \
static constexpr std::size_t index_of(PREFIX value) \
{ \
return SMART_ENUM_IMPL_REPEAT_MEMBERS(PREFIX, MEMBERS, MEMBER_INDEX_OF) throw std::invalid_argument("value"); \
}
#define SMART_ENUM_IMPL_MEMBER_INDEX_OF(PREFIX, NAME, MEMBER, INDEX) \
value == PREFIX :: NAME ? INDEX ## u :
// to_string
#define SMART_ENUM_IMPL_TO_STRING(PREFIX, MEMBERS) \
static const char *to_string(PREFIX value) \
{ \
switch(value) \
{ \
SMART_ENUM_IMPL_REPEAT_MEMBERS(PREFIX, MEMBERS, MEMBER_TO_STRING) \
} \
\
throw std::invalid_argument("value"); \
}
#define SMART_ENUM_IMPL_MEMBER_TO_STRING(PREFIX, NAME, MEMBER, INDEX) \
case PREFIX :: NAME: \
return BOOST_PP_STRINGIZE(NAME);
// value_of
#define SMART_ENUM_IMPL_VALUE_OF(PREFIX, MEMBERS) \
static constexpr PREFIX value_of(std::size_t index) \
{ \
return SMART_ENUM_IMPL_REPEAT_MEMBERS(PREFIX, MEMBERS, MEMBER_VALUE_OF) throw std::invalid_argument("index"); \
}
#define SMART_ENUM_IMPL_MEMBER_VALUE_OF(PREFIX, NAME, MEMBER, INDEX) \
index == INDEX ? PREFIX :: NAME :
// namespaces
#define SMART_ENUM_IMPL_NAMESPACE_END(_) \
}
#define SMART_ENUM_IMPL_NAMESPACE_START(NAMESPACE) \
namespace NAMESPACE {
// utils
#define SMART_ENUM_IMPL_ARG_TO_TUPLE(ARG) \
(BOOST_PP_REMOVE_PARENS(ARG))
#define SMART_ENUM_IMPL_ARGS_TO_TUPLES(ARGS) \
BOOST_PP_ENUM(BOOST_PP_TUPLE_SIZE(ARGS), SMART_ENUM_IMPL_ARGS_TO_TUPLES_1, ARGS)
#define SMART_ENUM_IMPL_ARGS_TO_TUPLES_1(_, INDEX, ARGS) \
SMART_ENUM_IMPL_ARG_TO_TUPLE(BOOST_PP_TUPLE_ELEM(INDEX, ARGS))
#define SMART_ENUM_IMPL_REPEAT(MACRO, TUPLE) \
BOOST_PP_EXPR_IIF \
( \
BOOST_PP_IS_BEGIN_PARENS(TUPLE), \
BOOST_PP_REPEAT(BOOST_PP_TUPLE_SIZE(TUPLE), SMART_ENUM_IMPL_REPEAT_1, (TUPLE, MACRO)) \
)
#define SMART_ENUM_IMPL_REPEAT_1(_, INDEX, TUPLE_MACRO) \
BOOST_PP_CAT(SMART_ENUM_IMPL_, BOOST_PP_TUPLE_ELEM(1, TUPLE_MACRO)) \
(BOOST_PP_TUPLE_ELEM(INDEX, BOOST_PP_TUPLE_ELEM(0, TUPLE_MACRO)))
#define SMART_ENUM_IMPL_ENUM_MEMBERS(PREFIX, MEMBERS, MACRO) \
BOOST_PP_ENUM(BOOST_PP_TUPLE_SIZE(MEMBERS), SMART_ENUM_IMPL_PROCESS_MEMBERS_1, (PREFIX, MEMBERS, MACRO))
#define SMART_ENUM_IMPL_REPEAT_MEMBERS(PREFIX, MEMBERS, MACRO) \
BOOST_PP_REPEAT(BOOST_PP_TUPLE_SIZE(MEMBERS), SMART_ENUM_IMPL_PROCESS_MEMBERS_1, (PREFIX, MEMBERS, MACRO))
#define SMART_ENUM_IMPL_PROCESS_MEMBERS_1(_, INDEX, PREFIX_MEMBERS_MACRO) \
SMART_ENUM_IMPL_PROCESS_MEMBERS_2 \
( \
BOOST_PP_TUPLE_ELEM(0, PREFIX_MEMBERS_MACRO), \
BOOST_PP_TUPLE_ELEM(INDEX, BOOST_PP_TUPLE_ELEM(1, PREFIX_MEMBERS_MACRO)), \
BOOST_PP_TUPLE_ELEM(2, PREFIX_MEMBERS_MACRO), \
INDEX \
)
#define SMART_ENUM_IMPL_PROCESS_MEMBERS_2(PREFIX, MEMBER, MACRO, INDEX) \
BOOST_PP_CAT(SMART_ENUM_IMPL_, MACRO)(PREFIX, BOOST_PP_TUPLE_ELEM(0, MEMBER), MEMBER, INDEX)
#define SMART_ENUM_IMPL_TUPLE_LAST_ELEMENT(TUPLE) \
BOOST_PP_TUPLE_ELEM(BOOST_PP_DEC(BOOST_PP_TUPLE_SIZE(TUPLE)), TUPLE)
namespace smart_enum
{
template
<
typename Enum
>
class enum_iterator;
template
<
typename Enum
>
struct enum_range;
namespace detail
{
template
<
typename Enum
>
struct enum_traits_base
{
static constexpr bool is_enum_class =
std::integral_constant
<
bool, std::is_enum<Enum>::value && !std::is_convertible<Enum, int>::value
>
::value;
};
}
template
<
typename Enum
>
struct enum_traits {};
template
<
typename Enum
>
constexpr enum_iterator<Enum> begin()
{
return enum_iterator<Enum>{enum_traits<Enum>::value_of(0)};
}
template
<
typename Enum
>
typename enum_traits<Enum>::data_type data(Enum value)
{
return enum_traits<Enum>::data(value);
}
template
<
typename Enum
>
constexpr enum_iterator<Enum> end()
{
return {};
}
template
<
typename Enum
>
constexpr std::size_t count()
{
return enum_traits<Enum>::count;
}
template
<
typename Enum
>
Enum from_string(const char *s)
{
return enum_traits<Enum>::from_string(s);
}
template
<
typename Enum
>
constexpr const char *full_name()
{
return enum_traits<Enum>::full_name;
}
template
<
typename Enum
>
constexpr std::size_t index_of(Enum value)
{
return enum_traits<Enum>::index_of(value);
}
template
<
typename Enum
>
constexpr bool is_enum_class()
{
return enum_traits<Enum>::is_enum_class;
}
template
<
typename Enum
>
constexpr const char *name()
{
return enum_traits<Enum>::name;
}
template
<
typename Enum
>
constexpr enum_range<Enum> range()
{
return {};
}
template
<
typename Enum
>
const char *to_string(Enum value)
{
return enum_traits<Enum>::to_string(value);
}
template
<
typename Enum
>
constexpr Enum value_of(std::size_t index)
{
return enum_traits<Enum>::value_of(index);
}
template
<
typename Enum
>
class enum_iterator : public boost::iterator_facade
<
enum_iterator<Enum>, Enum, boost::random_access_traversal_tag, Enum
>
{
public:
constexpr enum_iterator()
: index_{ count<Enum>() }
{
}
constexpr explicit enum_iterator(Enum value)
: index_{ index_of(value) }
{
}
private:
std::size_t index_;
void advance(std::ptrdiff_t n)
{
index_ += n;
}
void decrement()
{
--index_;
}
constexpr Enum dereference() const
{
return value_of<Enum>(index_);
}
constexpr std::ptrdiff_t distance_to(const enum_iterator &other) const
{
return other.index_ - index_;
}
constexpr bool equal(const enum_iterator &other) const
{
return other.index_ == index_;
}
void increment()
{
++index_;
}
friend class boost::iterator_core_access;
};
template
<
typename Enum
>
struct enum_range
{
constexpr enum_iterator<Enum> begin() const
{
return smart_enum::begin<Enum>();
}
constexpr enum_iterator<Enum> end() const
{
return smart_enum::end<Enum>();
}
};
}
#endif