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