| 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 |
|
| 18 |
#include <grpc/support/port_platform.h> |
| 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 |
|
| 26 |
#include "src/core/lib/promise/detail/basic_join.h" |
| 27 |
#include "src/core/lib/promise/detail/status.h" |
| 28 |
#include "src/core/lib/promise/poll.h" |
| 29 |
|
| 30 |
namespace grpc_core { |
| 31 |
|
| 32 |
namespace promise_detail { |
| 33 |
|
| 34 |
// Extract the T from a StatusOr<T> |
| 35 |
template <typename T> |
| 36 |
T IntoResult(absl::StatusOr<T>* status) { |
| 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 |