-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsmart_enum.h
More file actions
709 lines (585 loc) · 25.7 KB
/
smart_enum.h
File metadata and controls
709 lines (585 loc) · 25.7 KB
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
_
___ _ __ ___ __ _ _ __| |_ ___ _ __ _ _ _ __ ___
/ __| '_ ` _ \ / _` | '__| __| / _ \ '_ \| | | | '_ ` _ \
\__ \ | | | | | (_| | | | |_ | __/ | | | |_| | | | | | |
|___/_| |_| |_|\__,_|_| \__| \___|_| |_|\__,_|_| |_| |_|
Smart Enum
Version 0.1.0
https://github.com/seleznevae/smart_enum
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
Copyright (c) 2017 Anton Seleznev <seleznevae@protonmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
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 AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef SMART_ENUM_H
#define SMART_ENUM_H
#include <type_traits>
#include <array>
#pragma GCC diagnostic ignored "-Wswitch"
namespace smart_enum
{
namespace detail
{
template <template <int, int> class SpecClass, int k>
using is_specialized_for_first = std::is_default_constructible<SpecClass<k, 0>>;
template <typename EnumType, int k, bool is_specialized>
struct enum_sz_impl_class;
template <typename EnumType, int k >
struct enum_sz_impl_class<EnumType, k, true> {
static constexpr size_t enum_sz() { return 1 + enum_sz_impl_class<EnumType,
k + 1,
is_specialized_for_first<EnumType:: template class_to_spec, k + 1>::value
>::enum_sz(); }
};
template <typename EnumType, int k>
struct enum_sz_impl_class<EnumType, k, false> {
static constexpr size_t enum_sz() { return 0; }
};
template <typename EnumType, int k>
constexpr size_t enum_size_impl()
{
return enum_sz_impl_class<EnumType, k, is_specialized_for_first<EnumType:: template class_to_spec, k>::value >::enum_sz();
}
} //detail
template <typename EnumType>
constexpr size_t enum_size()
{
return detail::enum_size_impl<EnumType, EnumType::base_value + 1>();
}
///////////////////////////////////////////////////////////////////////////////
namespace detail
{
template<class T>
struct void_t {
typedef void type;
};
}
template<class T, class U = void>
struct is_smart_enum {
enum { value = 0 };
};
template<class T>
struct is_smart_enum<T, typename smart_enum::detail::void_t<typename T::is_smart_enum>::type > {
enum { value = 1 };
};
template <typename T>
constexpr bool is_smart_enum_v = smart_enum::is_smart_enum<T>::value;
////////////////////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename T, typename U, bool is_T_integral, bool is_T_smart_enum>
struct enum_cast_impl {
static_assert((smart_enum::is_smart_enum_v<U> || std::is_integral<T>::value)
&& (smart_enum::is_smart_enum_v<T> || std::is_integral<U>::value),
"Argument and return value of smart_enum::enum_cast should be an integral or"
" a smart enum");
};
template <typename T, typename U>
struct enum_cast_impl<T, U, false, true> {
static_assert((smart_enum::is_smart_enum_v<U> || std::is_integral<U>::value)
&& (smart_enum::is_smart_enum_v<T> || std::is_integral<T>::value),
"Argument and return value of smart_enum::enum_cast should be an integral or"
" a smart enum");
static constexpr T enum_cast(U value, bool *ok = nullptr)
{
T result(typename T::intern_help_type{static_cast<typename T::value_t>(value)});
if (ok)
*ok = result.is_valid();
return result;
}
};
template <typename T, typename U>
struct enum_cast_impl<T, U, true, false> {
static_assert((smart_enum::is_smart_enum_v<U> || std::is_integral<U>::value)
&& (smart_enum::is_smart_enum_v<T> || std::is_integral<T>::value),
"Argument and return value of smart_enum::enum_cast should be an integral or"
" a smart enum");
static constexpr T enum_cast(U value, bool *ok = nullptr)
{
if (ok)
*ok = true;
return static_cast<T>(value.enum_member.value);
}
};
}
template <typename T, typename U>
constexpr T enum_cast(U value, bool *ok = nullptr)
{
return detail::enum_cast_impl<T, U,
std::is_integral<T>::value,
smart_enum::is_smart_enum_v<T>
>::enum_cast(value, ok);
}
/////////////////////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename EnumType, int i, int k >
struct enum_check_impl_class {
constexpr static bool check(EnumType enum_value)
{
return enum_value.enum_member == static_cast<typename EnumType::internal_enum_t>(EnumType::template class_to_spec<EnumType::base_value+1+i,0>::value)
? true
: detail::enum_check_impl_class<EnumType, i + 1, enum_size<EnumType>()>::check(enum_value);
}
};
template <typename EnumType, int k >
struct enum_check_impl_class<EnumType, k, k> {
constexpr static bool check(EnumType) { return false; }
};
} //detail
template <typename EnumType>
constexpr bool enum_check(EnumType enum_value)
{
return detail::enum_check_impl_class<EnumType, 0, enum_size<EnumType>()>::check(enum_value);
}
//////////////////////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename EnumType, int i, int k >
struct index_of_impl_class {
constexpr static ssize_t index_of(EnumType enum_value)
{
return enum_value.enum_member == static_cast<typename EnumType::internal_enum_t>(EnumType::template class_to_spec<EnumType::base_value+1+i,0>::value)
? i
: detail::index_of_impl_class<EnumType, i + 1, enum_size<EnumType>()>::index_of(enum_value);
}
};
template <typename EnumType, int k >
struct index_of_impl_class<EnumType, k, k> {
constexpr static ssize_t index_of(EnumType) { return -1; }
};
} //detail
template <typename EnumType>
constexpr ssize_t index_of(EnumType value)
{
return detail::index_of_impl_class<EnumType, 0, enum_size<EnumType>()>::index_of(value);
}
/////////////////////////////////////////////////////////////////////////////////////////////
template <typename EnumType, size_t n>
constexpr EnumType
get_n()
{
static_assert(n < smart_enum::enum_size<EnumType>(), "Incorrect get argument");
constexpr auto tmp = static_cast<typename EnumType::internal_enum_t>(EnumType::template class_to_spec<EnumType::base_value+1+n,0>::value);
return EnumType(typename EnumType::intern_help_type(tmp));
}
template <typename EnumType, size_t n>
constexpr const char*
get_name_n()
{
static_assert(n < smart_enum::enum_size<EnumType>(), "Incorrect get argument");
return EnumType::template class_to_spec<EnumType::base_value+1+n,0>::name();
}
template <typename EnumType, size_t n>
constexpr const char*
get_description_n()
{
static_assert(n < smart_enum::enum_size<EnumType>(), "Incorrect get argument");
return EnumType::template class_to_spec<EnumType::base_value+1+n,0>::description();
}
//////////////////////////////////////////////////////////////////////////////////////////////
namespace detail
{
namespace array_detail
{
template<long unsigned... Is> struct seq{};
template<long unsigned N, long unsigned... Is>
struct gen_seq : gen_seq<N-1, N-1, Is...>{};
template<long unsigned... Is>
struct gen_seq<0, Is...> : seq<Is...>{};
template<typename T, long unsigned N1, long unsigned... I1, long unsigned N2, long unsigned... I2>
// Expansion pack
constexpr std::array<T, N1+N2> concat_impl(const std::array<T, N1>& a1, const std::array<T, N2>& a2, seq<I1...>, seq<I2...>){
return { a1[I1]..., a2[I2]... };
}
template<typename T, long unsigned N1, long unsigned N2>
constexpr std::array<T, N1+N2> concat(const std::array<T, N1>& a1, const std::array<T, N2>& a2)
{
return concat_impl(a1, a2, gen_seq<N1>{}, gen_seq<N2>{});
}
} //array_detail
template <typename EnumType, int i, int k >
struct values_impl_class {
constexpr static std::array<EnumType, k - i>
values()
{
return array_detail::concat(std::array<EnumType, 1> {smart_enum::get_n<EnumType, i>()},
detail::values_impl_class<EnumType, i + 1, k>::values());
}
};
template <typename EnumType, int k >
struct values_impl_class<EnumType, k, k> {
constexpr static std::array<EnumType, 0>
values() { return {}; }
};
} //detail
template <typename EnumType>
constexpr std::array<EnumType, smart_enum::enum_size<EnumType>()>
values()
{
return detail::values_impl_class<EnumType, 0, enum_size<EnumType>()>::values();
}
//////////////////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename EnumType, int i, int k >
struct names_impl_class {
constexpr static std::array<const char*, k - i>
names()
{
return array_detail::concat(std::array<const char*, 1> {smart_enum::get_name_n<EnumType, i>()},
detail::names_impl_class<EnumType, i + 1, k>::names());
}
};
template <typename EnumType, int k >
struct names_impl_class<EnumType, k, k> {
constexpr static std::array<const char*, 0>
names() { return {}; }
};
}
template <typename EnumType>
constexpr std::array<const char*, smart_enum::enum_size<EnumType>()>
names()
{
return detail::names_impl_class<EnumType, 0, enum_size<EnumType>()>::names();
}
//////////////////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename EnumType, int i, int k >
struct descriptions_impl_class {
constexpr static std::array<const char*, k - i>
descriptions()
{
return array_detail::concat(std::array<const char*, 1> {smart_enum::get_description_n<EnumType, i>()},
detail::descriptions_impl_class<EnumType, i + 1, k>::descriptions());
}
};
template <typename EnumType, int k >
struct descriptions_impl_class<EnumType, k, k> {
constexpr static std::array<const char*, 0>
descriptions() { return {}; }
};
}
template <typename EnumType>
constexpr std::array<const char*, smart_enum::enum_size<EnumType>()>
descriptions()
{
return detail::descriptions_impl_class<EnumType, 0, enum_size<EnumType>()>::descriptions();
}
/////////////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename SmartEnum, size_t CurPos, size_t Size>
struct enum_to_string_impl {
static constexpr
const char* to_string(ssize_t pos_to_extract)
{
return CurPos == pos_to_extract
? get_name_n<SmartEnum, CurPos>()
: enum_to_string_impl<SmartEnum, CurPos+1, Size>::to_string(pos_to_extract)
;
}
};
template <typename SmartEnum, size_t Size>
struct enum_to_string_impl<SmartEnum, Size, Size>
{
static constexpr
const char* to_string(ssize_t /*pos_to_extract*/)
{
return "Unknown";
}
};
}
template <typename EnumType>
constexpr const char* to_string(EnumType enum_value, bool *ok = nullptr)
{
ssize_t index = smart_enum::index_of(enum_value);
if (index == -1) {
return ok
? (*ok = false, "Unknown")
: (throw std::bad_alloc(), "Unknown");
}
if (ok)
*ok = true;
return detail::enum_to_string_impl< EnumType,
0,
smart_enum::enum_size<EnumType>()
>::to_string(index);
}
////////////////////////////////////////////////////////////////////////////////
namespace detail
{
constexpr int ct_strcmp(const char* lhs, const char*rhs)
{
return (lhs[0] == rhs[0]) ? ((lhs[0] == '\0') ? 0 : ct_strcmp(lhs + 1, rhs + 1))
: ((lhs[0] > rhs[0]) ? 1 : -1);
}
template <typename SmartEnum, size_t CurPos, size_t Size>
struct enum_from_string_impl {
static constexpr
SmartEnum from_string(const char* str, bool *ok = nullptr)
{
if (ct_strcmp(get_name_n<SmartEnum, CurPos>(), str) == 0) {
if (ok)
*ok = true;
return smart_enum::get_n<SmartEnum, CurPos>();
} else {
return enum_from_string_impl<SmartEnum, CurPos+1, Size>::from_string(str, ok);
}
}
};
template <typename SmartEnum, size_t Size>
struct enum_from_string_impl<SmartEnum, Size, Size>
{
static constexpr
SmartEnum from_string(const char* /*str*/, bool *ok = nullptr)
{
// return ok ? (*ok = false , smart_enum::get_n<SmartEnum, 0>())
// : (throw std::bad_alloc(), smart_enum::get_n<SmartEnum, 0>());
if (ok)
*ok = false;
else
throw std::bad_alloc();
return smart_enum::get_n<SmartEnum, 0>();
}
};
}
template <typename EnumType>
constexpr EnumType from_string(const char*str, bool *ok = nullptr)
{
return detail::enum_from_string_impl< EnumType,
0,
smart_enum::enum_size<EnumType>()
>::from_string(str, ok);
}
/////////////////////////////////////////////////////////////////////////////////
namespace detail
{
template <typename SmartEnum, size_t CurPos, size_t Size>
struct enum_to_description_impl {
static constexpr
const char* description(ssize_t pos_to_extract)
{
return CurPos == pos_to_extract
? get_description_n<SmartEnum, CurPos>()
: enum_to_description_impl<SmartEnum, CurPos+1, Size>::description(pos_to_extract)
;
}
};
template <typename SmartEnum, size_t Size>
struct enum_to_description_impl<SmartEnum, Size, Size>
{
static constexpr
const char* description(ssize_t /*pos_to_extract*/)
{
return "Unknown";
}
};
}
template <typename EnumType>
constexpr const char* description(EnumType enum_value, bool *ok = nullptr)
{
ssize_t index = smart_enum::index_of(enum_value);
if (index == -1) {
return ok
? (*ok = false, "Unknown")
: (throw std::bad_alloc(), "Unknown");
}
if (ok)
*ok = true;
return detail::enum_to_description_impl< EnumType,
0,
smart_enum::enum_size<EnumType>()
>::description(index);
}
} //smart_enum
///////////////////////////////////////////////////////////////////////////////
namespace test_nms{
struct test_class {
template <typename T, ssize_t BaseValue>
struct CCC_base;
template <typename T, ssize_t BaseValue>
struct CCC_base;
template <typename T>
struct CCC_base<T, __COUNTER__> {
using underl_t = int;
enum class internal_enum_t:underl_t {
};
using value_t = internal_enum_t;
constexpr CCC_base(internal_enum_t member_): enum_member(member_) { }
static constexpr auto base_value = __COUNTER__ - 1;
// constructor helper
template <underl_t k>
struct helper_class {
constexpr helper_class(internal_enum_t v): value(static_cast<underl_t>(v)) { }
// constexpr helper_class(const helper_class<k> &v): value(v.value) { }
underl_t value;
constexpr operator internal_enum_t() const { return static_cast<internal_enum_t>(value); }
constexpr bool operator ==(helper_class<k> rhs) { return this->value == rhs.value; }
constexpr bool operator !=(helper_class<k> rhs) { return this->value != rhs.value; }
};
using intern_help_type = helper_class<1>;
intern_help_type enum_member;
using base_type = CCC_base<T, __COUNTER__ - 2>;
using is_smart_enum = std::true_type;
static constexpr size_t size() { return smart_enum::enum_size<T>(); }
constexpr operator internal_enum_t() const { return static_cast<internal_enum_t>(enum_member.value); }
constexpr bool is_valid() const { return smart_enum::enum_check<T>(static_cast<const T&>(*this)); }
constexpr ssize_t index() const { return smart_enum::index_of<T>(static_cast<const T&>(*this)); }
constexpr static auto values() { return smart_enum::values<T>(); }
constexpr static auto names() { return smart_enum::names<T>(); }
constexpr static auto descriptions() { return smart_enum::descriptions<T>(); }
constexpr const char* to_string(bool *ok = nullptr)const { return smart_enum::to_string<T>(static_cast<const T&>(*this), ok); }
static constexpr T from_string(const char* str, bool *ok = nullptr) { return smart_enum::from_string<T>(str, ok); }
constexpr const char* description(bool *ok = nullptr)const { return smart_enum::description<T>(static_cast<const T&>(*this), ok); }
template <typename U = underl_t>
constexpr U to_integral() const { return smart_enum::enum_cast<U>(static_cast<const T&>(*this)); }
template <typename U>
static constexpr T from_integral(U integr_v, bool *ok = nullptr) { return smart_enum::enum_cast<T>(integr_v, ok); }
};
template <int k, int DummyInt>
struct smart_enum_mutual_alias;
using CCC = smart_enum_mutual_alias<__COUNTER__ - 3, 0>;
template <int DummyInt>
struct smart_enum_mutual_alias<__COUNTER__ - 4, DummyInt>:
public CCC_base<smart_enum_mutual_alias<__COUNTER__ - 5, 0>, __COUNTER__ - 6>{
using base_class_enum_elem0 = CCC_base<smart_enum_mutual_alias<base_value, DummyInt>, base_value>;
static constexpr underl_t AAA_counter_enum_elem0 = __COUNTER__ - 6;
static constexpr intern_help_type enum_elem0 = static_cast<typename base_class_enum_elem0::internal_enum_t>(4);
template <int, int dummy>
struct class_to_spec;
template <int dummy>
struct class_to_spec<AAA_counter_enum_elem0, dummy> {
static constexpr underl_t value = static_cast<underl_t>(enum_elem0.value);
static constexpr auto name() { return "enum_elem0_name"; }
static constexpr auto description() { return "enum_elem0_description"; }
};
constexpr smart_enum_mutual_alias(helper_class<AAA_counter_enum_elem0 - base_value> v): base_type(static_cast<internal_enum_t>(v.value)) {}
using base_class_enum_elem1 = CCC_base<smart_enum_mutual_alias<base_value, DummyInt>, base_value>;
static constexpr underl_t AAA_counter_enum_elem1 = __COUNTER__ - 6;
static constexpr intern_help_type enum_elem1 = static_cast<typename base_class_enum_elem1::internal_enum_t>(9);
template <int, int dummy>
struct class_to_spec;
template <int dummy>
struct class_to_spec<AAA_counter_enum_elem1, dummy> {
static constexpr underl_t value = static_cast<underl_t>(enum_elem1.value);
static constexpr auto name() { return "enum_elem1_name"; }
static constexpr auto description() { return "enum_elem1_description"; }
};
constexpr smart_enum_mutual_alias(helper_class<AAA_counter_enum_elem1 - base_value> v): base_type(static_cast<internal_enum_t>(v.value)) {}
//default_value
using base_class_enum_elem2 = CCC_base<smart_enum_mutual_alias<base_value - 1, DummyInt>, base_value>;
static constexpr underl_t AAA_counter_enum_elem2 = __COUNTER__ - 6;
static constexpr intern_help_type enum_elem2 = static_cast<typename base_class_enum_elem1::internal_enum_t>(class_to_spec<AAA_counter_enum_elem2 - 1, 0>::value + 1);
template <int, int dummy>
struct class_to_spec;
template <int dummy>
struct class_to_spec<AAA_counter_enum_elem2, dummy> {
static constexpr underl_t value = static_cast<underl_t>(enum_elem2.value);
static constexpr auto name() { return "enum_elem2_name"; }
static constexpr auto description() { return "enum_elem2_description"; }
};
constexpr smart_enum_mutual_alias(helper_class<AAA_counter_enum_elem2 - base_value> v): base_type(static_cast<internal_enum_t>(v.value)) {}
};
};
}
//////////////////////////////////////////////////////////////////////////////
#define SMART_ENUM2(enum_name,enum_und_type) \
template <typename T, ssize_t BaseValue>\
struct smart_enum_base;\
template <typename T>\
struct smart_enum_base<T, __COUNTER__> {\
using underl_t = enum_und_type;\
enum class internal_enum_t:underl_t {\
};\
using value_t = internal_enum_t;\
\
constexpr smart_enum_base(internal_enum_t member_): enum_member(member_) { }\
static constexpr int base_value = __COUNTER__ - 1;\
\
template <underl_t k>\
struct helper_class {\
constexpr helper_class(internal_enum_t v): value(static_cast<underl_t>(v)) { }\
underl_t value;\
constexpr operator internal_enum_t() const { return static_cast<internal_enum_t>(value); }\
};\
\
using intern_help_type = helper_class<1>;\
intern_help_type enum_member;\
using base_type = smart_enum_base<T, __COUNTER__ - 2>; \
using is_smart_enum = std::true_type; \
\
static constexpr size_t size() { return smart_enum::enum_size<T>(); }\
constexpr operator internal_enum_t() const { return static_cast<internal_enum_t>(enum_member.value); }\
constexpr bool is_valid() const { return smart_enum::enum_check<T>(static_cast<const T&>(*this)); }\
constexpr ssize_t index() const { return smart_enum::index_of<T>(static_cast<const T&>(*this)); }\
constexpr static auto values() { return smart_enum::values<T>(); } \
constexpr static auto names() { return smart_enum::names<T>(); }\
constexpr static auto descriptions() { return smart_enum::descriptions<T>(); }\
constexpr const char* to_string(bool *ok = nullptr) const { return smart_enum::to_string<T>(static_cast<const T&>(*this), ok); }\
static constexpr T from_string(const char* str, bool *ok = nullptr) { return smart_enum::from_string<T>(str, ok); } \
constexpr const char* description(bool *ok = nullptr)const { return smart_enum::description<T>(static_cast<const T&>(*this), ok); }\
template <typename U = underl_t> \
constexpr U to_integral() const { return smart_enum::enum_cast<U>(static_cast<const T&>(*this)); } \
template <typename U> \
static constexpr T from_integral(U integr_v, bool *ok = nullptr) { return smart_enum::enum_cast<T>(integr_v, ok); } \
};\
\
template <int k, int DummyInt> \
struct smart_enum_mutual_alias;\
\
using enum_name = smart_enum_mutual_alias<__COUNTER__ - 3, 0>; \
\
template <int DummyInt> \
struct smart_enum_mutual_alias<__COUNTER__ - 4, DummyInt>: \
public smart_enum_base<smart_enum_mutual_alias<__COUNTER__ - 5, 0>, __COUNTER__ - 6>
#define SMART_ENUM1_(enum_name) \
SMART_ENUM2(enum_name, int)
#define SMART_ENUM1(enum_name) \
SMART_ENUM1_(enum_name)
#define SMART_ENUM_ELEM4(elem_id, elem_value, elem_name, elem_description)\
\
using base_class_##elem_id = smart_enum_base<smart_enum_mutual_alias<base_value, DummyInt>, base_value>;\
static constexpr underl_t counter_enum_##elem_id = __COUNTER__ - 6;\
static constexpr intern_help_type elem_id = static_cast<typename base_class_##elem_id::value_t>(elem_value);\
template <int, int dummy>\
struct class_to_spec;\
template <int dummy>\
struct class_to_spec<counter_enum_##elem_id, dummy> {\
static constexpr underl_t value = static_cast<underl_t>(elem_id.value);\
static constexpr auto name() { return elem_name ; }\
static constexpr auto description() { return elem_description ; }\
};\
constexpr smart_enum_mutual_alias(helper_class<counter_enum_##elem_id - base_value> v): base_type(static_cast<internal_enum_t>(v.value)) {}
#define SMART_ENUM_ELEM3_(elem_id, elem_value, elem_name)\
SMART_ENUM_ELEM4(elem_id, elem_value,elem_name,elem_name)
#define SMART_ENUM_ELEM3(elem_id, elem_value, elem_name)\
SMART_ENUM_ELEM3_(elem_id, elem_value, elem_name)
#define SMART_ENUM_ELEM2_(elem_id, elem_value)\
SMART_ENUM_ELEM3(elem_id, elem_value, #elem_id)
#define SMART_ENUM_ELEM2(elem_id, elem_value)\
SMART_ENUM_ELEM2_(elem_id, elem_value)
#define SMART_ENUM_ELEM1_(elem_id)\
SMART_ENUM_ELEM2(elem_id, (class_to_spec<counter_enum_##elem_id - 1, 0>::value + 1))
#define SMART_ENUM_ELEM1(elem_id)\
SMART_ENUM_ELEM1_(elem_id)
#define SMART_ENUM_GET_MACRO4(_1,_2,_3,_4,NAME,...) NAME
#define SM_ENUM_ELEM(...) SMART_ENUM_GET_MACRO4(__VA_ARGS__, SMART_ENUM_ELEM4, SMART_ENUM_ELEM3, SMART_ENUM_ELEM2, SMART_ENUM_ELEM1)(__VA_ARGS__)
#define SMART_ENUM_GET_MACRO2(_1,_2,NAME,...) NAME
#define SMART_ENUM(...) SMART_ENUM_GET_MACRO2(__VA_ARGS__, SMART_ENUM2, SMART_ENUM1)(__VA_ARGS__)
#endif // SMART_ENUM_H