HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
task.hpp
1// Copyright Take Vos 2022.
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 "utility/module.hpp"
8#include <coroutine>
9#include <type_traits>
10
11namespace hi::inline v1 {
12
13template<typename T>
14class task;
15
16template<typename T>
18 T _value;
19
20 void return_void() noexcept
21 {
23 }
24
25 void return_value(std::convertible_to<T> auto &&value) noexcept
26 {
27 _value = hi_forward(value);
28 }
29};
30
31template<>
32struct task_promise_base<void> {
33 void return_void() noexcept {}
34};
35
36template<typename T>
38 using value_type = T;
39 using handle_type = std::coroutine_handle<task_promise<value_type>>;
41
42 static void unhandled_exception()
43 {
44 throw;
45 }
46
47 task<value_type> get_return_object()
48 {
49 return task_type{handle_type::from_promise(*this)};
50 }
51
52 static std::suspend_never initial_suspend() noexcept
53 {
54 return {};
55 }
56
57 static std::suspend_never final_suspend() noexcept
58 {
59 return {};
60 }
61};
62
67template<typename T = void>
68class task {
69public:
70 using value_type = T;
72 using handle_type = std::coroutine_handle<promise_type>;
73
74 explicit task(handle_type coroutine) : _coroutine(coroutine) {}
75
76 task() = default;
77 ~task()
78 {
79 }
80
81 task(task const &) = delete;
82 task(task &&) = delete;
83 task &operator=(task const &) = delete;
84 task &operator=(task &&) = delete;
85
86private:
87 handle_type _coroutine;
88};
89
90} // namespace hi::inline v1
#define hi_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:264
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition utility.hpp:29
DOXYGEN BUG.
Definition algorithm.hpp:13
Co-routine task.
Definition task.hpp:68
Definition task.hpp:17
Definition task.hpp:37