From f4c27c34e8efbdb3aefa687f04a788f61b8605f7 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 15 Jan 2021 12:18:18 -0800 Subject: [PATCH] Using type_caster_base::src_and_type directly, removing copy. Also renaming one cast to cast_const_raw_ptr, for clarity. --- tests/test_classh_wip.cpp | 49 ++++++++++----------------------------- 1 file changed, 12 insertions(+), 37 deletions(-) diff --git a/tests/test_classh_wip.cpp b/tests/test_classh_wip.cpp index f5bf70189..cb78fd8fd 100644 --- a/tests/test_classh_wip.cpp +++ b/tests/test_classh_wip.cpp @@ -117,14 +117,14 @@ struct type_caster : smart_holder_type_caster_load { } static handle cast(mpty const *src, return_value_policy policy, handle parent) { - // type_caster_base BEGIN - // clang-format off - auto st = src_and_type(src); - return cast( // Originally type_caster_generic::cast. - st.first, policy, parent, st.second, - make_copy_constructor(src), make_move_constructor(src)); - // clang-format on - // type_caster_base END + auto st = type_caster_base::src_and_type(src); + return cast_const_raw_ptr( // Originally type_caster_generic::cast. + st.first, + policy, + parent, + st.second, + make_copy_constructor(src), + make_move_constructor(src)); } static handle cast(mpty *src, return_value_policy policy, handle parent) { @@ -156,35 +156,9 @@ struct type_caster : smart_holder_type_caster_load { // clang-format on - using itype = mpty; - // type_caster_base BEGIN // clang-format off - // Returns a (pointer, type_info) pair taking care of necessary type lookup for a - // polymorphic type (using RTTI by default, but can be overridden by specializing - // polymorphic_type_hook). If the instance isn't derived, returns the base version. - static std::pair src_and_type(const itype *src) { - auto &cast_type = typeid(itype); - const std::type_info *instance_type = nullptr; - const void *vsrc = polymorphic_type_hook::get(src, instance_type); - if (instance_type && !same_type(cast_type, *instance_type)) { - // This is a base pointer to a derived type. If the derived type is registered - // with pybind11, we want to make the full derived object available. - // In the typical case where itype is polymorphic, we get the correct - // derived pointer (which may be != base pointer) by a dynamic_cast to - // most derived type. If itype is not polymorphic, we won't get here - // except via a user-provided specialization of polymorphic_type_hook, - // and the user has promised that no this-pointer adjustment is - // required in that case, so it's OK to use static_cast. - if (const auto *tpi = get_type_info(*instance_type)) - return {vsrc, tpi}; - } - // Otherwise we have either a nullptr, an `itype` pointer, or an unknown derived pointer, so - // don't do a cast - return type_caster_generic::src_and_type(src, cast_type, instance_type); - } - using Constructor = void *(*)(const void *); /* Only enabled when the types are {copy,move}-constructible *and* when the type @@ -210,7 +184,8 @@ struct type_caster : smart_holder_type_caster_load { // type_caster_base END // Originally type_caster_generic::cast. - PYBIND11_NOINLINE static handle cast(const void *_src, + PYBIND11_NOINLINE static handle cast_const_raw_ptr( + const void *_src, return_value_policy policy, handle parent, const detail::type_info *tinfo, @@ -316,7 +291,7 @@ struct type_caster> : smart_holder_type_caster_load } auto src_raw_ptr = src.get(); - auto st = type_caster::src_and_type(src_raw_ptr); + auto st = type_caster_base::src_and_type(src_raw_ptr); if (st.first == nullptr) return none().release(); // PyErr was set already. @@ -384,7 +359,7 @@ struct type_caster> : smart_holder_type_caster_load } auto src_raw_ptr = src.get(); - auto st = type_caster::src_and_type(src_raw_ptr); + auto st = type_caster_base::src_and_type(src_raw_ptr); if (st.first == nullptr) return none().release(); // PyErr was set already.