1 // Copyright 2023 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_SWITCH_H
16 #define GRPC_SRC_CORE_LIB_PROMISE_SWITCH_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <memory>
21 #include <utility>
22
23 #include "src/core/lib/promise/detail/promise_factory.h"
24 #include "src/core/lib/promise/if.h"
25
26 namespace grpc_core {
27
28 namespace promise_detail {
29 template <typename D, typename F>
30 struct Case {
31 D discriminator;
32 F factory;
33 };
34
35 template <typename F>
36 struct Default {
37 F factory;
38 };
39 } // namespace promise_detail
40
41 template <typename D, typename PromiseFactory>
Case(D discriminator,PromiseFactory f)42 auto Case(D discriminator, PromiseFactory f) {
43 return promise_detail::Case<D, PromiseFactory>{discriminator, std::move(f)};
44 }
45
46 template <typename PromiseFactory>
Default(PromiseFactory f)47 auto Default(PromiseFactory f) {
48 return promise_detail::Default<PromiseFactory>{std::move(f)};
49 }
50
51 // Given a list of cases that result in promise factories, return a single
52 // promise chosen by the discriminator (the first argument of this function).
53 // e.g.:
54 // Switch(1, Case<1>([] { return 43; }), Case<2>([] { return 44; }))
55 // resolves to 43.
56 // TODO(ctiller): consider writing a code-generator like we do for seq/join
57 // so that this lowers into a C switch statement.
58 template <typename D, typename F>
Switch(D,promise_detail::Default<F> def)59 auto Switch(D, promise_detail::Default<F> def) {
60 return promise_detail::OncePromiseFactory<void, F>(std::move(def.factory))
61 .Make();
62 }
63
64 template <typename D, typename F, typename... Others>
Switch(D discriminator,promise_detail::Case<D,F> c,Others...others)65 auto Switch(D discriminator, promise_detail::Case<D, F> c, Others... others) {
66 return If(discriminator == c.discriminator, std::move(c.factory),
67 Switch(discriminator, std::move(others)...));
68 }
69
70 } // namespace grpc_core
71
72 #endif // GRPC_SRC_CORE_LIB_PROMISE_SWITCH_H
73