From 6ff547e18a4b2347802db4f4117db8393603daea Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 1 Jul 2024 12:39:45 -0700 Subject: [PATCH] Introduce type_caster_base<>::unique_ptr_to_python() --- include/pybind11/cast.h | 3 +-- include/pybind11/detail/type_caster_base.h | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 88ad045c7..d19a6c15e 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -932,8 +932,7 @@ struct move_only_holder_caster> { "Holder classes are only supported for custom types"); static handle cast(std::unique_ptr &&src, return_value_policy, handle) { - auto *ptr = src.get(); - return type_caster_base::cast_holder(ptr, std::addressof(src)); + return type_caster_base::unique_ptr_to_python(std::move(src)); } static constexpr auto name = type_caster_base::name; }; diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index fd8c81b9a..1630c16fb 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -1160,6 +1160,22 @@ public: holder); } + template + static handle unique_ptr_to_python(std::unique_ptr &&unq_ptr) { + auto *src = unq_ptr.get(); + auto st = src_and_type(src); + if (st.second->default_holder) { + throw std::runtime_error("BAKEIN_WIP"); + } + return type_caster_generic::cast(st.first, + return_value_policy::take_ownership, + {}, + st.second, + nullptr, + nullptr, + std::addressof(unq_ptr)); + } + template using cast_op_type = detail::cast_op_type;