| 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_POLL_H |
| 16 |
#define GRPC_CORE_LIB_PROMISE_POLL_H |
| 17 |
|
| 18 |
#include <grpc/support/port_platform.h> |
| 19 |
|
| 20 |
#include <stddef.h> |
| 21 |
|
| 22 |
#include <string> |
| 23 |
|
| 24 |
#include "absl/types/variant.h" |
| 25 |
|
| 26 |
namespace grpc_core { |
| 27 |
|
| 28 |
// A type that signals a Promise is still pending and not yet completed. |
| 29 |
// Allows writing 'return Pending{}' and with automatic conversions gets |
| 30 |
// upgraded to a Poll<> object. |
| 31 |
struct Pending { |
| 32 |
constexpr bool operator==(Pending) const { return true; } |
| 33 |
}; |
| 34 |
|
| 35 |
// The result of polling a Promise once. |
| 36 |
// |
| 37 |
// Can be either pending - the Promise has not yet completed, or ready - |
| 38 |
// indicating that the Promise has completed AND should not be polled again. |
| 39 |
template <typename T> |
| 40 |
using Poll = absl::variant<Pending, T>; |
| 41 |
|
| 42 |
template <typename T, typename U> |
| 43 |
Poll<T> poll_cast(Poll<U> poll) { |
| 44 |
if (absl::holds_alternative<Pending>(poll)) return Pending{}; |
| 45 |
return std::move(absl::get<U>(poll)); |
| 46 |
} |
| 47 |
|
| 48 |
// Variant of Poll that serves as a ready value |
| 49 |
static constexpr size_t kPollReadyIdx = 1; |
| 50 |
|
| 51 |
// PollTraits tells us whether a type is Poll<> or some other type, and is |
| 52 |
// leveraged in the PromiseLike/PromiseFactory machinery to select the |
| 53 |
// appropriate implementation of those concepts based upon the return type of a |
| 54 |
// lambda, for example (via enable_if). |
| 55 |
template <typename T> |
| 56 |
struct PollTraits { |
| 57 |
static constexpr bool is_poll() { return false; } |
| 58 |
}; |
| 59 |
|
| 60 |
template <typename T> |
| 61 |
struct PollTraits<Poll<T>> { |
| 62 |
using Type = T; |
| 63 |
static constexpr bool is_poll() { return true; } |
| 64 |
}; |
| 65 |
|
| 66 |
// Convert a poll to a string |
| 67 |
template <typename T, typename F> |
| 68 |
std::string PollToString( |
| 69 |
const Poll<T>& poll, |
| 70 |
F t_to_string = [](const T& t) { return t.ToString(); }) { |
| 71 |
if (absl::holds_alternative<Pending>(poll)) { |
| 72 |
return "<<pending>>"; |
| 73 |
} |
| 74 |
return t_to_string(absl::get<T>(poll)); |
| 75 |
} |
| 76 |
|
| 77 |
} // namespace grpc_core |
| 78 |
|
| 79 |
#endif // GRPC_CORE_LIB_PROMISE_POLL_H |