race.h
Go to the documentation of this file.
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_RACE_H
16 #define GRPC_CORE_LIB_PROMISE_RACE_H
17 
19 
20 #include <type_traits>
21 
22 #include "absl/types/variant.h"
23 
25 
26 namespace grpc_core {
27 
28 namespace promise_detail {
29 
30 // Implementation type for Race combinator.
31 template <typename... Promises>
32 class Race;
33 
34 template <typename Promise, typename... Promises>
35 class Race<Promise, Promises...> {
36  public:
37  using Result = decltype(std::declval<Promise>()());
38 
39  explicit Race(Promise promise, Promises... promises)
40  : promise_(std::move(promise)), next_(std::move(promises)...) {}
41 
43  // Check our own promise.
44  auto r = promise_();
45  if (absl::holds_alternative<Pending>(r)) {
46  // Check the rest of them.
47  return next_();
48  }
49  // Return the first ready result.
50  return std::move(absl::get<kPollReadyIdx>(std::move(r)));
51  }
52 
53  private:
54  // The Promise checked by this instance.
56  // We recursively expand to check the rest of the instances.
57  Race<Promises...> next_;
58 };
59 
60 template <typename Promise>
61 class Race<Promise> {
62  public:
63  using Result = decltype(std::declval<Promise>()());
64  explicit Race(Promise promise) : promise_(std::move(promise)) {}
65  Result operator()() { return promise_(); }
66 
67  private:
69 };
70 
71 } // namespace promise_detail
72 
76 template <typename... Promises>
77 promise_detail::Race<Promises...> Race(Promises... promises) {
78  return promise_detail::Race<Promises...>(std::move(promises)...);
79 }
80 
81 } // namespace grpc_core
82 
83 #endif // GRPC_CORE_LIB_PROMISE_RACE_H
grpc_core::promise_detail::Race< Promise >::Result
decltype(std::declval< Promise >()()) Result
Definition: race.h:63
grpc_core::promise_detail::Race< Promise >::promise_
Promise promise_
Definition: race.h:68
grpc_core::promise_detail::Race< Promise, Promises... >::Result
decltype(std::declval< Promise >()()) Result
Definition: race.h:37
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::promise_detail::Race< Promise, Promises... >::next_
Race< Promises... > next_
Definition: race.h:57
grpc_core::Race
promise_detail::Race< Promises... > Race(Promises... promises)
Definition: race.h:77
grpc_core::promise_detail::Race< Promise >::Race
Race(Promise promise)
Definition: race.h:64
promise_
absl::optional< ArenaPromise< ServerMetadataHandle > > promise_
Definition: filter_fuzzer.cc:550
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::promise_detail::Race< Promise, Promises... >::operator()
Result operator()()
Definition: race.h:42
grpc_core::promise_detail::Race< Promise >::operator()
Result operator()()
Definition: race.h:65
grpc_core::promise_detail::Race
Definition: race.h:32
poll.h
grpc_core::Promise
std::function< Poll< T >()> Promise
Definition: promise/promise.h:37
fix_build_deps.r
r
Definition: fix_build_deps.py:491
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_core::promise_detail::Race< Promise, Promises... >::Race
Race(Promise promise, Promises... promises)
Definition: race.h:39
grpc_core::promise_detail::Race< Promise, Promises... >::promise_
Promise promise_
Definition: race.h:55
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:59