C/C++ API Reference
Loading...
Searching...
No Matches
future_task.h
1// Copyright 2026 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://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, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include "pw_async2/context.h"
17#include "pw_async2/future.h"
18#include "pw_async2/poll.h"
19#include "pw_async2/task.h"
20
21namespace pw::async2 {
22
24
38template <typename T>
39class FutureTask final : public Task {
40 public:
42 using future_type = std::remove_reference_t<T>;
43
45 using value_type = typename future_type::value_type;
46
53 template <typename Arg, typename... Args>
54 explicit constexpr FutureTask(Arg&& arg, Args&&... args)
55 : Task(PW_ASYNC_TASK_NAME("FutureTask")),
56 future_(std::forward<Arg>(arg), std::forward<Args>(args)...),
57 output_(Pending()) {}
58
59 ~FutureTask() override { Deregister(); }
60
64 Poll<value_type> TakePoll() { return std::move(output_); }
65
69 FutureValue<future_type>& value() & { return output_.value(); }
71 const FutureValue<future_type>& value() const& { return output_.value(); }
73 FutureValue<future_type>&& value() && { return std::move(output_).value(); }
75 const FutureValue<future_type>&& value() const&& {
76 return std::move(output_).value();
77 }
78
82 Task::Join();
83 return *output_;
84 }
85
86 private:
87 static_assert(Future<future_type>);
88
89 Poll<> DoPend(Context& cx) override {
90 output_ = future_.Pend(cx);
91 return output_.Readiness();
92 }
93
94 T future_;
95 Poll<value_type> output_;
96};
97
98template <typename T>
99FutureTask(T&& value) -> FutureTask<T>;
100
102
103} // namespace pw::async2
Definition: context.h:46
Definition: future_task.h:39
constexpr FutureTask(Arg &&arg, Args &&... args)
Definition: future_task.h:54
FutureValue< future_type > & value() &
Definition: future_task.h:69
FutureValue< future_type > && value() &&
Definition: future_task.h:73
typename future_type::value_type value_type
The type produced by this tasks's future when it completes.
Definition: future_task.h:45
Poll DoPend(Context &cx) override
Definition: future_task.h:89
const FutureValue< future_type > && value() const &&
Definition: future_task.h:75
std::remove_reference_t< T > future_type
The type of the future that is pended by this task.
Definition: future_task.h:42
FutureValue< future_type > & Wait()
Definition: future_task.h:81
const FutureValue< future_type > & value() const &
Definition: future_task.h:71
Poll< value_type > TakePoll()
Definition: future_task.h:64
Definition: poll.h:138
Definition: task.h:67
Definition: future.h:47
std::conditional_t< std::is_void_v< typename T::value_type >, ReadyType, typename T::value_type > FutureValue
Definition: future.h:107
constexpr Poll Readiness() const noexcept
Definition: poll.h:218
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:353
constexpr value_type & value() &noexcept
Definition: poll.h:228
#define PW_ASYNC_TASK_NAME(name)
Generates a token for use as a task name.
Definition: task.h:35