-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflection.hpp
2311 lines (2072 loc) · 113 KB
/
reflection.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
/// \mainpage A single-include library for reflection in C++ programs
/// <pre style="line-height:1.0">
/// ██
/// ██
/// ██
/// ██
/// ██ ██ ██ ██ ██ ██
/// ██ ██ ██ ██ ██ ██
/// ██ ██ ██ ██ ██
/// ██ ██ ██████ ██ ██
/// ██ ██ ██ ██
/// ██ ██ ██ ██ ██ ██
/// ██ ██ ██ ██ ██ ██
/// ██ ██
/// ██ ██
/// </pre>
///
/// This library allows you to write and use reflective classes in C++
/// ##Features
/// + Support for writing reflective classes
/// + Accessing member fields and functions through reflectors
/// + Templates for record classes
///
/// See the README.md file from this file's git repository
///
/// \author Johannes Milczewski
/// \copyright
/// \parblock
/// <pre>
/// Copyright Johannes Milczewski 2020 - 2021.
/// Distributed under the Boost Software License Version 1.0.
///
/// 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.
/// </pre>
/// \endparblock
/// \version 1.0
#ifndef OCEAD_REFLECTION_HPP
#define OCEAD_REFLECTION_HPP
/// \defgroup reflection Reflection
/// \brief Everything needed for writing reflective classes
/// \defgroup options Options
/// \brief Macros for configuring this header
/// \defgroup reflector Reflectors
/// \brief Symbols for working with reflective classes
/// \defgroup record Records
/// \brief Symbols for working with records
#include <cassert>
#include <functional>
#include <iostream>
#include <type_traits>
//region Reflection options
#ifdef _DOXYGEN_
/// \brief Disables reflection if defined
/// \ingroup options
#define OCEAD_NO_REFLECTION
#endif
#ifndef OCEAD_REFLECTION_COUNTER_TYPE
#include <cstddef>
/// \brief A constexpr type
/// that can store program-wide unique IDs for reflective symbols
///
/// Default is <code>std::size_t</code>
/// \ingroup options
#define OCEAD_REFLECTION_COUNTER_TYPE std::size_t
#endif
#ifndef OCEAD_REFLECTION_ID_TYPE
#include <cstddef>
/// \brief A constexpr type
/// that can store class-wide unique IDs for reflective symbols
///
/// Default is <code>std::size_t</code>
/// \ingroup options
#define OCEAD_REFLECTION_ID_TYPE std::size_t
#endif
#ifndef OCEAD_REFLECTION_TYPEID_TYPE
#include <cstddef>
/// \brief A constexpr type
/// that can store unique hashes or descriptors for C/C++ types
///
/// Default is <code>char const *</code>
/// \ingroup options
#define OCEAD_REFLECTION_TYPEID_TYPE char const *
#endif
#ifndef OCEAD_REFLECTION_IDENTIFIER_MACRO
/// \brief Macro for obtaining a unique hash or descriptor for C/C++ types
/// \param type A valid C/C++ typename
///
/// Default is <code>ocead::reflection::typename_hash(#type)</code>
/// \ingroup options
#define OCEAD_REFLECTION_IDENTIFIER_MACRO(type) #type
#endif
#ifndef OCEAD_REFLECTION_NAME_TYPE
/// \brief A constexpr type
/// that can store the names of reflective symbols
///
/// Default is <code>char const *</code>
/// \ingroup options
#define OCEAD_REFLECTION_NAME_TYPE char const *
#endif
#ifndef OCEAD_REFLECTION_CONTAINER_TEMPLATE
/// \brief Macro for modifying the type of reflective symbols
///
/// Default is <code>ocead::reflection::type_identity</code>
/// \ingroup options
#define OCEAD_REFLECTION_CONTAINER_TEMPLATE ocead::reflection::type_identity
#endif
#ifndef OCEAD_REFLECTION_ACCESSOR_TYPE
/// \brief A C/C++ type that can hold pointers to accessor functions
///
/// Default is <code>ocead::reflection::accessor_t</code>
/// \ingroup options
#define OCEAD_REFLECTION_ACCESSOR_TYPE ocead::reflection::accessor_t
#endif
#ifndef OCEAD_REFLECTION_CONST_ACCESSOR_TYPE
/// \brief A C/C++ type that can hold pointers to const accessor functions
///
/// Default is <code>ocead::reflection::const_accessor_t</code>
/// \ingroup options
#define OCEAD_REFLECTION_CONST_ACCESSOR_TYPE ocead::reflection::const_accessor_t
#endif
#ifndef OCEAD_REFLECTION_INVOKER_TYPE
/// \brief A C/C++ type that can hold pointers to invoker functions
///
/// Default is <code>ocead::reflection::invoker_t</code>
/// \ingroup options
#define OCEAD_REFLECTION_INVOKER_TYPE ocead::reflection::invoker_t
#endif
#ifndef OCEAD_REFLECTION_CONST_INVOKER_TYPE
/// \brief A C/C++ type that can hold pointers to const invoker functions
///
/// Default is <code>ocead::reflection::invoker_t</code>
/// \ingroup options
#define OCEAD_REFLECTION_CONST_INVOKER_TYPE ocead::reflection::const_invoker_t
#endif
#ifndef OCEAD_REFLECTION_SHARED_PTR_TYPE
#include <memory>
/// \brief A C++ smart pointer type,
/// that stores the contained objects destructor function
///
/// Default is <code>std::shared_ptr<void></code>
/// \ingroup options
#define OCEAD_REFLECTION_SHARED_PTR_TYPE std::shared_ptr<void>
#endif
#ifndef OCEAD_REFLECTION_SYMBOL_DESCRIPTOR_TYPE
/// \brief A type that describes single reflective symbols
///
/// Default is <code>ocead::reflection::symbol_descriptor</code>
/// \ingroup options
#define OCEAD_REFLECTION_SYMBOL_DESCRIPTOR_TYPE \
ocead::reflection::symbol_descriptor
#endif
#ifndef OCEAD_REFLECTION_REFLECTOR_MAP_TYPE
#include <map>
#include <utility>
/// \brief A C++ template type that satisfies <i>AssociativeContainer</i>
///
/// Default is <code>std::map</code>
/// \ingroup options
#define OCEAD_REFLECTION_REFLECTOR_MAP_TYPE std::map
#endif
#ifndef OCEAD_REFLECTION_CLASS_DESCRIPTOR_TYPE
/// \brief A type that describes a reflective class
///
/// Default is <code>ocead::reflection::class_descriptor</code>
/// \ingroup options
#define OCEAD_REFLECTION_CLASS_DESCRIPTOR_TYPE \
ocead::reflection::class_descriptor
#endif
#ifndef OCEAD_REFLECTION_DEFAULT_PROPERTY_VISIBILITY
/// \brief The default visibility for reflective properties
///
/// Default is <code>private:</code>
/// \ingroup options
#define OCEAD_REFLECTION_DEFAULT_PROPERTY_VISIBILITY private:
#endif
#ifndef OCEAD_REFLECTION_DEFAULT_FUNCTION_VISIBILITY
/// \brief The default visibility for reflective function
///
/// Default is <code>public:</code>
/// \ingroup options
#define OCEAD_REFLECTION_DEFAULT_FUNCTION_VISIBILITY public:
#endif
#ifndef OCEAD_REFLECTION_DEFAULT_CONSTRUCTOR_VISIBILITY
/// \brief The default visibility for reflective constructor
///
/// Default is <code>public:</code>
/// \ingroup options
#define OCEAD_REFLECTION_DEFAULT_CONSTRUCTOR_VISIBILITY public:
#endif
#ifndef OCEAD_REFLECTION_DEFAULT_DESTRUCTOR_VISIBILITY
/// \brief The default visibility for reflective destructors
///
/// Default is <code>public:</code>
/// \ingroup options
#define OCEAD_REFLECTION_DEFAULT_DESTRUCTOR_VISIBILITY public:
#endif
#ifndef OCEAD_REFLECTION_ADD_PREFIX
/// \brief Macro that is applied to every additional symbol
/// this library declares in reflective classes
/// \param symbol Name to apply this macro on
///
/// Default is <code>_</code><code>reflection_##symbol</code>
/// \ingroup options
#define OCEAD_REFLECTION_ADD_PREFIX(symbol) _reflection_##symbol
#endif
//endregion
//region Record forwarding declarations
namespace ocead::reflection {
template<typename RCTp, OCEAD_REFLECTION_ID_TYPE ID>
struct record_piece;
}
//endregion
//region Reflection macros
//region Pre-symbol
#define OCEAD_REFLECTION_PRE_SYMBOL(prop_type, id, name, num) \
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(property_id)<num, _> { \
static constexpr OCEAD_REFLECTION_ID_TYPE value = id; \
}; \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(property_name)<num, _> { \
static constexpr OCEAD_REFLECTION_NAME_TYPE value = #name; \
}; \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(type)<id, _> { \
using type = prop_type; \
}; \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(property_typeid)<num, _> { \
static constexpr OCEAD_REFLECTION_TYPEID_TYPE value = \
OCEAD_REFLECTION_IDENTIFIER_MACRO(prop_type); \
};
//endregion
//region Begin
#ifndef OCEAD_NO_REFLECTION
#define OCEAD_REFLECTION_BEGIN_REFLECTIVE(class_name) \
private: \
static constexpr OCEAD_REFLECTION_COUNTER_TYPE \
OCEAD_REFLECTION_ADD_PREFIX(BEGIN_ID) = __COUNTER__ + 1; \
\
using OCEAD_REFLECTION_ADD_PREFIX(class_type) = class_name; \
\
template<OCEAD_REFLECTION_COUNTER_TYPE num, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(symbol_type) {}; \
\
template<OCEAD_REFLECTION_ID_TYPE id, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(type) {}; \
\
template<OCEAD_REFLECTION_ID_TYPE id, typename = void> \
using OCEAD_REFLECTION_ADD_PREFIX(type_t) = \
typename OCEAD_REFLECTION_ADD_PREFIX(type)<id>::type; \
\
template<OCEAD_REFLECTION_COUNTER_TYPE num, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(property_id) {}; \
\
template<OCEAD_REFLECTION_COUNTER_TYPE num, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(property_name) {}; \
\
template<OCEAD_REFLECTION_COUNTER_TYPE num, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(property_typeid) {}; \
\
template<OCEAD_REFLECTION_COUNTER_TYPE num, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(property_accessor) {}; \
\
template<OCEAD_REFLECTION_COUNTER_TYPE num, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(property_const_accessor) {}; \
\
template<OCEAD_REFLECTION_COUNTER_TYPE num, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(symbol_descriptor) {}; \
\
template<OCEAD_REFLECTION_ID_TYPE id, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(constructor) { \
void *operator()(void *args) { return nullptr; } \
}; \
\
template<OCEAD_REFLECTION_ID_TYPE id, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(placement_constructor) { \
void *operator()(void *ptr, void *args) { return nullptr; } \
}; \
\
template<OCEAD_REFLECTION_ID_TYPE id, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(shared_constructor) { \
OCEAD_REFLECTION_SHARED_PTR_TYPE operator()(void *args) { \
return OCEAD_REFLECTION_SHARED_PTR_TYPE(); \
} \
}; \
\
template<OCEAD_REFLECTION_ID_TYPE id, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(destructor) { \
void operator()(void *ptr) { \
delete static_cast<OCEAD_REFLECTION_ADD_PREFIX(class_type) *>( \
ptr); \
} \
}; \
\
struct OCEAD_REFLECTION_ADD_PREFIX(deleter) { \
void operator()(void *&ptr) { \
delete static_cast<OCEAD_REFLECTION_ADD_PREFIX(class_type) *>( \
ptr); \
ptr = nullptr; \
} \
}; \
\
static void OCEAD_REFLECTION_ADD_PREFIX(delete)(void *&ptr) { \
return OCEAD_REFLECTION_ADD_PREFIX(deleter)()(ptr); \
} \
\
template<OCEAD_REFLECTION_COUNTER_TYPE num, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(signature_info) {}; \
\
template<OCEAD_REFLECTION_ID_TYPE ID, typename = void> \
struct OCEAD_REFLECTION_ADD_PREFIX(static_accessor); \
\
template<OCEAD_REFLECTION_ID_TYPE ID> \
auto OCEAD_REFLECTION_ADD_PREFIX(static_access)() \
const->std::add_lvalue_reference_t<std::add_const_t< \
typename OCEAD_REFLECTION_ADD_PREFIX(type) < ID, \
void>::type> > { \
return OCEAD_REFLECTION_ADD_PREFIX(static_accessor)<ID, void>()( \
*this); \
}
#else
#define OCEAD_REFLECTION_NOTHING
#define OCEAD_REFLECTION_BEGIN_REFLECTIVE(...) OCEAD_REFLECTION_NOTHING
#endif
//endregion
//region Property
#ifndef OCEAD_NO_REFLECTION
#define OCEAD_REFLECTION_PROPERTY_DETAIL(access, prop_type, id, name, num) \
private: \
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(symbol_type)<num, _> { \
static constexpr auto value = ocead::reflection::symbol_t( \
ocead::reflection::symbol_t::SYMBOL_PROPERTY | \
(std::is_const_v<prop_type> \
? ocead::reflection::symbol_t::SYMBOL_CONST \
: ocead::reflection::symbol_t()) | \
(std::is_reference_v<prop_type> \
? ocead::reflection::symbol_t::SYMBOL_REFERENCE \
: ocead::reflection::symbol_t())); \
}; \
\
OCEAD_REFLECTION_PRE_SYMBOL(prop_type, id, name, num) \
\
\
private: \
static void *OCEAD_REFLECTION_ADD_PREFIX(access_##name)(void *ptr) { \
if constexpr (std::is_const_v<decltype(name)>) { \
return nullptr; \
} else { \
using unconst = std::remove_const_t<decltype(name)>; \
return &const_cast<unconst &>( \
static_cast<OCEAD_REFLECTION_ADD_PREFIX(class_type) *>( \
ptr) \
->name); \
} \
} \
\
static void const *OCEAD_REFLECTION_ADD_PREFIX(access_##name##_const)( \
void const *ptr) { \
return &static_cast<OCEAD_REFLECTION_ADD_PREFIX(class_type) const *>( \
ptr) \
->name; \
} \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(property_accessor)<num, _> { \
static constexpr OCEAD_REFLECTION_ACCESSOR_TYPE value = \
(OCEAD_REFLECTION_ACCESSOR_TYPE) &OCEAD_REFLECTION_ADD_PREFIX( \
class_type):: \
OCEAD_REFLECTION_ADD_PREFIX(access_##name); \
}; \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(property_const_accessor)<num, _> { \
static constexpr OCEAD_REFLECTION_CONST_ACCESSOR_TYPE value = \
&OCEAD_REFLECTION_ADD_PREFIX(class_type):: \
OCEAD_REFLECTION_ADD_PREFIX(access_##name##_const); \
}; \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(symbol_descriptor)<num, _> { \
static constexpr OCEAD_REFLECTION_SYMBOL_DESCRIPTOR_TYPE value = \
OCEAD_REFLECTION_SYMBOL_DESCRIPTOR_TYPE{ \
num, \
OCEAD_REFLECTION_ADD_PREFIX(symbol_type) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_id) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_typeid) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_name) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_accessor) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_const_accessor) < \
num > ::value}; \
}; \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(static_accessor)<id, _> { \
auto &operator()(OCEAD_REFLECTION_ADD_PREFIX(class_type) const &ref) { \
return ref.name; \
}; \
}; \
\
access alignas(std::max(alignof(prop_type), std::size_t(1))) \
OCEAD_REFLECTION_CONTAINER_TEMPLATE<prop_type>::type name
#else
#define OCEAD_REFLECTION_PROPERTY_DETAIL(access, prop_type, id, name, num) \
access prop_type name
#endif
#define OCEAD_REFLECTION_DEFAULT_VISIBLE_PROPERTY(type, id, name, num) \
OCEAD_REFLECTION_PROPERTY_DETAIL( \
OCEAD_REFLECTION_DEFAULT_PROPERTY_VISIBILITY, type, id, name, num)
#define OCEAD_REFLECTION_GET_PROPERTY_MACRO(_1, _2, _3, _4, _5, NAME, ...) NAME
#define OCEAD_REFLECTION_PROPERTY(...) \
OCEAD_REFLECTION_GET_PROPERTY_MACRO( \
__VA_ARGS__, OCEAD_REFLECTION_PROPERTY_DETAIL, \
OCEAD_REFLECTION_DEFAULT_VISIBLE_PROPERTY, UNUSED, UNUSED, \
UNSUSED) \
(__VA_ARGS__)
//endregion
//region Function
#ifndef OCEAD_NO_REFLECTION
#define OCEAD_REFLECTION_FUNCTION_DETAIL(access, prop_type, id, name, sig, \
num) \
\
private: \
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(signature_info)<num, _> { \
using type = ocead::typing::signature_info<prop_type( \
OCEAD_REFLECTION_ADD_PREFIX(class_type)::*) sig>; \
}; \
\
private: \
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(symbol_type)<num, _> { \
static constexpr ocead::reflection::symbol_t value = \
ocead::reflection::symbol_t( \
ocead::reflection::symbol_t::SYMBOL_FUNCTION | \
(std::is_const_v<typename OCEAD_REFLECTION_ADD_PREFIX( \
signature_info) < num, \
_>::type::class_type > \
? ocead::reflection::symbol_t::SYMBOL_CONST \
: ocead::reflection::symbol_t())); \
}; \
\
OCEAD_REFLECTION_PRE_SYMBOL(prop_type, id, name, num) \
\
static void OCEAD_REFLECTION_ADD_PREFIX(invoke_##name##_##num)( \
void *_ret, \
std::conditional_t<std::is_const_v<OCEAD_REFLECTION_ADD_PREFIX( \
signature_info) < num, \
void>::type::class_type>, \
void const *, void * > _obj, void *_args) { \
using signature_info = \
OCEAD_REFLECTION_ADD_PREFIX(signature_info<num>)::type; \
using RTp = signature_info::return_type; \
using PRTp = ocead::typing::simplify<RTp>::type; \
auto &obj = *static_cast<signature_info::class_type *>(_obj); \
auto &args = *static_cast<signature_info::arguments_type *>(_args); \
auto fun = static_cast<signature_info::ptr_type>( \
&OCEAD_REFLECTION_ADD_PREFIX(class_type)::name); \
if (_ret) { \
auto *ret = static_cast<PRTp *>(_ret); \
new (ret) PRTp(ocead::typing::simplify<RTp>()( \
ocead::typing::apply_return<int>( \
fun, \
std::tuple_cat(std::make_tuple(std::ref(obj)), \
args)))); \
} else { \
ocead::typing::apply_return<int>( \
fun, \
std::tuple_cat(std::make_tuple(std::ref(obj)), args)); \
} \
} \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(symbol_descriptor)<num, _> { \
static constexpr OCEAD_REFLECTION_SYMBOL_DESCRIPTOR_TYPE value = \
OCEAD_REFLECTION_SYMBOL_DESCRIPTOR_TYPE{ \
num, \
OCEAD_REFLECTION_ADD_PREFIX(symbol_type) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_id) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_typeid) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_name) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(invoke_##name##_##num)}; \
}; \
\
access prop_type name sig
#else
#define OCEAD_REFLECTION_FUNCTION_DETAIL(access, prop_type, id, name, sig, \
num) \
access prop_type name sig
#endif
#define OCEAD_REFLECTION_DEFAULT_VISIBLE_FUNCTION(type, id, name, sig, num) \
OCEAD_REFLECTION_FUNCTION_DETAIL( \
OCEAD_REFLECTION_DEFAULT_FUNCTION_VISIBILITY, type, id, name, sig, \
num)
#define OCEAD_REFLECTION_GET_FUNCTION_MACRO(_1, _2, _3, _4, _5, _6, NAME, ...) \
NAME
#define OCEAD_REFLECTION_FUNCTION(...) \
OCEAD_REFLECTION_GET_FUNCTION_MACRO( \
__VA_ARGS__, OCEAD_REFLECTION_FUNCTION_DETAIL, \
OCEAD_REFLECTION_DEFAULT_VISIBLE_FUNCTION, UNUSED, UNUSED, UNUSED, \
UNUSED) \
(__VA_ARGS__)
//endregion
//region Constructor
#ifndef OCEAD_NO_REFLECTION
#define OCEAD_REFLECTION_CONSTRUCTOR_DETAIL(access, id, name, sig, num) \
private: \
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(symbol_type)<num, _> { \
static constexpr ocead::reflection::symbol_t value = \
ocead::reflection::symbol_t::SYMBOL_CONSTRUCTOR; \
}; \
\
OCEAD_REFLECTION_PRE_SYMBOL(name, id, name, num) \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(constructor)<id, _> { \
void *operator()(void *args) { \
using Args = ocead::typing::signature_info< \
std::function<void sig>>::arguments_type; \
return ocead::make_new_from_tuple<name>( \
*static_cast<Args *>(args)); \
} \
}; \
\
static void *OCEAD_REFLECTION_ADD_PREFIX(construct_##num)(void *args) { \
return OCEAD_REFLECTION_ADD_PREFIX(constructor<id>)()(args); \
} \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(placement_constructor)<id, _> { \
void *operator()(void *ptr, void *args) { \
using Args = ocead::typing::signature_info< \
std::function<void sig>>::arguments_type; \
return ocead ::make_new_from_tuple<name>( \
static_cast<name *>(ptr), *static_cast<Args *>(args)); \
} \
}; \
\
static void *OCEAD_REFLECTION_ADD_PREFIX(placement_construct_##num)( \
void *ptr, void *args) { \
return OCEAD_REFLECTION_ADD_PREFIX(placement_constructor<id>)()(ptr, \
args); \
} \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(shared_constructor)<id, _> { \
OCEAD_REFLECTION_SHARED_PTR_TYPE operator()(void *args) { \
using Deleter = OCEAD_REFLECTION_ADD_PREFIX(deleter); \
auto *ptr = OCEAD_REFLECTION_ADD_PREFIX(constructor<id>)()(args); \
return std::shared_ptr<void>(ptr, Deleter()); \
} \
}; \
\
static OCEAD_REFLECTION_SHARED_PTR_TYPE OCEAD_REFLECTION_ADD_PREFIX( \
shared_construct_##num)(void *args) { \
return OCEAD_REFLECTION_ADD_PREFIX(shared_constructor<id>)()(args); \
} \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(symbol_descriptor)<num, _> { \
static constexpr OCEAD_REFLECTION_SYMBOL_DESCRIPTOR_TYPE value = \
OCEAD_REFLECTION_SYMBOL_DESCRIPTOR_TYPE{ \
num, \
OCEAD_REFLECTION_ADD_PREFIX(symbol_type) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_id) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_typeid) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_name) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(construct_##num), \
OCEAD_REFLECTION_ADD_PREFIX( \
placement_construct_##num), \
OCEAD_REFLECTION_ADD_PREFIX(shared_construct_##num)}; \
}; \
\
access name sig
#else
#define OCEAD_REFLECTION_CONSTRUCTOR_DETAIL(access, id, name, sig, num) \
access name sig
#endif
#define OCEAD_REFLECTION_DEFAULT_VISIBLE_CONSTRUCTOR(id, name, sig, num) \
OCEAD_REFLECTION_CONSTRUCTOR_DETAIL( \
OCEAD_REFLECTION_DEFAULT_CONSTRUCTOR_VISIBILITY, id, name, sig, \
num)
#define OCEAD_REFLECTION_GET_CONSTRUCTOR_MACRO(_1, _2, _3, _4, _5, NAME, ...) \
NAME
#define OCEAD_REFLECTION_CONSTRUCTOR(...) \
OCEAD_REFLECTION_GET_CONSTRUCTOR_MACRO( \
__VA_ARGS__, OCEAD_REFLECTION_CONSTRUCTOR_DETAIL, \
OCEAD_REFLECTION_DEFAULT_VISIBLE_CONSTRUCTOR, UNUSED, UNUSED, \
UNUSED) \
(__VA_ARGS__)
//endregion
//region Destructor
#ifndef OCEAD_NO_REFLECTION
#define OCEAD_REFLECTION_DESTRUCTOR_DETAIL(access, id, name, num) \
private: \
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(symbol_type)<num, _> { \
static constexpr ocead::reflection::symbol_t value = \
ocead::reflection::symbol_t::SYMBOL_DESTRUCTOR; \
}; \
\
OCEAD_REFLECTION_PRE_SYMBOL(name, id, name, num) \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(destructor)<id, _> { \
void operator()(void *ptr) { \
static_cast<OCEAD_REFLECTION_ADD_PREFIX(class_type) *>(ptr) \
->~name(); \
} \
}; \
\
static void OCEAD_REFLECTION_ADD_PREFIX(destroy_##num)(void *ptr) { \
return OCEAD_REFLECTION_ADD_PREFIX(destructor<id>)()(ptr); \
} \
\
template<typename _> \
struct OCEAD_REFLECTION_ADD_PREFIX(symbol_descriptor)<num, _> { \
static constexpr OCEAD_REFLECTION_SYMBOL_DESCRIPTOR_TYPE value = \
OCEAD_REFLECTION_SYMBOL_DESCRIPTOR_TYPE{ \
num, \
OCEAD_REFLECTION_ADD_PREFIX(symbol_type) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_id) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_typeid) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(property_name) < num > \
::value, \
OCEAD_REFLECTION_ADD_PREFIX(destroy_##num), \
OCEAD_REFLECTION_ADD_PREFIX(delete)}; \
}; \
\
access ~name()
#else
#define OCEAD_REFLECTION_DESTRUCTOR_DETAIL(access, id, name, num) access ~name()
#endif
#define OCEAD_REFLECTION_DEFAULT_VISIBLE_DESTRUCTOR(id, name, num) \
OCEAD_REFLECTION_DESTRUCTOR_DETAIL( \
OCEAD_REFLECTION_DEFAULT_DESTRUCTOR_VISIBILITY, id, name, num)
#define OCEAD_REFLECTION_GET_DESTRUCTOR_MACRO(_1, _2, _3, _4, NAME, ...) NAME
#define OCEAD_REFLECTION_DESTRUCTOR(...) \
OCEAD_REFLECTION_GET_DESTRUCTOR_MACRO( \
__VA_ARGS__, OCEAD_REFLECTION_DESTRUCTOR_DETAIL, \
OCEAD_REFLECTION_DEFAULT_VISIBLE_DESTRUCTOR, UNUSED, UNUSED) \
(__VA_ARGS__)
//endregion
//region End
#ifdef __cpp_rtti
#define OCEAD_REFLECTION_RTTI_REFLECTOR \
public: \
const OCEAD_REFLECTION_CLASS_DESCRIPTOR_TYPE &reflect() const override { \
static const OCEAD_REFLECTION_CLASS_DESCRIPTOR_TYPE refl{ \
OCEAD_REFLECTION_CLASS_DESCRIPTOR_TYPE ::reflect< \
OCEAD_REFLECTION_ADD_PREFIX(class_type)>()}; \
return refl; \
}
#else
#define OCEAD_REFLECTION_RTTI_REFLECTOR
#endif
#ifndef OCEAD_NO_REFLECTION
#define OCEAD_REFLECTION_END_REFLECTIVE() \
private: \
static constexpr OCEAD_REFLECTION_COUNTER_TYPE \
OCEAD_REFLECTION_ADD_PREFIX(END_ID) = __COUNTER__; \
\
static OCEAD_REFLECTION_COUNTER_TYPE OCEAD_REFLECTION_ADD_PREFIX( \
property_count)() { \
return OCEAD_REFLECTION_ADD_PREFIX(END_ID) - \
OCEAD_REFLECTION_ADD_PREFIX(BEGIN_ID); \
} \
\
static const OCEAD_REFLECTION_CLASS_DESCRIPTOR_TYPE & \
OCEAD_REFLECTION_ADD_PREFIX(class_descriptor)() { \
static const OCEAD_REFLECTION_CLASS_DESCRIPTOR_TYPE refl{ \
OCEAD_REFLECTION_CLASS_DESCRIPTOR_TYPE::reflect< \
OCEAD_REFLECTION_ADD_PREFIX(class_type)>()}; \
return refl; \
} \
\
OCEAD_REFLECTION_RTTI_REFLECTOR \
\
friend struct ocead::reflection::symbol_descriptor; \
friend class ocead::reflection::class_descriptor; \
friend const OCEAD_REFLECTION_CLASS_DESCRIPTOR_TYPE & \
ocead::reflection::reflect<OCEAD_REFLECTION_ADD_PREFIX(class_type)>(); \
template<typename CTp, OCEAD_REFLECTION_ID_TYPE ID> \
friend struct ocead::reflection::record_piece
#else
#define OCEAD_REFLECTION_END_REFLECTIVE()
#endif
//endregion
//endregion
//region Static for
#ifndef OCEAD_STATIC_FOR_HPP
#define OCEAD_STATIC_FOR_HPP
#include <utility>
namespace ocead {
namespace detail {
template<typename I, I Start, I End, template<I> class Func,
typename... Args>
struct static_for_inner {
constexpr void operator()(Args &&...args) {
Func<Start>()(args...);
static_for_inner<I, Start + 1, End, Func, Args...>()(
std::forward<Args>(args)...);
}
};
template<typename I, I End, template<I> class Func, typename... Args>
struct static_for_inner<I, End, End, Func, Args...> {
constexpr void operator()(Args &&...) {}
};
}// namespace detail
/// \brief Produces a compile-time loop over a range of incrementable values
/// \tparam I Incrementing type
/// \tparam Start Start value
/// \tparam End End value
/// \tparam Func Function type to call in the loop
/// \tparam Args Argument types to call <code>Func</code> with
/// \param args Argument to call <code>Func</code> with
template<typename I, I Start, I End, template<I> class Func,
typename... Args>
constexpr void static_for(Args... args) {
static_assert(Start <= End, "Start must be less than or equal to End");
detail::static_for_inner<I, Start, End, Func, Args &&...>()(
std::forward<Args>(args)...);
}
};// namespace ocead
#endif//OCEAD_STATIC_FOR_HPP
//endregion
//region Reflection structures
namespace ocead {
/// \brief Converts enum values into the corresponding underlying values,
/// returns non-enum values as is
/// \tparam ETp Maybe-enum type
/// \param val Value to maybe convert
/// \return The underlying value
template<typename ETp,
std::enable_if_t<!std::is_enum_v<ETp>, void *> = nullptr>
constexpr ETp underlying(ETp val) {
return val;
}
/// \brief Converts enum values into the corresponding underlying values,
/// returns non-enum values as is
/// \tparam ETp Maybe-enum type
/// \param val Value to maybe convert
/// \return The underlying value
template<typename ETp,
std::enable_if_t<std::is_enum_v<ETp>, void *> = nullptr>
constexpr auto underlying(ETp const &val) ->
typename std::underlying_type<ETp>::type {
return typename std::underlying_type<ETp>::type(val);
}
/// \brief Converts enum values into the corresponding underlying values,
/// returns non-enum values as is
/// \tparam ETp Maybe-enum type
/// \param val Value to maybe convert
/// \return The underlying value
template<typename ETp,
std::enable_if_t<std::is_enum_v<ETp>, void *> = nullptr>
constexpr auto underlying(ETp &&val) ->
typename std::underlying_type<ETp>::type {
return typename std::underlying_type<ETp>::type(val);
}
namespace detail {
template<class T, class Tuple, std::size_t... I>
constexpr T *make_new_from_tuple_impl(T *ptr, Tuple &&t,
std::index_sequence<I...>) {
return new (ptr) T(std::get<I>(std::forward<Tuple>(t))...);
}
template<class T, class Tuple, std::size_t... I>
constexpr T *make_new_from_tuple_impl(Tuple &&t,
std::index_sequence<I...>) {
return new T(std::get<I>(std::forward<Tuple>(t))...);
}
}// namespace detail
/// \brief Constructs an instance of <code>T</code>
/// from a tuple of arguments with placement <code>new</code>
/// \tparam T Type
/// \tparam Tuple Arguments tuple type
/// \param ptr Address where the instance shall be constructed
/// \param t Arguments
/// \return Pointer to the created instance
template<typename T, typename Tuple>
constexpr T *make_new_from_tuple(T *ptr, Tuple &&t) {
return detail::make_new_from_tuple_impl<T>(
ptr, std::forward<Tuple>(t),
std::make_index_sequence<
std::tuple_size_v<std::remove_reference_t<Tuple>>>{});
}
/// \brief Constructs an instance of <code>T</code>
/// from a tuple of arguments with <code>new</code>
/// \tparam T Type
/// \tparam Tuple Arguments tuple type
/// \param t Arguments
/// \return Pointer to the created instance
template<typename T, typename Tuple>
constexpr T *make_new_from_tuple(Tuple &&t) {
return detail::make_new_from_tuple_impl<T>(
std::forward<Tuple>(t),
std::make_index_sequence<
std::tuple_size_v<std::remove_reference_t<Tuple>>>{});
}
namespace typing {
/// \brief Checks if a function returns void
/// \tparam FTp Function type
/// \tparam Args Argument types
template<typename FTp, typename... Args>
struct returns_void {
static constexpr bool value =
std::is_void_v<std::invoke_result_t<FTp, Args...>>;
};
/// \brief Checks if a function returns void
/// \tparam FTp Function type
/// \tparam Args Argument types
template<typename FTp, typename... Args>
constexpr auto returns_void_v = returns_void<FTp, Args...>::value;
/// \brief Variant of <code>std::apply</code>
/// that always returns a value
/// \tparam RTp Return type, if <code>FTp(Args ...)</code>
/// returns <code>void</code>
/// \tparam FTp Function type
/// \tparam Args Argument types
/// \param fun Function pointer to call
/// \param args Arguments, with which the function is called
/// \return <code>RTp{}</code> if <code>FTp(Args ...)</code> returns
/// void, otherwise the value returned by <code>FTp(Args ...)</code>
template<typename RTp, typename FTp, typename... Args,
std::enable_if_t<returns_void_v<FTp, Args...>, void *> =
nullptr>
auto apply_return(FTp &&fun, std::tuple<Args...> &&args) {
std::apply<FTp, std::tuple<Args...>>(
fun, std::forward<std::tuple<Args...>>(args));
return RTp();
}
/// \brief Variant of <code>std::apply</code>
/// that always returns a value
/// \tparam RTp Return type, if <code>FTp(Args ...)</code>
/// returns <code>void</code>
/// \tparam FTp Function type
/// \tparam Args Argument types
/// \param fun Function pointer to call
/// \param args Arguments, with which the function is called
/// \return <code>RTp{}</code> if <code>FTp(Args ...)</code> returns
/// void, otherwise the value returned by <code>FTp(Args ...)</code>
template<typename RTp, typename FTp, typename... Args,
std::enable_if_t<!returns_void_v<FTp, Args...>, void *> =
nullptr>
auto apply_return(FTp &&fun, std::tuple<Args...> &&args) {
return std::apply<FTp, std::tuple<Args...>>(
fun, std::forward<std::tuple<Args...>>(args));
}
template<typename...>
struct signature_info {};
template<typename RTp, typename... Args>
struct signature_info<RTp (*)(Args...)> {
using ptr_type = RTp (*)(Args...);
using fun_type = std::function<RTp(Args...)>;
using return_type = RTp;
using always_return_type =
std::conditional<std::is_void<RTp>::value, int, RTp>;
using class_type = void;
using arguments_type = std::tuple<Args...>;
static constexpr auto arguments_count = sizeof...(Args);
};
template<typename RTp, typename CTp, typename... Args>
struct signature_info<RTp (CTp::*)(Args...)> {
using ptr_type = RTp (CTp::*)(Args...);
using fun_type = std::function<RTp(Args...)>;
using return_type = RTp;
using always_return_type =
std::conditional<std::is_void<RTp>::value, int, RTp>;
using class_type = CTp;
using arguments_type = std::tuple<Args...>;
static constexpr auto arguments_count = sizeof...(Args);
};