HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
awaitable_stop_token_intf.hpp
1// Copyright Take Vos 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "awaitable.hpp"
8#include "../concurrency/concurrency.hpp"
9#include "../time/time.hpp"
10#include "../macros.hpp"
11#include <chrono>
12#include <coroutine>
13#include <functional>
14#include <stop_token>
15
16hi_export_module(hikogui.dispatch : awaitable_stop_token_intf);
17
18hi_export namespace hi::inline v1 {
19class loop;
20
22public:
23 awaitable_stop_token(awaitable_stop_token &&) noexcept = default;
24 awaitable_stop_token &operator=(awaitable_stop_token &&) noexcept = default;
25
26 awaitable_stop_token(awaitable_stop_token const &other) noexcept : _stop_token(other._stop_token), _stop_callback_ptr() {}
27
28 awaitable_stop_token &operator=(awaitable_stop_token const &other) noexcept
29 {
30 _stop_callback_ptr = nullptr;
31 _stop_token = other._stop_token;
32 return *this;
33 }
34
35 awaitable_stop_token(std::stop_token const &stop_token) noexcept : _stop_token(stop_token), _stop_callback_ptr()
36 {
37 }
38
39 [[nodiscard]] bool await_ready() const noexcept
40 {
41 return _stop_token.stop_requested();
42 }
43
44 void await_suspend(std::coroutine_handle<> handle) noexcept;
45
46 void await_resume() noexcept {
47 _stop_callback_ptr = nullptr;
48 }
49
50private:
51 struct callback_wrapper {
52 void operator()() noexcept;
53
54 loop *await_loop;
55 std::coroutine_handle<> handle;
56 };
57
58 std::stop_token _stop_token;
60};
61
62template<>
63struct awaitable_cast<std::stop_token> {
64 template<typename RHS>
65 decltype(auto) operator()(RHS&& rhs) noexcept
66 {
67 return awaitable_stop_token{std::forward<RHS>(rhs)};
68 }
69};
70
71} // namespace hi::inline v1
STL namespace.
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
Definition awaitable.hpp:41
Definition awaitable_stop_token_intf.hpp:21
Definition loop_win32_intf.hpp:32