From 874f69d898cb468d0f1f61545cb8002ff4d02c00 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 4 Jul 2024 16:09:01 -0700 Subject: [PATCH] Add `cast(const std::unique_ptr &, ...)`. Remove the last 3 remaining BAKEIN_BREAK in test_class_sh_basic.cpp --- include/pybind11/cast.h | 14 ++++++++++++++ tests/test_class_sh_basic.cpp | 7 +++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index a481a30e6..4d15cafa6 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -988,6 +988,20 @@ public: std::move(src), policy, parent); } + static handle + cast(const std::unique_ptr &src, return_value_policy policy, handle parent) { + if (!src) { + return none().release(); + } + if (policy == return_value_policy::automatic) { + policy = return_value_policy::reference_internal; + } + if (policy != return_value_policy::reference_internal) { + throw cast_error("Invalid return_value_policy for unique_ptr&"); + } + return type_caster_base::cast(src.get(), policy, parent); + } + bool load(handle src, bool convert) { return base::template load_impl< move_only_holder_caster>>(src, convert); diff --git a/tests/test_class_sh_basic.cpp b/tests/test_class_sh_basic.cpp index e41f77176..fb9395180 100644 --- a/tests/test_class_sh_basic.cpp +++ b/tests/test_class_sh_basic.cpp @@ -202,9 +202,8 @@ TEST_SUBMODULE(class_sh_basic, m) { .def("pass_valu", &uconsumer::pass_valu) .def("pass_rref", &uconsumer::pass_rref) .def("rtrn_valu", &uconsumer::rtrn_valu) - // BAKEIN_BREAK .def("rtrn_lref", &uconsumer::rtrn_lref) - // BAKEIN_BREAK .def("rtrn_cref", &uconsumer::rtrn_cref) - ; + .def("rtrn_lref", &uconsumer::rtrn_lref) + .def("rtrn_cref", &uconsumer::rtrn_cref); // Helpers for testing. // These require selected functions above to work first, as indicated: @@ -212,7 +211,7 @@ TEST_SUBMODULE(class_sh_basic, m) { m.def("get_ptr", get_ptr); // pass_cref m.def("unique_ptr_roundtrip", unique_ptr_roundtrip); // pass_uqmp, rtrn_uqmp - // BAKEIN_BREAK m.def("unique_ptr_cref_roundtrip", unique_ptr_cref_roundtrip); + m.def("unique_ptr_cref_roundtrip", unique_ptr_cref_roundtrip); py::classh(m, "SharedPtrStash") .def(py::init<>())