18#include "pw_async2/internal/poll_internal.h"
19#include "pw_polyfill/language_feature_macros.h"
20#include "pw_string/to_string.h"
35struct PW_NODISCARD_STR(
36 "`Poll`-returning functions may or may not have completed. Their "
51template <
typename T = ReadyType>
52class PW_NODISCARD_STR(
53 "`Poll`-returning functions may or may not have completed. Their "
54 "return value should be examined.")
Poll {
60 constexpr Poll(
const Poll&) =
default;
61 constexpr Poll& operator=(
const Poll&) =
default;
63 constexpr Poll& operator=(
Poll&&) =
default;
74 internal_poll::EnableIfImplicitlyConvertible<T, const U&> = 0>
75 constexpr Poll(
const Poll<U>& other) : value_(other.value_) {}
77 internal_poll::EnableIfExplicitlyConvertible<T, const U&> = 0>
78 explicit constexpr Poll(
const Poll<U>& other) : value_(other.value_) {}
81 internal_poll::EnableIfImplicitlyConvertible<T, U&&> = 0>
82 constexpr Poll(Poll<U>&& other)
83 : value_(std::move(other.value_)) {}
85 internal_poll::EnableIfExplicitlyConvertible<T, U&&> = 0>
86 explicit constexpr Poll(Poll<U>&& other) : value_(std::move(other.value_)) {}
95 template <
typename U = T,
96 internal_poll::EnableIfImplicitlyInitializable<T, U> = 0>
98 : Poll(std::in_place, std::forward<U>(u)) {}
100 template <
typename U = T,
101 internal_poll::EnableIfExplicitlyInitializable<T, U> = 0>
102 explicit constexpr Poll(U&& u)
103 : Poll(std::in_place, std::forward<U>(u)) {}
106 template <
typename... Args>
107 constexpr Poll(std::in_place_t, Args&&... args)
108 : value_(std::in_place, std::move(args)...) {}
111 constexpr Poll(T&& value) : value_(std::move(value)) {}
112 constexpr Poll& operator=(T&& value) {
113 value_ = std::optional<T>(std::move(value));
118 constexpr Poll(PendingType) noexcept : value_() {}
119 constexpr Poll& operator=(PendingType)
noexcept {
120 value_ = std::nullopt;
125 constexpr bool IsReady() const noexcept {
return value_.has_value(); }
128 constexpr bool IsPending() const noexcept {
return !value_.has_value(); }
143 constexpr T&
value() &
noexcept {
return *value_; }
144 constexpr const T& value() const& noexcept {
return *value_; }
145 constexpr T&& value() &&
noexcept {
return std::move(*value_); }
146 constexpr const T&& value() const&& noexcept {
return std::move(*value_); }
151 constexpr const T*
operator->() const noexcept {
return &*value_; }
152 constexpr T* operator->() noexcept {
return &*value_; }
157 constexpr const T&
operator*() const& noexcept {
return *value_; }
158 constexpr T& operator*() &
noexcept {
return *value_; }
159 constexpr const T&& operator*() const&& noexcept {
160 return std::move(*value_);
162 constexpr T&& operator*() &&
noexcept {
return std::move(*value_); }
171 template <
typename U>
173 std::optional<T> value_;
178Poll(T value) -> Poll<T>;
186constexpr bool operator==(
const Poll<T>& lhs,
const Poll<T>& rhs) {
187 if (lhs.IsReady() && rhs.IsReady()) {
190 return lhs.IsReady() == rhs.IsReady();
199constexpr bool operator!=(
const Poll<T>& lhs,
const Poll<T>& rhs) {
200 return !(lhs == rhs);
205constexpr bool operator==(
const Poll<T>& lhs, PendingType) {
206 return lhs.IsPending();
211constexpr bool operator!=(
const Poll<T>& lhs, PendingType) {
212 return !lhs.IsPending();
217constexpr bool operator==(PendingType,
const Poll<T>& rhs) {
218 return rhs.IsPending();
223constexpr bool operator!=(PendingType,
const Poll<T>& rhs) {
224 return !rhs.IsPending();
229constexpr bool operator==(ReadyType, ReadyType) {
return true; }
230constexpr bool operator!=(ReadyType, ReadyType) {
return false; }
233constexpr bool operator==(PendingType, PendingType) {
return true; }
234constexpr bool operator!=(PendingType, PendingType) {
return false; }
237inline constexpr Poll<> Ready() {
return Poll(ReadyType{}); }
241template <
typename T,
typename... Args>
242constexpr Poll<T> Ready(std::in_place_t, Args&&... args) {
243 return Poll<T>(std::in_place, std::forward<Args>(args)...);
248constexpr Poll<std::remove_reference_t<T>> Ready(T&& value) {
249 return Poll<std::remove_reference_t<T>>(std::forward<T>(value));
253inline constexpr PendingType Pending() {
return PendingType(); }
260inline StatusWithSize ToString(
const async2::ReadyType&, span<char> buffer) {
261 return ToString(
"Ready", buffer);
265inline StatusWithSize ToString(
const async2::PendingType&, span<char> buffer) {
266 return ToString(
"Pending", buffer);
271inline StatusWithSize ToString(
const async2::Poll<T>& poll, span<char> buffer) {
272 if (poll.IsReady()) {
275 s.UpdateAndAdd(ToString(*poll, buffer.subspan(s.size())));
276 s.UpdateAndAdd(ToString(
")", buffer.subspan(s.size())));
280 return ToString(async2::PendingType{}, buffer);
284inline StatusWithSize ToString(
const async2::Poll<>& poll, span<char> buffer) {
285 if (poll.IsReady()) {
286 return ToString(async2::ReadyType{}, buffer);
288 return ToString(async2::PendingType{}, buffer);
constexpr void UpdateAndAdd(StatusWithSize new_status_with_size)
Definition: status_with_size.h:126
constexpr bool IsReady() const noexcept
Returns whether or not this value is Ready.
Definition: poll.h:125
constexpr const T * operator->() const noexcept
Definition: poll.h:151
constexpr Poll Readiness() const noexcept
Definition: poll.h:132
constexpr bool IsPending() const noexcept
Returns whether or not this value is Pending.
Definition: poll.h:128
constexpr T & value() &noexcept
Definition: poll.h:143
constexpr Poll(const Poll< U > &other)
Definition: poll.h:75
Poll()=delete
Basic constructors.
constexpr void IgnorePoll() const
Definition: poll.h:168
constexpr const T & operator*() const &noexcept
Definition: poll.h:157
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27