try_seq.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_SEQ_H
16 #define GRPC_CORE_LIB_PROMISE_TRY_SEQ_H
17 
19 
20 #include <type_traits>
21 #include <utility>
22 
23 #include "absl/meta/type_traits.h"
24 #include "absl/status/status.h"
25 #include "absl/status/statusor.h"
26 
30 
31 namespace grpc_core {
32 
33 namespace promise_detail {
34 
35 template <typename T, typename Ignored = void>
37  using UnwrappedType = T;
39  template <typename Next>
40  static auto CallFactory(Next* next, T&& value)
41  -> decltype(next->Once(std::forward<T>(value))) {
42  return next->Once(std::forward<T>(value));
43  }
44  template <typename F, typename Elem>
45  static auto CallSeqFactory(F& f, Elem&& elem, T&& value)
46  -> decltype(f(std::forward<Elem>(elem), std::forward<T>(value))) {
47  return f(std::forward<Elem>(elem), std::forward<T>(value));
48  }
49  template <typename Result, typename RunNext>
50  static Poll<Result> CheckResultAndRunNext(T prior, RunNext run_next) {
51  return run_next(std::move(prior));
52  }
53 };
54 
55 template <typename T>
57  using UnwrappedType = T;
59  template <typename Next>
61  -> decltype(next->Once(std::move(*status))) {
62  return next->Once(std::move(*status));
63  }
64  template <typename F, typename Elem>
65  static auto CallSeqFactory(F& f, Elem&& elem, absl::StatusOr<T> value)
66  -> decltype(f(std::forward<Elem>(elem), std::move(*value))) {
67  return f(std::forward<Elem>(elem), std::move(*value));
68  }
69  template <typename Result, typename RunNext>
71  RunNext run_next) {
72  if (!prior.ok()) return Result(prior.status());
73  return run_next(std::move(prior));
74  }
75 };
76 // If there exists a function 'IsStatusOk(const T&) -> bool' then we assume that
77 // T is a status type for the purposes of promise sequences, and a non-OK T
78 // should terminate the sequence and return.
79 template <typename T>
81  T, absl::enable_if_t<
82  std::is_same<decltype(IsStatusOk(std::declval<T>())), bool>::value,
83  void>> {
84  using UnwrappedType = void;
85  using WrappedType = T;
86  template <typename Next>
87  static auto CallFactory(Next* next, T&&) -> decltype(next->Once()) {
88  return next->Once();
89  }
90  template <typename Result, typename RunNext>
91  static Poll<Result> CheckResultAndRunNext(T prior, RunNext run_next) {
92  if (!IsStatusOk(prior)) return Result(std::move(prior));
93  return run_next(std::move(prior));
94  }
95 };
96 template <>
98  using UnwrappedType = void;
100  template <typename Next>
102  -> decltype(next->Once()) {
103  return next->Once();
104  }
105  template <typename Result, typename RunNext>
107  RunNext run_next) {
108  if (!prior.ok()) return Result(std::move(prior));
109  return run_next(std::move(prior));
110  }
111 };
112 
113 template <typename T>
115 
116 template <typename... Fs>
117 using TrySeq = BasicSeq<TrySeqTraits, Fs...>;
118 
119 } // namespace promise_detail
120 
121 // Try a sequence of operations.
122 // * Run the first functor as a promise.
123 // * Feed its success result into the second functor to create a promise,
124 // then run that.
125 // * ...
126 // * Feed the second-final success result into the final functor to create a
127 // promise, then run that, with the overall success result being that
128 // promises success result.
129 // If any step fails, fail everything.
130 // Functors can return StatusOr<> to signal that a value is fed forward, or
131 // Status to indicate only success/failure. In the case of returning Status,
132 // the construction functors take no arguments.
133 template <typename... Functors>
134 promise_detail::TrySeq<Functors...> TrySeq(Functors... functors) {
135  return promise_detail::TrySeq<Functors...>(std::move(functors)...);
136 }
137 
138 // Try a sequence of operations of unknown length.
139 // Asynchronously:
140 // for (element in (begin, end)) {
141 // auto r = wait_for factory(element, argument);
142 // if (!r.ok()) return r;
143 // argument = *r;
144 // }
145 // return argument;
146 template <typename Iter, typename Factory, typename Argument>
147 promise_detail::BasicSeqIter<promise_detail::TrySeqTraits, Factory, Argument,
148  Iter>
149 TrySeqIter(Iter begin, Iter end, Argument argument, Factory factory) {
151  Argument, Iter>(
152  begin, end, std::move(factory), std::move(argument));
153 }
154 
155 } // namespace grpc_core
156 
157 #endif // GRPC_CORE_LIB_PROMISE_TRY_SEQ_H
grpc_core::promise_detail::BasicSeqIter
Definition: basic_seq.h:410
grpc_core::promise_detail::TrySeqTraitsWithSfinae< absl::StatusOr< T > >::CallFactory
static auto CallFactory(Next *next, absl::StatusOr< T > &&status) -> decltype(next->Once(std::move(*status)))
Definition: try_seq.h:60
grpc_core::promise_detail::TrySeqTraitsWithSfinae< absl::Status >::CallFactory
static auto CallFactory(Next *next, absl::Status &&) -> decltype(next->Once())
Definition: try_seq.h:101
begin
char * begin
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1007
absl::StatusOr
ABSL_NAMESPACE_BEGIN class ABSL_MUST_USE_RESULT StatusOr
Definition: abseil-cpp/absl/status/internal/statusor_internal.h:29
grpc_core
Definition: call_metric_recorder.h:31
elem
Timer elem
Definition: event_engine/iomgr_event_engine/timer_heap_test.cc:109
grpc_core::IsStatusOk
bool IsStatusOk(const absl::Status &status)
Definition: src/core/lib/promise/detail/status.h:46
status
absl::Status status
Definition: rls.cc:251
absl::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: abseil-cpp/absl/meta/type_traits.h:631
grpc_core::promise_detail::TrySeqTraitsWithSfinae
Definition: try_seq.h:36
grpc_core::promise_detail::TrySeqTraitsWithSfinae< T, absl::enable_if_t< std::is_same< decltype(IsStatusOk(std::declval< T >())), bool >::value, void > >::CheckResultAndRunNext
static Poll< Result > CheckResultAndRunNext(T prior, RunNext run_next)
Definition: try_seq.h:91
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
T
#define T(upbtypeconst, upbtype, ctype, default_value)
grpc_core::promise_detail::TrySeqTraitsWithSfinae< T, absl::enable_if_t< std::is_same< decltype(IsStatusOk(std::declval< T >())), bool >::value, void > >::UnwrappedType
void UnwrappedType
Definition: try_seq.h:84
grpc_core::promise_detail::TrySeqTraitsWithSfinae::CallFactory
static auto CallFactory(Next *next, T &&value) -> decltype(next->Once(std::forward< T >(value)))
Definition: try_seq.h:40
gen_gtest_pred_impl.Iter
def Iter(n, format, sep='')
Definition: bloaty/third_party/googletest/googletest/scripts/gen_gtest_pred_impl.py:188
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
grpc_core::promise_detail::TrySeqTraitsWithSfinae::CallSeqFactory
static auto CallSeqFactory(F &f, Elem &&elem, T &&value) -> decltype(f(std::forward< Elem >(elem), std::forward< T >(value)))
Definition: try_seq.h:45
grpc_core::promise_detail::TrySeqTraitsWithSfinae< absl::StatusOr< T > >::CheckResultAndRunNext
static Poll< Result > CheckResultAndRunNext(absl::StatusOr< T > prior, RunNext run_next)
Definition: try_seq.h:70
re2::Result
TestInstance::Result Result
Definition: bloaty/third_party/re2/re2/testing/tester.cc:96
grpc_core::promise_detail::TrySeqTraits
TrySeqTraitsWithSfinae< T > TrySeqTraits
Definition: try_seq.h:114
grpc_core::promise_detail::TrySeqTraitsWithSfinae< absl::Status >::CheckResultAndRunNext
static Poll< Result > CheckResultAndRunNext(absl::Status prior, RunNext run_next)
Definition: try_seq.h:106
argument
Definition: third_party/boringssl-with-bazel/src/tool/internal.h:108
grpc_core::promise_detail::TrySeqTraitsWithSfinae< T, absl::enable_if_t< std::is_same< decltype(IsStatusOk(std::declval< T >())), bool >::value, void > >::WrappedType
T WrappedType
Definition: try_seq.h:85
grpc_core::TrySeqIter
promise_detail::BasicSeqIter< promise_detail::TrySeqTraits, Factory, Argument, Iter > TrySeqIter(Iter begin, Iter end, Argument argument, Factory factory)
Definition: try_seq.h:149
grpc_core::promise_detail::TrySeqTraitsWithSfinae< T, absl::enable_if_t< std::is_same< decltype(IsStatusOk(std::declval< T >())), bool >::value, void > >::CallFactory
static auto CallFactory(Next *next, T &&) -> decltype(next->Once())
Definition: try_seq.h:87
grpc_core::TrySeq
promise_detail::TrySeq< Functors... > TrySeq(Functors... functors)
Definition: try_seq.h:134
grpc_core::promise_detail::TrySeqTraitsWithSfinae< absl::Status >::UnwrappedType
void UnwrappedType
Definition: try_seq.h:98
value
const char * value
Definition: hpack_parser_table.cc:165
absl::Status
ABSL_NAMESPACE_BEGIN class ABSL_MUST_USE_RESULT Status
Definition: abseil-cpp/absl/status/internal/status_internal.h:36
absl::StatusOr::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: abseil-cpp/absl/status/statusor.h:491
grpc_core::promise_detail::TrySeqTraitsWithSfinae::UnwrappedType
T UnwrappedType
Definition: try_seq.h:37
poll.h
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
next
AllocList * next[kMaxLevel]
Definition: abseil-cpp/absl/base/internal/low_level_alloc.cc:100
grpc_core::promise_detail::TrySeqTraitsWithSfinae< absl::StatusOr< T > >::UnwrappedType
T UnwrappedType
Definition: try_seq.h:57
status.h
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
grpc_core::promise_detail::BasicSeq
Definition: basic_seq.h:199
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
grpc_core::promise_detail::TrySeqTraitsWithSfinae< absl::StatusOr< T > >::CallSeqFactory
static auto CallSeqFactory(F &f, Elem &&elem, absl::StatusOr< T > value) -> decltype(f(std::forward< Elem >(elem), std::move(*value)))
Definition: try_seq.h:65
grpc_core::promise_detail::TrySeqTraitsWithSfinae::CheckResultAndRunNext
static Poll< Result > CheckResultAndRunNext(T prior, RunNext run_next)
Definition: try_seq.h:50
basic_seq.h
absl::StatusOr::status
const Status & status() const &
Definition: abseil-cpp/absl/status/statusor.h:678
port_platform.h


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