15namespace hi::inline v1 {
26template<
typename T =
void>
47 void return_value(std::convertible_to<value_type>
auto &&value)
noexcept
52 void unhandled_exception()
noexcept
57 std::suspend_never final_suspend()
noexcept
59 switch (_value_ptr->index()) {
62 if constexpr (std::is_default_constructible_v<value_type>) {
63 _notifier(value_type{});
69 _notifier(std::get<2>(*_value_ptr));
71 default: hi_no_default();
77 _value_ptr = std::make_shared<return_value_type>();
78 return scoped_task{handle_type::from_promise(*
this), _value_ptr};
89 using handle_type = std::coroutine_handle<promise_type>;
91 scoped_task(handle_type coroutine, const_return_value_ptr_type value_ptr) noexcept :
92 _coroutine(coroutine), _value_ptr(
std::move(value_ptr))
98 if (_value_ptr and not completed()) {
100 _coroutine.destroy();
104 scoped_task() =
default;
108 scoped_task(scoped_task
const &) =
delete;
109 scoped_task &operator=(scoped_task
const &) =
delete;
111 scoped_task(scoped_task &&other)
noexcept
113 _coroutine = std::exchange(other._coroutine, {});
114 _value_ptr = std::exchange(other._value_ptr, {});
117 scoped_task &operator=(scoped_task &&other)
noexcept
119 _coroutine = std::exchange(other._coroutine, {});
120 _value_ptr = std::exchange(other._value_ptr, {});
128 return _value_ptr->index() != 0;
133 explicit operator bool() const noexcept
143 [[nodiscard]] value_type
const &
value()
const
145 switch (_value_ptr->index()) {
147 case 2:
return std::get<2>(*_value_ptr);
148 default: hi_no_default();
167 notifier_type::token_type
subscribe(std::invocable<value_type>
auto &&callback)
noexcept
169 return _coroutine.promise()._notifier.subscribe(
hi_forward(callback));
174 handle_type _coroutine;
175 const_return_value_ptr_type _value_ptr;
184 using value_type = void;
198 struct promise_type {
202 void return_void()
noexcept
207 void unhandled_exception()
noexcept
212 std::suspend_never final_suspend()
noexcept
214 switch (_value_ptr->index()) {
223 default: hi_no_default();
228 _value_ptr = std::make_shared<return_value_type>();
229 return scoped_task{handle_type::from_promise(*
this), _value_ptr};
232 std::suspend_never initial_suspend()
noexcept
238 using handle_type = std::coroutine_handle<promise_type>;
241 _coroutine(coroutine), _value_ptr(
std::move(value_ptr))
247 if (_value_ptr and not completed()) {
248 hi_axiom(_coroutine);
249 _coroutine.destroy();
253 scoped_task() =
default;
254 scoped_task(scoped_task
const &) =
delete;
255 scoped_task &operator=(scoped_task
const &) =
delete;
257 scoped_task(scoped_task &&other)
noexcept
259 _coroutine = std::exchange(other._coroutine, {});
260 _value_ptr = std::exchange(other._value_ptr, {});
263 scoped_task &operator=(scoped_task &&other)
noexcept
265 _coroutine = std::exchange(other._coroutine, {});
266 _value_ptr = std::exchange(other._value_ptr, {});
275 return _value_ptr->index() != 0;
281 explicit operator bool() const noexcept
294 switch (_value_ptr->index()) {
297 default: hi_no_default();
304 notifier_type::token_type
subscribe(std::invocable<>
auto &&callback)
noexcept
306 return _coroutine.promise()._notifier.subscribe(
hi_forward(callback));
311 handle_type _coroutine;
312 const_return_value_ptr_type _value_ptr;
This file includes required definitions.
#define hi_forward(x)
Forward a value, based on the decltype of the value.
Definition required.hpp:29
A scoped_task.
Definition scoped_task.hpp:27
bool completed() const noexcept
Check if the co-routine has completed.
Definition scoped_task.hpp:126
value_type const & operator*() const
Get the return value returned from co_return.
Definition scoped_task.hpp:157
std::variant< std::monostate, std::exception_ptr, value_type > return_value_type
The return value type.
Definition scoped_task.hpp:38
value_type const & value() const
Get the return value returned from co_return.
Definition scoped_task.hpp:143
notifier_type::token_type subscribe(std::invocable< value_type > auto &&callback) noexcept
Subscribe a callback for when the co-routine is completed.
Definition scoped_task.hpp:167
Definition scoped_task.hpp:43
std::suspend_never initial_suspend() noexcept
Before we enter the coroutine, allow the caller to set the callback.
Definition scoped_task.hpp:83
bool completed() const noexcept
Definition scoped_task.hpp:273
std::variant< std::monostate, std::exception_ptr, std::monostate > return_value_type
The return value type.
Definition scoped_task.hpp:193
void value() const
Get the return value returned from co_return.
Definition scoped_task.hpp:292
notifier_type::token_type subscribe(std::invocable<> auto &&callback) noexcept
Definition scoped_task.hpp:304
T current_exception(T... args)
T rethrow_exception(T... args)