ifdef out more code if PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT is not defined.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2024-07-19 10:43:02 -07:00
parent 41433f632a
commit 4a66b46080
10 changed files with 54 additions and 20 deletions

View File

@ -156,8 +156,7 @@ void construct(value_and_holder &v_h, Alias<Class> *alias_ptr, bool) {
// holder. This also handles types like std::shared_ptr<T> and std::unique_ptr<T> where T is a // holder. This also handles types like std::shared_ptr<T> and std::unique_ptr<T> where T is a
// derived type (through those holder's implicit conversion from derived class holder // derived type (through those holder's implicit conversion from derived class holder
// constructors). // constructors).
template <typename Class, template <typename Class, detail::enable_if_t<!is_smart_holder<Holder<Class>>::value, int> = 0>
detail::enable_if_t<!std::is_same<Holder<Class>, smart_holder>::value, int> = 0>
void construct(value_and_holder &v_h, Holder<Class> holder, bool need_alias) { void construct(value_and_holder &v_h, Holder<Class> holder, bool need_alias) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
auto *ptr = holder_helper<Holder<Class>>::get(holder); auto *ptr = holder_helper<Holder<Class>>::get(holder);
@ -199,6 +198,8 @@ void construct(value_and_holder &v_h, Alias<Class> &&result, bool) {
v_h.value_ptr() = new Alias<Class>(std::move(result)); v_h.value_ptr() = new Alias<Class>(std::move(result));
} }
#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
namespace originally_smart_holder_type_casters_h { namespace originally_smart_holder_type_casters_h {
template <typename T, typename D> template <typename T, typename D>
smart_holder smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr, smart_holder smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr,
@ -215,7 +216,7 @@ smart_holder smart_holder_from_shared_ptr(std::shared_ptr<T> shd_ptr) {
template <typename Class, template <typename Class,
typename D = std::default_delete<Cpp<Class>>, typename D = std::default_delete<Cpp<Class>>,
detail::enable_if_t<std::is_same<Holder<Class>, smart_holder>::value, int> = 0> detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
void construct(value_and_holder &v_h, std::unique_ptr<Cpp<Class>, D> &&unq_ptr, bool need_alias) { void construct(value_and_holder &v_h, std::unique_ptr<Cpp<Class>, D> &&unq_ptr, bool need_alias) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
auto *ptr = unq_ptr.get(); auto *ptr = unq_ptr.get();
@ -237,7 +238,7 @@ void construct(value_and_holder &v_h, std::unique_ptr<Cpp<Class>, D> &&unq_ptr,
template <typename Class, template <typename Class,
typename D = std::default_delete<Alias<Class>>, typename D = std::default_delete<Alias<Class>>,
detail::enable_if_t<std::is_same<Holder<Class>, smart_holder>::value, int> = 0> detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
void construct(value_and_holder &v_h, void construct(value_and_holder &v_h,
std::unique_ptr<Alias<Class>, D> &&unq_ptr, std::unique_ptr<Alias<Class>, D> &&unq_ptr,
bool /*need_alias*/) { bool /*need_alias*/) {
@ -249,8 +250,7 @@ void construct(value_and_holder &v_h,
v_h.type->init_instance(v_h.inst, &smhldr); v_h.type->init_instance(v_h.inst, &smhldr);
} }
template <typename Class, template <typename Class, detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
detail::enable_if_t<std::is_same<Holder<Class>, smart_holder>::value, int> = 0>
void construct(value_and_holder &v_h, std::shared_ptr<Cpp<Class>> &&shd_ptr, bool need_alias) { void construct(value_and_holder &v_h, std::shared_ptr<Cpp<Class>> &&shd_ptr, bool need_alias) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias); PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
auto *ptr = shd_ptr.get(); auto *ptr = shd_ptr.get();
@ -264,8 +264,7 @@ void construct(value_and_holder &v_h, std::shared_ptr<Cpp<Class>> &&shd_ptr, boo
v_h.type->init_instance(v_h.inst, &smhldr); v_h.type->init_instance(v_h.inst, &smhldr);
} }
template <typename Class, template <typename Class, detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
detail::enable_if_t<std::is_same<Holder<Class>, smart_holder>::value, int> = 0>
void construct(value_and_holder &v_h, void construct(value_and_holder &v_h,
std::shared_ptr<Alias<Class>> &&shd_ptr, std::shared_ptr<Alias<Class>> &&shd_ptr,
bool /*need_alias*/) { bool /*need_alias*/) {
@ -276,6 +275,8 @@ void construct(value_and_holder &v_h,
v_h.type->init_instance(v_h.inst, &smhldr); v_h.type->init_instance(v_h.inst, &smhldr);
} }
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
// Implementing class for py::init<...>() // Implementing class for py::init<...>()
template <typename... Args> template <typename... Args>
struct constructor { struct constructor {

View File

@ -472,6 +472,8 @@ inline PyThreadState *get_thread_state_unchecked() {
void keep_alive_impl(handle nurse, handle patient); void keep_alive_impl(handle nurse, handle patient);
inline PyObject *make_new_instance(PyTypeObject *type); inline PyObject *make_new_instance(PyTypeObject *type);
#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
// SMART_HOLDER_WIP: Needs refactoring of existing pybind11 code. // SMART_HOLDER_WIP: Needs refactoring of existing pybind11 code.
inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo); inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo);
@ -828,6 +830,8 @@ struct load_helper : value_and_holder_helper {
PYBIND11_NAMESPACE_END(smart_holder_type_caster_support) PYBIND11_NAMESPACE_END(smart_holder_type_caster_support)
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
class type_caster_generic { class type_caster_generic {
public: public:
PYBIND11_NOINLINE explicit type_caster_generic(const std::type_info &type_info) PYBIND11_NOINLINE explicit type_caster_generic(const std::type_info &type_info)

View File

@ -4,17 +4,30 @@
#pragma once #pragma once
#include "common.h"
#include "internals.h" #include "internals.h"
#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT #include <type_traits>
# include "common.h" #ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
# include "smart_holder_poc.h" # include "smart_holder_poc.h"
#endif
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
using pybindit::memory::smart_holder; using pybindit::memory::smart_holder;
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
#endif #endif
PYBIND11_NAMESPACE_BEGIN(detail)
#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
template <typename H>
using is_smart_holder = std::is_same<H, smart_holder>;
#else
template <typename>
struct is_smart_holder : std::false_type {};
#endif
PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

View File

@ -2194,7 +2194,7 @@ private:
/// an optional pointer to an existing holder to use; if not specified and the instance is /// 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. /// `.owned`, a new holder will be constructed to manage the value pointer.
template <typename H = holder_type, template <typename H = holder_type,
detail::enable_if_t<!std::is_same<H, smart_holder>::value, int> = 0> detail::enable_if_t<!detail::is_smart_holder<H>::value, int> = 0>
static void init_instance(detail::instance *inst, const void *holder_ptr) { 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))); auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type)));
if (!v_h.instance_registered()) { if (!v_h.instance_registered()) {
@ -2229,8 +2229,9 @@ private:
return true; return true;
} }
#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
template <typename H = holder_type, template <typename H = holder_type,
detail::enable_if_t<std::is_same<H, smart_holder>::value, int> = 0> detail::enable_if_t<detail::is_smart_holder<H>::value, int> = 0>
static void init_instance(detail::instance *inst, const void *holder_const_void_ptr) { 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: // Need for const_cast is a consequence of the type_info::init_instance type:
// void (*init_instance)(instance *, const void *); // void (*init_instance)(instance *, const void *);
@ -2263,6 +2264,7 @@ private:
= pointee_depends_on_holder_owner; = pointee_depends_on_holder_owner;
v_h.set_holder_constructed(); v_h.set_holder_constructed();
} }
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
/// Deallocates an instance; via holder, if constructed; otherwise via operator delete. /// Deallocates an instance; via holder, if constructed; otherwise via operator delete.
static void dealloc(detail::value_and_holder &v_h) { static void dealloc(detail::value_and_holder &v_h) {

View File

@ -4,6 +4,10 @@
#pragma once #pragma once
#include "detail/internals.h"
#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
# include "detail/common.h" # include "detail/common.h"
# include "detail/using_smart_holder.h" # include "detail/using_smart_holder.h"
# include "detail/value_and_holder.h" # include "detail/value_and_holder.h"
@ -59,3 +63,5 @@ struct trampoline_self_life_support {
}; };
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT

View File

@ -34,6 +34,7 @@ struct AbaseAlias : Abase<SerNo> {
} }
}; };
#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
template <> template <>
struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support { struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support {
using Abase<1>::Abase; using Abase<1>::Abase;
@ -45,6 +46,7 @@ struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support {
other_val); other_val);
} }
}; };
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
template <int SerNo> template <int SerNo>
int AddInCppRawPtr(const Abase<SerNo> *obj, int other_val) { int AddInCppRawPtr(const Abase<SerNo> *obj, int other_val) {

View File

@ -37,9 +37,11 @@ protected:
Big5() : history{"DefaultConstructor"} {} Big5() : history{"DefaultConstructor"} {}
}; };
#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
struct Big5Trampoline : Big5, py::trampoline_self_life_support { struct Big5Trampoline : Big5, py::trampoline_self_life_support {
using Big5::Big5; using Big5::Big5;
}; };
#endif
} // namespace } // namespace

View File

@ -71,9 +71,11 @@ struct SftSharedPtrStash {
} }
}; };
#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
struct SftTrampoline : Sft, py::trampoline_self_life_support { struct SftTrampoline : Sft, py::trampoline_self_life_support {
using Sft::Sft; using Sft::Sft;
}; };
#endif
long use_count(const std::shared_ptr<Sft> &obj) { return obj.use_count(); } long use_count(const std::shared_ptr<Sft> &obj) { return obj.use_count(); }

View File

@ -32,7 +32,6 @@ private:
}; };
#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT #ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
class PyClass : public Class, public py::trampoline_self_life_support { class PyClass : public Class, public py::trampoline_self_life_support {
public: public:
std::unique_ptr<Class> clone() const override { std::unique_ptr<Class> clone() const override {
@ -41,7 +40,6 @@ public:
int foo() const override { PYBIND11_OVERRIDE_PURE(int, Class, foo); } int foo() const override { PYBIND11_OVERRIDE_PURE(int, Class, foo); }
}; };
#endif #endif
} // namespace class_sh_trampoline_basic } // namespace class_sh_trampoline_basic

View File

@ -31,6 +31,8 @@ int get_from_cpp_plainc_ptr(const Base *b) { return b->get() + 4000; }
int get_from_cpp_unique_ptr(std::unique_ptr<Base> b) { return b->get() + 5000; } int get_from_cpp_unique_ptr(std::unique_ptr<Base> b) { return b->get() + 5000; }
#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
struct BaseVirtualOverrider : Base, py::trampoline_self_life_support { struct BaseVirtualOverrider : Base, py::trampoline_self_life_support {
using Base::Base; using Base::Base;
@ -43,6 +45,8 @@ struct CppDerivedVirtualOverrider : CppDerived, py::trampoline_self_life_support
int get() const override { PYBIND11_OVERRIDE(int, CppDerived, get); } int get() const override { PYBIND11_OVERRIDE(int, CppDerived, get); }
}; };
#endif
} // namespace class_sh_virtual_py_cpp_mix } // namespace class_sh_virtual_py_cpp_mix
} // namespace pybind11_tests } // namespace pybind11_tests