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

WIP Lazy coroutines

parent 7e33cffa
...@@ -13,7 +13,6 @@ namespace typon ...@@ -13,7 +13,6 @@ namespace typon
template <typename T> template <typename T>
struct result struct result
{ {
std::exception_ptr _exception; std::exception_ptr _exception;
union union
{ {
...@@ -56,14 +55,12 @@ namespace typon ...@@ -56,14 +55,12 @@ namespace typon
} }
return std::move(_value); return std::move(_value);
} }
}; };
template <typename T> template <typename T>
struct result<T&> struct result<T&>
{ {
T* _value; T* _value;
std::exception_ptr _exception; std::exception_ptr _exception;
...@@ -85,16 +82,16 @@ namespace typon ...@@ -85,16 +82,16 @@ namespace typon
} }
return *_value; return *_value;
} }
}; };
template <> template <>
struct result<void> struct result<void>
{ {
std::exception_ptr _exception; std::exception_ptr _exception;
void return_void() noexcept {}
void unhandled_exception() noexcept void unhandled_exception() noexcept
{ {
_exception = std::current_exception(); _exception = std::current_exception();
...@@ -107,7 +104,6 @@ namespace typon ...@@ -107,7 +104,6 @@ namespace typon
std::rethrow_exception(_exception); std::rethrow_exception(_exception);
} }
} }
}; };
} }
......
...@@ -3,7 +3,6 @@ ...@@ -3,7 +3,6 @@
#include <coroutine> #include <coroutine>
#include <promise.hpp>
#include <result.hpp> #include <result.hpp>
...@@ -11,49 +10,69 @@ namespace typon ...@@ -11,49 +10,69 @@ namespace typon
{ {
template <typename T = void> template <typename T = void>
//struct [[nodiscard]] task struct [[nodiscard]] task
struct task
{ {
struct promise_type; struct promise_type;
std::coroutine_handle<promise_type> _coroutine; std::coroutine_handle<promise_type> _coroutine;
result<T> _result;
struct promise_type : promise<T> ~task()
{ {
_coroutine.destroy();
}
// std::coroutine_handle<> _continuation; struct promise_type : result<T>
{
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 get_return_object() noexcept
{ {
task _task { std::coroutine_handle<promise_type>::from_promise(*this), result<T> {} }; return { std::coroutine_handle<promise_type>::from_promise(*this) };
this->_result = std::addressof(_task._result);
return _task;
} }
std::suspend_never initial_suspend() noexcept std::suspend_always initial_suspend() noexcept
{ {
return {}; return {};
} }
std::suspend_never final_suspend() noexcept final_awaitable final_suspend() noexcept
{ {
return {}; return {};
} }
}; };
// auto operator co_await() const&& noexcept auto operator co_await() const&& noexcept
// { {
// struct awaitable struct awaitable
// { {
// bool await_ready() noexcept 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