• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SRC_CORE_LIB_PROMISE_IF_H
16 #define GRPC_SRC_CORE_LIB_PROMISE_IF_H
17 
18 #include <grpc/support/port_platform.h>
19 
20 #include <memory>
21 #include <utility>
22 
23 #include "absl/status/statusor.h"
24 #include "absl/types/variant.h"
25 
26 #include "src/core/lib/gprpp/construct_destruct.h"
27 #include "src/core/lib/promise/detail/promise_factory.h"
28 #include "src/core/lib/promise/detail/promise_like.h"
29 #include "src/core/lib/promise/poll.h"
30 
31 namespace grpc_core {
32 
33 namespace promise_detail {
34 
35 template <typename CallPoll, typename T, typename F>
ChooseIf(CallPoll call_poll,bool result,T * if_true,F * if_false)36 typename CallPoll::PollResult ChooseIf(CallPoll call_poll, bool result,
37                                        T* if_true, F* if_false) {
38   if (result) {
39     auto promise = if_true->Make();
40     return call_poll(promise);
41   } else {
42     auto promise = if_false->Make();
43     return call_poll(promise);
44   }
45 }
46 
47 template <typename CallPoll, typename T, typename F>
ChooseIf(CallPoll call_poll,absl::StatusOr<bool> result,T * if_true,F * if_false)48 typename CallPoll::PollResult ChooseIf(CallPoll call_poll,
49                                        absl::StatusOr<bool> result, T* if_true,
50                                        F* if_false) {
51   if (!result.ok()) {
52     return typename CallPoll::PollResult(result.status());
53   } else if (*result) {
54     auto promise = if_true->Make();
55     return call_poll(promise);
56   } else {
57     auto promise = if_false->Make();
58     return call_poll(promise);
59   }
60 }
61 
62 template <typename C, typename T, typename F>
63 class If {
64  private:
65   using TrueFactory = promise_detail::OncePromiseFactory<void, T>;
66   using FalseFactory = promise_detail::OncePromiseFactory<void, F>;
67   using ConditionPromise = PromiseLike<C>;
68   using TruePromise = typename TrueFactory::Promise;
69   using FalsePromise = typename FalseFactory::Promise;
70   using Result =
71       typename PollTraits<decltype(std::declval<TruePromise>()())>::Type;
72 
73  public:
If(C condition,T if_true,F if_false)74   If(C condition, T if_true, F if_false)
75       : state_(Evaluating{ConditionPromise(std::move(condition)),
76                           TrueFactory(std::move(if_true)),
77                           FalseFactory(std::move(if_false))}) {}
78 
operator()79   Poll<Result> operator()() {
80     return absl::visit(CallPoll<false>{this}, state_);
81   }
82 
83  private:
84   struct Evaluating {
85     ConditionPromise condition;
86     TrueFactory if_true;
87     FalseFactory if_false;
88   };
89   using State = absl::variant<Evaluating, TruePromise, FalsePromise>;
90   State state_;
91 
92   template <bool kSetState>
93   struct CallPoll {
94     using PollResult = Poll<Result>;
95 
96     If* const self;
97 
operatorCallPoll98     PollResult operator()(Evaluating& evaluating) const {
99       static_assert(
100           !kSetState,
101           "shouldn't need to set state coming through the initial branch");
102       auto r = evaluating.condition();
103       if (auto* p = r.value_if_ready()) {
104         return ChooseIf(CallPoll<true>{self}, std::move(*p),
105                         &evaluating.if_true, &evaluating.if_false);
106       }
107       return Pending();
108     }
109 
110     template <class Promise>
operatorCallPoll111     PollResult operator()(Promise& promise) const {
112       auto r = promise();
113       if (kSetState && r.pending()) {
114         self->state_.template emplace<Promise>(std::move(promise));
115       }
116       return r;
117     }
118   };
119 };
120 
121 template <typename T, typename F>
122 class If<bool, T, F> {
123  private:
124   using TrueFactory = promise_detail::OncePromiseFactory<void, T>;
125   using FalseFactory = promise_detail::OncePromiseFactory<void, F>;
126   using TruePromise = typename TrueFactory::Promise;
127   using FalsePromise = typename FalseFactory::Promise;
128   using Result =
129       typename PollTraits<decltype(std::declval<TruePromise>()())>::Type;
130 
131  public:
If(bool condition,T if_true,F if_false)132   If(bool condition, T if_true, F if_false) : condition_(condition) {
133     TrueFactory true_factory(std::move(if_true));
134     FalseFactory false_factory(std::move(if_false));
135     if (condition_) {
136       Construct(&if_true_, true_factory.Make());
137     } else {
138       Construct(&if_false_, false_factory.Make());
139     }
140   }
~If()141   ~If() {
142     if (condition_) {
143       Destruct(&if_true_);
144     } else {
145       Destruct(&if_false_);
146     }
147   }
148 
149   If(const If&) = delete;
150   If& operator=(const If&) = delete;
If(If && other)151   If(If&& other) noexcept : condition_(other.condition_) {
152     if (condition_) {
153       Construct(&if_true_, std::move(other.if_true_));
154     } else {
155       Construct(&if_false_, std::move(other.if_false_));
156     }
157   }
158   If& operator=(If&& other) noexcept {
159     if (&other == this) return *this;
160     Destruct(this);
161     Construct(this, std::move(other));
162     return *this;
163   }
164 
operator()165   Poll<Result> operator()() {
166 #ifndef NDEBUG
167     asan_canary_ = std::make_unique<int>(1 + *asan_canary_);
168 #endif
169     if (condition_) {
170       return if_true_();
171     } else {
172       return if_false_();
173     }
174   }
175 
176  private:
177   bool condition_;
178   union {
179     TruePromise if_true_;
180     FalsePromise if_false_;
181   };
182   // Make failure to destruct show up in ASAN builds.
183 #ifndef NDEBUG
184   std::unique_ptr<int> asan_canary_ = std::make_unique<int>(0);
185 #endif
186 };
187 
188 }  // namespace promise_detail
189 
190 // If promise combinator.
191 // Takes 3 promise factories, and evaluates the first.
192 // If it returns failure, returns failure for the entire combinator.
193 // If it returns true, evaluates the second promise.
194 // If it returns false, evaluates the third promise.
195 // If C is a constant, it's guaranteed that one of the promise factories
196 // if_true or if_false will be evaluated before returning from this function.
197 // This makes it safe to capture lambda arguments in the promise factory by
198 // reference.
199 template <typename C, typename T, typename F>
If(C condition,T if_true,F if_false)200 promise_detail::If<C, T, F> If(C condition, T if_true, F if_false) {
201   return promise_detail::If<C, T, F>(std::move(condition), std::move(if_true),
202                                      std::move(if_false));
203 }
204 
205 }  // namespace grpc_core
206 
207 #endif  // GRPC_SRC_CORE_LIB_PROMISE_IF_H
208