Pigweed
 
Loading...
Searching...
No Matches
pend_func_awaitable.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 <type_traits>
17#include <utility>
18
19#include "pw_async2/dispatcher.h"
20#include "pw_async2/poll.h"
21#include "pw_function/function.h"
22
23namespace pw::async2 {
24
29template <typename T, typename Func = Function<Poll<T>(Context&)>>
31 public:
32 using CallableType = Func;
33
37 constexpr PendFuncAwaitable(Func&& func) : func_(std::forward<Func>(func)) {}
38
42 Poll<T> Pend(pw::async2::Context& cx) { return func_(cx); }
43
44 private:
45 Func func_;
46};
47
48// Template deduction guide to allow inferring T from a Callable's return type.
49// This also ensures that when constructing a PendFuncAwaitable from a callable,
50// the template Func parameter is set based on the Callable type, which helps
51// optimize the amount of storage needed.
52template <typename Callable>
53PendFuncAwaitable(Callable) -> PendFuncAwaitable<
54 typename std::invoke_result<Callable, Context&>::type::OutputType,
55 typename std::remove_reference<Callable>::type>;
56
57} // namespace pw::async2
Definition: dispatcher_base.h:52
Definition: pend_func_awaitable.h:30
constexpr PendFuncAwaitable(Func &&func)
Definition: pend_func_awaitable.h:37
Poll< T > Pend(pw::async2::Context &cx)
Definition: pend_func_awaitable.h:42
Definition: poll.h:54