mirror of
https://github.com/MoleskiCoder/EightBit.git
synced 2024-12-29 07:30:14 +00:00
Refactor co_generator_t into something a little more comprehensible to me.
This commit is contained in:
parent
42e72ce077
commit
c5ea0c2c21
@ -8,53 +8,68 @@
|
|||||||
namespace EightBit {
|
namespace EightBit {
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct co_generator_t final {
|
class co_generator_t final {
|
||||||
|
public:
|
||||||
|
class promise_type;
|
||||||
|
|
||||||
struct promise_type;
|
private:
|
||||||
using handle_type = std::coroutine_handle<promise_type>;
|
using handle_t = std::coroutine_handle<promise_type>;
|
||||||
|
|
||||||
struct promise_type {
|
class promise_type final {
|
||||||
|
private:
|
||||||
T value_;
|
T value_;
|
||||||
std::exception_ptr exception_;
|
std::exception_ptr exception_;
|
||||||
|
|
||||||
co_generator_t get_return_object() {
|
public:
|
||||||
return co_generator_t(handle_type::from_promise(*this));
|
|
||||||
}
|
|
||||||
std::suspend_always initial_suspend() { return {}; }
|
|
||||||
std::suspend_always final_suspend() noexcept { return {}; }
|
|
||||||
void unhandled_exception() { exception_ = std::current_exception(); }
|
|
||||||
template<std::convertible_to<T> From> // C++20 concept
|
template<std::convertible_to<T> From> // C++20 concept
|
||||||
std::suspend_always yield_value(From&& from) {
|
[[nodiscard]] constexpr std::suspend_always yield_value(From&& from) noexcept {
|
||||||
value_ = std::forward<From>(from);
|
value_ = std::forward<From>(from);
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
void return_void() {}
|
|
||||||
|
[[nodiscard]] constexpr std::suspend_always initial_suspend() noexcept { return {}; }
|
||||||
|
[[nodiscard]] constexpr std::suspend_always final_suspend() noexcept { return {}; }
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr co_generator_t get_return_object() noexcept {
|
||||||
|
return co_generator_t(handle_t::from_promise(*this));
|
||||||
|
}
|
||||||
|
|
||||||
|
constexpr void return_void() {}
|
||||||
|
|
||||||
|
constexpr void unhandled_exception() noexcept { exception_ = std::current_exception(); }
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr auto& value() noexcept { return value_; };
|
||||||
|
[[nodiscard]] constexpr auto& exception() noexcept { return exception_; };
|
||||||
};
|
};
|
||||||
|
|
||||||
handle_type h_;
|
handle_t h_;
|
||||||
|
|
||||||
co_generator_t(handle_type h) : h_(h) {}
|
|
||||||
~co_generator_t() { h_.destroy(); }
|
|
||||||
explicit operator bool() {
|
|
||||||
fill();
|
|
||||||
return !h_.done();
|
|
||||||
}
|
|
||||||
T operator()() {
|
|
||||||
fill();
|
|
||||||
full_ = false;
|
|
||||||
return std::move(h_.promise().value_);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool full_ = false;
|
bool full_ = false;
|
||||||
|
|
||||||
void fill() {
|
constexpr void fill() {
|
||||||
if (!full_) {
|
if (!full_) {
|
||||||
h_();
|
h_();
|
||||||
if (h_.promise().exception_)
|
if (h_.promise().exception())
|
||||||
std::rethrow_exception(h_.promise().exception_);
|
std::rethrow_exception(h_.promise().exception());
|
||||||
full_ = true;
|
full_ = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
|
co_generator_t(handle_t h) noexcept : h_(h) {}
|
||||||
|
|
||||||
|
~co_generator_t() noexcept { h_.destroy(); }
|
||||||
|
|
||||||
|
constexpr explicit operator bool() {
|
||||||
|
fill();
|
||||||
|
return !h_.done();
|
||||||
|
}
|
||||||
|
|
||||||
|
[[nodiscard]] constexpr T operator()() {
|
||||||
|
fill();
|
||||||
|
full_ = false;
|
||||||
|
return std::move(h_.promise().value());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user