tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
coroutine_handle.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_COROUTINE_COROUTINE_HANDLE_HPP
5#define TETL_COROUTINE_COROUTINE_HANDLE_HPP
6
7#include <etl/_cstddef/nullptr_t.hpp>
8#include <etl/_functional/hash.hpp>
9
10#if defined(__cpp_coroutines)
11
12namespace etl {
13
14/// \ingroup coroutine
15template <typename Promise = void>
16struct coroutine_handle;
17
18/// \ingroup coroutine
19template <>
20struct coroutine_handle<void> {
21 constexpr coroutine_handle() noexcept = default;
22
23 constexpr coroutine_handle(nullptr_t handle) noexcept
24 : _handle(handle)
25 {
26 }
27
28 constexpr auto operator=(nullptr_t) noexcept -> coroutine_handle&
29 {
30 _handle = nullptr;
31 return *this;
32 }
33
34 [[nodiscard]] constexpr auto address() const noexcept -> void*
35 {
36 return _handle;
37 }
38
39 [[nodiscard]] static constexpr auto from_address(void* addr) noexcept -> coroutine_handle
40 {
41 auto self = coroutine_handle{};
42 self._handle = addr;
43 return self;
44 }
45
46 [[nodiscard]] constexpr explicit operator bool() const noexcept
47 {
48 return _handle != nullptr;
49 }
50
51 [[nodiscard]] auto done() const noexcept -> bool
52 {
53 return __builtin_coro_done(_handle);
54 }
55
56 auto operator()() const -> void
57 {
58 resume();
59 }
60
61 auto resume() const -> void
62 {
63 __builtin_coro_resume(_handle);
64 }
65
66 auto destroy() const -> void
67 {
68 __builtin_coro_destroy(_handle);
69 }
70
71protected:
72 void* _handle{nullptr};
73};
74
75/// \relates coroutine_handle
76/// \ingroup coroutine
77template <typename T>
78struct hash<coroutine_handle<T>> {
79 [[nodiscard]] auto operator()(coroutine_handle<T> const& v) const noexcept -> size_t
80 {
81 return hash<void*>()(v.address());
82 }
83};
84
85} // namespace etl
86
87#endif // defined(__cpp_coroutines)
88
89#endif // TETL_COROUTINE_COROUTINE_HANDLE_HPP