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/future.h"
17#include "pw_async2/poll.h"
18#include "pw_async2/task.h"
19
20namespace pw::async2 {
21
23
37template <typename T>
38class FutureTask final : public Task {
39 public:
41 using future_type = std::remove_reference_t<T>;
42
44 using value_type = typename future_type::value_type;
45
52 template <typename Arg, typename... Args>
53 explicit constexpr FutureTask(Arg&& arg, Args&&... args)
54 : Task(PW_ASYNC_TASK_NAME("FutureTask")),
55 future_(std::forward<Arg>(arg), std::forward<Args>(args)...),
56 output_(Pending()) {}
57
58 ~FutureTask() override { Deregister(); }
59
63 Poll<value_type> TakePoll() { return std::move(output_); }
64
68 FutureValue<future_type>& value() & { return output_.value(); }
70 const FutureValue<future_type>& value() const& { return output_.value(); }
72 FutureValue<future_type>&& value() && { return std::move(output_).value(); }
74 const FutureValue<future_type>&& value() const&& {
75 return std::move(output_).value();
76 }
77
81 Task::Join();
82 return *output_;
83 }
84
85 private:
86 static_assert(Future<future_type>);
87
88 Poll<> DoPend(Context& cx) override {
89 output_ = future_.Pend(cx);
90 return output_.Readiness();
91 }
92
93 T future_;
94 Poll<value_type> output_;
95};
96
97template <typename T>
98FutureTask(T&& value) -> FutureTask<T>;
99
101
102} // namespace pw::async2
Definition: task.h:45
Definition: future_task.h:38
constexpr FutureTask(Arg &&arg, Args &&... args)
Definition: future_task.h:53
FutureValue< future_type > & value() &
Definition: future_task.h:68
FutureValue< future_type > && value() &&
Definition: future_task.h:72
typename future_type::value_type value_type
The type produced by this tasks's future when it completes.
Definition: future_task.h:44
Poll DoPend(Context &cx) override
Definition: future_task.h:88
const FutureValue< future_type > && value() const &&
Definition: future_task.h:74
std::remove_reference_t< T > future_type
The type of the future that is pended by this task.
Definition: future_task.h:41
FutureValue< future_type > & Wait()
Definition: future_task.h:80
const FutureValue< future_type > & value() const &
Definition: future_task.h:70
Poll< value_type > TakePoll()
Definition: future_task.h:63
Definition: poll.h:138
Definition: task.h:123
Definition: future.h:48
std::conditional_t< std::is_void_v< typename T::value_type >, ReadyType, typename T::value_type > FutureValue
Definition: future.h:110
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:33