From eb15f1a384a89bd2f9c20e4ba958233ae6d3247a Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sat, 6 Jul 2024 14:29:22 -0700 Subject: [PATCH] WIP: remove convert_type() call from loaded_as_shared_ptr() --- include/pybind11/cast.h | 32 +++++++++++----------- include/pybind11/detail/type_caster_base.h | 6 ++-- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 1bdd9a483..18c96d293 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -794,11 +794,11 @@ protected: } } - bool load_value(value_and_holder &&v_h) { + void load_value(value_and_holder &&v_h) { if (v_h.holder_constructed()) { value = v_h.value_ptr(); holder = v_h.template holder(); - return true; + return; } throw cast_error("Unable to cast from non-held to held instance (T& to Holder) " #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) @@ -848,6 +848,9 @@ public: using base::value; bool load(handle src, bool convert) { + if (typeinfo->default_holder) { + return type_caster_generic::load(src, convert); + } return base::template load_impl>>( src, convert); } @@ -877,7 +880,7 @@ public: explicit operator std::shared_ptr &() { if (typeinfo->default_holder) { - shared_ptr_holder = sh_load_helper.loaded_as_shared_ptr(); + shared_ptr_holder = sh_load_helper.loaded_as_shared_ptr(value); } return shared_ptr_holder; } @@ -900,11 +903,11 @@ protected: friend class type_caster_generic; void check_holder_compat() {} - bool load_value_shared_ptr(const value_and_holder &v_h) { + void load_value_shared_ptr(const value_and_holder &v_h) { if (v_h.holder_constructed()) { value = v_h.value_ptr(); shared_ptr_holder = v_h.template holder>(); - return true; + return; } throw cast_error("Unable to cast from non-held to held instance (T& to Holder) " #if !defined(PYBIND11_DETAILED_ERROR_MESSAGES) @@ -916,16 +919,13 @@ protected: #endif } - bool load_value_smart_holder(const value_and_holder &v_h) { - sh_load_helper.loaded_v_h = v_h; - return true; - } - - bool load_value(value_and_holder &&v_h) { + void load_value(value_and_holder &&v_h) { if (typeinfo->default_holder) { - return load_value_smart_holder(v_h); + sh_load_helper.loaded_v_h = v_h; + type_caster_generic::load_value(std::move(v_h)); + return; } - return load_value_shared_ptr(v_h); + load_value_shared_ptr(v_h); } template , @@ -938,7 +938,7 @@ protected: detail::enable_if_t::value, int> = 0> bool try_implicit_casts(handle src, bool convert) { if (typeinfo->default_holder) { - throw std::runtime_error("BAKEIN_WIP: try_implicit_casts"); + return type_caster_generic::try_implicit_casts(src, convert); } for (auto &cast : typeinfo->implicit_casts) { copyable_holder_caster sub_caster(*cast.first); @@ -1029,11 +1029,11 @@ public: move_only_holder_caster>>(src, convert); } - bool load_value(value_and_holder &&v_h) { + void load_value(value_and_holder &&v_h) { if (typeinfo->default_holder) { sh_load_helper.loaded_v_h = v_h; sh_load_helper.loaded_v_h.type = get_type_info(typeid(type)); - return true; + return; } throw std::runtime_error("BAKEIN_WIP: What is the best behavior here?"); } diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index 27432ea10..779bb15a4 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -833,7 +833,8 @@ struct load_helper : value_and_holder_helper { shared_ptr_parent_life_support(parent.ptr())); } - std::shared_ptr loaded_as_shared_ptr(handle responsible_parent = nullptr) const { + std::shared_ptr loaded_as_shared_ptr(void *void_raw_ptr, + handle responsible_parent = nullptr) const { if (!have_holder()) { return nullptr; } @@ -846,8 +847,7 @@ struct load_helper : value_and_holder_helper { } throw std::runtime_error("Non-owning holder (loaded_as_shared_ptr)."); } - auto *void_raw_ptr = hld.template as_raw_ptr_unowned(); - auto *type_raw_ptr = convert_type(void_raw_ptr); + auto *type_raw_ptr = static_cast(void_raw_ptr); if (hld.pointee_depends_on_holder_owner) { auto *vptr_gd_ptr = std::get_deleter(hld.vptr); if (vptr_gd_ptr != nullptr) {