for_each.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_FOR_EACH_H
16 #define GRPC_CORE_LIB_PROMISE_FOR_EACH_H
17 
19 
20 #include <type_traits>
21 #include <utility>
22 
23 #include "absl/status/status.h"
24 #include "absl/types/variant.h"
25 
28 
29 namespace grpc_core {
30 
31 namespace for_each_detail {
32 
33 // Helper function: at the end of each iteration of a for-each loop, this is
34 // called. If the iteration failed, return failure. If the iteration succeeded,
35 // then call the next iteration.
36 template <typename Reader, typename CallPoll>
38  CallPoll call_poll) {
39  if (r->ok()) {
40  auto next = reader->Next();
41  return call_poll(next);
42  }
43  return std::move(*r);
44 }
45 
46 // Done creates statuses for the end of the iteration. It's templated on the
47 // type of the result of the ForEach loop, so that we can introduce new types
48 // easily.
49 template <typename T>
50 struct Done;
51 
52 template <>
53 struct Done<absl::Status> {
54  static absl::Status Make() { return absl::OkStatus(); }
55 };
56 
57 template <typename Reader, typename Action>
58 class ForEach {
59  private:
60  using ReaderNext = decltype(std::declval<Reader>().Next());
61  using ReaderResult = typename PollTraits<
62  decltype(std::declval<ReaderNext>()())>::Type::value_type;
65 
66  public:
67  using Result =
69  ForEach(Reader reader, Action action)
70  : reader_(std::move(reader)),
72  state_(reader_.Next()) {}
73 
74  ForEach(const ForEach&) = delete;
75  ForEach& operator=(const ForEach&) = delete;
76  // noexcept causes compiler errors on older gcc's
77  // NOLINTNEXTLINE(performance-noexcept-move-constructor)
78  ForEach(ForEach&&) = default;
79  // noexcept causes compiler errors on older gcc's
80  // NOLINTNEXTLINE(performance-noexcept-move-constructor)
81  ForEach& operator=(ForEach&&) = default;
82 
84  return absl::visit(CallPoll<false>{this}, state_);
85  }
86 
87  private:
88  Reader reader_;
91 
92  // Call the inner poll function, and if it's finished, start the next
93  // iteration. If kSetState==true, also set the current state in self->state_.
94  // We omit that on the first iteration because it's common to poll once and
95  // not change state, which saves us some work.
96  template <bool kSetState>
97  struct CallPoll {
98  ForEach* const self;
99 
101  auto r = reader_next();
102  if (auto* p = absl::get_if<kPollReadyIdx>(&r)) {
103  if (p->has_value()) {
104  auto action = self->action_factory_.Repeated(std::move(**p));
105  return CallPoll<true>{self}(action);
106  } else {
107  return Done<Result>::Make();
108  }
109  }
110  if (kSetState) {
111  self->state_.template emplace<ReaderNext>(std::move(reader_next));
112  }
113  return Pending();
114  }
115 
117  auto r = promise();
118  if (auto* p = absl::get_if<kPollReadyIdx>(&r)) {
119  return FinishIteration(p, &self->reader_, CallPoll<true>{self});
120  }
121  if (kSetState) {
122  self->state_.template emplace<ActionPromise>(std::move(promise));
123  }
124  return Pending();
125  }
126  };
127 };
128 
129 } // namespace for_each_detail
130 
132 template <typename Reader, typename Action>
135  std::move(action));
136 }
137 
138 } // namespace grpc_core
139 
140 #endif // GRPC_CORE_LIB_PROMISE_FOR_EACH_H
Type
struct Type Type
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:673
grpc_core::for_each_detail::ForEach::ActionPromise
typename ActionFactory::Promise ActionPromise
Definition: for_each.h:64
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::for_each_detail::ForEach::Result
typename PollTraits< decltype(std::declval< ActionPromise >()())>::Type Result
Definition: for_each.h:68
absl::OkStatus
Status OkStatus()
Definition: third_party/abseil-cpp/absl/status/status.h:882
grpc_core::for_each_detail::ForEach::CallPoll::self
ForEach *const self
Definition: for_each.h:98
grpc_core::for_each_detail::ForEach::reader_
Reader reader_
Definition: for_each.h:88
grpc_core::for_each_detail::ForEach::operator=
ForEach & operator=(const ForEach &)=delete
grpc_core::ForEach
for_each_detail::ForEach< Reader, Action > ForEach(Reader reader, Action action)
For each item acquired by calling Reader::Next, run the promise Action.
Definition: for_each.h:133
grpc_core::for_each_detail::ForEach::state_
absl::variant< ReaderNext, ActionPromise > state_
Definition: for_each.h:90
grpc_core::for_each_detail::ForEach::CallPoll::operator()
Poll< Result > operator()(ActionPromise &promise)
Definition: for_each.h:116
absl::base_internal::Next
static AllocList * Next(int i, AllocList *prev, LowLevelAlloc::Arena *arena)
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:453
promise_factory.h
grpc_core::for_each_detail::ForEach
Definition: for_each.h:58
grpc_core::Pending
Definition: poll.h:29
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::for_each_detail::ForEach::ForEach
ForEach(Reader reader, Action action)
Definition: for_each.h:69
grpc_core::for_each_detail::ForEach::action_factory_
ActionFactory action_factory_
Definition: for_each.h:89
grpc_core::PollTraits
Definition: poll.h:54
absl::compare_internal::value_type
int8_t value_type
Definition: abseil-cpp/absl/types/compare.h:45
grpc_core::for_each_detail::ForEach::operator()
Poll< Result > operator()()
Definition: for_each.h:83
grpc_core::for_each_detail::ForEach::CallPoll
Definition: for_each.h:97
grpc_core::for_each_detail::ForEach::ReaderResult
typename PollTraits< decltype(std::declval< ReaderNext >()())>::Type::value_type ReaderResult
Definition: for_each.h:62
grpc_core::for_each_detail::Done
Definition: for_each.h:50
client.action
action
Definition: examples/python/xds/client.py:49
grpc_core::promise_detail::PromiseFactory< ReaderResult, Action >
poll.h
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
grpc_core::for_each_detail::ForEach::CallPoll::operator()
Poll< Result > operator()(ReaderNext &reader_next)
Definition: for_each.h:100
next
AllocList * next[kMaxLevel]
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:100
fix_build_deps.r
r
Definition: fix_build_deps.py:491
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc_core::for_each_detail::Done< absl::Status >::Make
static absl::Status Make()
Definition: for_each.h:54
grpc_core::promise_detail::PromiseFactory< ReaderResult, Action >::Promise
decltype(PromiseFactoryImpl(std::move(f_), std::declval< ReaderResult >())) Promise
Definition: promise_factory.h:156
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::visit
variant_internal::VisitResult< Visitor, Variants... > visit(Visitor &&vis, Variants &&... vars)
Definition: abseil-cpp/absl/types/variant.h:430
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
reader
void reader(void *n)
Definition: libuv/docs/code/locks/main.c:8
grpc_core::for_each_detail::FinishIteration
Poll< absl::Status > FinishIteration(absl::Status *r, Reader *reader, CallPoll call_poll)
Definition: for_each.h:37
grpc_core::for_each_detail::ForEach::ReaderNext
decltype(std::declval< Reader >().Next()) ReaderNext
Definition: for_each.h:60
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:22