Introduce type_caster_base<>::unique_ptr_to_python()

This commit is contained in:
Ralf W. Grosse-Kunstleve 2024-07-01 12:39:45 -07:00
parent 5518e01562
commit 6ff547e18a
2 changed files with 17 additions and 2 deletions

View File

@ -932,8 +932,7 @@ struct move_only_holder_caster<type, std::unique_ptr<type, deleter>> {
"Holder classes are only supported for custom types"); "Holder classes are only supported for custom types");
static handle cast(std::unique_ptr<type, deleter> &&src, return_value_policy, handle) { static handle cast(std::unique_ptr<type, deleter> &&src, return_value_policy, handle) {
auto *ptr = src.get(); return type_caster_base<type>::unique_ptr_to_python(std::move(src));
return type_caster_base<type>::cast_holder(ptr, std::addressof(src));
} }
static constexpr auto name = type_caster_base<type>::name; static constexpr auto name = type_caster_base<type>::name;
}; };

View File

@ -1160,6 +1160,22 @@ public:
holder); holder);
} }
template <typename T, typename D>
static handle unique_ptr_to_python(std::unique_ptr<T, D> &&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 <typename T> template <typename T>
using cast_op_type = detail::cast_op_type<T>; using cast_op_type = detail::cast_op_type<T>;