1<%!
2
3  Copyright (c) Facebook, Inc. and its affiliates.
4
5  Licensed under the Apache License, Version 2.0 (the "License");
6  you may not use this file except in compliance with the License.
7  You may obtain a copy of the License at
8
9      http://www.apache.org/licenses/LICENSE-2.0
10
11  Unless required by applicable law or agreed to in writing, software
12  distributed under the License is distributed on an "AS IS" BASIS,
13  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  See the License for the specific language governing permissions and
15  limitations under the License.
16
17%><%#service:functions%><%#function:return_type%>
18<%^function:starts_interaction?%>
19<%^function:eb%>
20
21<% > service_common/function_return_type%> <% > service_common/service_class_name%>::<%function:cpp_name%>(<% > service_common/function_return_param%><% > service_common/function_param_list_commented_out%>) {
22  apache::thrift::detail::si::throw_app_exn_unimplemented("<%function:name%>");
23}
24
25folly::SemiFuture<<% > service_common/async_return_type%>> <% > service_common/service_class_name%>::semifuture_<%function:cpp_name%>(<% > service_common/function_param_list%>) {
26  auto expected{apache::thrift::detail::si::InvocationType::SemiFuture};
27  __fbthrift_invocation_<%function:cpp_name%>.compare_exchange_strong(expected, apache::thrift::detail::si::InvocationType::Sync, std::memory_order_relaxed);
28<%^type:resolves_to_complex_return?%>
29<%^type:void?%>
30  return <%function:cpp_name%>(<% > service_common/param_list_move%>);
31<%/type:void?%>
32<%#type:void?%>
33  <%function:cpp_name%>(<% > service_common/param_list_move%>);
34  return folly::makeSemiFuture();
35<%/type:void?%>
36<%/type:resolves_to_complex_return?%>
37<%#type:resolves_to_complex_return?%>
38<%#function:stack_arguments?%>
39  <%type:cpp_type%> ret;
40  <%function:cpp_name%>(ret<%function:comma%><% > service_common/param_list_move%>);
41<%/function:stack_arguments?%>
42<%^function:stack_arguments?%>
43  auto ret = std::make_unique<<%type:cpp_type%>>();
44  <%function:cpp_name%>(*ret<%function:comma%><% > service_common/param_list_move%>);
45<%/function:stack_arguments?%>
46  return folly::makeSemiFuture(std::move(ret));
47<%/type:resolves_to_complex_return?%>
48}
49<%^service:interaction?%>
50
51folly::Future<<% > service_common/async_return_type%>> <%service:name%>SvIf::future_<%function:cpp_name%>(<% > service_common/function_param_list%>) {
52  auto expected{apache::thrift::detail::si::InvocationType::Future};
53  __fbthrift_invocation_<%function:cpp_name%>.compare_exchange_strong(expected, apache::thrift::detail::si::InvocationType::SemiFuture, std::memory_order_relaxed);
54  return apache::thrift::detail::si::future(semifuture_<%function:cpp_name%>(<% > service_common/param_list_move%>), getInternalKeepAlive());
55}
56<%/service:interaction?%>
57<%#function:coroutine?%>
58
59#if FOLLY_HAS_COROUTINES
60folly::coro::Task<<% > service_common/callback_type%>> <% > service_common/service_class_name%>::co_<%function:cpp_name%>(<% > service_common/function_param_list%>) {
61  auto expected{apache::thrift::detail::si::InvocationType::Coro};
62  __fbthrift_invocation_<%function:cpp_name%>.compare_exchange_strong(expected, apache::thrift::detail::si::InvocationType::<%^service:interaction?%>Future<%/service:interaction?%><%#service:interaction?%>SemiFuture<%/service:interaction?%>, std::memory_order_relaxed);
63  return folly::coro::toTask(<%^service:interaction?%>future_<%/service:interaction?%><%#service:interaction?%>semifuture_<%/service:interaction?%><%function:cpp_name%>(<% > service_common/param_list_move%>));
64}
65
66folly::coro::Task<<% > service_common/callback_type%>> <% > service_common/service_class_name%>::co_<%function:cpp_name%>(apache::thrift::RequestParams /* params */<%function:comma%><% > service_common/function_param_list%>) {
67  auto expected{apache::thrift::detail::si::InvocationType::CoroParam};
68  __fbthrift_invocation_<%function:cpp_name%>.compare_exchange_strong(expected, apache::thrift::detail::si::InvocationType::Coro, std::memory_order_relaxed);
69  return co_<%function:cpp_name%>(<% > service_common/param_list_move%>);
70}
71#endif // FOLLY_HAS_COROUTINES
72<%/function:coroutine?%>
73<%#function:oneway?%>
74
75void <% > service_common/service_class_name%>::async_tm_<%function:cpp_name%>(std::unique_ptr<apache::thrift::HandlerCallbackBase> callback<%function:comma%><% > service_common/function_param_list%>) {
76<%/function:oneway?%>
77<%^function:oneway?%>
78
79void <% > service_common/service_class_name%>::async_tm_<%function:cpp_name%>(std::unique_ptr<apache::thrift::HandlerCallback<<% > types/unique_ptr_type%>>> callback<%function:comma%><% > service_common/function_param_list%>) {
80<%/function:oneway?%>
81  // It's possible the coroutine versions will delegate to a future-based
82  // version. If that happens, we need the RequestParams arguments to be
83  // available to the future through the thread-local backchannel, so we create
84  // a RAII object that sets up RequestParams and clears them on destruction.
85  apache::thrift::detail::si::AsyncTmPrep asyncTmPrep(this, callback.get());
86  auto invocationType = __fbthrift_invocation_<%function:cpp_name%>.load(std::memory_order_relaxed);
87  try {
88    switch (invocationType) {
89      case apache::thrift::detail::si::InvocationType::AsyncTm:
90      {
91<%#function:coroutine?%>
92#if FOLLY_HAS_COROUTINES
93        __fbthrift_invocation_<%function:cpp_name%>.compare_exchange_strong(invocationType, apache::thrift::detail::si::InvocationType::CoroParam, std::memory_order_relaxed);
94        apache::thrift::RequestParams params{callback->getRequestContext(),
95          callback->getThreadManager(), callback->getEventBase()};
96        auto task = co_<%function:cpp_name%>(params<%function:comma%><% > service_common/param_list_move%>);
97        apache::thrift::detail::si::<%#function:oneway?%>async_tm_coro_oneway<%/function:oneway?%><%^function:oneway?%>async_tm_coro<%/function:oneway?%>(std::move(callback), std::move(task));
98        return;
99#else // FOLLY_HAS_COROUTINES
100<%/function:coroutine?%>
101<%^service:interaction?%>
102        __fbthrift_invocation_<%function:cpp_name%>.compare_exchange_strong(invocationType, apache::thrift::detail::si::InvocationType::Future, std::memory_order_relaxed);
103<%/service:interaction?%>
104<%#service:interaction?%>
105        __fbthrift_invocation_<%function:cpp_name%>.compare_exchange_strong(invocationType, apache::thrift::detail::si::InvocationType::SemiFuture, std::memory_order_relaxed);
106<%/service:interaction?%>
107        FOLLY_FALLTHROUGH;
108<%#function:coroutine?%>
109#endif // FOLLY_HAS_COROUTINES
110<%/function:coroutine?%>
111      }
112<%^service:interaction?%>
113      case apache::thrift::detail::si::InvocationType::Future:
114      {
115        auto fut = future_<%function:cpp_name%>(<% > service_common/param_list_move%>);
116        apache::thrift::detail::si::<%#function:oneway?%>async_tm_future_oneway<%/function:oneway?%><%^function:oneway?%>async_tm_future<%/function:oneway?%>(std::move(callback), std::move(fut));
117        return;
118      }
119<%/service:interaction?%>
120      case apache::thrift::detail::si::InvocationType::SemiFuture:
121      {
122        auto fut = semifuture_<%function:cpp_name%>(<% > service_common/param_list_move%>);
123        apache::thrift::detail::si::<%#function:oneway?%>async_tm_semifuture_oneway<%/function:oneway?%><%^function:oneway?%>async_tm_semifuture<%/function:oneway?%>(std::move(callback), std::move(fut));
124        return;
125      }
126<%#function:coroutine?%>
127#if FOLLY_HAS_COROUTINES
128      case apache::thrift::detail::si::InvocationType::CoroParam:
129      {
130        apache::thrift::RequestParams params{callback->getRequestContext(),
131          callback->getThreadManager(), callback->getEventBase()};
132        auto task = co_<%function:cpp_name%>(params<%function:comma%><% > service_common/param_list_move%>);
133        apache::thrift::detail::si::<%#function:oneway?%>async_tm_coro_oneway<%/function:oneway?%><%^function:oneway?%>async_tm_coro<%/function:oneway?%>(std::move(callback), std::move(task));
134        return;
135      }
136      case apache::thrift::detail::si::InvocationType::Coro:
137      {
138        auto task = co_<%function:cpp_name%>(<% > service_common/param_list_move%>);
139        apache::thrift::detail::si::<%#function:oneway?%>async_tm_coro_oneway<%/function:oneway?%><%^function:oneway?%>async_tm_coro<%/function:oneway?%>(std::move(callback), std::move(task));
140        return;
141      }
142#endif // FOLLY_HAS_COROUTINES
143<%/function:coroutine?%>
144      case apache::thrift::detail::si::InvocationType::Sync:
145      {
146<%^type:resolves_to_complex_return?%>
147<%^type:void?%>
148        callback->result(<%function:cpp_name%>(<% > service_common/param_list_move%>));
149<%/type:void?%>
150<%#type:void?%>
151        <%function:cpp_name%>(<% > service_common/param_list_move%>);
152<%^function:oneway?%>
153        callback->done();
154<%/function:oneway?%>
155<%/type:void?%>
156<%/type:resolves_to_complex_return?%>
157<%#type:resolves_to_complex_return?%>
158        <% > types/type%> _return;
159        <%function:cpp_name%>(_return<%function:comma%><% > service_common/param_list_move%>);
160        callback->result(_return);
161<%/type:resolves_to_complex_return?%>
162        return;
163      }
164      default:
165      {
166        folly::assume_unreachable();
167      }
168    }
169  } catch (...) {
170    callback->exception(std::current_exception());
171  }
172}
173<%/function:eb%>
174<%#function:eb%><%!Event base codegen%>
175<%#function:oneway?%>
176
177void <% > service_common/service_class_name%>::async_eb_<%function:cpp_name%>(std::unique_ptr<apache::thrift::HandlerCallbackBase> /*callback*/<%function:comma%><% > service_common/function_param_list_commented_out%>) {
178  LOG(DFATAL) << "Function <%function:cpp_name%> is unimplemented";
179}
180<%/function:oneway?%>
181<%^function:oneway?%>
182
183void <% > service_common/service_class_name%>::async_eb_<%function:cpp_name%>(std::unique_ptr<apache::thrift::HandlerCallback<<% > types/unique_ptr_type%>>> callback<%function:comma%><% > service_common/function_param_list_commented_out%>) {
184  callback->exception(apache::thrift::detail::si::create_app_exn_unimplemented("<%function:name%>"));
185}
186<%/function:oneway?%>
187<%/function:eb%>
188<%/function:starts_interaction?%>
189<%/function:return_type%><%/service:functions%>
190