Debian Codegraph @

src/core/lib/promise/if.h

raw
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
18 #include <grpc/support/port_platform.h>
19
20 #include <type_traits>
21
22 #include "absl/status/statusor.h"
23 #include "absl/types/variant.h"
24
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
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->Make();
38 return call_poll(promise);
39 } else {
40 auto promise = if_false->Make();
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->Make();
53 return call_poll(promise);
54 } else {
55 auto promise = if_false->Make();
56 return call_poll(promise);
57 }
58 }
59
60 template <typename C, typename T, typename F>
61 class If {
62 private:
63 using TrueFactory = promise_detail::OncePromiseFactory<void, T>;
64 using FalseFactory = promise_detail::OncePromiseFactory<void, F>;
65 using ConditionPromise = PromiseLike<C>;
66 using TruePromise = typename TrueFactory::Promise;
67 using FalsePromise = typename FalseFactory::Promise;
68 using Result =
69 typename PollTraits<decltype(std::declval<TruePromise>()())>::Type;
70
71 public:
72 If(C condition, T if_true, F if_false)
73 : state_(Evaluating{ConditionPromise(std::move(condition)),
74 TrueFactory(std::move(if_true)),
75 FalseFactory(std::move(if_false))}) {}
76
77 Poll<Result> operator()() {
78 return absl::visit(CallPoll<false>{this}, state_);
79 }
80
81 private:
82 struct Evaluating {
83 ConditionPromise condition;
84 TrueFactory if_true;
85 FalseFactory if_false;
86 };
87 using State = absl::variant<Evaluating, TruePromise, FalsePromise>;
88 State state_;
89
90 template <bool kSetState>
91 struct CallPoll {
92 using PollResult = Poll<Result>;
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