C/C++ API Reference
Loading...
Searching...
No Matches
callback_task.h
1// Copyright 2025 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 <functional>
17#include <type_traits>
18
19#include "pw_async2/poll.h"
20#include "pw_async2/task.h"
21#include "pw_function/function.h"
22
23namespace pw::async2 {
24namespace internal {
25
26template <typename T = ReadyType, bool kReschedule = false>
27class CallbackTask : public Task {
28 public:
29 using ValueType = T;
30 using Pendable = Function<Poll<T>(Context&)>;
31
32 using Callback = std::conditional_t<std::is_same_v<T, ReadyType>,
33 Function<void()>,
34 Function<void(T)>>;
35
36 CallbackTask(Callback&& callback, Pendable&& pendable)
37 : callback_(std::move(callback)), pendable_(std::move(pendable)) {}
38
39 private:
40 Poll<> DoPend(Context& cx) final {
41 Poll<T> poll = pendable_(cx);
42 if (poll.IsPending()) {
43 return Pending();
44 }
45
46 if constexpr (std::is_same_v<T, ReadyType>) {
47 callback_();
48 } else {
49 callback_(std::move(*poll));
50 }
51
52 if constexpr (kReschedule) {
53 cx.ReEnqueue();
54 return Pending();
55 } else {
56 return Ready();
57 }
58 }
59
60 Callback callback_;
61 Pendable pendable_;
62};
63
64} // namespace internal
65
67
74template <typename T = ReadyType>
76
84template <typename T = ReadyType>
86
87template <auto kFunc,
88 typename Callback,
89 typename T = typename UnwrapPoll<
90 std::invoke_result_t<decltype(kFunc), Context&>>::Type>
91OneshotCallbackTask<T> OneshotCallbackTaskFor(Callback&& callback) {
92 return OneshotCallbackTask<T>(std::forward<Callback>(callback),
93 std::forward<decltype(kFunc)>(kFunc));
94}
95
96template <
97 auto kMemberFunc,
98 typename Class,
99 typename Callback,
100 typename T = typename UnwrapPoll<
101 std::invoke_result_t<decltype(kMemberFunc), Class&, Context&>>::Type>
102OneshotCallbackTask<T> OneshotCallbackTaskFor(Class& obj, Callback&& callback) {
103 return OneshotCallbackTask<T>(
104 std::forward<Callback>(callback),
105 [&obj](Context& cx) { return std::invoke(kMemberFunc, &obj, cx); });
106}
107
108template <auto kFunc,
109 typename Callback,
110 typename T = typename UnwrapPoll<
111 std::invoke_result_t<decltype(kFunc), Context&>>::Type>
112RecurringCallbackTask<T> RecurringCallbackTaskFor(Callback&& callback) {
113 return RecurringCallbackTask<T>(std::forward<Callback>(callback),
114 std::forward<decltype(kFunc)>(kFunc));
115}
116
117template <
118 auto kMemberFunc,
119 typename Class,
120 typename Callback,
121 typename T = typename UnwrapPoll<
122 std::invoke_result_t<decltype(kMemberFunc), Class&, Context&>>::Type>
123RecurringCallbackTask<T> RecurringCallbackTaskFor(Class& obj,
124 Callback&& callback) {
125 return RecurringCallbackTask<T>(
126 std::forward<Callback>(callback),
127 [&obj](Context& cx) { return std::invoke(kMemberFunc, &obj, cx); });
128}
129
131
132} // namespace pw::async2
Definition: context.h:55
Definition: poll.h:60
Definition: task.h:63
Definition: callback_task.h:27
Poll DoPend(Context &cx) final
Definition: callback_task.h:40
constexpr bool IsPending() const noexcept
Returns whether or not this value is Pending.
Definition: poll.h:136
constexpr PendingType Pending()
Returns a value indicating that an operation was not yet able to complete.
Definition: poll.h:271
constexpr Poll Ready()
Returns a value indicating completion.
Definition: poll.h:255
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:73
fit::callback_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Callback
Definition: function.h:128
Definition: poll.h:274