16#include "pw_assert/assert.h"
17#include "pw_async2/future.h"
24class BroadcastValueProvider;
37 :
Base(Base::kMovedFrom), value_(std::move(other.value_)) {
38 Base::MoveFrom(other);
43 value_ = std::move(other.value_);
44 Base::MoveFrom(other);
51 return ValueFuture(std::in_place, std::move(value));
56 template <
typename... Args>
58 return ValueFuture(std::in_place, std::forward<Args>(args)...);
66 static constexpr const char kWaitReason[] =
"ValueFuture";
70 explicit ValueFuture(SingleFutureProvider<ValueFuture<T>>& provider)
73 template <
typename... Args>
74 explicit ValueFuture(std::in_place_t, Args&&... args)
75 : Base(Base::kReadyForCompletion),
76 value_(std::in_place, std::forward<Args>(args)...) {}
78 template <
typename... Args>
79 void Resolve(Args&&... args) {
81 std::lock_guard guard(Base::lock());
82 PW_ASSERT(!value_.has_value());
83 value_.emplace(std::forward<Args>(args)...);
89 Poll<T> DoPend(Context&) {
90 std::lock_guard guard(Base::lock());
91 if (value_.has_value()) {
92 T value = std::move(value_.value());
94 return Ready(std::move(value));
99 std::optional<T> value_;
111 :
Base(Base::kMovedFrom),
112 completed_(std::exchange(other.completed_,
true)) {
113 Base::MoveFrom(other);
117 completed_ = std::exchange(other.completed_,
true);
118 Base::MoveFrom(other);
129 static constexpr const char kWaitReason[] =
"ValueFuture";
137 :
Base(Base::kReadyForCompletion), completed_(
true) {}
141 std::lock_guard guard(Base::lock());
142 PW_ASSERT(!completed_);
150 std::lock_guard guard(Base::lock());
157 bool completed_ =
false;
180 template <
typename U = T, std::enable_if_t<!std::is_
void_v<U>,
int> = 0>
182 while (
auto future = provider_.Pop()) {
183 future->get().Resolve(value);
188 template <
typename U = T, std::enable_if_t<std::is_
void_v<U>,
int> = 0>
190 while (
auto future = provider_.Pop()) {
191 future->get().Resolve();
231 template <
typename... Args,
233 std::enable_if_t<!std::is_void_v<U>,
int> = 0>
235 if (
auto future = provider_.Take()) {
236 future->get().Resolve(std::forward<Args>(args)...);
241 template <
typename U = T, std::enable_if_t<std::is_
void_v<U>,
int> = 0>
243 if (
auto future = provider_.Take()) {
244 future->get().Resolve();
Definition: value_future.h:172
ValueFuture< T > Get()
Definition: value_future.h:177
void Resolve(const U &value)
Resolves every pending ValueFuture with a copy of the provided value.
Definition: value_future.h:181
void Resolve()
Resolves every pending ValueFuture.
Definition: value_future.h:189
void Wake()
Wakes the task waiting on the future.
Definition: future.h:386
Definition: value_future.h:106
Definition: value_future.h:32
static ValueFuture Resolved(Args &&... args)
Definition: value_future.h:57
static ValueFuture Resolved(T value)
Creates a ValueFuture that is already resolved with the given value.
Definition: value_future.h:50
Definition: value_future.h:206
ValueFuture< T > Get()
Definition: value_future.h:211
void Resolve(Args &&... args)
Resolves the pending ValueFuture by constructing its value in-place.
Definition: value_future.h:234
bool has_future()
Returns true if the provider stores a pending future.
Definition: value_future.h:228
void Resolve()
Resolves the pending ValueFuture.
Definition: value_future.h:242
std::optional< ValueFuture< T > > TryGet()
Definition: value_future.h:220
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:271
constexpr Poll Ready()
Returns a value indicating completion.
Definition: poll.h:255