C/C++ API Reference
Loading...
Searching...
No Matches
func_task.h
1// Copyright 2023 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 <optional>
17#include <type_traits>
18
19#include "pw_async2/task.h"
20#include "pw_function/function.h"
21
22namespace pw::async2 {
23
25
37template <typename Func = Function<Poll<>(Context&)>>
38class FuncTask final : public Task {
39 public:
41 template <typename F>
42 explicit constexpr FuncTask(F&& func) : func_(std::forward<F>(func)) {}
43
44 FuncTask(const FuncTask&) = delete;
45 FuncTask& operator=(const FuncTask&) = delete;
46
47 FuncTask(FuncTask&&) = delete;
48 FuncTask& operator=(FuncTask&&) = delete;
49
50 ~FuncTask() override { Deregister(); }
51
52 private:
53 Poll<> DoPend(Context& cx) override { return func_(cx); }
54
55 Func func_;
56};
57
58template <typename Func>
59FuncTask(Func&&) -> FuncTask<std::decay_t<Func>>;
60
62enum class ReturnValuePolicy : bool { kKeep, kDiscard };
63
79template <typename Func = Function<Poll<>()>,
80 ReturnValuePolicy = std::is_void_v<std::invoke_result_t<Func>>
81 ? ReturnValuePolicy::kDiscard
82 : ReturnValuePolicy::kKeep>
83class RunOnceTask final : public Task {
84 public:
86 using value_type =
87 std::conditional_t<std::is_void_v<std::invoke_result_t<Func>>,
89 std::invoke_result_t<Func>>;
90
92 explicit constexpr RunOnceTask(Func&& func)
93 : func_(std::forward<Func>(func)) {}
94
95 RunOnceTask(const RunOnceTask&) = delete;
96 RunOnceTask& operator=(const RunOnceTask&) = delete;
97
98 RunOnceTask(RunOnceTask&&) = delete;
99 RunOnceTask& operator=(RunOnceTask&&) = delete;
100
101 ~RunOnceTask() override { Deregister(); }
102
105 bool has_value() const { return return_value_.has_value(); }
106
110 value_type& value() { return return_value_.value(); }
111
113 const value_type& value() const { return return_value_.value(); }
114
117 Task::Join();
118 return *return_value_;
119 }
120
121 private:
122 Poll<> DoPend(Context&) override {
123 if constexpr (std::is_void_v<std::invoke_result_t<Func>>) {
124 func_();
125 return_value_.emplace();
126 } else {
127 return_value_ = func_();
128 }
129 return Ready();
130 }
131
132 Func func_;
133 std::optional<value_type> return_value_;
134};
135
141template <typename Func>
142class RunOnceTask<Func, ReturnValuePolicy::kDiscard> final : public Task {
143 public:
144 explicit constexpr RunOnceTask(Func&& func)
145 : func_(std::forward<Func>(func)) {}
146
147 RunOnceTask(const RunOnceTask&) = delete;
148 RunOnceTask& operator=(const RunOnceTask&) = delete;
149
150 RunOnceTask(RunOnceTask&&) = delete;
151 RunOnceTask& operator=(RunOnceTask&&) = delete;
152
153 ~RunOnceTask() override { Deregister(); }
154
155 private:
156 Poll<> DoPend(Context&) override {
157 (void)func_();
158 return Ready();
159 }
160
161 Func func_;
162};
163
164template <typename Func>
165RunOnceTask(Func&&) -> RunOnceTask<Func>;
166
168
169} // namespace pw::async2
Definition: context.h:46
Definition: func_task.h:38
Poll DoPend(Context &cx) override
Definition: func_task.h:53
constexpr FuncTask(F &&func)
Creates a new Task that delegates Pend to func.
Definition: func_task.h:42
Definition: poll.h:138
Poll DoPend(Context &) override
Definition: func_task.h:156
Definition: func_task.h:83
constexpr RunOnceTask(Func &&func)
Creates a new Task that runs the provided function once.
Definition: func_task.h:92
Poll DoPend(Context &) override
Definition: func_task.h:122
std::conditional_t< std::is_void_v< std::invoke_result_t< Func > >, ReadyType, std::invoke_result_t< Func > > value_type
The return value of the function.
Definition: func_task.h:89
value_type & value()
Definition: func_task.h:110
const value_type & value() const
Definition: func_task.h:113
bool has_value() const
Definition: func_task.h:105
value_type & Wait()
Blocks until the task completes and returns a reference its return value.
Definition: func_task.h:116
Definition: task.h:78
constexpr Poll Ready()
Returns a value indicating completion.
Definition: poll.h:337
ReturnValuePolicy
Whether to store or discard the function's return value in RunOnceTask.
Definition: func_task.h:62
Definition: poll.h:40