|
| 1 | +#ifndef _FPGEN_GENERATOR |
| 2 | +#define _FPGEN_GENERATOR |
| 3 | + |
| 4 | +#ifdef __clang__ |
| 5 | +#include <concepts> |
| 6 | +#include <experimental/coroutine> |
| 7 | +namespace std { |
| 8 | +using namespace experimental; |
| 9 | +} |
| 10 | +#else |
| 11 | +#include <concepts> |
| 12 | +#include <coroutine> |
| 13 | +#endif |
| 14 | + |
| 15 | +#include <exception> |
| 16 | + |
| 17 | +namespace fpgen { |
| 18 | +#ifdef __clang__ |
| 19 | +template <typename T> |
| 20 | +using enabler = |
| 21 | + typename std::enable_if<std::is_copy_assignable<T>::value, bool>::type; |
| 22 | + |
| 23 | +template <typename T, enabler<T> = true> class generator { |
| 24 | +#else |
| 25 | +template <std::copyable T> class generator { |
| 26 | +#endif |
| 27 | +public: |
| 28 | + struct promise_type { |
| 29 | + using value_type = T; |
| 30 | + using except_type = std::exception_ptr; |
| 31 | + using gen_type = generator<T>; |
| 32 | + using suspend_type = std::suspend_always; |
| 33 | + |
| 34 | + value_type value; |
| 35 | + except_type ex; |
| 36 | + |
| 37 | + gen_type get_return_object() { return gen_type(*this); } |
| 38 | + suspend_type initial_suspend() { return {}; } |
| 39 | + suspend_type final_suspend() noexcept { return {}; } |
| 40 | + void return_value(value_type v) { value = v; } |
| 41 | + suspend_type yield_value(value_type v) { |
| 42 | + value = v; |
| 43 | + return {}; |
| 44 | + } |
| 45 | + |
| 46 | + void unhandled_exception() { ex = std::current_exception(); } |
| 47 | + }; |
| 48 | + struct iterator_type { |
| 49 | + using gen_t = generator<T>; |
| 50 | + using iter_t = iterator_type; |
| 51 | + using value_t = T; |
| 52 | + |
| 53 | + gen_t &source; |
| 54 | + bool is_finished; |
| 55 | + value_t value; |
| 56 | + |
| 57 | + iterator_type(gen_t &source, bool is_finised) |
| 58 | + : source{source}, is_finished{is_finised} { |
| 59 | + if (!is_finised) |
| 60 | + ++(*this); |
| 61 | + } |
| 62 | + iterator_type(gen_t &source, bool is_finished, value_t value) |
| 63 | + : source{source}, is_finished{is_finished}, value{value} {} |
| 64 | + |
| 65 | + iter_t operator++() { |
| 66 | + if (!source) |
| 67 | + is_finished = true; |
| 68 | + value = source(); |
| 69 | + return {source, is_finished, value}; |
| 70 | + } |
| 71 | + operator bool() { return static_cast<bool>(source); } |
| 72 | + bool operator!=(iter_t &other) { return is_finished != other.is_finished; } |
| 73 | + value_t operator*() { return value; } |
| 74 | + operator value_t() { return value; } |
| 75 | + }; |
| 76 | + |
| 77 | + using handle_type = std::coroutine_handle<promise_type>; |
| 78 | + using value_type = T; |
| 79 | + |
| 80 | + generator(promise_type &p) : _h{handle_type::from_promise(p)} {} |
| 81 | + operator handle_type() const { return _h; } |
| 82 | + operator bool() const { return !_h.done(); } |
| 83 | + |
| 84 | + iterator_type begin() { return {*this, false}; } |
| 85 | + iterator_type end() { return {*this, true}; } |
| 86 | + |
| 87 | + value_type operator()() { |
| 88 | + if (*this) |
| 89 | + _h(); |
| 90 | + if (_h.promise().ex) |
| 91 | + std::rethrow_exception(_h.promise().ex); |
| 92 | + return _h.promise().value; |
| 93 | + } |
| 94 | + |
| 95 | +private: |
| 96 | + handle_type _h; |
| 97 | +}; |
| 98 | + |
| 99 | +} // namespace fpgen |
| 100 | + |
| 101 | +#endif |
0 commit comments