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/future.h"
20#include "pw_async2/poll.h"
21#include "pw_async2/task.h"
22#include "pw_function/function.h"
23
24namespace pw::async2 {
25namespace internal {
26
27template <typename FutureType>
28using CallbackType = std::conditional_t<
29 std::is_same_v<typename FutureType::value_type, ReadyType>,
30 Function<void()>,
31 Function<void(typename FutureType::value_type)>>;
32
33} // namespace internal
34
36
42template <typename FutureType,
43 typename Func = internal::CallbackType<FutureType>>
44class CallbackTask final : public Task {
45 public:
46 using value_type = typename FutureType::value_type;
47
48 static_assert(is_future_v<FutureType>,
49 "CallbackTask can only be used with Future types");
50
53 constexpr CallbackTask(Func&& callback, FutureType&& future)
54 : Task(PW_ASYNC_TASK_NAME("CallbackTask")),
55 future_(std::move(future)),
56 callback_(std::move(callback)) {}
57
58 ~CallbackTask() override { Deregister(); }
59
64 template <typename Callback, typename... Args>
65 static constexpr auto Emplace(Callback&& callback, Args&&... future_args) {
66 static_assert(sizeof...(Args) >= 1u,
67 "Cannot default construct a Future with Emplace");
69 std::forward<Callback>(callback), std::forward<Args>(future_args)...);
70 }
71
72 private:
73 template <typename, typename>
74 friend class CallbackTask;
75
76 template <typename... Args>
77 constexpr CallbackTask(Func&& callback, Args&&... future_args)
78 : future_(std::forward<Args>(future_args)...),
79 callback_(std::move(callback)) {}
80
81 Poll<> DoPend(Context& cx) final {
82 Poll<value_type> poll = future_.Pend(cx);
83 if (poll.IsPending()) {
84 return Pending();
85 }
86
87 if constexpr (std::is_same_v<value_type, ReadyType>) {
88 callback_();
89 } else {
90 callback_(std::move(*poll));
91 }
92
93 return Ready();
94 }
95
96 FutureType future_;
97 Func callback_;
98};
99
100template <typename FutureType, typename Func>
101CallbackTask(Func&&, FutureType&&) -> CallbackTask<FutureType, Func>;
102
103// TODO: b/458069794 - Add StreamCallbackTask.
104
106
107} // namespace pw::async2
Definition: callback_task.h:44
static constexpr auto Emplace(Callback &&callback, Args &&... future_args)
Definition: callback_task.h:65
Poll DoPend(Context &cx) final
Definition: callback_task.h:81
constexpr CallbackTask(Func &&callback, FutureType &&future)
Definition: callback_task.h:53
Definition: context.h:54
Definition: poll.h:60
Definition: task.h:62
#define PW_ASYNC_TASK_NAME(name)
Generates a token for use as a task name.
Definition: task.h:30
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