forked from vesoft-inc/fbthrift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflection.h
1922 lines (1839 loc) · 49.1 KB
/
reflection.h
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef THRIFT_FATAL_REFLECTION_H_
#define THRIFT_FATAL_REFLECTION_H_ 1
#include <cstdint>
#include <type_traits>
#include <utility>
#include <fatal/type/conditional.h>
#include <fatal/type/convert.h>
#include <fatal/type/data_member_getter.h>
#include <fatal/type/enum.h>
#include <fatal/type/foreach.h>
#include <fatal/type/get.h>
#include <fatal/type/get_type.h>
#include <fatal/type/list.h>
#include <fatal/type/pair.h>
#include <fatal/type/registry.h>
#include <fatal/type/search.h>
#include <fatal/type/traits.h>
#include <fatal/type/transform.h>
#include <fatal/type/variant_traits.h>
#include <folly/CppAttributes.h>
#include <folly/Traits.h>
#include <folly/functional/Invoke.h>
#include <thrift/lib/cpp2/FieldRef.h>
#include <thrift/lib/cpp2/FieldRefTraits.h>
#include <thrift/lib/cpp2/TypeClass.h>
#include <thrift/lib/cpp2/reflection/internal/reflection-inl-pre.h>
#if !defined(_MSC_VER) || _MSC_VER >= 2000
static_assert(__cplusplus >= 201703L, "__cplusplus >= 201703L");
#endif
namespace apache {
namespace thrift {
/**
* READ ME FIRST: this file is divided into sections for each specific
* reflection API.
*
* To quickly navigate this file, look for the string "SECTION: " without the
* quotes.
*
* Note that the compile-time reflection API depends on metadata that's
* generated by the Thrift compiler. In order to have this metadata available,
* there are three simple but necessary steps:
*
* - enable code generation for `cpp2` as the target language;
* - enable `reflection` as one of the `thrift_cpp2_options` flags;
* - include the appropriate file containing the metadata (say your module is
* defined in `some_dir/Module.thrift`, corresponding files can be included
* from `some_dir/gen-cpp2/`):
*
* - Module_fatal.h: general module metadata
* - Module_fatal_struct.h: metadata for all structures
* - Module_fatal_enum.h: metadata for all enumerations
* - Module_fatal_union.h: metadata for all variants (Thrift unions)
* - Module_fatal_types.h: convenience header that includes metadata for
* modules, structures, enumerations and variants
* - Module_fatal_all.h: convenience header that includes all the
* metadata generated by the Thrift compiler
*
* Thrift breaks the metadata up in several files to help reduce compilation
* times by including only what's needed.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
//////////////////////////////////////////////
// SECTION: IMPORTANT NOTES AND CONVENTIONS //
//////////////////////////////////////////////
/**
* NOTE ON COMPILE-TIME STRINGS: many strings found in the Thrift file are
* converted to compile-time strings in the form of a `fatal::sequence`
* of `char`.
*
* They are often represented as general C++ identifiers. Not all strings are
* directly representable as C++ identifiers though, given that not all
* characters are accepted as identifier names, only [_a-zA-Z0-9]. When that's
* the case, the invalid characters are replaced by an underscode (_).
*
* Names starting with numbers are prefixed with 's_'. For example, the string
* "42 is it" could be represented by the identifier 's_42_is_it'.
*
* Collisions are solved by appending a positive integer starting at 1 and
* growing by 1 per collision, in the order the identifiers appear. For
* instance, say there are three strings "a_", "a " and "a.". "a_" could be
* represented by the identifier 'a_' while "a " could be represented by 'a_1'
* and "a." could be 'a_2'.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
////////////////////////////////////////////
// SECTION: TYPE ALIASES AND ENUMERATIONS //
////////////////////////////////////////////
/**
* An alias to the type used by Thrift as a struct's field ID.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using field_id_t = std::int16_t;
/**
* An alias to the type used by Thrift as a type's unique identifier.
*
* NOTE: this is a legacy feature and should be avoided on new code.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using legacy_type_id_t = std::uint64_t;
/**
* Represents whether a field is required to be set in a given structure or not.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
enum class optionality {
/**
* Field is required.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
required,
/**
* Field is optional.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
optional,
/**
* Field is optional on the reading side but required on the writing side.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
required_of_writer,
/**
* Field has terse write semantics.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
terse,
};
/////////////////////////////
// SECTION: TYPE CLASS API //
/////////////////////////////
/**
* Returns the type class of a thrift class, which is either a structure, a
* variant, or an exception.
*
* Example:
*
* /////////////////////
* // MyModule.thrift //
* /////////////////////
* namespace cpp2 My.Namespace
*
* struct MyStruct {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* union MyUnion {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* enum MyEnum { a, b, c }
*
* typedef list<string> MyList;
*
* /////////////
* // foo.cpp //
* /////////////
*
* // yields `type_class::structure`
* using result1 = reflect_type_class_of_thrift_class<MyStruct>;
*
* // yields `type_class::variant`
* using result1 = reflect_type_class_of_thrift_class<MyUnion>;
*
* // yields `type_class::unknown`
* using result2 = reflect_type_class_of_thrift_class<MyEnum>;
*
* // yields `type_class::unknown`
* using result3 = reflect_type_class_of_thrift_class<MyList>;
*
* // yields `type_class::unknown`
* using result4 = reflect_type_class_of_thrift_class<void>;
*/
template <typename T>
using reflect_type_class_of_thrift_class =
typename detail::reflect_type_class_of_thrift_class_impl<T>::type;
/**
* Returns the type class of a thrift class, which is either a structure, a
* variant, or an exception, or of a thrift enum.
*
* Example:
*
* /////////////////////
* // MyModule.thrift //
* /////////////////////
* namespace cpp2 My.Namespace
*
* struct MyStruct {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* union MyUnion {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* enum MyEnum { a, b, c }
*
* typedef list<string> MyList;
*
* /////////////
* // foo.cpp //
* /////////////
*
* // yields `type_class::structure`
* using result1 = reflect_type_class_of_thrift_class_enum<MyStruct>;
*
* // yields `type_class::variant`
* using result1 = reflect_type_class_of_thrift_class_enum<MyUnion>;
*
* // yields `type_class::enumeration`
* using result2 = reflect_type_class_of_thrift_class_enum<MyEnum>;
*
* // yields `type_class::unknown`
* using result3 = reflect_type_class_of_thrift_class_enum<MyList>;
*
* // yields `type_class::unknown`
* using result4 = reflect_type_class_of_thrift_class_enum<void>;
*/
template <typename T>
using reflect_type_class_of_thrift_class_enum =
typename detail::reflect_type_class_of_thrift_class_enum_impl<T>::type;
/**
* Returns the type class of a thrift class, which is either a structure, a
* variant, or an exception, or of a thrift enum.
*
* Example:
*
* /////////////////////
* // MyModule.thrift //
* /////////////////////
* namespace cpp2 My.Namespace
*
* struct MyStruct {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* union MyUnion {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* enum MyEnum { a, b, c }
*
* typedef list<string> MyList;
*
* /////////////
* // foo.cpp //
* /////////////
*
* // yields `type_class::structure`
* using result1 = reflect_type_class_of_thrift_class_enum<MyStruct>;
*
* // yields `type_class::variant`
* using result1 = reflect_type_class_of_thrift_class_enum<MyUnion>;
*
* // yields `type_class::enumeration`
* using result2 = reflect_type_class_of_thrift_class_enum<MyEnum>;
*
* // yields `type_class::unknown`
* using result3 = reflect_type_class_of_thrift_class_enum<MyList>;
*
* // yields `type_class::unknown`
* using result4 = reflect_type_class_of_thrift_class_enum<void>;
*/
template <typename T>
using reflect_type_class_of_thrift_class_enum =
typename detail::reflect_type_class_of_thrift_class_enum_impl<T>::type;
/////////////////////////
// SECTION: MODULE API //
/////////////////////////
/**
* Holds reflection metadata for stuff defined in a Thrift file.
*
* NOTE: this class template is only intended to be instantiated by Thrift.
* Users should ignore the template parameters taken by it and focus simply on
* the members provided.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename Traits>
struct reflected_module {
/**
* The name.
*
* A `fatal::constant_sequence` (compile-time string) representing the module
* name.
*
* Example:
*
* // MyModule.thrift
*
* namespace cpp2 My.Namespace
* struct MyStruct {}
*
* // C++
*
* using info = reflect_module<My::Namespace::MyModule_tags::module>;
* using name = typename info::name;
* // yields `fatal::constant_sequence<
* // char, 'M', 'y', 'M', 'o', 'd', 'u', 'l', 'e',
* // >`
*/
using name = typename Traits::name;
/**
* The map from language to namespace.
*
* A `fatal::list` of `fatal::pair` where the key is a `fatal::sequence`
* (compile-time string) representing the language, associated with a
* compile-time string representing the namespace.
*
* Example:
*
* // MyModule.thrift
*
* namespace cpp My.NamespaceCpp
* namespace cpp2 My.Namespace
* namespace java My.Namespace
*
* struct MyStruct {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* // C++
*
* using info = reflect_module<My::Namespace::MyModule_tags::module>;
*
* FATAL_S(cpp, "cpp");
* FATAL_S(cpp2, "cpp2");
* FATAL_S(java, "java");
*
* // yields `fatal::sequence<
* // char,
* // 'M', 'y', ':', ':', 'N', 'a', 'm', 'e',
* // 's', 'p', 'a', 'c', 'e', 'C', 'p', 'p'
* // >`
* using result1 = fatal::get<info::namespaces, cpp>;
*
* // yields `fatal::sequence<
* // char, 'M', 'y', ':', ':', 'N', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e'
* // >`
* using result2 = fatal::get<info::namespaces, cpp2>;
*
* // yields `fatal::sequence<
* // char, 'M', 'y', '.', 'N', 'a', 'm', 'e', 's', 'p', 'a', 'c', 'e'
* // >`
* using result3 = fatal::get<info::namespaces, java>;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using namespaces = typename Traits::namespaces;
/**
* The list of enumerations with reflection metadata available.
*
* A `fatal::list` of `fatal::pair` where the first type is the one generated
* by the Thrift compiler for each enumeration, and the second one is the
* compile-time string (`fatal::sequence`) representing the enumeration name.
*
* Use `fatal::enum_traits` to retrieve reflection information for each
* enumeration (fatal/type/enum.h).
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using enums = typename Traits::enums;
/**
* The list of unions with reflection metadata available.
*
* A `fatal::list` of `fatal::pair` where the first type is the one generated
* by the Thrift compiler for each union, and the second one is the
* compile-time string (`fatal::sequence`) representing the union name.
*
* Use `fatal::variant_traits` to retrieve reflection information for each
* union (fatal/type/variant_traits.h).
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using unions = typename Traits::unions;
/**
* The list of structs with reflection metadata available.
*
* A `fatal::list` of `fatal::pair` where the first type is the one generated
* by the Thrift compiler for each struct, and the second one is the
* compile-time string (`fatal::sequence`) representing the struct name.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using structs = typename Traits::structs;
/**
* The list of services with reflection metadata available.
*
* A `fatal::list` of compile-time strings
* (`fatal::sequence`) representing each service name.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using services = typename Traits::services;
};
/**
* Retrieves reflection metadata (as a `reflected_module`) associated with the
* given reflection metadata tag.
*
* The Thrift compiler generates a reflection metadata tag for each Thrift file
* named `namespace::thriftfilename_tags::module`.
*
* If the given tag does not represent a Thrift module, or if there's no
* reflection metadata available for it, compilation will fail.
*
* See the documentation on `reflected_module` (above) for more information on
* the returned type.
*
* Example:
*
* /////////////////////
* // MyModule.thrift //
* /////////////////////
* namespace cpp2 My.Namespace
*
* enum MyEnum1 { a, b, c }
* enum MyEnum2 { x, y, x }
*
* //////////////////
* // whatever.cpp //
* //////////////////
* using info = reflect_module<My::Namespace::MyModule_tags::module>;
*
* // yields `2`
* auto result1 = info::enums::size;
*
* // fails compilation
* using result2 = reflect_module<void>;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename Tag>
using reflect_module = fatal::
registry_lookup<apache::thrift::detail::reflection_metadata_tag, Tag>;
/**
* Retrieves reflection metadata (as a `reflected_module`) associated with the
* given reflection metadata tag.
*
* The Thrift compiler generates a reflection metadata tag for each Thrift file
* named `namespace::thriftfilename_tags::module`.
*
* If the given tag does not represent a Thrift module, or if there's no
* reflection metadata available for it, `Default` will be returned.s If
* `Default` is not specified, it defaults to void.
*
* See the documentation on `reflected_module` (above) for more information on
* the returned type.
*
* Example:
*
* /////////////////////
* // MyModule.thrift //
* /////////////////////
* namespace cpp2 My.Namespace
*
* enum MyEnum1 { a, b, c }
* enum MyEnum2 { x, y, x }
*
* //////////////////
* // whatever.cpp //
* //////////////////
* using info = try_reflect_module<
* My::Namespace::MyModule_tags::module,
* void
* >;
*
* // yields `2`
* auto result1 = info::enums::size;
*
* // yields `void`
* using result2 = try_reflect_module<int>;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename Tag, typename Default = void>
using try_reflect_module = fatal::try_registry_lookup<
apache::thrift::detail::reflection_metadata_tag,
Tag,
Default>;
/**
* Tells whether the given type is a tag that represents the reflection metadata
* of the types declared in a Thrift file.
*
* Example:
*
* /////////////////////
* // MyModule.thrift //
* /////////////////////
* namespace cpp2 My.Namespace
*
* struct MyStruct {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* /////////////
* // foo.cpp //
* /////////////
*
* // yields `std::true_type`
* using result1 = is_reflectable_module<MyModule_tags::module>;
*
* // yields `std::false_type`
* using result2 = is_reflectable_module<MyStruct>;
*
* // yields `std::false_type`
* using result3 = is_reflectable_module<void>;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename T>
using is_reflectable_module = std::integral_constant<
bool,
!std::is_same<try_reflect_module<T, void>, void>::value>;
/**
* Gets the reflection metadata tag for the Thrift file where the type `T` is
* declared.
*
* The type `T` must be either a struct, enum or union.
*
* Example:
*
* // MyModule.thrift
*
* namespace cpp2 My.Namespace
*
* struct MyStruct {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* enum MyEnum { a, b, c }
*
* // C++
*
* // yields `My::Namespace::MyModule_tags::module`
* using result1 = reflect_module_tag<MyStruct>;
*
* // yields `My::Namespace::MyModule_tags::module`
* using result2 = reflect_module_tag<MyEnum>;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename T>
using reflect_module_tag =
typename apache::thrift::detail::reflect_module_tag_selector<
reflect_type_class_of_thrift_class_enum<T>,
T,
false>::type;
/**
* Tries to get the reflection metadata tag for the Thrift file where the type
* `T` is declared.
*
e:
*
* // MyModule.thrift
*
* namespace cpp2 My.Namespace
*
* struct MyStruct {
* 1: i32 a
* 2: string b
* 3: double c
* }
*
* enum MyEnum { a, b, c }
*
* // C++
*
* // yields `My::Namespace::MyModule_tags::module`
* using result1 = reflect_module_tag<MyStruct, void>;
*
* // yields `My::Namespace::MyModule_tags::module`
* using result2 = reflect_module_tag<MyEnum, void>;
*
* // yields `void`
* using result3 = reflect_module_tag<int, void>;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename T, typename Default = void>
using try_reflect_module_tag =
typename apache::thrift::detail::reflect_module_tag_selector<
reflect_type_class_of_thrift_class_enum<T>,
T,
true,
Default>::type;
/**
* Represents an annotation from `reflected_annotations::map`.
*
* For the examples below, consider code generated for this Thrift file:
*
* struct Foo {
* 1: i32 z
* } (
* a = '{not a valid format}',
* b = '{"valid": "format", "foo": 10, "bar": true, "x": [-5, 0, 5]}',
* c = '"hello"'
* }
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename Key, typename Value>
struct annotation {
/**
* Represents the annotation key as a compile-time string, in the form of a
* `fatal::sequence` of type `char`.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using key = Key;
/**
* Represents the annotation value as a compile-time string, in the form of a
* `fatal::sequence` of type `char`.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using value = Value;
};
/**
* Holds reflection metadata for annotations.
*
* For the examples below, consider code generated for this Thrift file:
*
* /////////////////////
* // MyModule.thrift //
* /////////////////////
* namespace cpp2 My.Namespace
*
* struct MyStruct {
* 1: i32 a
* 2: string b
* 3: double c
* } (
* some.annotation = "some value",
* another.annotation = "another value",
* )
*
* NOTE: this class template is only intended to be instantiated by Thrift.
* Users should ignore the template parameters taken by it and focus simply on
* the members provided.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename Metadata>
struct reflected_annotations {
/**
* An implementation defined type that provides the names for each annotation
* key as a member type alias named after the key.
*
* These type aliases are used as the key for the `map` member.
*
* Look for 'NOTE ON COMPILE-TIME STRINGS' for how the strings are converted
* to C++ identifiers - caveat: instead of using the order they appear as the
* precedence for collision resolution, it uses lexicographical order of the
* keys.
*
* Example:
*
* using annotations = reflect_struct<MyStruct>::annotations;
*
* // yields `fatal::sequence<char,
* // 's', 'o', 'm', 'e', '.',
* // 'a', 'n', 'n', 'o', 't', 'a', 't', 'i', 'o', 'n'
* // >`
* using result1 = annotations::keys::some_annotation;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using keys = typename Metadata::keys;
/**
* An implementation defined type that provides the names for each annotation
* value as a member type alias named after the key.
*
* These type aliases are used as the value for the `map` member.
*
* Look for 'NOTE ON COMPILE-TIME STRINGS' for how the strings are converted
* to C++ identifiers - caveat: instead of using the order they appear as the
* precedence for collision resolution, it uses lexicographical order of the
* keys.
*
* Example:
*
* using annotations = reflect_struct<MyStruct>::annotations;
*
* // yields `fatal::sequence<char,
* // 's', 'o', 'm', 'e', ' ', 'v', 'a', 'l', 'u', 'e'
* // >`
* using result1 = annotations::values::some_annotation;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using values = typename Metadata::values;
/**
* A list of `annotation` representing the annotations declared in the Thrift
* file, sorted by keys.
*
* See the `annotation` type for more information.
*
* Example:
*
* // yields an instantiation of the `reflected_annotations` template
* using annotations = reflect_struct<MyStruct>::annotations;
*
* FATAL_S(key, "another.annotation");
*
* // yields `fatal::sequence<char,
* // 'a', 'n', 'o', 't', 'h', 'e', 'r', ' ', 'v', 'a', 'l', 'u', 'e'
* // >`
* using result1 = fatal::get<annotations::map, key>::value;
*
* // yields `fatal::sequence<char,
* // 's', 'o', 'm', 'e', ' ', 'v', 'a', 'l', 'u', 'e'
* // >`
* using result2 = fatal::get<
* annotations::map,
* annotations::keys::some_annotation
* >::value;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using map = typename Metadata::map;
};
////////////////////////////
// SECTION: STRUCTURE API //
////////////////////////////
/**
* Holds reflection metadata for a given struct.
*
* NOTE: this class template is only intended to be instantiated by Thrift.
* Users should ignore the template parameters taken by it and focus simply on
* the members provided.
*
* For the examples below, consider code generated for this Thrift file:
*
* /////////////////////
* // MyModule.thrift //
* /////////////////////
* namespace cpp2 My.Namespace
*
* struct MyStruct {
* 1: i32 a
* 2: string b
* 3: double c (
* member.note = "member text",
* another.member.note = "another member text",
* )
* } (
* some.annotation = "some value",
* another.annotation = "another value",
* )
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename Traits>
struct reflected_struct {
/**
* A type alias for the struct itself.
*
* Example:
*
* using info = reflect_struct<MyStruct>;
*
* // yields `MyStruct`
* using result = info::type;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using type = typename Traits::type;
/**
* A compile-time string representing the struct name.
*
* Example:
*
* using info = reflect_struct<MyStruct>;
*
* // yields `fatal::sequence<
* // char,
* // 'M', 'y', 'S', 't', 'r', 'u', 'c', 't'
* // >`
* using result = info::name;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using name = typename Traits::name;
/**
* The reflection metadata tag for the Thrift file where this structure is
* declared.
*
* Example:
*
* using info = reflect_struct<MyStruct>;
*
* // yields `My::Namespace::MyModule_tags::module`
* using result = info::module;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using module = typename Traits::metadata::module;
/**
* An implementation defined type template that provides the appropriate
* `reflected_struct_data_member` for each data member as a member type alias
* with the same name.
*
* These type aliases are used as the type mapped by the `members` member
* offered by the `reflected_struct` class.
*
* An optional transform can be specified, which will be applied on top of the
* members' `reflected_struct_data_member`.
*
* Example:
*
* using info = reflect_struct<MyStruct>;
*
* // yields `fatal::sequence<char, 'a'>`
* using result1 = info::member::a::name;
*
* // yields `std::int32_t`
* using result2 = info::member::a::type;
*
* // yields `1`
* using result3 = info::member::a::id::value;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using member = typename Traits::member;
/**
* A `fatal::list` of `reflected_struct_data_member` representing each member.
*
* See the documentation for `reflected_struct_data_member` (below) for more
* information on its members.
*
* Example:
*
* struct visitor {
* template <typename MemberInfo>
* void operator ()(fatal::tag<MemberInfo>) {
* using name = typename MemberInfo::name;
* std::cout << "- member: " << fatal::z_data<name>() << '\n';
* }
* };
*
* using info = reflect_struct<MyStruct>;
*
* // prints the names of all members of `MyStruct`
* fatal::foreach<info::members>(visitor());
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using members = typename Traits::members;
/**
* An instantiation of `reflected_annotations` representing the annotations
* declared for this type in the Thrift file.
*
* Example:
*
* using info = reflect_struct<MyStruct>;
*
* // yields `fatal::sequence<char,
* // 'a', 'n', 'o', 't', 'h', 'e', 'r', ' ', 'v', 'a', 'l', 'u', 'e'
* // >`
* using result = info::annotations::values::another_annotation;
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using annotations = typename Traits::metadata::annotations;
/**
* An implementation defined type that provides the annotations for each
* member of the struct. Each member's annotations are represented by an
* instance of `reflected_annotations` named after the member itself.
*
* Example:
*
* using info = reflect_struct<MyStruct>;
*
* // yields `fatal::sequence<char,
* // 'm', 'e', 'm', 'b', 'e', 'r', ' ', 't', 'e', 'x', 't'
* // >`
* using result1 = info::members_annotations::c::values::member_note
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using members_annotations = typename Traits::members_annotations;
/**
* A unique identifier generated by thrift for this structure.
*
* NOTE: this is a legacy feature and should be avoided on new code.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
using legacy_id = typename Traits::metadata::legacy_id;
};
/**
* Holds reflection metadata for a given struct's data member.
*
* NOTE: this class template is only intended to be instantiated by Thrift.
* Users should ignore the template parameters taken by it and focus simply on
* the members provided.
*
* @author: Marcelo Juchem <marcelo@fb.com>
*/
template <typename Traits>
struct reflected_struct_data_member {
/**
* A `fatal::sequence` of `char` representing the data member name as
* a compile-time string.
*
* Example:
*
* // MyModule.thrift
*
* namespace cpp2 My.Namespace
*
* struct MyStruct {
* 1: i32 fieldA
* 2: string fieldB
* 3: double fieldC
* }
*