From 99e9595a9cbea4040284118ffcf5f7f93e27b994 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 4 Feb 2021 12:09:49 -0800 Subject: [PATCH] Introducing PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS. Using it in test_smart_ptr.cpp. With this test_smart_ptr builds with PYBIND11_USE_SMART_HOLDER_AS_DEFAULT and all but one test run successfully. --- include/pybind11/cast.h | 16 ++++++++++++++-- tests/test_smart_ptr.cpp | 31 +++++++++++++++++++++++++++---- 2 files changed, 41 insertions(+), 6 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 9584b02df..d0adc0332 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -955,7 +955,7 @@ protected: static Constructor make_move_constructor(...) { return nullptr; } }; -//DETAIL/SMART_HOLDER_TYPE_CASTERS_H/////////////////////////////////////////////////////////////// +//DETAIL/SMART_HOLDER_TYPE_CASTERS_H/BEGIN///////////////////////////////////////////////////////// //FWD begin inline void register_instance(instance *self, void *valptr, const type_info *tinfo); @@ -1585,14 +1585,26 @@ struct smart_holder_type_caster> } #endif -//DETAIL/SMART_HOLDER_TYPE_CASTERS_H/////////////////////////////////////////////////////////////// +//DETAIL/SMART_HOLDER_TYPE_CASTERS_H/END/////////////////////////////////////////////////////////// #ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT +#define PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(T, H) + template class type_caster : public type_caster_base { }; #else +#define PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(T, H) \ + namespace pybind11 { \ + namespace detail { \ + template <> \ + class type_caster : public type_caster_base {}; \ + template <> \ + class type_caster : public type_caster_holder {}; \ + } \ + } + template class type_caster : public smart_holder_type_caster {}; template diff --git a/tests/test_smart_ptr.cpp b/tests/test_smart_ptr.cpp index cf31b8386..bebc336b0 100644 --- a/tests/test_smart_ptr.cpp +++ b/tests/test_smart_ptr.cpp @@ -271,6 +271,29 @@ namespace { }; } +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(Object, ref) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MyObject1, ref) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MyObject2, std::shared_ptr) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MyObject3, std::shared_ptr) +using unique_ptr_myobject4_nodelete = std::unique_ptr; +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MyObject4, unique_ptr_myobject4_nodelete) +using unique_ptr_myobject4a_nodelete = std::unique_ptr; +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MyObject4a, unique_ptr_myobject4a_nodelete) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MyObject4b, std::unique_ptr) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(MyObject5, huge_unique_ptr) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(SharedPtrRef::A, std::shared_ptr) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(SharedPtrRef, std::unique_ptr) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(SharedFromThisRef::B, std::shared_ptr) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(SharedFromThisRef, std::unique_ptr) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(SharedFromThisVirt, std::shared_ptr) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(C, custom_unique_ptr) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(TypeForHolderWithAddressOf, shared_ptr_with_addressof_operator) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(TypeForMoveOnlyHolderWithAddressOf, unique_ptr_with_addressof_operator) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(HeldByDefaultHolder, std::unique_ptr) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(ElementBase, std::shared_ptr) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(ElementA, std::shared_ptr) +PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(ElementList, std::shared_ptr) + TEST_SUBMODULE(smart_ptr, m) { // test_smart_ptr @@ -338,7 +361,7 @@ TEST_SUBMODULE(smart_ptr, m) { .def_readwrite("value", &MyObject4a::value) .def_static("cleanup_all_instances", &MyObject4a::cleanupAllInstances); - py::class_(m, "MyObject4b") + py::class_>(m, "MyObject4b") .def(py::init()); py::class_>(m, "MyObject5") @@ -347,7 +370,7 @@ TEST_SUBMODULE(smart_ptr, m) { using A = SharedPtrRef::A; py::class_>(m, "A"); - py::class_(m, "SharedPtrRef") + py::class_>(m, "SharedPtrRef") .def(py::init<>()) .def_readonly("ref", &SharedPtrRef::value) .def_property_readonly("copy", [](const SharedPtrRef &s) { return s.value; }, @@ -360,7 +383,7 @@ TEST_SUBMODULE(smart_ptr, m) { using B = SharedFromThisRef::B; py::class_>(m, "B"); - py::class_(m, "SharedFromThisRef") + py::class_>(m, "SharedFromThisRef") .def(py::init<>()) .def_readonly("bad_wp", &SharedFromThisRef::value) .def_property_readonly("ref", [](const SharedFromThisRef &s) -> const B & { return *s.shared; }) @@ -395,7 +418,7 @@ TEST_SUBMODULE(smart_ptr, m) { .def_readwrite("value", &TypeForMoveOnlyHolderWithAddressOf::value) .def("print_object", [](const TypeForMoveOnlyHolderWithAddressOf *obj) { py::print(obj->toString()); }); - py::class_(m, "HeldByDefaultHolder") + py::class_>(m, "HeldByDefaultHolder") .def(py::init<>()) .def_static("load_shared_ptr", [](std::shared_ptr) {});