forked from veselink1/refl-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrefl.hpp
3201 lines (2768 loc) · 118 KB
/
refl.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
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
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// The MIT License (MIT)
//
// Copyright (c) 2020 Veselin Karaganev (@veselink1) and Contributors
//
// 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 REFL_INCLUDE_HPP
#define REFL_INCLUDE_HPP
// Support infrastructure for refl-ht
#ifdef REFL_PREPROCESSOR
#define REFL(...) typedef void __refl_autogenerated_marker__;
#else
#define REFL(...)
#endif
#ifndef REFL_PREPROCESSOR
#include <stddef.h> // size_t
#include <cstring>
#include <array>
#include <memory>
#include <utility> // std::move, std::forward
#include <optional>
#include <tuple>
#include <type_traits>
#include <ostream>
#include <sstream>
#include <iomanip> // std::quoted
#ifdef _MSC_VER
// Disable VS warning for "Not enough arguments for macro"
// (emitted when a REFL_ macro is not provided any attributes)
#pragma warning( disable : 4003 )
#endif
#if defined(__clang__)
#if __has_feature(cxx_rtti)
#define REFL_RTTI_ENABLED
#endif
#elif defined(__GNUG__)
#if defined(__GXX_RTTI)
#define REFL_RTTI_ENABLED
#endif
#elif defined(_MSC_VER)
#if defined(_CPPRTTI)
#define REFL_RTTI_ENABLED
#endif
#endif
/**
* @brief The top-level refl-cpp namespace
* It contains a few core refl-cpp namespaces and directly exposes core classes and functions.
* <ul>
* <li>util - utility functions (for_each, map_to_tuple, etc.)</li>
* <li>trait - type-traits and other operations on types (is_function_v, map_t, etc.)</li>
* <li>runtime - utility functions and classes that always have a runtime overhead (proxy<T>, debug_str, etc.)</li>
* <li>member - contains the empty classes member and function (used for tagging)</li>
* <li>descriptor - contains the non-specialized member types (type|field_descriptor<T, N>, and operations on them (get_property, get_display_name, etc.))</li>
* </ul>
*
* using util::type_list; <br>
* using descriptor::type_descriptor; <br>
* using descriptor::field_descriptor; <br>
* using descriptor::function_descriptor; <br>
* using util::const_string; <br>
* using util::make_const_string; <br>
*/
namespace refl
{
/**
* @brief Contains utility types and functions for working with those types.
*/
namespace util
{
/**
* Represents a compile-time string. Used in refl-cpp
* for representing names of reflected types and members.
* Supports constexpr concatenation and substring,
* and is explicitly-convertible to const char* and std::string.
* REFL_MAKE_CONST_STRING can be used to create an instance from a literal string.
*
* @param <N> The length of the string excluding the terminating '\0' character.
* @see refl::descriptor::base_member_descriptor::name
*/
template <size_t N>
struct const_string
{
/** The largest positive value size_t can hold. */
static constexpr size_t npos = static_cast<size_t>(-1);
/** The length of the string excluding the terminating '\0' character. */
static constexpr size_t size = N;
/**
* The statically-sized character buffer used for storing the string.
*/
char data[N + 1];
/**
* Creates an empty const_string.
*/
constexpr const_string() noexcept
: data{}
{
}
/**
* Creates a copy of a const_string.
*/
constexpr const_string(const const_string<N>& other) noexcept
: data{}
{
for (size_t i = 0; i < N; i++)
data[i] = other.data[i];
}
/**
* Creates a const_string by copying the contents of data.
*/
constexpr const_string(const char(&data)[N + 1]) noexcept
: data{}
{
for (size_t i = 0; i < N; i++)
this->data[i] = data[i];
}
/**
* Explicitly converts to const char*.
*/
explicit constexpr operator const char*() const noexcept
{
return data;
}
/**
* Explicitly converts to std::string.
*/
explicit operator std::string() const noexcept
{
return data;
}
/**
* Returns a pointer to the contained zero-terminated string.
*/
constexpr const char* c_str() const noexcept
{
return data;
}
/**
* Returns the contained string as an std::string.
*/
std::string str() const noexcept
{
return data;
}
/**
* A constexpr version of std::string::substr.
*/
template <size_t Pos, size_t Count = npos>
constexpr auto substr() const noexcept
{
static_assert(Pos <= N);
constexpr size_t NewSize = std::min(Count, N - Pos);
char buf[NewSize + 1]{};
for (size_t i = 0; i < NewSize; i++) {
buf[i] = data[Pos + i];
}
return const_string<NewSize>(buf);
}
};
/**
* Creates an empty instance of const_string<N>
*
* @see refl::util::const_string
*/
constexpr const_string<0> make_const_string() noexcept
{
return {};
}
/**
* Creates an instance of const_string<N>
*
* @see refl::util::const_string
*/
template <size_t N>
constexpr const_string<N - 1> make_const_string(const char(&str)[N]) noexcept
{
return str;
}
/**
* Concatenates two const_strings together.
*
* @see refl::util::const_string
*/
template <size_t N, size_t M>
constexpr const_string<N + M> operator+(const const_string<N>& a, const const_string<M>& b) noexcept
{
char data[N + M + 1] { };
for (size_t i = 0; i < N; i++)
data[i] = a.data[i];
for (size_t i = 0; i < M; i++)
data[N + i] = b.data[i];
return data;
}
/**
* Concatenates a const_string with a C-style string.
*
* @see refl::util::const_string
*/
template <size_t N, size_t M>
constexpr const_string<N + M - 1> operator+(const const_string<N>& a, const char(&b)[M]) noexcept
{
return a + make_const_string(b);
}
/**
* Concatenates a C-style string with a const_string.
*
* @see refl::util::const_string
*/
template <size_t N, size_t M>
constexpr const_string<N + M - 1> operator+(const char(&a)[N], const const_string<M>& b) noexcept
{
return make_const_string(a) + b;
}
/**
* Compares two const_strings for equality.
*
* @see refl::util::const_string
*/
template <size_t N, size_t M>
constexpr bool operator==(const const_string<N>& a, const const_string<M>& b) noexcept
{
if constexpr (N != M) {
return false;
}
else {
for (size_t i = 0; i < M; i++) {
if (a.data[i] != b.data[i]) {
return false;
}
}
return true;
}
}
/**
* Compares two const_strings for equality.
*
* @see refl::util::const_string
*/
template <size_t N, size_t M>
constexpr bool operator!=(const const_string<N>& a, const const_string<M>& b) noexcept
{
return !(a == b);
}
/**
* Compares a const_string with a C-style string for equality.
*
* @see refl::util::const_string
*/
template <size_t N, size_t M>
constexpr bool operator==(const const_string<N>& a, const char(&b)[M]) noexcept
{
return a == make_const_string(b);
}
/**
* Compares a const_string with a C-style string for equality.
*
* @see refl::util::const_string
*/
template <size_t N, size_t M>
constexpr bool operator!=(const const_string<N>& a, const char(&b)[M]) noexcept
{
return a != make_const_string(b);
}
/**
* Compares a C-style string with a const_string for equality.
*
* @see refl::util::const_string
*/
template <size_t N, size_t M>
constexpr bool operator==(const char(&a)[N], const const_string<M>& b) noexcept
{
return make_const_string(a) == b;
}
/**
* Compares a C-style string with a const_string for equality.
*
* @see refl::util::const_string
*/
template <size_t N, size_t M>
constexpr bool operator!=(const char(&a)[N], const const_string<M>& b) noexcept
{
return make_const_string(a) != b;
}
template <size_t N>
constexpr std::ostream& operator<<(std::ostream& os, const const_string<N>& str) noexcept
{
return os << str.c_str();
}
namespace detail
{
constexpr size_t strlen(const char* const str)
{
return *str ? 1 + strlen(str + 1) : 0;
}
template <size_t N>
constexpr const_string<N> copy_from_unsized(const char* const str)
{
const_string<N> cstr;
for (size_t i = 0; i < N; i++) {
cstr.data[i] = str[i];
}
return cstr;
}
} // namespace detail
} // namespace util
using util::const_string;
using util::make_const_string;
/**
* Converts a compile-time available const char* value to a const_string<N>.
* The argument must be a *core constant expression* and be null-terminated.
*
* @see refl::util::const_string
*/
#define REFL_MAKE_CONST_STRING(CString) \
(::refl::util::detail::copy_from_unsized<::refl::util::detail::strlen(CString)>(CString))
/**
* The contents of the refl::detail::macro_exports namespace
* is implicitly available in the context of REFL_TYPE/FIELD/FUNC macros.
* It is used to export the refl::attr:: standard attributes.
*/
namespace detail
{
namespace macro_exports
{
}
}
} // namespace refl
/**
* refl_impl is an internal namespace that should not be used by the users of refl-cpp.
*/
namespace refl_impl
{
/**
* Contains the generated metadata types.
* (i.e. type_info__)
*/
namespace metadata
{
// Import everyting from macro_exports here to make it visible in REFL_ macro context.
using namespace refl::detail::macro_exports;
/**
* The core reflection metadata type.
* type_info__ holds data for a type T.
*
* The non-specialized type_info__ type has a member typedef invalid_marker
* that can be used to detect it.
*
* Specializations of this type should provide all members of this
* generic definition, except invalid_marker.
*
* @typeparam <T> The reflected type.
*/
template <typename T>
struct type_info__
{
/** Used for detecting this non-specialized type_info__ instance. */
struct invalid_marker{};
/**
* This is a placeholder definition of which no type instances should be created.
*/
template <size_t, typename>
struct member;
/** The number of reflected members of the target type T. */
static constexpr size_t member_count{ 0 };
/** This is a placeholder definition which shold not be referenced by well-formed programs. */
static constexpr refl::const_string<0> name{ "" };
/** This is a placeholder definition which shold not be referenced by well-formed programs. */
static constexpr std::tuple<> attributes{ };
};
/**
* Specializes type_info__ so that a type's const-qualification is effectively discarded.
*/
template <typename T>
struct type_info__<const T> : public type_info__<T> {};
/**
* Specializes type_info__ so that a type's volatile-qualification is effectively discarded.
*/
template <typename T>
struct type_info__<volatile T> : public type_info__<T> {};
/**
* Specializes type_info__ so that a type's const-volatile-qualification is effectively discarded.
*/
template <typename T>
struct type_info__<const volatile T> : public type_info__<T> {};
} // namespace metadata
} // namespace refl_impl
namespace refl {
/**
* @brief Provides type-level operations for refl-cpp related use-cases.
*
* The refl::trait namespace provides type-level operations useful
* for compile-time metaprogramming.
*/
namespace trait
{
/**
* Removes all reference and cv-qualifiers from T.
* Equivalent to std::remove_cvref which is not currently
* available on all C++17 compilers.
*/
template <typename T>
struct remove_qualifiers
{
typedef std::remove_cv_t<std::remove_reference_t<T>> type;
};
/**
* Removes all reference and cv-qualifiers from T.
* Equivalent to std::remove_cvref_t which is not currently
* available on all C++17 compilers.
*/
template <typename T>
using remove_qualifiers_t = typename remove_qualifiers<T>::type;
namespace detail
{
/** SFIANE support for detecting whether there is a type_info__ specialization for T. */
template <typename T>
decltype(typename refl_impl::metadata::type_info__<remove_qualifiers_t<T>>::invalid_marker{}, std::false_type{}) is_reflectable_test(int);
/** SFIANE support for detecting whether there is a type_info__ specialization for T. */
template <typename T>
std::true_type is_reflectable_test(...);
}
/**
* Checks whether there is reflection metadata for the type T.
* Inherits from std::bool_constant<>
*
* @see REFL_TYPE
* @see REFL_AUTO
*/
template <typename T>
struct is_reflectable : decltype(detail::is_reflectable_test<T>(0))
{
};
/**
* Checks whether there is reflection metadata for the type T.
* Inherits from std::bool_constant<>
*
* @see refl::trait::is_reflectable
*/
template <typename T>
[[maybe_unused]] static constexpr bool is_reflectable_v{ is_reflectable<T>::value };
namespace detail
{
/** SFIANE support for detecting whether the type T supports member .begin() and .end() operations. */
template <typename U>
[[maybe_unused]] static auto is_container_test(int) -> decltype(std::declval<U>().begin(), std::declval<U>().end(), std::true_type{});
/** SFIANE support for detecting whether the type T supports member .begin() and .end() operations. */
template <typename U>
[[maybe_unused]] static std::false_type is_container_test(...);
}
/**
* Checks whether objects of the type T support member .begin() and .end() operations.
*/
template <typename T>
struct is_container : decltype(detail::is_container_test<T>(0))
{
};
/**
* Checks whether objects of the type T support member .begin() and .end() operations.
*/
template <typename T>
[[maybe_unused]] static constexpr bool is_container_v{ is_container<T>::value };
}
/**
* @brief Contains tag types denoting the different types of reflectable members.
*
* This namespace contains a number of empty types that correspond to
* the different member types that refl-cpp supports reflection over.
*/
namespace member
{
/**
* An empty type which is equivalent to refl::member_descriptor_base::member_type
* when the reflected member is a field.
*
* @see refl::descriptor::field_descriptor
*/
struct field {};
/**
* An empty type which is equivalent to refl::member_descriptor_base::member_type
* when the reflected member is a function.
*
* @see refl::descriptor::function_descriptor
*/
struct function {};
}
namespace util
{
/**
* Represents a compile-time list of types provided as variadic template parameters.
* type_list is an empty TrivialType. Instances of it can freely be created to communicate
* the list of represented types. type_lists support many standard operations that are
* implicitly available with ADL-lookup. type_list is used by refl-cpp mostly to represent
* the list of refl::field_descriptor, refl::function_descriptor specializations that
* allow the compile-time reflection of a type's members.
*
* @see refl::util::for_each
* @see refl::util::map_to_array
* @see refl::util::map_to_tuple
* @see refl::member_list
*
* # Examples
* ```
* for_each(type_list<int, float>(), [](auto) { ... });
* ```
*/
template <typename... Ts>
struct type_list
{
/** The number of types in this type_list */
static constexpr intptr_t size = sizeof...(Ts);
};
} // namespace util
// type_list is implicitly available in the top-level refl namespace.
using util::type_list;
namespace trait
{
namespace detail
{
template <size_t N, typename... Ts>
struct get;
template <size_t N>
struct get<N>
{
static_assert(N > 0, "Missing arguments list for get<N, Ts...>!");
};
template <size_t N, typename T, typename... Ts>
struct get<N, T, Ts...> : public get<N - 1, Ts...>
{
};
template <typename T, typename... Ts>
struct get<0, T, Ts...>
{
typedef T type;
};
template <size_t N, typename... Ts>
struct skip;
template <size_t N, typename T, typename... Ts>
struct skip<N, T, Ts...> : public skip<N - 1, Ts...>
{
static_assert(sizeof...(Ts) + 1 >= N, "Insufficient number of arguments!");
};
template <typename T>
struct skip<0, T>
{
typedef type_list<T> type;
};
template <typename... Ts>
struct skip<0, Ts...>
{
typedef type_list<Ts...> type;
};
}
template <size_t, typename>
struct get;
/**
* Provides a member typedef named type which is eqiuvalent to the
* N-th type of the provided type_list.
*/
template <size_t N, typename... Ts>
struct get<N, type_list<Ts...>> : detail::get<N, Ts...>
{
};
/**
* Equivalent to the N-th type of the provided type_list.
* @see get
*/
template <size_t N, typename TypeList>
using get_t = typename get<N, TypeList>::type;
template <size_t, typename>
struct skip;
/**
* Skips the first N types in the provided type_list.
* Provides a member typedef equivalent to the resuling type_list.
*/
template <size_t N, typename... Ts>
struct skip<N, type_list<Ts...>> : detail::skip<N, Ts...>
{
};
/**
* Skips the first N types in the provided type_list.
* @see skip
*/
template <size_t N, typename TypeList>
using skip_t = typename skip<N, TypeList>::type;
template <typename T>
struct as_type_list;
/**
* Provides a member typedef which is a type_list specialization with
* template type parameters equivalent to the type parameters of the provided
* type. The provided type must be a template specialization.
* Rerefence and CV-qualifiers are discarded.
*/
template <template <typename...> typename T, typename... Ts>
struct as_type_list<T<Ts...>>
{
typedef type_list<Ts...> type;
};
template <typename T>
struct as_type_list : public as_type_list<remove_qualifiers_t<T>>
{
};
/**
* A typedef for a type_list specialization with
* template type parameters equivalent to the type parameters of the provided
* type. The provided type must be a template specialization.
* Rerefence and CV-qualifiers are discarded.
* @see as_type_list
*/
template <typename T>
using as_type_list_t = typename as_type_list<T>::type;
} // namespace trait
/**
* @brief Contains the definitions of the built-in attributes
*
* Contains the definitions of the built-in attributes which
* are implicitly available in macro context as well as the
* attr::usage namespace which contains constraints
* for user-provieded attributes.
*
* # Examples
* ```
* REFL_TYPE(Point, debug(custom_printer))
* REFL_FIELD(x)
* REFL_FIELD(y)
* REFL_END
* ```
*/
namespace attr
{
/**
* @brief Contains a number of constraints applicable to refl-cpp attributes.
*
* Contains base types which create compile-time constraints
* that are verified by refl-cpp. These base-types must be inherited
* by custom attribute types.
*/
namespace usage
{
/**
* Specifies that an attribute type inheriting from this type can
* only be used with REFL_TYPE()
*/
struct type {};
/**
* Specifies that an attribute type inheriting from this type can
* only be used with REFL_FUNC()
*/
struct function {};
/**
* Specifies that an attribute type inheriting from this type can
* only be used with REFL_FIELD()
*/
struct field {};
/**
* Specifies that an attribute type inheriting from this type can
* only be used with REFL_FUNC or REFL_FIELD.
*/
struct member : public function, public field{};
/**
* Specifies that an attribute type inheriting from this type can
* only be used with any one of REFL_TYPE, REFL_FIELD, REFL_FUNC.
*/
struct any : public member, public type {};
}
} // namespace attr
namespace util
{
/**
* Ignores all parameters. Can take an optional template parameter
* specifying the return type of ignore. The return object is iniailized by {}.
*/
template <typename T = int, typename... Ts>
constexpr int ignore(Ts&&...) noexcept
{
return {};
}
/**
* Returns the input paratemeter as-is. Useful for expanding variadic
* template lists when only one arguments is known to be present.
*/
template <typename T>
constexpr decltype(auto) identity(T&& t) noexcept
{
return t;
}
/**
* Adds const to the input reference.
*/
template <typename T>
constexpr const T& make_const(const T& value) noexcept
{
return value;
}
/**
* Adds const to the input reference.
*/
template <typename T>
constexpr const T& make_const(T& value) noexcept
{
return value;
}
} // namespace util
namespace trait
{
template <typename>
struct first;
/**
* Accesses first type in the list.
*/
template <typename T, typename... Ts>
struct first<type_list<T, Ts...>>
{
using type = T;
};
/**
* Accesses last type in the list.
* @see last
*/
template <typename TypeList>
using first_t = typename first<TypeList>::type;
template <typename>
struct last;
/**
* Accesses last type in the list.
*/
template <typename T, typename... Ts>
struct last<type_list<T, Ts...>>
{
using type = get_t<sizeof...(Ts), type_list<T, Ts...>>;
};
/**
* Accesses last type in the list.
* @see last
*/
template <typename TypeList>
using last_t = typename last<TypeList>::type;
template <typename>
struct tail;
/**
* Returns all but the first element of the list.
*/
template <typename T, typename... Ts>
struct tail<type_list<T, Ts...>>
{
using type = type_list<Ts...>;
};
/**
* Returns all but the first element of the list.
* @see tail
*/
template <typename TypeList>
using tail_t = typename tail<TypeList>::type;
namespace detail
{
template <typename, typename>
struct init;
template <typename... Us, typename T>
struct init<type_list<Us...>, type_list<T>>
{
using type = type_list<Us...>;
};
template <typename... Us, typename T, typename... Ts>
struct init<type_list<Us...>, type_list<T, Ts...>>
{
using type = typename init<type_list<Us..., T>, type_list<Ts...>>::type;
};
};
/**
* Returns all but the last element of the list.
*/
template <typename TypeList>
struct init : detail::init<type_list<>, TypeList>
{
};
/**
* Returns all but the last element of the list.
* @see tail
*/
template <typename TypeList>
using init_t = typename init<TypeList>::type;
template <typename, typename>
struct append;
/**
* Appends a type to the list.
*/
template <typename T, typename... Ts>
struct append<T, type_list<Ts...>>
{
using type = type_list<Ts..., T>;
};
/**
* Appends a type to the list.
* @see prepend
*/
template <typename T, typename TypeList>
using append_t = typename append<T, TypeList>::type;
template <typename, typename>
struct prepend;
/**
* Prepends a type to the list.
*/
template <typename T, typename... Ts>
struct prepend<T, type_list<Ts...>>
{
using type = type_list<T, Ts...>;
};
/**
* Prepends a type to the list.
* @see prepend
*/
template <typename T, typename TypeList>
using prepend_t = typename prepend<T, TypeList>::type;
namespace detail
{
template <typename, typename>
struct reverse_impl;
template <typename... Us>
struct reverse_impl<type_list<Us...>, type_list<>>
{
using type = type_list<Us...>;
};
template <typename... Us, typename T, typename... Ts>
struct reverse_impl<type_list<Us...>, type_list<T, Ts...>>
{
using type = typename reverse_impl<type_list<T, Us...>, type_list<Ts...>>::type;
};
} // namespace detail
/**
* Reverses a list of types.
*/
template <typename TypeList>
struct reverse : detail::reverse_impl<type_list<>, TypeList>
{
};
/**
* Reverses a list of types.
* @see reverse
*/
template <typename TypeList>
using reverse_t = typename reverse<TypeList>::type;
template <typename, typename>
struct concat;
/**
* Concatenates two lists together.
*/
template <typename... Ts, typename... Us>
struct concat<type_list<Ts...>, type_list<Us...>>
{
using type = type_list<Ts..., Us...>;
};
/**
* Concatenates two lists together.
* @see concat
*/
template <typename Lhs, typename Rhs>
using concat_t = typename concat<Lhs, Rhs>::type;
namespace detail
{
template <template<typename> typename, typename>
struct filter_impl;