-
Notifications
You must be signed in to change notification settings - Fork 10
Wrap structs #117
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
Merged
Merged
Wrap structs #117
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
91b8539
#116 Wrap plain structs and expose public data members
kwabenantim b9c1a52
#116 Demonstrate struct data members in the shapes example
kwabenantim fe33927
#116 Document struct wrapping and public data members
kwabenantim a416ba7
#116 Make example include guards word-separated
kwabenantim 2eecf89
#116 Cover struct-wrap edge cases
kwabenantim 0667667
#116 Skip C-array data members that can't be bound
kwabenantim 6f305e7
#116 Skip reference and nested-class data members
kwabenantim 55f9629
#116 Skip non-copy-assignable mutable data members
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| """Wrapper code writer for C++ public data members.""" | ||
|
|
||
| import logging | ||
| from typing import TYPE_CHECKING | ||
|
|
||
| from pygccxml import declarations | ||
|
|
||
| from cppwg.utils import utils | ||
| from cppwg.writers.base_writer import CppBaseWrapperWriter | ||
|
|
||
| if TYPE_CHECKING: | ||
| from string import Template | ||
|
|
||
| from pygccxml.declarations.class_declaration import class_t | ||
| from pygccxml.declarations.variable import variable_t | ||
|
|
||
| from cppwg.info.class_info import CppClassInfo | ||
|
|
||
|
|
||
| class CppClassMemberWrapperWriter(CppBaseWrapperWriter): | ||
| """ | ||
| Manage addition of public data member wrapper code. | ||
|
|
||
| Emits a ``.def_readwrite`` (or ``.def_readonly`` for a const member) binding | ||
| so a class's public data members are readable/writable from Python. | ||
|
|
||
| Attributes | ||
| ---------- | ||
| class_info : CppClassInfo | ||
| The class information for the class owning the member | ||
| variable_decl : pygccxml.declarations.variable_t | ||
| The pygccxml declaration object for the member variable | ||
| class_decl : pygccxml.declarations.class_t | ||
| The class declaration for the class owning the member | ||
| wrapper_templates : dict[str, Template] | ||
| Templates with placeholders for generating wrapper code | ||
| class_py_name : str | None | ||
| The Python name of the class e.g. 'Foo_2_2' | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| class_info: "CppClassInfo", | ||
| template_idx: int, | ||
| variable_decl: "variable_t", | ||
| wrapper_templates: dict[str, "Template"], | ||
| ) -> None: | ||
| super().__init__(wrapper_templates) | ||
|
|
||
| self.class_info: "CppClassInfo" = class_info | ||
| self.variable_decl: "variable_t" = variable_decl | ||
| self.class_decl: "class_t" = class_info.decls[template_idx] | ||
|
|
||
| self.class_py_name = class_info.py_names[template_idx] | ||
| if self.class_py_name is None: | ||
| self.class_py_name = self.class_decl.name | ||
|
|
||
| def exclude(self) -> bool: | ||
| """ | ||
| Check if the member should be excluded from the wrapper code. | ||
|
|
||
| Returns | ||
| ------- | ||
| bool | ||
| True if the member should be excluded, False otherwise. | ||
| """ | ||
| logger = logging.getLogger() | ||
| variable_decl = self.variable_decl | ||
|
|
||
| # Skip members marked for exclusion in the config. | ||
| excluded_variables = self.class_info.hierarchy_attribute_gather_flat( | ||
| "excluded_variables" | ||
| ) | ||
| if variable_decl.name in excluded_variables: | ||
| return True | ||
|
|
||
| # Skip members belonging to a nested class. The variables() query is | ||
| # recursive, so it also returns fields of nested classes (e.g. an | ||
| # iterator); binding one as &Class::field would name a member the class | ||
| # does not have. Mirrors the parent check in the method/constructor | ||
| # writers. | ||
| if variable_decl.parent is not self.class_decl: | ||
| logger.debug( | ||
| f"Skipping nested-class member {self.class_py_name}::" | ||
| f"{variable_decl.name}" | ||
| ) | ||
| return True | ||
|
|
||
| # A reference member (e.g. `T& field`) cannot be bound: you cannot form a | ||
| # pointer-to-member for a reference, so &Class::field is ill-formed. | ||
| if declarations.is_reference(variable_decl.decl_type): | ||
| logger.debug( | ||
| f"Skipping reference member {self.class_py_name}::" | ||
| f"{variable_decl.name}" | ||
| ) | ||
| return True | ||
|
|
||
| # A bitfield member has no address, so &Class::field is ill-formed and it | ||
| # cannot be bound with def_readwrite/def_readonly. | ||
| if variable_decl.bits is not None: | ||
| logger.debug( | ||
| f"Skipping bitfield member {self.class_py_name}::{variable_decl.name}" | ||
| ) | ||
| return True | ||
|
|
||
| # A C-style array member (e.g. `double coords[3]`) cannot be bound: a | ||
| # def_readwrite setter assigns to the member, but C arrays are not | ||
| # assignable, and pybind11 has no type caster for a raw array, so both | ||
| # def_readwrite and def_readonly fail to compile. is_const already sees | ||
| # through the array, so this also covers const arrays bound read-only. | ||
| if declarations.is_array(variable_decl.decl_type): | ||
|
kwabenantim marked this conversation as resolved.
|
||
| logger.debug( | ||
| f"Skipping array member {self.class_py_name}::{variable_decl.name}" | ||
| ) | ||
| return True | ||
|
|
||
| # Static data members need def_readwrite_static/def_readonly_static and, | ||
| # for in-class-initialised static const members, an out-of-line definition | ||
| # to take their address. Skip them for now (see issue #116 follow-up). | ||
| if ( | ||
| variable_decl.type_qualifiers is not None | ||
| and variable_decl.type_qualifiers.has_static | ||
| ): | ||
| logger.debug( | ||
| f"Skipping static member {self.class_py_name}::{variable_decl.name}" | ||
| ) | ||
| return True | ||
|
|
||
| # A mutable member is bound read-write, whose pybind11 setter assigns to | ||
| # the member (obj.*pm = value). If the type is not copy-assignable (e.g. | ||
| # std::unique_ptr, std::atomic, or a class with a deleted operator=) that | ||
| # assignment does not compile, so skip it. A const member is bound | ||
| # read-only (no setter), so it is unaffected. | ||
| if not declarations.is_const( | ||
| variable_decl.decl_type | ||
| ) and not utils.type_is_copy_assignable(variable_decl.decl_type): | ||
| logger.debug( | ||
| f"Skipping non-copy-assignable member " | ||
| f"{self.class_py_name}::{variable_decl.name}" | ||
| ) | ||
| return True | ||
|
|
||
| return False | ||
|
|
||
| def generate_wrapper(self) -> str: | ||
| """ | ||
| Generate the member variable wrapper code. | ||
|
|
||
| Returns | ||
| ------- | ||
| str | ||
| The C++ wrapper code string, or "" if the member is excluded. | ||
| """ | ||
| if self.exclude(): | ||
| return "" | ||
|
|
||
| # A const member cannot be written from Python, so bind it read-only. | ||
| if declarations.is_const(self.variable_decl.decl_type): | ||
| access = "readonly" | ||
| else: | ||
| access = "readwrite" | ||
|
kwabenantim marked this conversation as resolved.
|
||
|
|
||
| return self.wrapper_templates["class_member"].substitute( | ||
| access=access, | ||
| member_name=self.variable_decl.name, | ||
| class_py_name=self.class_py_name, | ||
| ) | ||
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.