From 1b9f85a09e0f41d73a40f3bd4b4ee7b62a1021ce Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 23 Jun 2021 17:24:04 -0700 Subject: [PATCH] Preparation for being smarter about casting to `void *` to evade to shared_from_this mechanism. --- include/pybind11/detail/smart_holder_poc.h | 27 ++++++++++++------- .../detail/smart_holder_type_casters.h | 6 ++--- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/pybind11/detail/smart_holder_poc.h b/include/pybind11/detail/smart_holder_poc.h index f594cb257..48805069f 100644 --- a/include/pybind11/detail/smart_holder_poc.h +++ b/include/pybind11/detail/smart_holder_poc.h @@ -254,13 +254,17 @@ struct smart_holder { } template - static smart_holder from_raw_ptr_take_ownership(T *raw_ptr) { + static smart_holder from_raw_ptr_take_ownership(T *raw_ptr, bool void_cast_raw_ptr = false) { to_cout(""); to_cout("LOOOK smart_holder from_raw_ptr_take_ownership " + std::to_string(__LINE__) + " " + __FILE__); ensure_pointee_is_destructible("from_raw_ptr_take_ownership"); smart_holder hld; - hld.vptr.reset(static_cast(raw_ptr), make_guarded_builtin_delete(true)); + auto gd = make_guarded_builtin_delete(true); + if (void_cast_raw_ptr) + hld.vptr.reset(static_cast(raw_ptr), std::move(gd)); + else + hld.vptr.reset(raw_ptr, std::move(gd)); hld.vptr_is_using_builtin_delete = true; hld.is_populated = true; return hld; @@ -305,18 +309,21 @@ struct smart_holder { } template - static smart_holder from_unique_ptr(std::unique_ptr &&unq_ptr) { + static smart_holder from_unique_ptr(std::unique_ptr &&unq_ptr, + bool void_cast_raw_ptr = false) { to_cout("LOOOK smart_holder from_unique_ptr " + std::to_string(__LINE__) + " " + __FILE__); smart_holder hld; hld.rtti_uqp_del = &typeid(D); hld.vptr_is_using_builtin_delete = is_std_default_delete(*hld.rtti_uqp_del); - if (hld.vptr_is_using_builtin_delete) { - hld.vptr.reset(static_cast(unq_ptr.get()), - make_guarded_builtin_delete(true)); - } else { - hld.vptr.reset(static_cast(unq_ptr.get()), - make_guarded_custom_deleter(true)); - } + guarded_delete gd{nullptr, false}; + if (hld.vptr_is_using_builtin_delete) + gd = make_guarded_builtin_delete(true); + else + gd = make_guarded_custom_deleter(true); + if (void_cast_raw_ptr) + hld.vptr.reset(static_cast(unq_ptr.get()), std::move(gd)); + else + hld.vptr.reset(unq_ptr.get(), std::move(gd)); unq_ptr.release(); hld.is_populated = true; return hld; diff --git a/include/pybind11/detail/smart_holder_type_casters.h b/include/pybind11/detail/smart_holder_type_casters.h index 8cc161b34..19580c94d 100644 --- a/include/pybind11/detail/smart_holder_type_casters.h +++ b/include/pybind11/detail/smart_holder_type_casters.h @@ -314,7 +314,7 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag uninitialized_location, value_ptr_w_t, value_ptr_w_t)) { if (inst->owned) { new (uninitialized_location) - holder_type(holder_type::from_raw_ptr_take_ownership(value_ptr_w_t)); + holder_type(holder_type::from_raw_ptr_take_ownership(value_ptr_w_t, true)); } else { new (uninitialized_location) holder_type(holder_type::from_raw_ptr_unowned(value_ptr_w_t)); @@ -328,7 +328,7 @@ struct smart_holder_type_caster_class_hooks : smart_holder_type_caster_base_tag template static smart_holder smart_holder_from_unique_ptr(std::unique_ptr &&unq_ptr) { - return pybindit::memory::smart_holder::from_unique_ptr(std::move(unq_ptr)); + return pybindit::memory::smart_holder::from_unique_ptr(std::move(unq_ptr), true); } template @@ -801,7 +801,7 @@ struct smart_holder_type_caster> : smart_holder_type_caste void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr(); valueptr = src_raw_void_ptr; - auto smhldr = pybindit::memory::smart_holder::from_unique_ptr(std::move(src)); + auto smhldr = pybindit::memory::smart_holder::from_unique_ptr(std::move(src), true); tinfo->init_instance(inst_raw_ptr, static_cast(&smhldr)); if (policy == return_value_policy::reference_internal)