|
| 1 | +#include <pybind11/pybind11.h> |
| 2 | + |
| 3 | +#include "pybind11_tests.h" |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <memory> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +// The first base class. |
| 10 | +struct Base0 { |
| 11 | + virtual ~Base0() = default; |
| 12 | +}; |
| 13 | + |
| 14 | +using Base0Ptr = std::shared_ptr<Base0>; |
| 15 | + |
| 16 | +// The second base class. |
| 17 | +struct Base1 { |
| 18 | + virtual ~Base1() = default; |
| 19 | + std::vector<int> vec = {1, 2, 3, 4, 5}; |
| 20 | +}; |
| 21 | + |
| 22 | +using Base1Ptr = std::shared_ptr<Base1>; |
| 23 | + |
| 24 | +// The derived class. |
| 25 | +struct Derived : Base1, Base0 { |
| 26 | + ~Derived() override = default; |
| 27 | +}; |
| 28 | + |
| 29 | +using DerivedPtr = std::shared_ptr<Derived>; |
| 30 | + |
| 31 | +TEST_SUBMODULE(mi_debug, m) { |
| 32 | + // Expose the bases. |
| 33 | + pybind11::class_<Base0, Base0Ptr> bs0(m, "Base0"); |
| 34 | + pybind11::class_<Base1, Base1Ptr> bs1(m, "Base1"); |
| 35 | + // Expose the derived class. |
| 36 | + pybind11::class_<Derived, DerivedPtr, Base0, Base1>(m, "Derived").def(pybind11::init<>()); |
| 37 | + |
| 38 | + // A helper that returns a pointer to base. |
| 39 | + m.def("make_object", []() -> Base0Ptr { |
| 40 | + auto ret_der = std::make_shared<Derived>(); |
| 41 | + std::cout << "ret der ptr: " << ret_der.get() << std::endl; |
| 42 | + auto ret = Base0Ptr(ret_der); |
| 43 | + std::cout << "ret base ptr: " << ret.get() << std::endl; |
| 44 | + return ret; |
| 45 | + }); |
| 46 | + |
| 47 | + // A helper that accepts in input a pointer to derived. |
| 48 | + m.def("get_object_vec_size", [](const DerivedPtr &object) { |
| 49 | + std::cout << "der ptr: " << object.get() << std::endl; |
| 50 | + std::cout << object->vec.size() << std::endl; |
| 51 | + return object->vec.size(); |
| 52 | + }); |
| 53 | +} |
0 commit comments