EightBit/M6502/HarteTest_6502/co_generator_t.h
Adrian Conlon 9e9c15e289 Use C++20 co-routines as generators for opcode suite and test generation.
Signed-off-by: Adrian Conlon <adrian.conlon@gmail.com>
2021-10-27 09:53:58 +01:00

58 lines
1.4 KiB
C++

#pragma once
#include <coroutine>
#include <utility>
// from https://www.scs.stanford.edu/~dm/blog/c++-coroutines.html
template<typename T>
struct co_generator_t final {
struct promise_type;
using handle_type = std::coroutine_handle<promise_type>;
struct promise_type {
T value_;
std::exception_ptr exception_;
co_generator_t get_return_object() {
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
std::suspend_always yield_value(From&& from) {
value_ = std::forward<From>(from);
return {};
}
void return_void() {}
};
handle_type 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:
bool full_ = false;
void fill() {
if (!full_) {
h_();
if (h_.promise().exception_)
std::rethrow_exception(h_.promise().exception_);
full_ = true;
}
}
};