1#ifndef MRS_LIB_CORO_INTERNAL_CONTINUATION_HPP_
2#define MRS_LIB_CORO_INTERNAL_CONTINUATION_HPP_
10namespace mrs_lib::coro::internal
22 std::coroutine_handle<T> coroutine_handle_reinterpret_cast(std::coroutine_handle<> handle)
24 return std::coroutine_handle<T>::from_address(handle.address());
59 using TokenGetterFptr = std::stop_token (*)(std::coroutine_handle<T>);
82 static_assert(std::same_as<
decltype(&Trait::release_continuation), ContinuationGetterFptr<T>>,
83 "Wrong signature for CancellableContinuationFor<T>::release_continuation.");
84 static_assert(std::same_as<
decltype(&Trait::get_token), TokenGetterFptr<T>>,
"Wrong signature for CancellableContinuationFor<T>::get_token.");
85 return Trait::release_continuation(coroutine_handle_reinterpret_cast<T>(handle));
106 CancellableContinuation& operator=(CancellableContinuation&& other)
noexcept
108 std::ranges::swap(data_, other.data_);
119 return std::exchange(data_.handle,
nullptr);
129 ContinuationData local_data = release_data();
131 while (local_data.handle !=
nullptr)
134 local_data.handle.destroy();
135 local_data = continuation.release_data();
144 return data_.stop_token;
152 return continuation.data_.handle ==
nullptr;
159 struct ContinuationData
161 ContinuationGetterFptr<void> func =
nullptr;
162 std::coroutine_handle<> handle =
nullptr;
163 std::stop_token stop_token{};
169 ContinuationData release_data()
171 return std::exchange(data_, {});
174 ContinuationData data_;
Owning coroutine handle supporting cancellation.
Definition continuation.hpp:54
void cancel_and_destroy()
Cancel the stored continuation, if there is any.
Definition continuation.hpp:127
CancellableContinuation(std::coroutine_handle< T > handle)
Construct continuation that will own the passed handle.
Definition continuation.hpp:78
CancellableContinuation()=default
Construct empty continuation.
std::stop_token get_token() const
Get stop token associated with the continuation.
Definition continuation.hpp:142
friend bool operator==(const CancellableContinuation &continuation, std::nullptr_t)
Check if the current continuation is empty.
Definition continuation.hpp:150
std::coroutine_handle release()
Release ownership of the stored handle, giving it to the caller.
Definition continuation.hpp:117
CancellableContinuation(std::coroutine_handle< void >)=delete
Cancellable continuation cannot be constructed from type erased handle.
~CancellableContinuation()
Destroy stored continuation if there is any.
Definition continuation.hpp:96
Type trait to obtain data necessary for creating CancellableContinuation.
Definition continuation.hpp:37