-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[smart_holder] .def_readonly, .def_readwrite adaptors (continuation of PR #3581). #3844
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
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fa0ad8a
Transferred net diff from PR #3581, as-is.
rwgk 68305a3
Automatic `pre-commit run --all-files` fixes. NO manual changes.
rwgk 7dee4ad
Removing trailing `//` (originally added to manipulate clang-format),…
rwgk 07c807c
Renaming `xetter_cpp_function` to `property_cpp_function` as suggeste…
rwgk fc77747
Fully explain the terse variable naming scheme in test_class_sh_prope…
rwgk 8ec5721
Also use parametrize for readonly, readwrite (as suggested by @rainwo…
rwgk 3c4168c
Apply change suggested by @skylion007 (with clang-format).
rwgk 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,86 @@ | ||
| // The compact 4-character naming matches that in test_class_sh_basic.cpp | ||
| // Variable names are intentionally terse, to not distract from the more important C++ type names: | ||
| // valu(e), ref(erence), ptr or p (pointer), r = rvalue, m = mutable, c = const, | ||
| // sh = shared_ptr, uq = unique_ptr. | ||
|
|
||
| #include "pybind11/smart_holder.h" | ||
| #include "pybind11_tests.h" | ||
|
|
||
| #include <memory> | ||
|
|
||
| namespace test_class_sh_property { | ||
|
|
||
| struct ClassicField { | ||
| int num = -88; | ||
| }; | ||
|
|
||
| struct ClassicOuter { | ||
| ClassicField *m_mptr = nullptr; | ||
| const ClassicField *m_cptr = nullptr; | ||
| }; | ||
|
|
||
| struct Field { | ||
| int num = -99; | ||
| }; | ||
|
|
||
| struct Outer { | ||
| Field m_valu; | ||
| Field *m_mptr = nullptr; | ||
| const Field *m_cptr = nullptr; | ||
| std::unique_ptr<Field> m_uqmp; | ||
| std::unique_ptr<const Field> m_uqcp; | ||
| std::shared_ptr<Field> m_shmp; | ||
| std::shared_ptr<const Field> m_shcp; | ||
| }; | ||
|
|
||
| inline void DisownOuter(std::unique_ptr<Outer>) {} | ||
|
|
||
| } // namespace test_class_sh_property | ||
|
|
||
| PYBIND11_TYPE_CASTER_BASE_HOLDER(test_class_sh_property::ClassicField, | ||
| std::unique_ptr<test_class_sh_property::ClassicField>) | ||
| PYBIND11_TYPE_CASTER_BASE_HOLDER(test_class_sh_property::ClassicOuter, | ||
| std::unique_ptr<test_class_sh_property::ClassicOuter>) | ||
|
|
||
| PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_property::Field) | ||
| PYBIND11_SMART_HOLDER_TYPE_CASTERS(test_class_sh_property::Outer) | ||
|
|
||
| TEST_SUBMODULE(class_sh_property, m) { | ||
| using namespace test_class_sh_property; | ||
|
|
||
| py::class_<ClassicField, std::unique_ptr<ClassicField>>(m, "ClassicField") | ||
| .def(py::init<>()) | ||
| .def_readwrite("num", &ClassicField::num); | ||
|
|
||
| py::class_<ClassicOuter, std::unique_ptr<ClassicOuter>>(m, "ClassicOuter") | ||
| .def(py::init<>()) | ||
| .def_readonly("m_mptr_readonly", &ClassicOuter::m_mptr) | ||
| .def_readwrite("m_mptr_readwrite", &ClassicOuter::m_mptr) | ||
| .def_readwrite("m_cptr_readonly", &ClassicOuter::m_cptr) | ||
| .def_readwrite("m_cptr_readwrite", &ClassicOuter::m_cptr); | ||
|
|
||
| py::classh<Field>(m, "Field").def(py::init<>()).def_readwrite("num", &Field::num); | ||
|
|
||
| py::classh<Outer>(m, "Outer") | ||
| .def(py::init<>()) | ||
|
|
||
| .def_readonly("m_valu_readonly", &Outer::m_valu) | ||
| .def_readwrite("m_valu_readwrite", &Outer::m_valu) | ||
|
|
||
| .def_readonly("m_mptr_readonly", &Outer::m_mptr) | ||
| .def_readwrite("m_mptr_readwrite", &Outer::m_mptr) | ||
| .def_readonly("m_cptr_readonly", &Outer::m_cptr) | ||
| .def_readwrite("m_cptr_readwrite", &Outer::m_cptr) | ||
|
|
||
| // .def_readonly("m_uqmp_readonly", &Outer::m_uqmp) // Custom compilation Error. | ||
| .def_readwrite("m_uqmp_readwrite", &Outer::m_uqmp) | ||
| // .def_readonly("m_uqcp_readonly", &Outer::m_uqcp) // Custom compilation Error. | ||
| .def_readwrite("m_uqcp_readwrite", &Outer::m_uqcp) | ||
|
|
||
| .def_readwrite("m_shmp_readonly", &Outer::m_shmp) | ||
| .def_readwrite("m_shmp_readwrite", &Outer::m_shmp) | ||
| .def_readwrite("m_shcp_readonly", &Outer::m_shcp) | ||
| .def_readwrite("m_shcp_readwrite", &Outer::m_shcp); | ||
|
|
||
| m.def("DisownOuter", DisownOuter); | ||
| } |
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.