Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 22 additions & 46 deletions modules/async_context.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -819,29 +819,9 @@ struct final_awaiter
}
};

export template<typename T>
class promise : public promise_base
template<typename T>
struct promise_return_base
{
public:
using promise_base::promise_base; // Inherit constructors
using promise_base::operator new;
using promise_base::operator delete;
using our_handle = std::coroutine_handle<promise<T>>;

friend class future<T>;

constexpr final_awaiter<promise<T>> final_suspend() noexcept
{
return {};
}

void unhandled_exception() noexcept
{
*m_future_state = std::current_exception();
}

constexpr future<T> get_return_object() noexcept;

template<typename U>
void return_value(U&& p_value) noexcept
requires std::is_constructible_v<T, U&&>
Expand All @@ -850,40 +830,44 @@ public:
m_future_state->template emplace<T>(std::forward<U>(p_value));
}

protected:
future_state<T>* m_future_state;
};

export template<>
class promise<void> : public promise_base
template<>
struct promise_return_base<void>
{
void return_void() noexcept
{
*m_future_state = std::monostate{};
}

future_state<void>* m_future_state;
};

export template<typename T>
class promise
: public promise_base
, public promise_return_base<T>
{
public:
using promise_base::promise_base; // Inherit constructors
using promise_base::operator new;
using promise_base::operator delete;
using our_handle = std::coroutine_handle<promise<void>>;
using our_handle = std::coroutine_handle<promise<T>>;

friend class future<void>;
friend class future<T>;

constexpr final_awaiter<promise<void>> final_suspend() noexcept
constexpr final_awaiter<promise<T>> final_suspend() noexcept
{
return {};
}

constexpr future<void> get_return_object() noexcept;

void unhandled_exception() noexcept
{
*m_future_state = std::current_exception();
}

void return_void() noexcept
{
*m_future_state = std::monostate{};
*promise_return_base<T>::m_future_state = std::current_exception();
}

protected:
future_state<void>* m_future_state;
constexpr future<T> get_return_object() noexcept;
};

export template<typename T>
Expand Down Expand Up @@ -1101,14 +1085,6 @@ constexpr future<T> promise<T>::get_return_object() noexcept
return future<T>{ handle };
}

constexpr future<void> promise<void>::get_return_object() noexcept
{
using future_handle = std::coroutine_handle<promise<void>>;
auto handle = future_handle::from_promise(*this);
m_context->active_handle(handle);
return future<void>{ handle };
}

void context::unsafe_cancel()
{
// TODO(#38): Consider if a safe variant of cancel is achievable
Expand Down