-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Allow specifying custom base classes #4247
Draft
virtuald
wants to merge
2
commits into
pybind:master
Choose a base branch
from
virtuald:custom-base
base: master
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.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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 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 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,40 @@ | ||
/* | ||
tests/test_custom_base.cpp -- test custom type hierarchy support | ||
|
||
All rights reserved. Use of this source code is governed by a | ||
BSD-style license that can be found in the LICENSE file. | ||
*/ | ||
|
||
#include "pybind11_tests.h" | ||
|
||
namespace { | ||
|
||
struct Base { | ||
int i = 5; | ||
}; | ||
|
||
struct Derived { | ||
int j = 6; | ||
|
||
// just to prove the base can be anywhere | ||
Base base; | ||
}; | ||
|
||
} // namespace | ||
|
||
TEST_SUBMODULE(custom_base, m) { | ||
|
||
py::class_<Base>(m, "Base").def_readwrite("i", &Base::i); | ||
|
||
py::class_<Derived>(m, "Derived", py::custom_base<Base>([](void *o) -> void * { | ||
return &reinterpret_cast<Derived *>(o)->base; | ||
})) | ||
.def_readwrite("j", &Derived::j); | ||
|
||
m.def("create_derived", []() { return new Derived; }); | ||
m.def("create_base", []() { return new Base; }); | ||
|
||
m.def("base_i", [](Base *b) { return b->i; }); | ||
|
||
m.def("derived_j", [](Derived *d) { return d->j; }); | ||
}; |
This file contains 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,23 @@ | ||
from pybind11_tests import custom_base as m | ||
|
||
|
||
def test_cb_base(): | ||
b = m.create_base() | ||
|
||
assert isinstance(b, m.Base) | ||
assert b.i == 5 | ||
|
||
assert m.base_i(b) == 5 | ||
|
||
|
||
def test_cb_derived(): | ||
d = m.create_derived() | ||
|
||
assert isinstance(d, m.Derived) | ||
assert isinstance(d, m.Base) | ||
|
||
assert d.i == 5 | ||
assert d.j == 6 | ||
|
||
assert m.base_i(d) == 5 | ||
assert m.derived_j(d) == 6 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You really mean simple_ancestors=False, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't set that here, but that's what setting multiple_inheritance causes. See the TODO comment. :)