From 76fd3f8c39caa5efaeed45cba709a1630397bb4a Mon Sep 17 00:00:00 2001 From: Hao Zhang Date: Sun, 15 Mar 2026 17:54:21 +0800 Subject: [PATCH] chore: better generator.hh. --- include/ds/generator.hh | 124 +++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 72 deletions(-) diff --git a/include/ds/generator.hh b/include/ds/generator.hh index 8e5a3ba..3a207bf 100644 --- a/include/ds/generator.hh +++ b/include/ds/generator.hh @@ -2,45 +2,21 @@ #define DS_GENERATOR_HH #include -#include -#include namespace ds { template - class _generator_promise; - - template - class _generator { - using promise_type = _generator_promise; + struct _generator { + struct promise_type; + struct iterator_type; using handle_type = std::coroutine_handle; handle_type handle_; - public: - struct iterator { - handle_type h_; - - T& operator*() const noexcept { - return h_.promise().value_; - } - iterator& operator++() { - h_.resume(); - if (h_.done()) { - h_ = nullptr; - } - return *this; - } - bool operator==(std::nullptr_t) const noexcept { - return h_ == nullptr; - } - bool operator!=(std::nullptr_t) const noexcept { - return h_ != nullptr; - } - }; - - _generator(_generator&& other) noexcept : handle_(other.handle_) { + _generator() = default; + explicit _generator(handle_type h) : handle_(h) { } + _generator(_generator&& other) : handle_(other.handle_) { other.handle_ = nullptr; } - _generator& operator=(_generator&& other) noexcept { + _generator& operator=(_generator&& other) { if (this != &other) { if (handle_) { handle_.destroy(); @@ -50,14 +26,13 @@ namespace ds { } return *this; } - ~_generator() { if (handle_) { handle_.destroy(); } } - iterator begin() { + iterator_type begin() { if (!handle_) { return {nullptr}; } @@ -67,42 +42,54 @@ namespace ds { } return {handle_}; } - std::nullptr_t end() { - return nullptr; + iterator_type end() { + return {nullptr}; } - private: - _generator() noexcept : handle_(nullptr) { } - explicit _generator(handle_type h) noexcept : handle_(h) { } - friend class _generator_promise; - }; + struct promise_type { + T value_; - template - class _generator_promise { - public: - T value_; + _generator get_return_object() { + return _generator(handle_type::from_promise(*this)); + } + std::suspend_always initial_suspend() { + return {}; + } + std::suspend_always final_suspend() noexcept { + return {}; + } + std::suspend_always yield_value(T value) { + value_ = value; + return {}; + } + void unhandled_exception() { + throw; + } + void return_void() { } + }; - _generator get_return_object() noexcept { - return _generator(std::coroutine_handle<_generator_promise>::from_promise(*this)); - } - std::suspend_always initial_suspend() noexcept { - return {}; - } - std::suspend_always final_suspend() noexcept { - return {}; - } - std::suspend_always yield_value(T& value) noexcept { - value_ = value; - return {}; - } - std::suspend_always yield_value(T&& value) noexcept { - value_ = std::move(value); - return {}; - } - void return_void() noexcept { } - void unhandled_exception() { - std::terminate(); - } + struct iterator_type { + handle_type h_; + + T& operator*() const { + return h_.promise().value_; + } + iterator_type& operator++() { + if (h_) { + h_.resume(); + if (h_.done()) { + h_ = nullptr; + } + } + return *this; + } + bool operator==(const iterator_type& it) const { + return h_ == it.h_; + } + bool operator!=(const iterator_type& it) const { + return h_ != it.h_; + } + }; }; #if defined(__cpp_lib_generator) && 0 @@ -114,11 +101,4 @@ namespace ds { #endif } // namespace ds -namespace std { - template - struct coroutine_traits, Args...> { - using promise_type = ds::_generator_promise; - }; -} // namespace std - #endif