Skip to content

Commit

Permalink
ifdef out more code if PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPP…
Browse files Browse the repository at this point in the history
…ORT is not defined.
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jul 19, 2024
1 parent 41433f6 commit 4a66b46
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 18 deletions.
17 changes: 9 additions & 8 deletions include/pybind11/detail/init.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
// derived type (through those holder's implicit conversion from derived class holder
// constructors).
template <typename Class,
detail::enable_if_t<!std::is_same<Holder<Class>, smart_holder>::value, int> = 0>
template <typename Class, detail::enable_if_t<!is_smart_holder<Holder<Class>>::value, int> = 0>
void construct(value_and_holder &v_h, Holder<Class> holder, bool need_alias) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
auto *ptr = holder_helper<Holder<Class>>::get(holder);
Expand Down Expand Up @@ -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));
}

#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT

namespace originally_smart_holder_type_casters_h {
template <typename T, typename D>
smart_holder smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr,
Expand All @@ -215,7 +216,7 @@ smart_holder smart_holder_from_shared_ptr(std::shared_ptr<T> shd_ptr) {

template <typename 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) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
auto *ptr = unq_ptr.get();
Expand All @@ -237,7 +238,7 @@ void construct(value_and_holder &v_h, std::unique_ptr<Cpp<Class>, D> &&unq_ptr,

template <typename 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,
std::unique_ptr<Alias<Class>, D> &&unq_ptr,
bool /*need_alias*/) {
Expand All @@ -249,8 +250,7 @@ void construct(value_and_holder &v_h,
v_h.type->init_instance(v_h.inst, &smhldr);
}

template <typename Class,
detail::enable_if_t<std::is_same<Holder<Class>, smart_holder>::value, int> = 0>
template <typename Class, detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
void construct(value_and_holder &v_h, std::shared_ptr<Cpp<Class>> &&shd_ptr, bool need_alias) {
PYBIND11_WORKAROUND_INCORRECT_MSVC_C4100(need_alias);
auto *ptr = shd_ptr.get();
Expand All @@ -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);
}

template <typename Class,
detail::enable_if_t<std::is_same<Holder<Class>, smart_holder>::value, int> = 0>
template <typename Class, detail::enable_if_t<is_smart_holder<Holder<Class>>::value, int> = 0>
void construct(value_and_holder &v_h,
std::shared_ptr<Alias<Class>> &&shd_ptr,
bool /*need_alias*/) {
Expand All @@ -276,6 +275,8 @@ void construct(value_and_holder &v_h,
v_h.type->init_instance(v_h.inst, &smhldr);
}

#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT

// Implementing class for py::init<...>()
template <typename... Args>
struct constructor {
Expand Down
4 changes: 4 additions & 0 deletions include/pybind11/detail/type_caster_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,8 @@ inline PyThreadState *get_thread_state_unchecked() {
void keep_alive_impl(handle nurse, handle patient);
inline PyObject *make_new_instance(PyTypeObject *type);

#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT

// SMART_HOLDER_WIP: Needs refactoring of existing pybind11 code.
inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo);

Expand Down Expand Up @@ -828,6 +830,8 @@ struct load_helper : value_and_holder_helper {

PYBIND11_NAMESPACE_END(smart_holder_type_caster_support)

#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT

class type_caster_generic {
public:
PYBIND11_NOINLINE explicit type_caster_generic(const std::type_info &type_info)
Expand Down
19 changes: 16 additions & 3 deletions include/pybind11/detail/using_smart_holder.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,30 @@

#pragma once

#include "common.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"
#endif

PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
using pybindit::memory::smart_holder;
#endif

PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
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)
6 changes: 4 additions & 2 deletions include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -2194,7 +2194,7 @@ class class_ : public detail::generic_type {
/// 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 <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) {
auto v_h = inst->get_value_and_holder(detail::get_type_info(typeid(type)));
if (!v_h.instance_registered()) {
Expand Down Expand Up @@ -2229,8 +2229,9 @@ class class_ : public detail::generic_type {
return true;
}

#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
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) {
// Need for const_cast is a consequence of the type_info::init_instance type:
// void (*init_instance)(instance *, const void *);
Expand Down Expand Up @@ -2263,6 +2264,7 @@ class class_ : public detail::generic_type {
= pointee_depends_on_holder_owner;
v_h.set_holder_constructed();
}
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT

/// Deallocates an instance; via holder, if constructed; otherwise via operator delete.
static void dealloc(detail::value_and_holder &v_h) {
Expand Down
12 changes: 9 additions & 3 deletions include/pybind11/trampoline_self_life_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@

#pragma once

#include "detail/common.h"
#include "detail/using_smart_holder.h"
#include "detail/value_and_holder.h"
#include "detail/internals.h"

#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT

# include "detail/common.h"
# include "detail/using_smart_holder.h"
# include "detail/value_and_holder.h"

PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

Expand Down Expand Up @@ -59,3 +63,5 @@ struct trampoline_self_life_support {
};

PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)

#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
2 changes: 2 additions & 0 deletions tests/test_class_sh_trampoline_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct AbaseAlias : Abase<SerNo> {
}
};

#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
template <>
struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support {
using Abase<1>::Abase;
Expand All @@ -45,6 +46,7 @@ struct AbaseAlias<1> : Abase<1>, py::trampoline_self_life_support {
other_val);
}
};
#endif // PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT

template <int SerNo>
int AddInCppRawPtr(const Abase<SerNo> *obj, int other_val) {
Expand Down
2 changes: 2 additions & 0 deletions tests/test_class_sh_trampoline_self_life_support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ struct Big5 { // Also known as "rule of five".
Big5() : history{"DefaultConstructor"} {}
};

#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
struct Big5Trampoline : Big5, py::trampoline_self_life_support {
using Big5::Big5;
};
#endif

} // namespace

Expand Down
2 changes: 2 additions & 0 deletions tests/test_class_sh_trampoline_shared_from_this.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ struct SftSharedPtrStash {
}
};

#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT
struct SftTrampoline : Sft, py::trampoline_self_life_support {
using Sft::Sft;
};
#endif

long use_count(const std::shared_ptr<Sft> &obj) { return obj.use_count(); }

Expand Down
2 changes: 0 additions & 2 deletions tests/test_class_sh_trampoline_unique_ptr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class Class {
};

#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT

class PyClass : public Class, public py::trampoline_self_life_support {
public:
std::unique_ptr<Class> clone() const override {
Expand All @@ -41,7 +40,6 @@ class PyClass : public Class, public py::trampoline_self_life_support {

int foo() const override { PYBIND11_OVERRIDE_PURE(int, Class, foo); }
};

#endif

} // namespace class_sh_trampoline_basic
Expand Down
4 changes: 4 additions & 0 deletions tests/test_class_sh_virtual_py_cpp_mix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

#ifdef PYBIND11_HAVE_INTERNALS_WITH_SMART_HOLDER_SUPPORT

struct BaseVirtualOverrider : Base, py::trampoline_self_life_support {
using Base::Base;

Expand All @@ -43,6 +45,8 @@ struct CppDerivedVirtualOverrider : CppDerived, py::trampoline_self_life_support
int get() const override { PYBIND11_OVERRIDE(int, CppDerived, get); }
};

#endif

} // namespace class_sh_virtual_py_cpp_mix
} // namespace pybind11_tests

Expand Down

0 comments on commit 4a66b46

Please sign in to comment.