2021-03-19 19:18:39 +00:00
|
|
|
#include <pybind11/smart_holder.h>
|
|
|
|
|
2022-02-15 03:00:40 +00:00
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
2021-03-19 19:18:39 +00:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace pybind11_tests {
|
2021-03-22 19:16:29 +00:00
|
|
|
namespace class_sh_virtual_py_cpp_mix {
|
2021-03-19 19:18:39 +00:00
|
|
|
|
|
|
|
class Base {
|
|
|
|
public:
|
|
|
|
virtual ~Base() = default;
|
|
|
|
virtual int get() const { return 101; }
|
|
|
|
|
|
|
|
// Some compilers complain about implicitly defined versions of some of the following:
|
2021-07-02 23:51:24 +00:00
|
|
|
Base() = default;
|
2021-03-19 19:18:39 +00:00
|
|
|
Base(const Base &) = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
class CppDerivedPlain : public Base {
|
|
|
|
public:
|
|
|
|
int get() const override { return 202; }
|
|
|
|
};
|
|
|
|
|
|
|
|
class CppDerived : public Base {
|
|
|
|
public:
|
|
|
|
int get() const override { return 212; }
|
|
|
|
};
|
|
|
|
|
|
|
|
int get_from_cpp_plainc_ptr(const Base *b) { return b->get() + 4000; }
|
|
|
|
|
|
|
|
int get_from_cpp_unique_ptr(std::unique_ptr<Base> b) { return b->get() + 5000; }
|
|
|
|
|
2024-09-01 21:34:36 +00:00
|
|
|
#ifdef PYBIND11_SMART_HOLDER_ENABLED
|
2024-07-31 13:17:31 +00:00
|
|
|
|
2021-04-10 06:08:44 +00:00
|
|
|
struct BaseVirtualOverrider : Base, py::trampoline_self_life_support {
|
2021-03-19 19:18:39 +00:00
|
|
|
using Base::Base;
|
|
|
|
|
|
|
|
int get() const override { PYBIND11_OVERRIDE(int, Base, get); }
|
|
|
|
};
|
|
|
|
|
2021-04-10 06:08:44 +00:00
|
|
|
struct CppDerivedVirtualOverrider : CppDerived, py::trampoline_self_life_support {
|
2021-03-19 19:18:39 +00:00
|
|
|
using CppDerived::CppDerived;
|
|
|
|
|
|
|
|
int get() const override { PYBIND11_OVERRIDE(int, CppDerived, get); }
|
|
|
|
};
|
|
|
|
|
2024-07-31 13:17:31 +00:00
|
|
|
#endif
|
|
|
|
|
2021-03-22 19:16:29 +00:00
|
|
|
} // namespace class_sh_virtual_py_cpp_mix
|
2021-03-19 19:18:39 +00:00
|
|
|
} // namespace pybind11_tests
|
|
|
|
|
2024-07-22 08:56:42 +00:00
|
|
|
using namespace pybind11_tests::class_sh_virtual_py_cpp_mix;
|
2021-03-19 19:18:39 +00:00
|
|
|
|
2024-07-22 08:56:42 +00:00
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(Base)
|
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(CppDerivedPlain)
|
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(CppDerived)
|
2021-03-19 19:18:39 +00:00
|
|
|
|
2024-07-22 08:56:42 +00:00
|
|
|
TEST_SUBMODULE(class_sh_virtual_py_cpp_mix, m) {
|
2024-09-01 21:34:36 +00:00
|
|
|
m.attr("defined_PYBIND11_SMART_HOLDER_ENABLED") =
|
|
|
|
#ifndef PYBIND11_SMART_HOLDER_ENABLED
|
2024-07-31 13:17:31 +00:00
|
|
|
false;
|
|
|
|
#else
|
|
|
|
true;
|
|
|
|
|
2021-03-19 19:18:39 +00:00
|
|
|
py::classh<Base, BaseVirtualOverrider>(m, "Base").def(py::init<>()).def("get", &Base::get);
|
|
|
|
|
|
|
|
py::classh<CppDerivedPlain, Base>(m, "CppDerivedPlain").def(py::init<>());
|
|
|
|
|
|
|
|
py::classh<CppDerived, Base, CppDerivedVirtualOverrider>(m, "CppDerived").def(py::init<>());
|
|
|
|
|
|
|
|
m.def("get_from_cpp_plainc_ptr", get_from_cpp_plainc_ptr, py::arg("b"));
|
|
|
|
m.def("get_from_cpp_unique_ptr", get_from_cpp_unique_ptr, py::arg("b"));
|
2024-09-01 21:34:36 +00:00
|
|
|
#endif // PYBIND11_SMART_HOLDER_ENABLED
|
2021-03-19 19:18:39 +00:00
|
|
|
}
|