1010
1111#pragma once
1212
13+ #include < fboss/agent/if/gen-cpp2/common_types.h>
1314#include < fboss/thrift_cow/nodes/Types.h>
14- #include < thrift/lib/cpp2/reflection/reflection .h>
15+ #include < thrift/lib/cpp2/gen/module_types_h .h>
1516#include < type_traits>
1617
1718namespace facebook ::fboss::thrift_cow {
1819
19- // helper struct to read Thrift annotation allow_skip_thrift_cow
20- template <typename T, typename T2 = void >
21- struct read_annotation_allow_skip_thrift_cow {
22- static constexpr bool value = false ;
23- };
24-
25- // fatal respsents true and false as
26- // "constexpr" char sequence of "1" and "0", respectively
27- using fatal_true = fatal::sequence<char , ' 1' >;
28-
29- // need a little template specialization magic since annotation values are void
30- // when nothing is set. without this we can't try to pull out
31- // annotation allow_skip_thrift_cow on structs that don't have annotatiosn
32- template <>
33- struct read_annotation_allow_skip_thrift_cow <void > {
34- static constexpr bool value = false ;
35- };
36-
37- FATAL_S (allow_skip_thrift_cow_annotation, " allow_skip_thrift_cow" );
38-
39- template <typename Annotations>
40- struct read_annotation_allow_skip_thrift_cow <
41- Annotations,
42- typename std::enable_if_t <std::is_same_v<
43- typename Annotations::keys::allow_skip_thrift_cow,
44- allow_skip_thrift_cow_annotation>>> {
45- static constexpr bool value = std::is_same<
46- typename Annotations::values::allow_skip_thrift_cow,
47- fatal_true>::value;
48- };
49-
50- template <typename TC, typename TType, typename = void >
51- struct read_type_annotation_allow_skip_thrift_cow {
52- static constexpr bool value = false ;
53- };
54-
55- template <typename TC, typename TType>
56- struct read_type_annotation_allow_skip_thrift_cow <
57- TC,
58- TType,
59- typename std::enable_if_t <
60- std::is_same_v<TC, apache::thrift::type_class::structure>>> {
61- using annotations =
62- typename apache::thrift::reflect_struct<TType>::annotations;
63- static constexpr bool value =
64- read_annotation_allow_skip_thrift_cow<annotations>::value;
65- };
20+ // Concept to check if a Thrift field is annotated with AllowSkipThriftCow at
21+ // compile time
22+ template <typename TStruct, typename FieldId>
23+ concept field_allow_skip_thrift_cow = apache::thrift::is_thrift_class_v<
24+ TStruct> &&
25+ apache::thrift::has_field_annotation<facebook::fboss::AllowSkipThriftCow,
26+ TStruct,
27+ FieldId>();
28+
29+ // Concept to check if a Thrift type is annotated with AllowSkipThriftCow at
30+ // compile time
31+ template <typename TType>
32+ concept type_allow_skip_thrift_cow = apache::thrift::is_thrift_class_v<TType> &&
33+ apache::thrift::
34+ has_struct_annotation<facebook::fboss::AllowSkipThriftCow, TType>();
6635
6736template <typename TType, bool EnableHybridStorage = false >
6837struct ThriftStructResolver {
@@ -125,10 +94,7 @@ struct ConvertToNodeTraits<
12594 apache::thrift::type_class::structure,
12695 TType> {
12796 static constexpr bool skipThriftCow =
128- EnableHybridStorage &&
129- read_type_annotation_allow_skip_thrift_cow<
130- apache::thrift::type_class::structure,
131- TType>::value;
97+ EnableHybridStorage && type_allow_skip_thrift_cow<TType>;
13298 using default_type = std::conditional_t <
13399 skipThriftCow,
134100 ThriftHybridNode<apache::thrift::type_class::structure, TType>,
@@ -232,19 +198,25 @@ struct ConvertToImmutableNodeTraits {
232198template <typename Derived, typename Name>
233199struct ResolveMemberType : std::false_type {};
234200
235- template <typename Derived, typename Member, bool EnableHybridStorage>
201+ template <
202+ typename Struct,
203+ typename Derived,
204+ typename Member,
205+ bool EnableHybridStorage>
236206struct StructMemberTraits {
237207 using member = Member;
238- using traits = StructMemberTraits<Derived, Member, EnableHybridStorage>;
208+ using traits =
209+ StructMemberTraits<Struct, Derived, Member, EnableHybridStorage>;
239210 using name = typename Member::name;
240211 using ttype = typename Member::type;
241212 using tc = typename Member::type_class;
242213
243214 // read member annotations
244- using member_annotations = typename Member::annotations;
245215 static constexpr bool allowSkipThriftCow = EnableHybridStorage &&
246- (read_annotation_allow_skip_thrift_cow<member_annotations>::value ||
247- read_type_annotation_allow_skip_thrift_cow<tc, ttype>::value);
216+ (field_allow_skip_thrift_cow<
217+ Struct,
218+ apache::thrift::field_id<Member::id::value>> ||
219+ type_allow_skip_thrift_cow<ttype>);
248220
249221 // need to resolve here
250222 using default_type = std::conditional_t <
@@ -263,10 +235,10 @@ struct StructMemberTraits {
263235 default_type>;
264236};
265237
266- template <typename Derived, bool EnableHybridStorage>
238+ template <typename Struct, typename Derived, bool EnableHybridStorage>
267239struct ExtractStructFields {
268240 template <typename T>
269- using apply = StructMemberTraits<Derived, T, EnableHybridStorage>;
241+ using apply = StructMemberTraits<Struct, Derived, T, EnableHybridStorage>;
270242};
271243
272244template <typename Member, bool EnableHybridStorage = false >
0 commit comments