if.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_IF_H
16 #define GRPC_CORE_LIB_PROMISE_IF_H
17 
19 
20 #include <type_traits>
21 
22 #include "absl/status/statusor.h"
23 #include "absl/types/variant.h"
24 
28 
29 namespace grpc_core {
30 
31 namespace promise_detail {
32 
33 template <typename CallPoll, typename T, typename F>
34 typename CallPoll::PollResult ChooseIf(CallPoll call_poll, bool result,
35  T* if_true, F* if_false) {
36  if (result) {
37  auto promise = if_true->Once();
38  return call_poll(promise);
39  } else {
40  auto promise = if_false->Once();
41  return call_poll(promise);
42  }
43 }
44 
45 template <typename CallPoll, typename T, typename F>
46 typename CallPoll::PollResult ChooseIf(CallPoll call_poll,
47  absl::StatusOr<bool> result, T* if_true,
48  F* if_false) {
49  if (!result.ok()) {
50  return typename CallPoll::PollResult(result.status());
51  } else if (*result) {
52  auto promise = if_true->Once();
53  return call_poll(promise);
54  } else {
55  auto promise = if_false->Once();
56  return call_poll(promise);
57  }
58 }
59 
60 template <typename C, typename T, typename F>
61 class If {
62  private:
68  using Result =
70 
71  public:
72  If(C condition, T if_true, F if_false)
74  TrueFactory(std::move(if_true)),
75  FalseFactory(std::move(if_false))}) {}
76 
78  return absl::visit(CallPoll<false>{this}, state_);
79  }
80 
81  private:
82  struct Evaluating {
86  };
89 
90  template <bool kSetState>
91  struct CallPoll {
93 
94  If* const self;
95 
96  PollResult operator()(Evaluating& evaluating) const {
97  static_assert(
98  !kSetState,
99  "shouldn't need to set state coming through the initial branch");
100  auto r = evaluating.condition();
101  if (auto* p = absl::get_if<kPollReadyIdx>(&r)) {
102  return ChooseIf(CallPoll<true>{self}, std::move(*p),
103  &evaluating.if_true, &evaluating.if_false);
104  }
105  return Pending();
106  }
107 
108  template <class Promise>
109  PollResult operator()(Promise& promise) const {
110  auto r = promise();
111  if (kSetState && absl::holds_alternative<Pending>(r)) {
112  self->state_.template emplace<Promise>(std::move(promise));
113  }
114  return r;
115  }
116  };
117 };
118 
119 } // namespace promise_detail
120 
121 // If promise combinator.
122 // Takes 3 promise factories, and evaluates the first.
123 // If it returns failure, returns failure for the entire combinator.
124 // If it returns true, evaluates the second promise.
125 // If it returns false, evaluates the third promise.
126 template <typename C, typename T, typename F>
127 promise_detail::If<C, T, F> If(C condition, T if_true, F if_false) {
128  return promise_detail::If<C, T, F>(std::move(condition), std::move(if_true),
129  std::move(if_false));
130 }
131 
132 } // namespace grpc_core
133 
134 #endif // GRPC_CORE_LIB_PROMISE_IF_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::promise_detail::If::Evaluating::if_true
TrueFactory if_true
Definition: if.h:84
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::promise_detail::ChooseIf
CallPoll::PollResult ChooseIf(CallPoll call_poll, bool result, T *if_true, F *if_false)
Definition: if.h:34
grpc_core::promise_detail::If::ConditionPromise
PromiseLike< C > ConditionPromise
Definition: if.h:65
grpc_core::promise_detail::If::FalsePromise
typename FalseFactory::Promise FalsePromise
Definition: if.h:67
grpc_core::promise_detail::If::Evaluating
Definition: if.h:82
T
#define T(upbtypeconst, upbtype, ctype, default_value)
promise_factory.h
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::promise_detail::If::TruePromise
typename TrueFactory::Promise TruePromise
Definition: if.h:66
grpc_core::PollTraits
Definition: poll.h:54
grpc_core::promise_detail::If::operator()
Poll< Result > operator()()
Definition: if.h:77
grpc_core::promise_detail::If::TrueFactory
promise_detail::PromiseFactory< void, T > TrueFactory
Definition: if.h:63
grpc_core::promise_detail::If::FalseFactory
promise_detail::PromiseFactory< void, F > FalseFactory
Definition: if.h:64
grpc_core::promise_detail::If::Evaluating::if_false
FalseFactory if_false
Definition: if.h:85
grpc_core::promise_detail::If
Definition: if.h:61
grpc_core::promise_detail::PromiseFactory< void, F >::Promise
decltype(PromiseFactoryImpl(std::move(f_))) Promise
Definition: promise_factory.h:176
grpc_core::promise_detail::If::state_
State state_
Definition: if.h:88
grpc_core::promise_detail::If::Result
typename PollTraits< decltype(std::declval< TruePromise >()())>::Type Result
Definition: if.h:69
promise_like.h
grpc_core::promise_detail::PromiseFactory< void, T >
poll.h
grpc_core::Promise
std::function< Poll< T >()> Promise
Definition: promise/promise.h:37
fix_build_deps.r
r
Definition: fix_build_deps.py:491
grpc_core::promise_detail::If::Evaluating::condition
ConditionPromise condition
Definition: if.h:83
grpc_core::promise_detail::PromiseFactory< void, F >
Definition: promise_factory.h:170
grpc_core::promise_detail::PromiseFactory< void, T >::Promise
decltype(PromiseFactoryImpl(std::move(f_), std::declval< void >())) Promise
Definition: promise_factory.h:156
grpc_core::If
promise_detail::If< C, T, F > If(C condition, T if_true, F if_false)
Definition: if.h:127
grpc_core::promise_detail::If::CallPoll::operator()
PollResult operator()(Promise &promise) const
Definition: if.h:109
grpc_core::promise_detail::If::CallPoll::operator()
PollResult operator()(Evaluating &evaluating) const
Definition: if.h:96
absl::StatusOr
Definition: abseil-cpp/absl/status/statusor.h:187
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
grpc_core::promise_detail::PromiseLike< C >
grpc_core::promise_detail::If::CallPoll
Definition: if.h:91
grpc_core::promise_detail::If::If
If(C condition, T if_true, F if_false)
Definition: if.h:72
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:00:14