18#include "pw_allocator/allocator.h"
19#include "pw_allocator/unique_ptr.h"
20#include "pw_async2/future.h"
21#include "pw_async2/poll.h"
39 virtual bool is_pendable()
const = 0;
40 virtual bool is_complete()
const = 0;
44template <
typename T,
typename FutureType>
47 explicit BoxedFutureImpl(FutureType&& future) : future_(std::move(future)) {}
49 bool is_pendable()
const override {
return future_.is_pendable(); }
50 bool is_complete()
const override {
return future_.is_complete(); }
70 BoxedFuture() : future_(nullptr) { state_.completed =
false; }
81 future_ = other.future_;
86 ~BoxedFuture() { Destroy(); }
90 return future_ !=
nullptr && future_->is_pendable();
95 return future_ ==
nullptr ? state_.completed : future_->is_complete();
100 PW_ASSERT(future_ !=
nullptr);
101 Poll<T> result = future_->Pend(cx);
105 state_.completed =
true;
111 template <
typename FutureType>
118 state_.allocator = allocator;
122 if (future_ !=
nullptr) {
123 state_.allocator = other.state_.allocator;
125 state_.completed = other.state_.completed;
127 other.future_ =
nullptr;
128 other.state_.completed =
false;
132 if (future_ !=
nullptr) {
133 Allocator* alloc = state_.allocator;
134 future_->~BoxedFutureBase();
135 alloc->Deallocate(future_);
139 internal::BoxedFutureBase<T>* future_;
141 Allocator* allocator;
159template <
typename FutureType>
162 static_assert(!std::is_lvalue_reference_v<FutureType>);
163 using ValueType =
typename std::decay_t<FutureType>::value_type;
166 auto ptr = alloc.
New<ImplType>(std::forward<FutureType>(future));
167 if (ptr ==
nullptr) {
Definition: allocator.h:45
std::enable_if_t<!std::is_array_v< T >, T * > New(Args &&... args)
Definition: allocator.h:66
bool is_complete() const
Returns whether the future has completed.
Definition: box.h:94
bool is_pendable() const
Returns whether Pend() can be called.
Definition: box.h:89
Poll< T > Pend(Context &cx)
Advances the future.
Definition: box.h:99
BoxedFuture()
Constructs an empty BoxedFuture.
Definition: box.h:70
friend BoxedFuture< typename std::decay_t< FutureType >::value_type > BoxFuture(Allocator &alloc, FutureType &&future)
Definition: box.h:160
constexpr bool IsReady() const noexcept
Returns whether or not this value is Ready.
Definition: poll.h:211