#include "pybind11_tests.h" #include "object.h" #include namespace test_class_sh_shared_from_this { class MyObject3 : public std::enable_shared_from_this { public: MyObject3(const MyObject3 &) = default; MyObject3(int value) : value(value) { print_created(this, toString()); } std::string toString() const { return "MyObject3[" + std::to_string(value) + "]"; } virtual ~MyObject3() { print_destroyed(this); } private: int value; }; struct SharedFromThisRef { struct B : std::enable_shared_from_this { B() { print_created(this); } B(const B &) : std::enable_shared_from_this() { print_copy_created(this); } B(B &&) : std::enable_shared_from_this() { print_move_created(this); } ~B() { print_destroyed(this); } }; B value = {}; std::shared_ptr shared = std::make_shared(); }; struct SharedFromThisVBase : std::enable_shared_from_this { SharedFromThisVBase() = default; SharedFromThisVBase(const SharedFromThisVBase &) = default; virtual ~SharedFromThisVBase() = default; }; struct SharedFromThisVirt : virtual SharedFromThisVBase {}; } // namespace test_class_sh_shared_from_this using namespace test_class_sh_shared_from_this; PYBIND11_SMART_HOLDER_TYPE_CASTERS(MyObject3) PYBIND11_SMART_HOLDER_TYPE_CASTERS(SharedFromThisRef::B) PYBIND11_SMART_HOLDER_TYPE_CASTERS(SharedFromThisRef) PYBIND11_SMART_HOLDER_TYPE_CASTERS(SharedFromThisVirt) TEST_SUBMODULE(class_sh_shared_from_this, m) { py::classh(m, "MyObject3") .def(py::init()); m.def("make_myobject3_1", []() { return new MyObject3(8); }); m.def("make_myobject3_2", []() { return std::make_shared(9); }); m.def("print_myobject3_1", [](const MyObject3 *obj) { py::print(obj->toString()); }); m.def("print_myobject3_2", [](std::shared_ptr obj) { py::print(obj->toString()); }); m.def("print_myobject3_3", [](const std::shared_ptr &obj) { py::print(obj->toString()); }); // m.def("print_myobject3_4", [](const std::shared_ptr *obj) { py::print((*obj)->toString()); }); using B = SharedFromThisRef::B; py::classh(m, "B"); py::classh(m, "SharedFromThisRef") .def(py::init<>()) .def_readonly("bad_wp", &SharedFromThisRef::value) .def_property_readonly("ref", [](const SharedFromThisRef &s) -> const B & { return *s.shared; }) .def_property_readonly("copy", [](const SharedFromThisRef &s) { return s.value; }, py::return_value_policy::automatic) // XXX XXX XXX copy) .def_readonly("holder_ref", &SharedFromThisRef::shared) .def_property_readonly("holder_copy", [](const SharedFromThisRef &s) { return s.shared; }, py::return_value_policy::automatic) // XXX XXX XXX copy) .def("set_ref", [](SharedFromThisRef &, const B &) { return true; }) .def("set_holder", [](SharedFromThisRef &, std::shared_ptr) { return true; }); static std::shared_ptr sft(new SharedFromThisVirt()); py::classh(m, "SharedFromThisVirt") .def_static("get", []() { return sft.get(); }, py::return_value_policy::reference); }