Debian Codegraph @

src/core/lib/promise/detail/promise_factory.h

raw
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef GRPC_CORE_LIB_PROMISE_DETAIL_PROMISE_FACTORY_H
16 #define GRPC_CORE_LIB_PROMISE_DETAIL_PROMISE_FACTORY_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <utility>
21
22 #include "absl/meta/type_traits.h"
23
24 #include "src/core/lib/promise/detail/promise_like.h"
25
26 // PromiseFactory is an adaptor class.
27 //
28 // Where a Promise is a thing that's polled periodically, a PromiseFactory
29 // creates a Promise. Within this Promise/Activity framework, PromiseFactory's
30 // then provide the edges for computation -- invoked at state transition
31 // boundaries to provide the new steady state.
32 //
33 // A PromiseFactory formally is f(A) -> Promise<T> for some types A & T.
34 // This get a bit awkward and inapproprate to write however, and so the type
35 // contained herein can adapt various kinds of callable into the correct form.
36 // Of course a callable of a single argument returning a Promise will see an
37 // identity translation. One taking no arguments and returning a Promise
38 // similarly.
39 //
40 // A Promise passed to a PromiseFactory will yield a PromiseFactory that
41 // returns just that Promise.
42 //
43 // Generalizing slightly, a callable taking a single argument A and returning a
44 // Poll<T> will yield a PromiseFactory that captures it's argument A and
45 // returns a Poll<T>.
46 //
47 // Since various consumers of PromiseFactory run either repeatedly through an
48 // overarching Promises lifetime, or just once, and we can optimize just once
49 // by moving the contents of the PromiseFactory, two factory methods are
50 // provided: Once, that can be called just once, and Repeated, that can (wait
51 // for it) be called Repeatedly.
52
53 namespace grpc_core {
54 namespace promise_detail {
55
56 // Helper trait: given a T, and T x, is calling x() legal?
57 template <typename T, typename Ignored = void>
58 struct IsVoidCallable {
59 static constexpr bool value = false;
60 };
61 template <typename F>
62 struct IsVoidCallable<F, absl::void_t<decltype(std::declval<F>()())>> {
63 static constexpr bool value = true;
64 };
65
66 // Given F(A,B,C,...), what's the return type?
67 template <typename T, typename Ignored = void>
68 struct ResultOfT;
69
70 template <typename F, typename... Args>
71 struct ResultOfT<F(Args...),
72 absl::void_t<decltype(std::declval<RemoveCVRef<F>>()(
73 std::declval<Args>()...))>> {
74 using T = decltype(std::declval<RemoveCVRef<F>>()(std::declval<Args>()...));
75 };
76
77 template <typename F, typename... Args>
78 struct ResultOfT<F(Args...)&,
79 absl::void_t<decltype(std::declval<RemoveCVRef<F>>()(
80 std::declval<Args>()...))>> {
81 using T = decltype(std::declval<RemoveCVRef<F>>()(std::declval<Args>()...));
82 };
83
84 template <typename F, typename... Args>
85 struct ResultOfT<const F(Args...)&,
86 absl::void_t<decltype(std::declval<RemoveCVRef<F>>()(
87 std::declval<Args>()...))>> {
88 using T = decltype(std::declval<RemoveCVRef<F>>()(std::declval<Args>()...));
89 };
90
91 template <typename T>
92 using ResultOf = typename ResultOfT<T>::T;
93
94 // Captures the promise functor and the argument passed.
95 // Provides the interface of a promise.
96 template <typename F, typename Arg>
97 class Curried {
98 public:
99 Curried(F&& f, Arg&& arg)
100 : f_(std::forward<F>(f)), arg_(std::forward<Arg>(arg)) {}
101 Curried(const F& f, Arg&& arg) : f_(f), arg_(std::forward<Arg>(arg)) {}
102 using Result = decltype(std::declval<F>()(std::declval<Arg>()));
103 Result operator()() { return f_(std::move(arg_)); }
104
105 private:
106 GPR_NO_UNIQUE_ADDRESS F f_;
107 GPR_NO_UNIQUE_ADDRESS Arg arg_;
108 };
109
110 // Promote a callable(A) -> T | Poll<T> to a PromiseFactory(A) -> Promise<T> by
111 // capturing A.
112 template <typename A, typename F>
113 absl::enable_if_t<!IsVoidCallable<ResultOf<F(A)>>::value,
114 PromiseLike<Curried<RemoveCVRef<F>, A>>>
115 PromiseFactoryImpl(F&& f, A&& arg) {
116 return Curried<RemoveCVRef<F>, A>(std::forward<F>(f), std::forward<A>(arg));
117 }
118
119 // Promote a callable() -> T|Poll<T> to a PromiseFactory(A) -> Promise<T>
120 // by dropping the argument passed to the factory.
121 template <typename A, typename F>
122 absl::enable_if_t<!IsVoidCallable<ResultOf<F()>>::value,
123 PromiseLike<RemoveCVRef<F>>>
124 PromiseFactoryImpl(F f, A&&) {
125 return PromiseLike<F>(std::move(f));
126 }
127
128 // Promote a callable() -> T|Poll<T> to a PromiseFactory() -> Promise<T>
129 template <typename F>
130 absl::enable_if_t<!IsVoidCallable<ResultOf<F()>>::value,
131 PromiseLike<RemoveCVRef<F>>>
132 PromiseFactoryImpl(F f) {
133 return PromiseLike<F>(std::move(f));
134 }
135
136 // Given a callable(A) -> Promise<T>, name it a PromiseFactory and use it.
137 template <typename A, typename F>
138 absl::enable_if_t<IsVoidCallable<ResultOf<F(A)>>::value,
139 PromiseLike<decltype(std::declval<F>()(std::declval<A>()))>>
140 PromiseFactoryImpl(F&& f, A&& arg) {
141 return f(std::forward<A>(arg));
142 }
143
144 // Given a callable(A) -> Promise<T>, name it a PromiseFactory and use it.
145 template <typename A, typename F>
146 absl::enable_if_t<IsVoidCallable<ResultOf<F(A)>>::value,
147 PromiseLike<decltype(std::declval<F>()(std::declval<A>()))>>
148 PromiseFactoryImpl(F& f, A&& arg) {
149 return f(std::forward<A>(arg));
150 }
151
152 // Given a callable() -> Promise<T>, promote it to a
153 // PromiseFactory(A) -> Promise<T> by dropping the first argument.
154 template <typename A, typename F>
155 absl::enable_if_t<IsVoidCallable<ResultOf<F()>>::value,
156 PromiseLike<decltype(std::declval<F>()())>>
157 PromiseFactoryImpl(F&& f, A&&) {
158 return f();
159 }
160
161 // Given a callable() -> Promise<T>, name it a PromiseFactory and use it.
162 template <typename F>
163 absl::enable_if_t<IsVoidCallable<ResultOf<F()>>::value,
164 PromiseLike<decltype(std::declval<F>()())>>
165 PromiseFactoryImpl(F&& f) {
166 return f();
167 };
168
169 template <typename A, typename F>
170 class OncePromiseFactory {
171 private:
172 GPR_NO_UNIQUE_ADDRESS F f_;
173
174 public:
175 using Arg = A;
176 using Promise =
177 decltype(PromiseFactoryImpl(std::move(f_), std::declval<A>()));
178
179 explicit OncePromiseFactory(F f) : f_(std::move(f)) {}
180
181 Promise Make(Arg&& a) {
182 return PromiseFactoryImpl(std::move(f_), std::forward<Arg>(a));
183 }
184 };
185
186 template <typename F>
187 class OncePromiseFactory<void, F> {
188 private:
189 GPR_NO_UNIQUE_ADDRESS F f_;
190
191 public:
192 using Arg = void;
193 using Promise = decltype(PromiseFactoryImpl(std::move(f_)));
194
195 explicit OncePromiseFactory(F f) : f_(std::move(f)) {}
196
197 Promise Make() { return PromiseFactoryImpl(std::move(f_)); }
198 };
199
200 template <typename A, typename F>
201 class RepeatedPromiseFactory {
202 private:
203 GPR_NO_UNIQUE_ADDRESS F f_;
204
205 public:
206 using Arg = A;
207 using Promise = decltype(PromiseFactoryImpl(f_, std::declval<A>()));
208
209 explicit RepeatedPromiseFactory(F f) : f_(std::move(f)) {}
210
211 Promise Make(Arg&& a) const {
212 return PromiseFactoryImpl(f_, std::forward<Arg>(a));
213 }
214 Promise Make(Arg&& a) { return PromiseFactoryImpl(f_, std::forward<Arg>(a)); }
215 };
216
217 template <typename F>
218 class RepeatedPromiseFactory<void, F> {
219 private:
220 GPR_NO_UNIQUE_ADDRESS F f_;
221
222 public:
223 using Arg = void;
224 using Promise = decltype(PromiseFactoryImpl(f_));
225
226 explicit RepeatedPromiseFactory(F f) : f_(std::move(f)) {}
227
228 Promise Make() const { return PromiseFactoryImpl(f_); }
229 Promise Make() { return PromiseFactoryImpl(f_); }
230 };
231
232 } // namespace promise_detail
233 } // namespace grpc_core
234
235 #endif // GRPC_CORE_LIB_PROMISE_DETAIL_PROMISE_FACTORY_H