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