C/C++ API Reference
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
26
31template <typename T, typename Func = Function<Poll<T>(Context&)>>
33 public:
34 using CallableType = Func;
35
39 constexpr PendFuncAwaitable(Func&& func) : func_(std::forward<Func>(func)) {}
40
44 Poll<T> Pend(pw::async2::Context& cx) { return func_(cx); }
45
46 private:
47 Func func_;
48};
49
50// Template deduction guide to allow inferring T from a Callable's return type.
51// This also ensures that when constructing a PendFuncAwaitable from a callable,
52// the template Func parameter is set based on the Callable type, which helps
53// optimize the amount of storage needed.
54template <typename Callable>
55PendFuncAwaitable(Callable) -> PendFuncAwaitable<
56 typename std::invoke_result<Callable, Context&>::type::value_type,
57 typename std::remove_reference<Callable>::type>;
58
60
61} // namespace pw::async2
Definition: context.h:55
Definition: pend_func_awaitable.h:32
constexpr PendFuncAwaitable(Func &&func)
Definition: pend_func_awaitable.h:39
Poll< T > Pend(pw::async2::Context &cx)
Definition: pend_func_awaitable.h:44
Definition: poll.h:60