C/C++ API Reference
Loading...
Searching...
No Matches
allocate_task.h
1// Copyright 2024 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_allocator/allocator.h"
17#include "pw_async2/dispatcher.h"
18#include "pw_async2/owned_task.h"
19
20namespace pw::async2 {
21namespace internal {
22
23template <typename Pendable>
24class AllocatedTask final : public OwnedTask {
25 public:
26 template <typename... Args>
27 AllocatedTask(pw::allocator::Deallocator& deallocator, Args&&... args)
28 : deallocator_(deallocator), pendable_(std::forward<Args>(args)...) {}
29
30 private:
31 Poll<> DoPend(Context& cx) override { return pendable_.Pend(cx); }
32
33 void DoDestroy() override { deallocator_.Delete(this); }
34
35 pw::allocator::Deallocator& deallocator_;
36 Pendable pendable_;
37};
38
39} // namespace internal
40
42
49template <typename Pendable>
50Task* AllocateTask(pw::allocator::Allocator& allocator, Pendable&& pendable) {
51 return allocator.New<internal::AllocatedTask<Pendable>>(
52 allocator, std::forward<Pendable>(pendable));
53}
54
61template <typename Pendable, typename... Args>
62Task* AllocateTask(pw::allocator::Allocator& allocator, Args&&... args) {
63 return allocator.New<internal::AllocatedTask<Pendable>>(
64 allocator, std::forward<Args>(args)...);
65}
66
68
69} // namespace pw::async2
Definition: allocator.h:36
std::enable_if_t<!std::is_array_v< T >, T * > New(Args &&... args)
Definition: allocator.h:57
Definition: context.h:54
Definition: owned_task.h:31
Definition: poll.h:60
Definition: task.h:62
Definition: allocate_task.h:24
Poll DoPend(Context &cx) override
Definition: allocate_task.h:31
void DoDestroy() override
Definition: allocate_task.h:33
Task * AllocateTask(pw::allocator::Allocator &allocator, Pendable &&pendable)
Definition: allocate_task.h:50