Async++ unknown
Async (co_await/co_return) code for C++
Loading...
Searching...
No Matches
defer.h
1#pragma once
2#include <asyncpp/detail/concepts.h>
4#include <utility>
5
6namespace asyncpp {
15 template<Dispatcher TDispatcher>
16 struct defer {
18 TDispatcher* target_dispatcher;
19
25 explicit constexpr defer(TDispatcher* target) noexcept : target_dispatcher(target) {}
26
31 explicit constexpr defer(TDispatcher& target) noexcept : target_dispatcher(&target) {}
32
37 [[nodiscard]] constexpr bool await_ready() const noexcept { return target_dispatcher == nullptr; }
42 void await_suspend(coroutine_handle<> hndl) const
43 noexcept(noexcept(target_dispatcher->push(std::declval<std::function<void()>>()))) {
44 target_dispatcher->push([hndl]() mutable { hndl.resume(); });
45 }
47 constexpr void await_resume() const noexcept {}
48 };
49} // namespace asyncpp
Provides a consistent import interface for coroutine, experimental/coroutine or a best effort fallbac...
Defer the current coroutine to a different dispatcher context.
Definition defer.h:16
constexpr bool await_ready() const noexcept
Check if await should suspend.
Definition defer.h:37
void await_suspend(coroutine_handle<> hndl) const noexcept(noexcept(target_dispatcher->push(std::declval< std::function< void()> >())))
Suspend the current coroutine and schedule resumption on the specified dispatcher.
Definition defer.h:42
constexpr defer(TDispatcher &target) noexcept
Construct with a reference to a dispatcher.
Definition defer.h:31
TDispatcher * target_dispatcher
The new dispatcher to resume execution on.
Definition defer.h:18
constexpr void await_resume() const noexcept
Called on resumption.
Definition defer.h:47
constexpr defer(TDispatcher *target) noexcept
Construct with a pointer to a dispatcher.
Definition defer.h:25