Preparation for being smarter about casting to void * to evade to shared_from_this mechanism.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-06-23 17:24:04 -07:00 committed by Ralf W. Grosse-Kunstleve
parent ccd16a1a14
commit 1b9f85a09e
2 changed files with 20 additions and 13 deletions

View File

@ -254,13 +254,17 @@ struct smart_holder {
} }
template <typename T> template <typename T>
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("");
to_cout("LOOOK smart_holder from_raw_ptr_take_ownership " + std::to_string(__LINE__) + " " to_cout("LOOOK smart_holder from_raw_ptr_take_ownership " + std::to_string(__LINE__) + " "
+ __FILE__); + __FILE__);
ensure_pointee_is_destructible<T>("from_raw_ptr_take_ownership"); ensure_pointee_is_destructible<T>("from_raw_ptr_take_ownership");
smart_holder hld; smart_holder hld;
hld.vptr.reset(static_cast<void *>(raw_ptr), make_guarded_builtin_delete<T>(true)); auto gd = make_guarded_builtin_delete<T>(true);
if (void_cast_raw_ptr)
hld.vptr.reset(static_cast<void *>(raw_ptr), std::move(gd));
else
hld.vptr.reset(raw_ptr, std::move(gd));
hld.vptr_is_using_builtin_delete = true; hld.vptr_is_using_builtin_delete = true;
hld.is_populated = true; hld.is_populated = true;
return hld; return hld;
@ -305,18 +309,21 @@ struct smart_holder {
} }
template <typename T, typename D> template <typename T, typename D>
static smart_holder from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr) { static smart_holder from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr,
bool void_cast_raw_ptr = false) {
to_cout("LOOOK smart_holder from_unique_ptr " + std::to_string(__LINE__) + " " + __FILE__); to_cout("LOOOK smart_holder from_unique_ptr " + std::to_string(__LINE__) + " " + __FILE__);
smart_holder hld; smart_holder hld;
hld.rtti_uqp_del = &typeid(D); hld.rtti_uqp_del = &typeid(D);
hld.vptr_is_using_builtin_delete = is_std_default_delete<T>(*hld.rtti_uqp_del); hld.vptr_is_using_builtin_delete = is_std_default_delete<T>(*hld.rtti_uqp_del);
if (hld.vptr_is_using_builtin_delete) { guarded_delete gd{nullptr, false};
hld.vptr.reset(static_cast<void *>(unq_ptr.get()), if (hld.vptr_is_using_builtin_delete)
make_guarded_builtin_delete<T>(true)); gd = make_guarded_builtin_delete<T>(true);
} else { else
hld.vptr.reset(static_cast<void *>(unq_ptr.get()), gd = make_guarded_custom_deleter<T, D>(true);
make_guarded_custom_deleter<T, D>(true)); if (void_cast_raw_ptr)
} hld.vptr.reset(static_cast<void *>(unq_ptr.get()), std::move(gd));
else
hld.vptr.reset(unq_ptr.get(), std::move(gd));
unq_ptr.release(); unq_ptr.release();
hld.is_populated = true; hld.is_populated = true;
return hld; return hld;

View File

@ -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)) { uninitialized_location, value_ptr_w_t, value_ptr_w_t)) {
if (inst->owned) { if (inst->owned) {
new (uninitialized_location) 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 { } else {
new (uninitialized_location) new (uninitialized_location)
holder_type(holder_type::from_raw_ptr_unowned(value_ptr_w_t)); 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 <typename T, typename D> template <typename T, typename D>
static smart_holder smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr) { static smart_holder smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&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 <typename T> template <typename T>
@ -801,7 +801,7 @@ struct smart_holder_type_caster<std::unique_ptr<T, D>> : smart_holder_type_caste
void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr(); void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr();
valueptr = src_raw_void_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<const void *>(&smhldr)); tinfo->init_instance(inst_raw_ptr, static_cast<const void *>(&smhldr));
if (policy == return_value_policy::reference_internal) if (policy == return_value_policy::reference_internal)