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_MAP_H
16 #define GRPC_SRC_CORE_LIB_PROMISE_MAP_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <stddef.h>
21
22 #include <tuple>
23 #include <utility>
24
25 #include "src/core/lib/promise/detail/promise_like.h"
26 #include "src/core/lib/promise/poll.h"
27
28 namespace grpc_core {
29
30 namespace promise_detail {
31
32 // Implementation of mapping combinator - use this via the free function below!
33 // Promise is the type of promise to poll on, Fn is a function that takes the
34 // result of Promise and maps it to some new type.
35 template <typename Promise, typename Fn>
36 class Map {
37 public:
Map(Promise promise,Fn fn)38 Map(Promise promise, Fn fn)
39 : promise_(std::move(promise)), fn_(std::move(fn)) {}
40
41 Map(const Map&) = delete;
42 Map& operator=(const Map&) = delete;
43 // NOLINTNEXTLINE(performance-noexcept-move-constructor): clang6 bug
44 Map(Map&& other) = default;
45 // NOLINTNEXTLINE(performance-noexcept-move-constructor): clang6 bug
46 Map& operator=(Map&& other) = default;
47
48 using PromiseResult = typename PromiseLike<Promise>::Result;
49 using Result =
50 RemoveCVRef<decltype(std::declval<Fn>()(std::declval<PromiseResult>()))>;
51
operator()52 Poll<Result> operator()() {
53 Poll<PromiseResult> r = promise_();
54 if (auto* p = r.value_if_ready()) {
55 return fn_(std::move(*p));
56 }
57 return Pending();
58 }
59
60 private:
61 PromiseLike<Promise> promise_;
62 Fn fn_;
63 };
64
65 } // namespace promise_detail
66
67 // Mapping combinator.
68 // Takes a promise, and a synchronous function to mutate its result, and
69 // returns a promise.
70 template <typename Promise, typename Fn>
Map(Promise promise,Fn fn)71 promise_detail::Map<Promise, Fn> Map(Promise promise, Fn fn) {
72 return promise_detail::Map<Promise, Fn>(std::move(promise), std::move(fn));
73 }
74
75 // Callable that takes a tuple and returns one element
76 template <size_t kElem>
77 struct JustElem {
78 template <typename... A>
79 auto operator()(std::tuple<A...>&& t) const
80 -> decltype(std::get<kElem>(std::forward<std::tuple<A...>>(t))) {
81 return std::get<kElem>(std::forward<std::tuple<A...>>(t));
82 }
83 template <typename... A>
84 auto operator()(const std::tuple<A...>& t) const
85 -> decltype(std::get<kElem>(t)) {
86 return std::get<kElem>(t);
87 }
88 };
89
90 } // namespace grpc_core
91
92 #endif // GRPC_SRC_CORE_LIB_PROMISE_MAP_H
93