| 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_FOR_EACH_H |
| 16 |
#define GRPC_CORE_LIB_PROMISE_FOR_EACH_H |
| 17 |
|
| 18 |
#include <grpc/support/port_platform.h> |
| 19 |
|
| 20 |
#include <utility> |
| 21 |
|
| 22 |
#include "absl/status/status.h" |
| 23 |
#include "absl/types/variant.h" |
| 24 |
|
| 25 |
#include "src/core/lib/promise/detail/promise_factory.h" |
| 26 |
#include "src/core/lib/promise/poll.h" |
| 27 |
|
| 28 |
namespace grpc_core { |
| 29 |
|
| 30 |
namespace for_each_detail { |
| 31 |
|
| 32 |
// Done creates statuses for the end of the iteration. It's templated on the |
| 33 |
// type of the result of the ForEach loop, so that we can introduce new types |
| 34 |
// easily. |
| 35 |
template <typename T> |
| 36 |
struct Done; |
| 37 |
|
| 38 |
template <> |
| 39 |
struct Done<absl::Status> { |
| 40 |
static absl::Status Make() { return absl::OkStatus(); } |
| 41 |
}; |
| 42 |
|
| 43 |
template <typename Reader, typename Action> |
| 44 |
class ForEach { |
| 45 |
private: |
| 46 |
using ReaderNext = decltype(std::declval<Reader>().Next()); |
| 47 |
using ReaderResult = |
| 48 |
typename PollTraits<decltype(std::declval<ReaderNext>()())>::Type; |
| 49 |
using ReaderResultValue = typename ReaderResult::value_type; |
| 50 |
using ActionFactory = |
| 51 |
promise_detail::RepeatedPromiseFactory<ReaderResultValue, Action>; |
| 52 |
using ActionPromise = typename ActionFactory::Promise; |
| 53 |
|
| 54 |
public: |
| 55 |
using Result = |
| 56 |
typename PollTraits<decltype(std::declval<ActionPromise>()())>::Type; |
| 57 |
ForEach(Reader reader, Action action) |
| 58 |
: reader_(std::move(reader)), |
| 59 |
action_factory_(std::move(action)), |
| 60 |
state_(reader_.Next()) {} |
| 61 |
|
| 62 |
ForEach(const ForEach&) = delete; |
| 63 |
ForEach& operator=(const ForEach&) = delete; |
| 64 |
// noexcept causes compiler errors on older gcc's |
| 65 |
// NOLINTNEXTLINE(performance-noexcept-move-constructor) |
| 66 |
ForEach(ForEach&&) = default; |
| 67 |
// noexcept causes compiler errors on older gcc's |
| 68 |
// NOLINTNEXTLINE(performance-noexcept-move-constructor) |
| 69 |
ForEach& operator=(ForEach&&) = default; |
| 70 |
|
| 71 |
Poll<Result> operator()() { return absl::visit(CallPoll{this}, state_); } |
| 72 |
|
| 73 |
private: |
| 74 |
struct InAction { |
| 75 |
InAction(ActionPromise promise, ReaderResult result) |
| 76 |
: promise(std::move(promise)), result(std::move(result)) {} |
| 77 |
ActionPromise promise; |
| 78 |
ReaderResult result; |
| 79 |
}; |
| 80 |
Reader reader_; |
| 81 |
ActionFactory action_factory_; |
| 82 |
absl::variant<ReaderNext, InAction> state_; |
| 83 |
|
| 84 |
// Call the inner poll function, and if it's finished, start the next |
| 85 |
// iteration. |
| 86 |
struct CallPoll { |
| 87 |
ForEach* const self; |
| 88 |
|
| 89 |
Poll<Result> operator()(ReaderNext& reader_next) { |
| 90 |
auto r = reader_next(); |
| 91 |
if (auto* p = absl::get_if<kPollReadyIdx>(&r)) { |
| 92 |
if (p->has_value()) { |
| 93 |
auto action = self->action_factory_.Make(std::move(**p)); |
| 94 |
return (*this)(self->state_.template emplace<InAction>( |
| 95 |
std::move(action), std::move(*p))); |
| 96 |
} else { |
| 97 |
return Done<Result>::Make(); |
| 98 |
} |
| 99 |
} |
| 100 |
return Pending(); |
| 101 |
} |
| 102 |
|
| 103 |
Poll<Result> operator()(InAction& in_action) { |
| 104 |
auto r = in_action.promise(); |
| 105 |
if (auto* p = absl::get_if<kPollReadyIdx>(&r)) { |
| 106 |
if (p->ok()) { |
| 107 |
return (*this)( |
| 108 |
self->state_.template emplace<ReaderNext>(self->reader_.Next())); |
| 109 |
} else { |
| 110 |
return std::move(*p); |
| 111 |
} |
| 112 |
} |
| 113 |
return Pending(); |
| 114 |
} |
| 115 |
}; |
| 116 |
}; |
| 117 |
|
| 118 |
} // namespace for_each_detail |
| 119 |
|
| 120 |
/// For each item acquired by calling Reader::Next, run the promise Action. |
| 121 |
template <typename Reader, typename Action> |
| 122 |
for_each_detail::ForEach<Reader, Action> ForEach(Reader reader, Action action) { |
| 123 |
return for_each_detail::ForEach<Reader, Action>(std::move(reader), |
| 124 |
std::move(action)); |
| 125 |
} |
| 126 |
|
| 127 |
} // namespace grpc_core |
| 128 |
|
| 129 |
#endif // GRPC_CORE_LIB_PROMISE_FOR_EACH_H |