forked from pybind/pybind11
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Content of PR pybind#4374 applied on top of smart_holder branch.
- Loading branch information
Ralf W. Grosse-Kunstleve
committed
Dec 2, 2022
1 parent
8720cf9
commit 62dc3b3
Showing
3 changed files
with
62 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#include <pybind11/pybind11.h> | ||
|
||
#include "pybind11_tests.h" | ||
|
||
#include <iostream> | ||
#include <memory> | ||
#include <vector> | ||
|
||
// The first base class. | ||
struct Base0 { | ||
virtual ~Base0() = default; | ||
}; | ||
|
||
using Base0Ptr = std::shared_ptr<Base0>; | ||
|
||
// The second base class. | ||
struct Base1 { | ||
virtual ~Base1() = default; | ||
std::vector<int> vec = {1, 2, 3, 4, 5}; | ||
}; | ||
|
||
using Base1Ptr = std::shared_ptr<Base1>; | ||
|
||
// The derived class. | ||
struct Derived : Base1, Base0 { | ||
~Derived() override = default; | ||
}; | ||
|
||
using DerivedPtr = std::shared_ptr<Derived>; | ||
|
||
TEST_SUBMODULE(mi_debug, m) { | ||
// Expose the bases. | ||
pybind11::class_<Base0, Base0Ptr> bs0(m, "Base0"); | ||
pybind11::class_<Base1, Base1Ptr> bs1(m, "Base1"); | ||
// Expose the derived class. | ||
pybind11::class_<Derived, DerivedPtr, Base0, Base1>(m, "Derived").def(pybind11::init<>()); | ||
|
||
// A helper that returns a pointer to base. | ||
m.def("make_object", []() -> Base0Ptr { | ||
auto ret_der = std::make_shared<Derived>(); | ||
std::cout << "ret der ptr: " << ret_der.get() << std::endl; | ||
auto ret = Base0Ptr(ret_der); | ||
std::cout << "ret base ptr: " << ret.get() << std::endl; | ||
return ret; | ||
}); | ||
|
||
// A helper that accepts in input a pointer to derived. | ||
m.def("get_object_vec_size", [](const DerivedPtr &object) { | ||
std::cout << "der ptr: " << object.get() << std::endl; | ||
std::cout << object->vec.size() << std::endl; | ||
return object->vec.size(); | ||
}); | ||
} |
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,8 @@ | ||
import pytest | ||
|
||
m = pytest.importorskip("pybind11_tests.mi_debug") | ||
|
||
|
||
def test_vec(): | ||
o = m.make_object() | ||
assert 5 == m.get_object_vec_size(o) |