-
Notifications
You must be signed in to change notification settings - Fork 10
Enum support #114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kwabenantim
wants to merge
18
commits into
develop
Choose a base branch
from
113-enum-support
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Enum support #114
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
fb3c163
#113 Support enums as a first-class entity
kwabenantim 108305b
#113 Don't report `enum class` declarations as unknown classes
kwabenantim b9cf2f6
#113 Demonstrate enum wrapping in the shapes example
kwabenantim bfae486
#113 Only emit .export_values() for unscoped enums
kwabenantim 4579d53
#113 Add an inheritable export_values config option for enums
kwabenantim 09b73a2
#113 Use a non-reserved include guard in ShapeKind.hpp
kwabenantim 2992181
#113 Use non-reserved include guards in the example headers
kwabenantim b5e9c65
#113 Refresh enum docs and comments
kwabenantim fed0ec2
#113 Cover enum info update_from_ns and the enum header include
kwabenantim 91e0194
#113 Cover use_all_enums discovery and the pathless-enum include branch
kwabenantim 4606811
#113 Clarify source_file vs source_file_path in the reference
kwabenantim e051181
#113 Allow source_file for free functions and enums
kwabenantim 5e4d305
#113 Test that discovered enums inherit export_values
kwabenantim 7c3f744
#113 Use the add_* helpers in the discovery loops
kwabenantim a8ea39e
#113 Correct the not-found hint for enums and free functions
kwabenantim fdfa131
#113 Skip class-nested enums in use_all_enums discovery
kwabenantim 75f1d2a
#113 Document that struct-wrapped enums go under classes
kwabenantim dfeffa1
#113 Replace Chaste-specific examples with cppwg ones
kwabenantim File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """Enum information structure.""" | ||
|
|
||
| import logging | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from cppwg.info.cpp_entity_info import CppEntityInfo | ||
| from cppwg.utils import utils | ||
|
|
||
| if TYPE_CHECKING: | ||
| from pygccxml.declarations.namespace import namespace_t | ||
|
|
||
|
|
||
| class CppEnumInfo(CppEntityInfo): | ||
| """ | ||
| An information structure for individual enums to be wrapped. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| scoped : bool | ||
| Whether the enum is a scoped enum (`enum class`/`enum struct`). Scoped | ||
| enums do not export their enumerators into the enclosing scope, so | ||
| pybind11's `.export_values()` is omitted for them by default. | ||
| Note | ||
| ---- | ||
| The inheritable `export_values` config option (defined on BaseInfo) overrides | ||
| whether `.export_values()` is emitted; see should_export_values. | ||
| """ | ||
|
|
||
| def __init__(self, name: str, enum_config: dict[str, Any] | None = None): | ||
| super().__init__(name, enum_config) | ||
|
|
||
| self.scoped: bool = False | ||
|
|
||
| def should_export_values(self) -> bool: | ||
| """ | ||
| Return whether to emit pybind11's `.export_values()` for this enum. | ||
|
|
||
| The `export_values` config option wins if set at this enum or anywhere up | ||
| the info tree (package/module); otherwise mirror the C++ enum kind - | ||
| export for an unscoped enum, not for a scoped one. | ||
| """ | ||
| override = self.hierarchy_attribute("export_values") | ||
| if override is not None: | ||
| return override | ||
| return not self.scoped | ||
|
|
||
| def update_from_ns(self, source_ns: "namespace_t") -> None: | ||
| """ | ||
| Update with information from the source namespace. | ||
|
|
||
| Adds the enum declaration and records whether the enum is scoped. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| source_ns : pygccxml.declarations.namespace_t | ||
| The source namespace | ||
| """ | ||
| enum_decls = source_ns.enumerations(self.name, allow_empty=True) | ||
|
|
||
| if not enum_decls: | ||
| # The enum's header was not parsed. For an explicitly listed enum, | ||
| # set source_file or source_file_path so its header is included - | ||
| # unless it is already pulled in by another wrapped entity declared | ||
| # in the same header. | ||
| logger = logging.getLogger() | ||
| logger.error( | ||
| f"Could not find enum {self.name}. Set source_file or " | ||
| "source_file_path in the config so that its header is included." | ||
| ) | ||
| raise RuntimeError(f"Could not find enum: {self.name}") | ||
|
|
||
| self.decls = [enum_decls[0]] | ||
|
|
||
| # pygccxml does not expose enum scopedness, so read it from the source | ||
| # file the enum was declared in (via the resolved decl's location). | ||
| self.scoped = utils.is_scoped_enum_in_source_file( | ||
| self.decls[0].location.file_name, self.decls[0].name | ||
| ) |
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.