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