18#include "pw_async2/internal/poll_internal.h"
19#include "pw_polyfill/language_feature_macros.h"
20#include "pw_string/to_string.h"
42 "`Poll`-returning functions may or may not have completed. Their "
57template <
typename T = ReadyType>
59 "`Poll`-returning functions may or may not have completed. Their "
60 "return value should be examined.")
Poll {
66 constexpr Poll(
const Poll&) =
default;
67 constexpr Poll& operator=(
const Poll&) =
default;
69 constexpr Poll& operator=(
Poll&&) =
default;
81 internal_poll::EnableIfImplicitlyConvertible<value_type, const U&> = 0>
82 constexpr Poll(
const Poll<U>& other) : value_(other.value_) {}
85 internal_poll::EnableIfExplicitlyConvertible<value_type, const U&> = 0>
86 explicit constexpr Poll(
const Poll<U>& other) : value_(other.value_) {}
89 internal_poll::EnableIfImplicitlyConvertible<value_type, U&&> = 0>
90 constexpr Poll(Poll<U>&& other)
91 : value_(std::move(other.value_)) {}
93 internal_poll::EnableIfExplicitlyConvertible<value_type, U&&> = 0>
94 explicit constexpr Poll(Poll<U>&& other) : value_(std::move(other.value_)) {}
103 template <
typename U = value_type,
104 internal_poll::EnableIfImplicitlyInitializable<value_type, U> = 0>
105 constexpr Poll(U&& u)
106 : Poll(std::in_place, std::forward<U>(u)) {}
108 template <
typename U = value_type,
109 internal_poll::EnableIfExplicitlyInitializable<value_type, U> = 0>
110 explicit constexpr Poll(U&& u)
111 : Poll(std::in_place, std::forward<U>(u)) {}
114 template <
typename... Args>
115 constexpr Poll(std::in_place_t, Args&&... args)
116 : value_(std::in_place, std::move(args)...) {}
119 constexpr Poll(value_type&& value) : value_(std::move(value)) {}
120 constexpr Poll& operator=(value_type&& value) {
121 value_ = std::optional<value_type>(std::move(value));
126 constexpr Poll(PendingType) noexcept : value_() {}
127 constexpr Poll& operator=(PendingType)
noexcept {
128 value_ = std::nullopt;
133 constexpr bool IsReady() const noexcept {
return value_.has_value(); }
136 constexpr bool IsPending() const noexcept {
return !value_.has_value(); }
151 constexpr value_type&
value() &
noexcept {
return *value_; }
152 constexpr const value_type& value() const& noexcept {
return *value_; }
153 constexpr value_type&& value() &&
noexcept {
return std::move(*value_); }
154 constexpr const value_type&& value() const&& noexcept {
155 return std::move(*value_);
161 constexpr const value_type*
operator->() const noexcept {
return &*value_; }
162 constexpr value_type* operator->() noexcept {
return &*value_; }
167 constexpr const value_type&
operator*() const& noexcept {
return *value_; }
168 constexpr value_type& operator*() &
noexcept {
return *value_; }
169 constexpr const value_type&& operator*() const&& noexcept {
170 return std::move(*value_);
172 constexpr value_type&& operator*() &&
noexcept {
return std::move(*value_); }
181 template <
typename U>
183 std::optional<value_type> value_;
188Poll(T value) -> Poll<T>;
218 return !(lhs == rhs);
247constexpr bool operator==(ReadyType, ReadyType) {
return true; }
248constexpr bool operator!=(ReadyType, ReadyType) {
return false; }
251constexpr bool operator==(PendingType, PendingType) {
return true; }
252constexpr bool operator!=(PendingType, PendingType) {
return false; }
259template <
typename T,
typename... Args>
261 return Poll<T>(std::in_place, std::forward<Args>(args)...);
289 return ToString(
"Ready", buffer);
293inline StatusWithSize ToString(
const async2::PendingType&, span<char> buffer) {
294 return ToString(
"Pending", buffer);
299inline StatusWithSize ToString(
const async2::Poll<T>& poll, span<char> buffer) {
300 if (poll.IsReady()) {
303 s.UpdateAndAdd(ToString(*poll, buffer.subspan(s.size())));
304 s.UpdateAndAdd(ToString(
")", buffer.subspan(s.size())));
308 return ToString(async2::PendingType{}, buffer);
312inline StatusWithSize ToString(
const async2::Poll<>& poll, span<char> buffer) {
313 if (poll.IsReady()) {
314 return ToString(async2::ReadyType{}, buffer);
316 return ToString(async2::PendingType{}, buffer);
Definition: status_with_size.h:51
constexpr void UpdateAndAdd(StatusWithSize new_status_with_size)
Definition: status_with_size.h:128
Definition: span_impl.h:235
constexpr const value_type & operator*() const &noexcept
Definition: poll.h:167
constexpr bool operator!=(const Poll< T > &lhs, const Poll< T > &rhs)
Definition: poll.h:217
constexpr bool IsReady() const noexcept
Returns whether or not this value is Ready.
Definition: poll.h:133
constexpr bool operator==(const Poll< T > &lhs, const Poll< T > &rhs)
Definition: poll.h:204
constexpr Poll Readiness() const noexcept
Definition: poll.h:140
constexpr const value_type * operator->() const noexcept
Definition: poll.h:161
constexpr bool IsPending() const noexcept
Returns whether or not this value is Pending.
Definition: poll.h:136
constexpr Poll(const Poll< U > &other)
Definition: poll.h:82
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:271
constexpr value_type & value() &noexcept
Definition: poll.h:151
Poll()=delete
Basic constructors.
constexpr void IgnorePoll() const
Definition: poll.h:178
constexpr Poll Ready()
Returns a value indicating completion.
Definition: poll.h:255
#define PW_NODISCARD_STR(str)
Definition: language_feature_macros.h:61
The Pigweed namespace.
Definition: alignment.h:27