#pragma once #include #include #include #include #include #include namespace Void::Coro { // ── Generator ────────────────────────────────────────────────────────────── // Lazy pull-based sequence driven by co_yield. Supports range-for. // The coroutine body is NOT resumed until the first iterator increment. // // Generator iota(int start) { // while (true) co_yield start++; // } // for (int v : iota(0) | take(5)) { ... } template class Generator { public: struct promise_type { T* current{nullptr}; std::exception_ptr exception; Generator get_return_object() { return Generator{std::coroutine_handle::from_promise(*this)}; } std::suspend_always initial_suspend() noexcept { return {}; } std::suspend_always final_suspend() noexcept { return {}; } template U> std::suspend_always yield_value(U&& v) noexcept(std::is_nothrow_constructible_v) { current = std::addressof(value_); value_ = std::forward(v); return {}; } void return_void() noexcept {} void unhandled_exception() { exception = std::current_exception(); } private: T value_{}; }; using handle_type = std::coroutine_handle; // ── construction ────────────────────────────────────────────────────────── explicit Generator(handle_type h) noexcept : handle_{h} {} Generator(const Generator&) = delete; Generator& operator=(const Generator&) = delete; Generator(Generator&& o) noexcept : handle_{std::exchange(o.handle_, {})} {} Generator& operator=(Generator&& o) noexcept { if (this != &o) { if (handle_) handle_.destroy(); handle_ = std::exchange(o.handle_, {}); } return *this; } ~Generator() { if (handle_) handle_.destroy(); } // ── range interface ─────────────────────────────────────────────────────── struct sentinel {}; struct iterator { using iterator_category = std::input_iterator_tag; using value_type = T; using difference_type = std::ptrdiff_t; handle_type handle; [[nodiscard]] bool operator==(sentinel) const noexcept { return handle.done(); } iterator& operator++() { handle.resume(); if (handle.done() && handle.promise().exception) std::rethrow_exception(handle.promise().exception); return *this; } void operator++(int) { ++(*this); } [[nodiscard]] T& operator*() const noexcept { return *handle.promise().current; } [[nodiscard]] T* operator->() const noexcept { return handle.promise().current; } }; [[nodiscard]] iterator begin() { handle_.resume(); // advance to the first yield if (handle_.done() && handle_.promise().exception) std::rethrow_exception(handle_.promise().exception); return iterator{handle_}; } [[nodiscard]] sentinel end() noexcept { return {}; } // ── explicit pull interface ─────────────────────────────────────────────── [[nodiscard]] bool done() const noexcept { return handle_.done(); } [[nodiscard]] bool valid() const noexcept { return handle_ && !handle_.done(); } // Resume and return the next value, or nullopt when exhausted. [[nodiscard]] std::optional next() { if (!handle_ || handle_.done()) return std::nullopt; handle_.resume(); if (handle_.done()) { if (handle_.promise().exception) std::rethrow_exception(handle_.promise().exception); return std::nullopt; } return *handle_.promise().current; } private: handle_type handle_; }; // ── generator combinators ───────────────────────────────────────────────────── // take(n) — yield at most n elements from g. template Generator take(Generator g, std::size_t n) { std::size_t i = 0; for (T& v : g) { if (i++ >= n) break; co_yield v; } } // filter(g, pred) — yield only elements satisfying pred. template requires std::predicate Generator filter(Generator g, Pred pred) { for (T& v : g) if (pred(v)) co_yield v; } // map(g, fn) — transform each element. template requires std::invocable Generator> map(Generator g, F fn) { for (T& v : g) co_yield fn(v); } // from_range(begin, end) — wrap an existing iterator range as a generator. template Generator::value_type> from_range(Iter first, Sentinel last) { for (; first != last; ++first) co_yield *first; } } // namespace Void::Coro