C/C++ API Reference
Loading...
Searching...
No Matches
task.h
1// Copyright 2022 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
18#include "pw_async/context.h"
19#include "pw_async/task_function.h"
20#include "pw_async_backend/task.h"
21
22namespace pw::async {
23
25
26namespace test {
27class FakeDispatcher;
28}
29
33class Task final {
34 public:
37 Task() : native_type_(*this) {}
38
40 explicit Task(TaskFunction&& f) : native_type_(*this, std::move(f)) {}
41
42 Task(const Task&) = delete;
43 Task& operator=(const Task&) = delete;
44 Task(Task&&) = delete;
45 Task& operator=(Task&&) = delete;
46
50 native_type_.set_function(std::move(f));
51 }
52
54 void operator()(Context& ctx, Status status) { native_type_(ctx, status); }
55
58 backend::NativeTask& native_type() { return native_type_; }
59 const backend::NativeTask& native_type() const { return native_type_; }
60
61 private:
62 backend::NativeTask native_type_;
63};
64
65} // namespace pw::async
Definition: status.h:86
Definition: task.h:33
Task(TaskFunction &&f)
Constructs a Task that calls f when executed on a Dispatcher.
Definition: task.h:40
void set_function(TaskFunction &&f)
Definition: task.h:49
backend::NativeTask & native_type()
Definition: task.h:58
void operator()(Context &ctx, Status status)
Executes this task.
Definition: task.h:54
Task()
Definition: task.h:37
Function< void(Context &, Status)> TaskFunction
Definition: task_function.h:39
(Deprecated) Async library
Definition: context.h:19
Contextual information provided by a Dispatcher to a running task.
Definition: context.h:27