diff --git a/CMakeLists.txt b/CMakeLists.txt index 3668f09c2..0964e2eef 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -156,6 +156,7 @@ set(PYBIND11_HEADERS include/pybind11/detail/smart_holder_poc.h include/pybind11/detail/type_caster_base.h include/pybind11/detail/typeid.h + include/pybind11/detail/using_smart_holder.h include/pybind11/detail/value_and_holder.h include/pybind11/attr.h include/pybind11/buffer_info.h diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index ba9f81c33..3a3a0c964 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1137,7 +1137,7 @@ struct is_holder_type> : std::true_type {}; #ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT template -struct is_holder_type : std::true_type {}; +struct is_holder_type : std::true_type {}; #endif #ifdef PYBIND11_DISABLE_HANDLE_TYPE_NAME_DEFAULT_IMPLEMENTATION // See PR #4888 diff --git a/include/pybind11/detail/init.h b/include/pybind11/detail/init.h index 9500ee7f1..75d25c119 100644 --- a/include/pybind11/detail/init.h +++ b/include/pybind11/detail/init.h @@ -10,7 +10,7 @@ #pragma once #include "class.h" -#include "smart_holder_poc.h" +#include "using_smart_holder.h" PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) @@ -156,10 +156,8 @@ void construct(value_and_holder &v_h, Alias *alias_ptr, bool) { // holder. This also handles types like std::shared_ptr and std::unique_ptr where T is a // derived type (through those holder's implicit conversion from derived class holder // constructors). -template < - typename Class, - detail::enable_if_t, pybindit::memory::smart_holder>::value, int> - = 0> +template , smart_holder>::value, int> = 0> void construct(value_and_holder &v_h, Holder holder, bool need_alias) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); auto *ptr = holder_helper>::get(holder); @@ -203,23 +201,21 @@ void construct(value_and_holder &v_h, Alias &&result, bool) { namespace originally_smart_holder_type_casters_h { template -pybindit::memory::smart_holder smart_holder_from_unique_ptr(std::unique_ptr &&unq_ptr, - bool void_cast_raw_ptr) { +smart_holder smart_holder_from_unique_ptr(std::unique_ptr &&unq_ptr, + bool void_cast_raw_ptr) { void *void_ptr = void_cast_raw_ptr ? static_cast(unq_ptr.get()) : nullptr; - return pybindit::memory::smart_holder::from_unique_ptr(std::move(unq_ptr), void_ptr); + return smart_holder::from_unique_ptr(std::move(unq_ptr), void_ptr); } template -pybindit::memory::smart_holder smart_holder_from_shared_ptr(std::shared_ptr shd_ptr) { - return pybindit::memory::smart_holder::from_shared_ptr(shd_ptr); +smart_holder smart_holder_from_shared_ptr(std::shared_ptr shd_ptr) { + return smart_holder::from_shared_ptr(shd_ptr); } } // namespace originally_smart_holder_type_casters_h -template < - typename Class, - typename D = std::default_delete>, - detail::enable_if_t, pybindit::memory::smart_holder>::value, int> - = 0> +template >, + detail::enable_if_t, smart_holder>::value, int> = 0> void construct(value_and_holder &v_h, std::unique_ptr, D> &&unq_ptr, bool need_alias) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); auto *ptr = unq_ptr.get(); @@ -239,11 +235,9 @@ void construct(value_and_holder &v_h, std::unique_ptr, D> &&unq_ptr, v_h.type->init_instance(v_h.inst, &smhldr); } -template < - typename Class, - typename D = std::default_delete>, - detail::enable_if_t, pybindit::memory::smart_holder>::value, int> - = 0> +template >, + detail::enable_if_t, smart_holder>::value, int> = 0> void construct(value_and_holder &v_h, std::unique_ptr, D> &&unq_ptr, bool /*need_alias*/) { @@ -255,10 +249,8 @@ void construct(value_and_holder &v_h, v_h.type->init_instance(v_h.inst, &smhldr); } -template < - typename Class, - detail::enable_if_t, pybindit::memory::smart_holder>::value, int> - = 0> +template , smart_holder>::value, int> = 0> void construct(value_and_holder &v_h, std::shared_ptr> &&shd_ptr, bool need_alias) { PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); auto *ptr = shd_ptr.get(); @@ -272,10 +264,8 @@ void construct(value_and_holder &v_h, std::shared_ptr> &&shd_ptr, boo v_h.type->init_instance(v_h.inst, &smhldr); } -template < - typename Class, - detail::enable_if_t, pybindit::memory::smart_holder>::value, int> - = 0> +template , smart_holder>::value, int> = 0> void construct(value_and_holder &v_h, std::shared_ptr> &&shd_ptr, bool /*need_alias*/) { diff --git a/include/pybind11/detail/type_caster_base.h b/include/pybind11/detail/type_caster_base.h index cd5bc52c7..1c6a82526 100644 --- a/include/pybind11/detail/type_caster_base.h +++ b/include/pybind11/detail/type_caster_base.h @@ -16,8 +16,8 @@ #include "descr.h" #include "dynamic_raw_ptr_cast_if_possible.h" #include "internals.h" -#include "smart_holder_poc.h" #include "typeid.h" +#include "using_smart_holder.h" #include "value_and_holder.h" #include @@ -484,9 +484,7 @@ struct value_and_holder_helper { return loaded_v_h.vh != nullptr && loaded_v_h.holder_constructed(); } - pybindit::memory::smart_holder &holder() const { - return loaded_v_h.holder(); - } + smart_holder &holder() const { return loaded_v_h.holder(); } void throw_if_uninitialized_or_disowned_holder(const char *typeid_name) const { static const std::string missing_value_msg = "Missing value for wrapped C++ type `"; @@ -548,7 +546,7 @@ handle smart_holder_from_unique_ptr(std::unique_ptr &&src, if (self_life_support != nullptr) { value_and_holder &v_h = self_life_support->v_h; if (v_h.inst != nullptr && v_h.vh != nullptr) { - auto &holder = v_h.holder(); + auto &holder = v_h.holder(); if (!holder.is_disowned) { pybind11_fail("smart_holder_from_unique_ptr: unexpected " "smart_holder.is_disowned failure."); @@ -576,8 +574,7 @@ handle smart_holder_from_unique_ptr(std::unique_ptr &&src, // SMART_HOLDER_WIP: IMPROVABLE: Is there a better solution? src_raw_void_ptr = nullptr; } - auto smhldr - = pybindit::memory::smart_holder::from_unique_ptr(std::move(src), src_raw_void_ptr); + auto smhldr = smart_holder::from_unique_ptr(std::move(src), src_raw_void_ptr); tinfo->init_instance(inst_raw_ptr, static_cast(&smhldr)); if (policy == return_value_policy::reference_internal) { @@ -639,8 +636,8 @@ handle smart_holder_from_shared_ptr(const std::shared_ptr &src, void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr(); valueptr = src_raw_void_ptr; - auto smhldr = pybindit::memory::smart_holder::from_shared_ptr( - std::shared_ptr(src, const_cast(st.first))); + auto smhldr + = smart_holder::from_shared_ptr(std::shared_ptr(src, const_cast(st.first))); tinfo->init_instance(inst_raw_ptr, static_cast(&smhldr)); if (policy == return_value_policy::reference_internal) { @@ -710,7 +707,7 @@ inline std::unique_ptr unique_with_deleter(T *raw_ptr, std::unique_ptr template struct load_helper : value_and_holder_helper { - using holder_type = pybindit::memory::smart_holder; + using holder_type = smart_holder; static std::shared_ptr make_shared_ptr_with_responsible_parent(T *raw_ptr, handle parent) { return std::shared_ptr(raw_ptr, shared_ptr_parent_life_support(parent.ptr())); diff --git a/include/pybind11/detail/using_smart_holder.h b/include/pybind11/detail/using_smart_holder.h new file mode 100644 index 000000000..1762944d0 --- /dev/null +++ b/include/pybind11/detail/using_smart_holder.h @@ -0,0 +1,14 @@ +// Copyright (c) 2024 The Pybind Development Team. +// All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +#pragma once + +#include "common.h" +#include "smart_holder_poc.h" + +PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) + +using pybindit::memory::smart_holder; + +PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 6fe93e0ae..ffce923ed 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -13,7 +13,7 @@ #include "detail/class.h" #include "detail/dynamic_raw_ptr_cast_if_possible.h" #include "detail/init.h" -#include "detail/smart_holder_poc.h" +#include "detail/using_smart_holder.h" #include "attr.h" #include "gil.h" #include "gil_safe_call_once.h" @@ -1800,7 +1800,7 @@ struct property_cpp_function< #ifdef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT // BAKEIN_WIP: Add comment to explain: This is meant for stress-testing only. template -using default_holder_type = pybindit::memory::smart_holder; +using default_holder_type = smart_holder; #else template using default_holder_type = std::unique_ptr; @@ -1863,7 +1863,7 @@ public: record.holder_enum_v = detail::holder_enum_t::std_unique_ptr; } else if (detail::is_instantiation::value) { record.holder_enum_v = detail::holder_enum_t::std_shared_ptr; - } else if (std::is_same::value) { + } else if (std::is_same::value) { record.holder_enum_v = detail::holder_enum_t::smart_holder; } else { record.holder_enum_v = detail::holder_enum_t::custom_holder; @@ -2193,8 +2193,7 @@ private: /// an optional pointer to an existing holder to use; if not specified and the instance is /// `.owned`, a new holder will be constructed to manage the value pointer. template ::value, int> - = 0> + detail::enable_if_t::value, int> = 0> static void init_instance(detail::instance *inst, const void *holder_ptr) { auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type))); if (!v_h.instance_registered()) { @@ -2230,7 +2229,7 @@ private: } template ::value, int> = 0> + detail::enable_if_t::value, int> = 0> static void init_instance(detail::instance *inst, const void *holder_const_void_ptr) { // Need for const_cast is a consequence of the type_info::init_instance type: // void (*init_instance)(instance *, const void *); diff --git a/include/pybind11/smart_holder.h b/include/pybind11/smart_holder.h index 8735ca95d..8d776d970 100644 --- a/include/pybind11/smart_holder.h +++ b/include/pybind11/smart_holder.h @@ -16,9 +16,9 @@ PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) // Supports easier switching between py::class_ and py::class_: // users can simply replace the `_` in `class_` with `h` or vice versa. template -class classh : public class_ { +class classh : public class_ { public: - using class_::class_; + using class_::class_; }; PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) diff --git a/include/pybind11/trampoline_self_life_support.h b/include/pybind11/trampoline_self_life_support.h index 47f623953..eac509438 100644 --- a/include/pybind11/trampoline_self_life_support.h +++ b/include/pybind11/trampoline_self_life_support.h @@ -5,7 +5,7 @@ #pragma once #include "detail/common.h" -#include "detail/smart_holder_poc.h" +#include "detail/using_smart_holder.h" #include "detail/value_and_holder.h" PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) @@ -40,7 +40,7 @@ struct trampoline_self_life_support { if (value_void_ptr != nullptr) { PyGILState_STATE threadstate = PyGILState_Ensure(); v_h.value_ptr() = nullptr; - v_h.holder().release_disowned(); + v_h.holder().release_disowned(); detail::deregister_instance(v_h.inst, value_void_ptr, v_h.type); Py_DECREF((PyObject *) v_h.inst); // Must be after deregister. PyGILState_Release(threadstate); diff --git a/tests/extra_python_package/test_files.py b/tests/extra_python_package/test_files.py index db2580df2..3dddce962 100644 --- a/tests/extra_python_package/test_files.py +++ b/tests/extra_python_package/test_files.py @@ -62,6 +62,7 @@ detail_headers = { "include/pybind11/detail/smart_holder_poc.h", "include/pybind11/detail/type_caster_base.h", "include/pybind11/detail/typeid.h", + "include/pybind11/detail/using_smart_holder.h", "include/pybind11/detail/value_and_holder.h", } diff --git a/tests/test_class.cpp b/tests/test_class.cpp index 2701632e1..a94b8e584 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -608,8 +608,7 @@ CHECK_NOALIAS(8); std::TYPE##_ptr>>::value, \ "DoesntBreak" #N " has wrong holder_type!") #define CHECK_SMART_HOLDER(N) \ - static_assert(std::is_same::value, \ + static_assert(std::is_same::value, \ "DoesntBreak" #N " has wrong holder_type!") CHECK_HOLDER(1, unique); CHECK_HOLDER(2, unique);