try_join.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_TRY_JOIN_H
16 #define GRPC_CORE_LIB_PROMISE_TRY_JOIN_H
17 
19 
20 #include <type_traits>
21 
22 #include "absl/meta/type_traits.h"
23 #include "absl/status/status.h"
24 #include "absl/status/statusor.h"
25 
29 
30 namespace grpc_core {
31 
32 namespace promise_detail {
33 
34 // Extract the T from a StatusOr<T>
35 template <typename T>
37  return std::move(**status);
38 }
39 
40 // TryJoin returns a StatusOr<tuple<A,B,C>> for f()->Poll<StatusOr<A>>,
41 // g()->Poll<StatusOr<B>>, h()->Poll<StatusOr<C>>. If one of those should be a
42 // Status instead, we need a placeholder type to return, and this is it.
43 struct Empty {};
44 inline Empty IntoResult(absl::Status*) { return Empty{}; }
45 
46 // Traits object to pass to BasicJoin
47 struct TryJoinTraits {
48  template <typename T>
49  using ResultType =
50  decltype(IntoResult(std::declval<absl::remove_reference_t<T>*>()));
51  template <typename T, typename F>
52  static auto OnResult(T result, F kontinue)
53  -> decltype(kontinue(IntoResult(&result))) {
54  using Result =
55  typename PollTraits<decltype(kontinue(IntoResult(&result)))>::Type;
56  if (!result.ok()) {
57  return Result(IntoStatus(&result));
58  }
59  return kontinue(IntoResult(&result));
60  }
61  template <typename T>
62  static absl::StatusOr<T> Wrap(T x) {
63  return absl::StatusOr<T>(std::move(x));
64  }
65 };
66 
67 // Implementation of TryJoin combinator.
68 template <typename... Promises>
69 using TryJoin = BasicJoin<TryJoinTraits, Promises...>;
70 
71 } // namespace promise_detail
72 
73 // Run all promises.
74 // If any fail, cancel the rest and return the failure.
75 // If all succeed, return Ok(tuple-of-results).
76 template <typename... Promises>
77 promise_detail::TryJoin<Promises...> TryJoin(Promises... promises) {
78  return promise_detail::TryJoin<Promises...>(std::move(promises)...);
79 }
80 
81 } // namespace grpc_core
82 
83 #endif // GRPC_CORE_LIB_PROMISE_TRY_JOIN_H
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
Type
struct Type Type
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:673
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::promise_detail::TryJoinTraits::Wrap
static absl::StatusOr< T > Wrap(T x)
Definition: try_join.h:62
status
absl::Status status
Definition: rls.cc:251
T
#define T(upbtypeconst, upbtype, ctype, default_value)
grpc_core::promise_detail::TryJoinTraits
Definition: try_join.h:47
grpc_core::promise_detail::IntoStatus
absl::Status IntoStatus(absl::StatusOr< T > *status)
Definition: src/core/lib/promise/detail/status.h:32
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
re2::Result
TestInstance::Result Result
Definition: bloaty/third_party/re2/re2/testing/tester.cc:96
grpc_core::PollTraits
Definition: poll.h:54
x
int x
Definition: bloaty/third_party/googletest/googlemock/test/gmock-matchers_test.cc:3610
grpc_core::promise_detail::TryJoinTraits::ResultType
decltype(IntoResult(std::declval< absl::remove_reference_t< T > * >())) ResultType
Definition: try_join.h:50
poll.h
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
status.h
basic_join.h
grpc_core::promise_detail::Empty
Definition: try_join.h:43
grpc_core::promise_detail::IntoResult
T IntoResult(absl::StatusOr< T > *status)
Definition: try_join.h:36
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
grpc_core::TryJoin
promise_detail::TryJoin< Promises... > TryJoin(Promises... promises)
Definition: try_join.h:77
absl::remove_reference_t
typename std::remove_reference< T >::type remove_reference_t
Definition: abseil-cpp/absl/meta/type_traits.h:597
grpc_core::promise_detail::TryJoinTraits::OnResult
static auto OnResult(T result, F kontinue) -> decltype(kontinue(IntoResult(&result)))
Definition: try_join.h:52
port_platform.h
grpc_core::promise_detail::BasicJoin
Definition: basic_join.h:136


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:41