Commit 09e7f7a0 authored by Xavier Thompson's avatar Xavier Thompson

WIP Lazy coroutines

parent 7e33cffa
......@@ -13,7 +13,6 @@ namespace typon
template <typename T>
struct result
{
std::exception_ptr _exception;
union
{
......@@ -56,14 +55,12 @@ namespace typon
}
return std::move(_value);
}
};
template <typename T>
struct result<T&>
{
T* _value;
std::exception_ptr _exception;
......@@ -85,16 +82,16 @@ namespace typon
}
return *_value;
}
};
template <>
struct result<void>
{
std::exception_ptr _exception;
void return_void() noexcept {}
void unhandled_exception() noexcept
{
_exception = std::current_exception();
......@@ -107,7 +104,6 @@ namespace typon
std::rethrow_exception(_exception);
}
}
};
}
......
......@@ -3,7 +3,6 @@
#include <coroutine>
#include <promise.hpp>
#include <result.hpp>
......@@ -11,49 +10,69 @@ namespace typon
{
template <typename T = void>
//struct [[nodiscard]] task
struct task
struct [[nodiscard]] task
{
struct promise_type;
std::coroutine_handle<promise_type> _coroutine;
result<T> _result;
struct promise_type : promise<T>
~task()
{
_coroutine.destroy();
}
struct promise_type : result<T>
{
// std::coroutine_handle<> _continuation;
std::coroutine_handle<> _continuation;
struct final_awaitable : std::suspend_always
{
template <typename Promise>
std::coroutine_handle<> await_suspend(std::coroutine_handle<Promise> coroutine) noexcept
{
return coroutine.promise()._continuation;
}
};
task get_return_object() noexcept
{
task _task { std::coroutine_handle<promise_type>::from_promise(*this), result<T> {} };
this->_result = std::addressof(_task._result);
return _task;
return { std::coroutine_handle<promise_type>::from_promise(*this) };
}
std::suspend_never initial_suspend() noexcept
std::suspend_always initial_suspend() noexcept
{
return {};
}
std::suspend_never final_suspend() noexcept
final_awaitable final_suspend() noexcept
{
return {};
}
};
// auto operator co_await() const&& noexcept
// {
// struct awaitable
// {
// bool await_ready() noexcept
// {
auto operator co_await() const&& noexcept
{
struct awaitable
{
std::coroutine_handle<promise_type> _coroutine;
bool await_ready() { return false; };
std::coroutine_handle<> await_suspend(std::coroutine_handle<> awaiting_coroutine) noexcept
{
_coroutine.promise()._continuation = awaiting_coroutine;
return _coroutine;
}
// }
// };
// }
decltype(auto) await_resume()
{
return _coroutine.promise().get();
}
};
return awaitable { _coroutine };
}
};
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment