Replace std::generator with custom Generator for libc++ compatibility - #12
Open
Stevvven777 wants to merge 4 commits into
Open
Replace std::generator with custom Generator for libc++ compatibility#12Stevvven777 wants to merge 4 commits into
Stevvven777 wants to merge 4 commits into
Conversation
…lity std::generator (P2502R2) is not yet implemented in libc++, which prevents building on macOS/Apple Clang. This adds a custom coroutine-based Generator class with equivalent API and zero-copy reference semantics.
There was a problem hiding this comment.
Pull request overview
This PR replaces usage of std::generator with a custom coroutine-based wf::Generator<T> to restore C++23 builds on libc++ (macOS/Apple Clang), where std::generator is not yet available.
Changes:
- Added
include/wforge/generator.himplementingwf::Generator<T>as arange-like coroutine type. - Updated 2D helper APIs (
tilesOnSegment,neighbors4,neighbors8) to returnwf::Generatorinstead ofstd::generator. - Updated
PixelFont::textBitmapto returnwf::Generatorinstead ofstd::generator.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
| include/wforge/generator.h | Introduces a custom coroutine generator type intended to replace std::generator on libc++. |
| include/wforge/2d.h | Switches 2D generator-returning APIs to wf::Generator and updates includes accordingly. |
| include/wforge/assets.h | Updates PixelFont::textBitmap declaration to return wf::Generator. |
| src/2d.cpp | Updates implementations of tile/neighbor enumeration to return wf::Generator (and drops <generator>). |
| src/font.cpp | Updates PixelFont::textBitmap definition to return wf::Generator. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+20
to
+44
| struct promise_type { | ||
| T current_value; | ||
|
|
||
| auto get_return_object() { | ||
| return Generator{handle_type::from_promise(*this)}; | ||
| } | ||
|
|
||
| auto initial_suspend() noexcept { | ||
| return std::suspend_always{}; | ||
| } | ||
|
|
||
| auto final_suspend() noexcept { | ||
| return std::suspend_always{}; | ||
| } | ||
|
|
||
| auto yield_value(T value) noexcept { | ||
| current_value = std::move(value); | ||
| return std::suspend_always{}; | ||
| } | ||
|
|
||
| void return_void() noexcept {} | ||
|
|
||
| void unhandled_exception() { | ||
| std::terminate(); | ||
| } |
Comment on lines
+71
to
+91
| struct iterator { | ||
| handle_type _handle; | ||
|
|
||
| iterator &operator++() { | ||
| _handle.resume(); | ||
| return *this; | ||
| } | ||
|
|
||
| const T &operator*() const noexcept { | ||
| return _handle.promise().current_value; | ||
| } | ||
|
|
||
| bool operator==(std::default_sentinel_t) const { | ||
| return !_handle || _handle.done(); | ||
| } | ||
| }; | ||
|
|
||
| iterator begin() { | ||
| _handle.resume(); | ||
| return {_handle}; | ||
| } |
Comment on lines
+4
to
+7
| #include <coroutine> | ||
| #include <exception> | ||
| #include <iterator> | ||
| #include <utility> |
Comment on lines
+4
to
6
| #include "wforge/generator.h" | ||
| #include <array> | ||
| #include <generator> | ||
| #include <utility> |
| Aspect | Before | After |
|--------|--------|-------|
| Value storage | Member T current_value | manual_lifetime union + placement new |
| Exception handling | std::terminate() | Store exception_ptr + rethrow |
| Lifetime tracking | None | _started flag |
| Incomplete destructor | No cleanup | Explicit destruct() to avoid leak |
| final_suspend | suspend_always{} | Custom final_awaiter (symmetric transfer) |
| iterator | Copyable, no typedefs | Move-only, full input_iterator typedefs |
| operator++(int) | Missing | Defined |
| operator* return | const T& | reference (value type for array<int,2>) |
| begin() guard | None | assert(_handle && !_started) |
| Exception check | None | Checked in begin() and operator++
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
std::generator(P2502R2) is not yet implemented in libc++, which prevents building on macOS/Apple Clang.Solution
Add a custom
wf::Generator<T>ininclude/wforge/generator.hthat replicatesstd::generator's coroutine-based API with identicalco_yield/co_returnsemantics and range-based for loop support.Key characteristics:
const T&fromoperator*— zero-copy semantics matchingstd::generatorstd::generatorwhen libc++ eventually supports itFiles changed:
include/wforge/generator.h— custom generator implementationinclude/wforge/2d.h,include/wforge/assets.h,src/2d.cpp,src/font.cpp— replacestd::generator→wf::Generator