src/core/lib/promise/map.h
Go to the documentation of this file.
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 
19 
20 #include <stddef.h>
21 
22 #include <tuple>
23 #include <type_traits>
24 #include <utility>
25 
26 #include "absl/types/variant.h"
27 
30 
31 namespace grpc_core {
32 
33 namespace promise_detail {
34 
35 // Implementation of mapping combinator - use this via the free function below!
36 // Promise is the type of promise to poll on, Fn is a function that takes the
37 // result of Promise and maps it to some new type.
38 template <typename Promise, typename Fn>
39 class Map {
40  public:
41  Map(Promise promise, Fn fn)
42  : promise_(std::move(promise)), fn_(std::move(fn)) {}
43 
45  using Result =
46  RemoveCVRef<decltype(std::declval<Fn>()(std::declval<PromiseResult>()))>;
47 
50  if (auto* p = absl::get_if<kPollReadyIdx>(&r)) {
51  return fn_(std::move(*p));
52  }
53  return Pending();
54  }
55 
56  private:
59 };
60 
61 } // namespace promise_detail
62 
63 // Mapping combinator.
64 // Takes a promise, and a synchronous function to mutate its result, and
65 // returns a promise.
66 template <typename Promise, typename Fn>
69 }
70 
71 // Callable that takes a tuple and returns one element
72 template <size_t kElem>
73 struct JustElem {
74  template <typename... A>
75  auto operator()(std::tuple<A...>&& t) const
76  -> decltype(std::get<kElem>(std::forward<std::tuple<A...>>(t))) {
77  return std::get<kElem>(std::forward<std::tuple<A...>>(t));
78  }
79  template <typename... A>
80  auto operator()(const std::tuple<A...>& t) const
81  -> decltype(std::get<kElem>(t)) {
82  return std::get<kElem>(t);
83  }
84 };
85 
86 } // namespace grpc_core
87 
88 #endif // GRPC_CORE_LIB_PROMISE_MAP_H
grpc_core::promise_detail::Map::fn_
Fn fn_
Definition: src/core/lib/promise/map.h:58
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::JustElem::operator()
auto operator()(const std::tuple< A... > &t) const -> decltype(std::get< kElem >(t))
Definition: src/core/lib/promise/map.h:80
grpc_core::JustElem::operator()
auto operator()(std::tuple< A... > &&t) const -> decltype(std::get< kElem >(std::forward< std::tuple< A... >>(t)))
Definition: src/core/lib/promise/map.h:75
grpc_core::Pending
Definition: poll.h:29
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
generate-asm-lcov.fn
fn
Definition: generate-asm-lcov.py:146
grpc_core::promise_detail::Map::Map
Map(Promise promise, Fn fn)
Definition: src/core/lib/promise/map.h:41
grpc_core::promise_detail::Map
Definition: src/core/lib/promise/map.h:39
grpc_core::JustElem
Definition: src/core/lib/promise/map.h:73
grpc_core::promise_detail::Map::operator()
Poll< Result > operator()()
Definition: src/core/lib/promise/map.h:48
grpc_core::promise_detail::Map::PromiseResult
typename PromiseLike< Promise >::Result PromiseResult
Definition: src/core/lib/promise/map.h:44
promise_like.h
poll.h
grpc_core::Promise
std::function< Poll< T >()> Promise
Definition: promise/promise.h:37
fix_build_deps.r
r
Definition: fix_build_deps.py:491
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
A
Definition: miscompile_with_no_unique_address_test.cc:23
grpc_core::promise_detail::RemoveCVRef
absl::remove_cv_t< absl::remove_reference_t< T > > RemoveCVRef
Definition: promise_like.h:80
grpc_core::Map
promise_detail::Map< Promise, Fn > Map(Promise promise, Fn fn)
Definition: src/core/lib/promise/map.h:67
grpc_core::promise_detail::Map::promise_
PromiseLike< Promise > promise_
Definition: src/core/lib/promise/map.h:57
absl::forward
constexpr T && forward(absl::remove_reference_t< T > &t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:230
absl::variant
Definition: abseil-cpp/absl/types/internal/variant.h:46
grpc_core::promise_detail::PromiseLike
Definition: promise_like.h:67
grpc_core::promise_detail::Map::Result
RemoveCVRef< decltype(std::declval< Fn >()(std::declval< PromiseResult >()))> Result
Definition: src/core/lib/promise/map.h:46
Fn
void Fn()
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:18