C/C++ API Reference
Loading...
Searching...
No Matches
enqueue_heap_func.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 "pw_async2/dispatcher.h"
17#include "pw_async2/owned_task.h"
18
19namespace pw::async2 {
20namespace internal {
21
22template <typename Func>
23class RunHeapFuncTask final : public OwnedTask {
24 public:
25 static Task& New(Func&& func) {
26 return *(new RunHeapFuncTask(std::forward<Func>(func)));
27 }
28
29 private:
30 RunHeapFuncTask(Func&& func) : func_(std::forward<Func>(func)) {}
31 Poll<> DoPend(Context&) override {
32 func_();
33 return Ready();
34 }
35 void DoDestroy() override { delete this; }
36 Func func_;
37};
38
39} // namespace internal
40
42
48template <typename Func>
49void EnqueueHeapFunc(Dispatcher& dispatcher, Func&& func) {
50 return dispatcher.Post(
51 internal::RunHeapFuncTask<Func>::New(std::forward<Func>(func)));
52}
53
55
56} // namespace pw::async2
Definition: context.h:54
Definition: dispatcher.h:53
void Post(Task &task)
Definition: owned_task.h:31
Definition: poll.h:60
Definition: task.h:62
Definition: enqueue_heap_func.h:23
void DoDestroy() override
Definition: enqueue_heap_func.h:35
Poll DoPend(Context &) override
Definition: enqueue_heap_func.h:31
void EnqueueHeapFunc(Dispatcher &dispatcher, Func &&func)
Definition: enqueue_heap_func.h:49
constexpr Poll Ready()
Returns a value indicating completion.
Definition: poll.h:255