Skip to content

Commit e1f919f

Browse files
evanjzoumeta-codesync[bot]
authored andcommitted
Simplify decorator helper structure
Summary: Simplify the templates using claude. Reviewed By: yfeldblum Differential Revision: D94697062 fbshipit-source-id: d45acf03dcf3fe609337a21155ffa4ee93b799d2
1 parent df10854 commit e1f919f

2 files changed

Lines changed: 83 additions & 214 deletions

File tree

third-party/thrift/src/thrift/lib/cpp2/async/processor/HandlerCallback.h

Lines changed: 59 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,41 @@ struct IsUniquePtr<std::unique_ptr<T>> {
4141
constexpr static bool value = true;
4242
};
4343

44+
/// Extracts the decorator arg from a compound response type that has a
45+
/// ResponseType typedef and .response member.
46+
template <CompoundResponseType T>
47+
struct CompoundResponseExtractor {
48+
using RawResponse = typename T::ResponseType;
49+
using InnerResponse = typename inner_type<RawResponse>::type;
50+
using ArgType = typename DecoratorArgType<InnerResponse>::type;
51+
52+
static constexpr ArgType extract(const T& compound) {
53+
if constexpr (IsUniquePtr<RawResponse>::value) {
54+
return *compound.response;
55+
} else {
56+
return compound.response;
57+
}
58+
}
59+
};
60+
61+
/// DecoratorAfterCallback for compound types with a .response member.
4462
template <typename T>
45-
concept ResponseIsUniquePtr = IsUniquePtr<T>::value;
63+
struct CompoundDecoratorAfterCallback
64+
: public DecoratorAfterCallbackWithResult<
65+
CompoundDecoratorAfterCallback<T>,
66+
const T&,
67+
typename CompoundResponseExtractor<T>::ArgType> {
68+
static constexpr auto extractDecoratorArg(const T& result) {
69+
return CompoundResponseExtractor<T>::extract(result);
70+
}
71+
};
72+
73+
/// DecoratorAfterCallback for types with no decorator-visible response.
74+
template <typename T>
75+
struct VoidDecoratorAfterCallback : public DecoratorAfterCallbackWithResult<
76+
VoidDecoratorAfterCallback<T>,
77+
const T&,
78+
void> {};
4679

4780
} // namespace detail
4881

@@ -268,23 +301,16 @@ struct TileAndResponse<InteractionIf, void> {
268301

269302
template <typename InteractionIf, typename Response>
270303
struct InteractionInnerResponseHelper {
271-
using DecoratorArgType = typename detail::DecoratorArgType<Response>::type;
304+
using InnerResp = typename detail::inner_type<Response>::type;
305+
using DecoratorArgType = typename detail::DecoratorArgType<InnerResp>::type;
272306

273307
static constexpr DecoratorArgType extractInnerResponse(
274308
const TileAndResponse<InteractionIf, Response>& result) {
275-
return result.response;
276-
}
277-
};
278-
279-
template <typename InteractionIf, typename Response>
280-
struct InteractionInnerResponseHelper<
281-
InteractionIf,
282-
std::unique_ptr<Response>> {
283-
using DecoratorArgType = typename detail::DecoratorArgType<Response>::type;
284-
285-
static constexpr DecoratorArgType extractInnerResponse(
286-
const TileAndResponse<InteractionIf, std::unique_ptr<Response>>& result) {
287-
return *result.response;
309+
if constexpr (detail::IsUniquePtr<Response>::value) {
310+
return *result.response;
311+
} else {
312+
return result.response;
313+
}
288314
}
289315
};
290316

@@ -293,32 +319,14 @@ struct InteractionInnerResponseHelper<InteractionIf, void> {
293319
using DecoratorArgType = void;
294320
};
295321

296-
template <typename InteractionIf, typename Response, typename StreamItem>
297-
struct InteractionInnerResponseHelper<
298-
InteractionIf,
299-
ResponseAndServerStream<Response, StreamItem>> {
300-
using DecoratorArgType = typename detail::DecoratorArgType<Response>::type;
322+
template <typename InteractionIf, detail::CompoundResponseType T>
323+
struct InteractionInnerResponseHelper<InteractionIf, T> {
324+
using Extractor = detail::CompoundResponseExtractor<T>;
325+
using DecoratorArgType = typename Extractor::ArgType;
301326

302327
static constexpr DecoratorArgType extractInnerResponse(
303-
const TileAndResponse<
304-
InteractionIf,
305-
ResponseAndServerStream<Response, StreamItem>>& result) {
306-
return result.response.response;
307-
}
308-
};
309-
310-
template <typename InteractionIf, typename Response, typename StreamItem>
311-
struct InteractionInnerResponseHelper<
312-
InteractionIf,
313-
ResponseAndServerStream<std::unique_ptr<Response>, StreamItem>> {
314-
using DecoratorArgType = typename detail::DecoratorArgType<Response>::type;
315-
316-
static constexpr DecoratorArgType extractInnerResponse(
317-
const TileAndResponse<
318-
InteractionIf,
319-
ResponseAndServerStream<std::unique_ptr<Response>, StreamItem>>&
320-
result) {
321-
return *result.response.response;
328+
const TileAndResponse<InteractionIf, T>& result) {
329+
return Extractor::extract(result.response);
322330
}
323331
};
324332

@@ -327,47 +335,6 @@ struct InteractionInnerResponseHelper<InteractionIf, ServerStream<StreamItem>> {
327335
using DecoratorArgType = void;
328336
};
329337

330-
template <
331-
typename InteractionIf,
332-
typename Response,
333-
typename SinkElement,
334-
typename FinalResponse>
335-
struct InteractionInnerResponseHelper<
336-
InteractionIf,
337-
ResponseAndSinkConsumer<Response, SinkElement, FinalResponse>> {
338-
using DecoratorArgType = typename detail::DecoratorArgType<Response>::type;
339-
340-
static constexpr DecoratorArgType extractInnerResponse(
341-
const TileAndResponse<
342-
InteractionIf,
343-
ResponseAndSinkConsumer<Response, SinkElement, FinalResponse>>&
344-
result) {
345-
return result.response.response;
346-
}
347-
};
348-
349-
template <
350-
typename InteractionIf,
351-
typename Response,
352-
typename SinkElement,
353-
typename FinalResponse>
354-
struct InteractionInnerResponseHelper<
355-
InteractionIf,
356-
ResponseAndSinkConsumer<
357-
std::unique_ptr<Response>,
358-
SinkElement,
359-
FinalResponse>> {
360-
using DecoratorArgType = typename detail::DecoratorArgType<Response>::type;
361-
362-
static constexpr DecoratorArgType extractInnerResponse(
363-
const TileAndResponse<
364-
InteractionIf,
365-
ResponseAndSinkConsumer<Response, SinkElement, FinalResponse>>&
366-
result) {
367-
return *result.response.response;
368-
}
369-
};
370-
371338
template <typename InteractionIf, typename SinkElement, typename FinalResponse>
372339
struct InteractionInnerResponseHelper<
373340
InteractionIf,
@@ -586,14 +553,6 @@ void HandlerCallback<T>::doResult(InputType r) {
586553
}
587554

588555
namespace detail {
589-
template <class S>
590-
struct inner_type {
591-
using type = S;
592-
};
593-
template <class S>
594-
struct inner_type<std::unique_ptr<S>> {
595-
using type = S;
596-
};
597556

598557
template <typename T>
599558
struct HandlerCallbackHelper {
@@ -634,36 +593,15 @@ template <typename Response, typename StreamItem>
634593
struct HandlerCallbackHelper<ResponseAndServerStream<Response, StreamItem>>
635594
: public HandlerCallbackHelperServerStream<
636595
ResponseAndServerStream<Response, StreamItem>> {
637-
struct DecoratorAfterCallback
638-
: public DecoratorAfterCallbackWithResult<
639-
DecoratorAfterCallback,
640-
const ResponseAndServerStream<Response, StreamItem>&,
641-
typename DecoratorArgType<
642-
typename inner_type<Response>::type>::type> {
643-
using ArgType =
644-
typename DecoratorArgType<typename inner_type<Response>::type>::type;
645-
646-
template <typename InnerResponseType>
647-
static constexpr ArgType extractDecoratorArg(
648-
const ResponseAndServerStream<InnerResponseType, StreamItem>& result) {
649-
return result.response;
650-
}
651-
652-
template <ResponseIsUniquePtr InnerResponseType>
653-
static constexpr ArgType extractDecoratorArg(
654-
const ResponseAndServerStream<InnerResponseType, StreamItem>& result) {
655-
return *result.response;
656-
}
657-
};
596+
using DecoratorAfterCallback = CompoundDecoratorAfterCallback<
597+
ResponseAndServerStream<Response, StreamItem>>;
658598
};
659599

660600
template <typename StreamItem>
661601
struct HandlerCallbackHelper<ServerStream<StreamItem>>
662602
: public HandlerCallbackHelperServerStream<ServerStream<StreamItem>> {
663-
struct DecoratorAfterCallback : public DecoratorAfterCallbackWithResult<
664-
DecoratorAfterCallback,
665-
const ServerStream<StreamItem>&,
666-
void> {};
603+
using DecoratorAfterCallback =
604+
VoidDecoratorAfterCallback<ServerStream<StreamItem>>;
667605
};
668606

669607
template <typename SinkInputType>
@@ -684,47 +622,16 @@ struct HandlerCallbackHelper<
684622
ResponseAndSinkConsumer<Response, SinkElement, FinalResponse>>
685623
: public HandlerCallbackHelperSink<
686624
ResponseAndSinkConsumer<Response, SinkElement, FinalResponse>> {
687-
struct DecoratorAfterCallback
688-
: public DecoratorAfterCallbackWithResult<
689-
DecoratorAfterCallback,
690-
const ResponseAndSinkConsumer<
691-
Response,
692-
SinkElement,
693-
FinalResponse>&,
694-
typename DecoratorArgType<
695-
typename inner_type<Response>::type>::type> {
696-
using ArgType =
697-
typename DecoratorArgType<typename inner_type<Response>::type>::type;
698-
699-
template <typename InnerResponseType>
700-
static constexpr ArgType extractDecoratorArg(
701-
const ResponseAndSinkConsumer<
702-
InnerResponseType,
703-
SinkElement,
704-
FinalResponse>& result) {
705-
return result.response;
706-
}
707-
708-
template <ResponseIsUniquePtr InnerResponseType>
709-
static constexpr ArgType extractDecoratorArg(
710-
const ResponseAndSinkConsumer<
711-
InnerResponseType,
712-
SinkElement,
713-
FinalResponse>& result) {
714-
return *result.response;
715-
}
716-
};
625+
using DecoratorAfterCallback = CompoundDecoratorAfterCallback<
626+
ResponseAndSinkConsumer<Response, SinkElement, FinalResponse>>;
717627
};
718628

719629
template <typename SinkElement, typename FinalResponse>
720630
struct HandlerCallbackHelper<SinkConsumer<SinkElement, FinalResponse>>
721631
: public HandlerCallbackHelperSink<
722632
SinkConsumer<SinkElement, FinalResponse>> {
723-
struct DecoratorAfterCallback
724-
: public DecoratorAfterCallbackWithResult<
725-
DecoratorAfterCallback,
726-
const SinkConsumer<SinkElement, FinalResponse>&,
727-
void> {};
633+
using DecoratorAfterCallback =
634+
VoidDecoratorAfterCallback<SinkConsumer<SinkElement, FinalResponse>>;
728635
};
729636

730637
template <typename In, typename Out>
@@ -738,10 +645,8 @@ struct HandlerCallbackHelper<StreamTransformation<In, Out>> {
738645
return cob(ctx, ex, std::move(input));
739646
}
740647

741-
struct DecoratorAfterCallback : public DecoratorAfterCallbackWithResult<
742-
DecoratorAfterCallback,
743-
const StreamTransformation<In, Out>&,
744-
void> {};
648+
using DecoratorAfterCallback =
649+
VoidDecoratorAfterCallback<StreamTransformation<In, Out>>;
745650
};
746651

747652
template <typename Response, typename In, typename Out>
@@ -756,29 +661,8 @@ struct HandlerCallbackHelper<
756661
return cob(ctx, ex, std::move(input));
757662
}
758663

759-
struct DecoratorAfterCallback
760-
: public DecoratorAfterCallbackWithResult<
761-
DecoratorAfterCallback,
762-
const ResponseAndStreamTransformation<Response, In, Out>&,
763-
typename DecoratorArgType<
764-
typename inner_type<Response>::type>::type> {
765-
using ArgType =
766-
typename DecoratorArgType<typename inner_type<Response>::type>::type;
767-
768-
template <typename InnerResponseType>
769-
static constexpr ArgType extractDecoratorArg(
770-
const ResponseAndStreamTransformation<InnerResponseType, In, Out>&
771-
result) {
772-
return result.response;
773-
}
774-
775-
template <ResponseIsUniquePtr InnerResponseType>
776-
static constexpr ArgType extractDecoratorArg(
777-
const ResponseAndStreamTransformation<InnerResponseType, In, Out>&
778-
result) {
779-
return *result.response;
780-
}
781-
};
664+
using DecoratorAfterCallback = CompoundDecoratorAfterCallback<
665+
ResponseAndStreamTransformation<Response, In, Out>>;
782666
};
783667

784668
} // namespace detail

third-party/thrift/src/thrift/lib/cpp2/server/DecoratorArgType.h

Lines changed: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ template <typename T>
3737
concept SmallTriviallyCopyable =
3838
std::is_trivially_copyable_v<T> && sizeof(T) <= 64;
3939

40+
/// Unwraps std::unique_ptr<T> to T; identity for non-unique_ptr types.
41+
template <typename T>
42+
struct inner_type {
43+
using type = T;
44+
};
45+
template <typename T>
46+
struct inner_type<std::unique_ptr<T>> {
47+
using type = T;
48+
};
49+
50+
/// Detects compound response types that have a ResponseType typedef and a
51+
/// .response member (e.g. ResponseAndServerStream, ResponseAndSinkConsumer,
52+
/// ResponseAndStreamTransformation).
53+
template <typename T>
54+
concept CompoundResponseType = requires { typename T::ResponseType; };
55+
4056
/**
4157
* DecoratorArgType is a helper class for determining the type of the arg
4258
* passed to the decorator. For small trivially copyable types <= 64 bytes,
@@ -54,52 +70,21 @@ struct DecoratorArgType<T> {
5470
};
5571

5672
/**
57-
* DecoratorReturnType is a helper class for determining the type of the arg
58-
* passed to the decorator after_ methods. For small trivially copyable types
59-
* <= 64 bytes, we just pass by value and allow the copy to happen. For
60-
* everything else, we pass by const reference.
73+
* DecoratorReturnType determines the type of the arg passed to the decorator
74+
* after_ methods. Delegates to DecoratorArgType for pass-by-value vs
75+
* const-ref logic. For compound response types, recursively unwraps to the
76+
* inner response type.
6177
*/
6278
template <typename T>
6379
struct DecoratorReturnType {
64-
using type = const T&;
80+
using type = typename DecoratorArgType<T>::type;
6581
};
6682

67-
template <SmallTriviallyCopyable T>
68-
struct DecoratorReturnType<T> {
69-
using type = T;
83+
template <CompoundResponseType T>
84+
struct DecoratorReturnType<T>
85+
: DecoratorReturnType<typename inner_type<typename T::ResponseType>::type> {
7086
};
7187

72-
template <typename Response, typename StreamItem>
73-
struct DecoratorReturnType<ResponseAndServerStream<Response, StreamItem>>
74-
: public DecoratorReturnType<Response> {};
75-
76-
template <typename Response, typename StreamItem>
77-
struct DecoratorReturnType<
78-
ResponseAndServerStream<std::unique_ptr<Response>, StreamItem>>
79-
: public DecoratorReturnType<Response> {};
80-
81-
template <typename Response, typename SinkElement, typename FinalResponse>
82-
struct DecoratorReturnType<
83-
ResponseAndSinkConsumer<Response, SinkElement, FinalResponse>>
84-
: public DecoratorReturnType<Response> {};
85-
86-
template <typename Response, typename SinkElement, typename FinalResponse>
87-
struct DecoratorReturnType<ResponseAndSinkConsumer<
88-
std::unique_ptr<Response>,
89-
SinkElement,
90-
FinalResponse>> : public DecoratorReturnType<Response> {};
91-
92-
template <typename Response, typename InputElement, typename OutputElement>
93-
struct DecoratorReturnType<
94-
ResponseAndStreamTransformation<Response, InputElement, OutputElement>>
95-
: public DecoratorReturnType<Response> {};
96-
97-
template <typename Response, typename InputElement, typename OutputElement>
98-
struct DecoratorReturnType<ResponseAndStreamTransformation<
99-
std::unique_ptr<Response>,
100-
InputElement,
101-
OutputElement>> : public DecoratorReturnType<Response> {};
102-
10388
} // namespace detail
10489

10590
} // namespace apache::thrift

0 commit comments

Comments
 (0)