Commit 5f506528 authored by Tom Niget's avatar Tom Niget

Add preliminary support for generators with new coroutine system

parent dfbd8f84
......@@ -12,17 +12,17 @@ namespace typon {
/**
* https://github.com/feabhas/coroutines-blog
*/
template <typename T>
class Generator
{
class Promise
{
template <typename T> class Generator {
class Promise {
public:
using value_type = std::optional<T>;
Promise() = default;
std::suspend_always initial_suspend() { return {}; }
std::suspend_always final_suspend() noexcept { final = true; return {}; }
std::suspend_always final_suspend() noexcept {
final = true;
return {};
}
void unhandled_exception() {
std::rethrow_exception(std::move(std::current_exception()));
}
......@@ -36,18 +36,14 @@ class Generator
// this->value = std::move(value);
// }
void return_void() {
this->value = std::nullopt;
}
void return_void() { this->value = std::nullopt; }
inline Generator get_return_object();
value_type get_value() {
return std::move(value);
}
value_type get_value() { return std::move(value); }
bool finished() {
//return !value.has_value();
// return !value.has_value();
return final;
}
......@@ -60,12 +56,12 @@ public:
using value_type = T;
using promise_type = Promise;
explicit Generator(std::coroutine_handle<Promise> handle)
: handle (handle)
{}
explicit Generator(std::coroutine_handle<Promise> handle) : handle(handle) {}
~Generator() {
if (handle) { handle.destroy(); }
if (handle) {
handle.destroy();
}
}
Promise::value_type next() {
......@@ -74,24 +70,21 @@ public:
handle.resume();
}
return handle.promise().get_value();
}
else {
} else {
return {};
}
}
struct end_iterator {};
class iterator
{
class iterator {
public:
using value_type = Promise::value_type;
using difference_type = std::ptrdiff_t;
using difference_type = std::ptrdiff_t;
using iterator_category = std::input_iterator_tag;
iterator() = default;
iterator(Generator& generator) : generator{&generator}
{}
iterator(Generator &generator) : generator{&generator} {}
value_type operator*() const {
if (generator) {
......@@ -107,26 +100,26 @@ public:
return {};
}
iterator& operator++() {
iterator &operator++() {
if (generator && generator->handle) {
generator->handle.resume();
}
return *this;
}
iterator& operator++(int) {
iterator &operator++(int) {
if (generator && generator->handle) {
generator->handle.resume();
}
return *this;
}
bool operator== (const end_iterator&) const {
bool operator==(const end_iterator &) const {
return generator ? generator->handle.promise().finished() : true;
}
private:
Generator* generator{};
Generator *generator{};
};
iterator begin() {
......@@ -134,24 +127,18 @@ public:
return ++it;
}
end_iterator end() {
return end_sentinel;
}
end_iterator end() { return end_sentinel; }
std::optional<value_type> py_next() {
return next();
}
std::optional<value_type> py_next() { return next(); }
private:
end_iterator end_sentinel{};
std::coroutine_handle<Promise> handle;
};
template <typename T>
inline Generator<T> Generator<T>::Promise::get_return_object()
{
return Generator{ std::coroutine_handle<Promise>::from_promise(*this) };
inline Generator<T> Generator<T>::Promise::get_return_object() {
return Generator{std::coroutine_handle<Promise>::from_promise(*this)};
}
} // namespace typon
......
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