Fix after rebase

parent a886a567
......@@ -107,7 +107,7 @@ set(PYBIND11_HEADERS
include/pybind11/detail/internals.h
include/pybind11/detail/smart_holder_init_inline_include.h
include/pybind11/detail/smart_holder_poc.h
include/pybind11/detail/smart_holder_type_casters_inline_include.h
include/pybind11/detail/smart_holder_type_casters.h
include/pybind11/detail/typeid.h
include/pybind11/attr.h
include/pybind11/buffer_info.h
......
#pragma once
#include "common.h"
#include <type_traits>
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
// #define PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
// Currently the main purpose of this switch is to enable non-intrusive comprehensive testing. If
// and when `smart_holder` will actually become the released default is currently open. In the
// meantime, the full functionality is easily available by using `py::classh`, which is just a
// handy shortcut for `py::class_<T, py::smart_holder>` (see `pybind11/smart_holder.h`). Classes
// wrapped in this way are fully compatible with everything existing.
#endif
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
PYBIND11_NAMESPACE_BEGIN(detail)
template <typename T>
struct is_smart_holder_type : std::false_type {};
// Tag to be used as base class, inspected by is_smart_holder_type_caster<T> test.
struct is_smart_holder_type_caster_base_tag {};
template <typename T>
struct is_smart_holder_type_caster;
PYBIND11_NAMESPACE_END(detail)
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
......@@ -11,7 +11,8 @@ void construct(value_and_holder &v_h, std::unique_ptr<Cpp<Class>, D> &&unq_ptr,
if (Class::has_alias && need_alias)
throw type_error("pybind11::init(): construction failed: returned std::unique_ptr pointee "
"is not an alias instance");
auto smhldr = smart_holder::from_unique_ptr(std::move(unq_ptr));
auto smhldr
= type_caster<Cpp<Class>>::template smart_holder_from_unique_ptr(std::move(unq_ptr));
v_h.value_ptr() = ptr;
v_h.type->init_instance(v_h.inst, &smhldr);
}
......@@ -24,7 +25,8 @@ void construct(value_and_holder &v_h,
bool /*need_alias*/) {
auto *ptr = unq_ptr.get();
no_nullptr(ptr);
auto smhldr = smart_holder::from_unique_ptr(std::move(unq_ptr));
auto smhldr
= type_caster<Alias<Class>>::template smart_holder_from_unique_ptr(std::move(unq_ptr));
v_h.value_ptr() = ptr;
v_h.type->init_instance(v_h.inst, &smhldr);
}
......@@ -37,7 +39,7 @@ void construct(value_and_holder &v_h, std::shared_ptr<Cpp<Class>> &&shd_ptr, boo
if (Class::has_alias && need_alias)
throw type_error("pybind11::init(): construction failed: returned std::shared_ptr pointee "
"is not an alias instance");
auto smhldr = smart_holder::from_shared_ptr(std::move(shd_ptr));
auto smhldr = type_caster<Cpp<Class>>::template smart_holder_from_shared_ptr(shd_ptr);
v_h.value_ptr() = ptr;
v_h.type->init_instance(v_h.inst, &smhldr);
}
......@@ -49,7 +51,7 @@ void construct(value_and_holder &v_h,
bool /*need_alias*/) {
auto *ptr = shd_ptr.get();
no_nullptr(ptr);
auto smhldr = smart_holder::from_shared_ptr(std::move(shd_ptr));
auto smhldr = type_caster<Alias<Class>>::template smart_holder_from_shared_ptr(shd_ptr);
v_h.value_ptr() = ptr;
v_h.type->init_instance(v_h.inst, &smhldr);
}
#ifndef PYBIND11_CAST_H_SMART_HOLDER_TYPE_CASTERS_INLINE_INCLUDE_SAFETY_GUARD
# error "THIS FILE MUST ONLY BE INCLUDED FROM pybind11/cast.h"
#endif
#pragma once
#define PYBIND11_SMART_HOLDER_TYPE_CASTERS_H_IS_INCLUDED
#include "is_smart_holder_type_caster.h"
#include "smart_holder_poc.h"
#include "type_caster_base.h"
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
......@@ -10,6 +11,9 @@ using pybindit::memory::smart_holder;
PYBIND11_NAMESPACE_BEGIN(detail)
template <>
struct is_smart_holder_type<smart_holder> : std::true_type {};
// SMART_HOLDER_WIP: Needs refactoring of existing pybind11 code.
inline void register_instance(instance *self, void *valptr, const type_info *tinfo);
inline bool deregister_instance(instance *self, void *valptr, const type_info *tinfo);
......@@ -266,6 +270,16 @@ struct smart_holder_type_caster_class_hooks : is_smart_holder_type_caster_base_t
}
v_h.set_holder_constructed();
}
template <typename T, typename D>
static smart_holder smart_holder_from_unique_ptr(std::unique_ptr<T, D> &&unq_ptr) {
return pybindit::memory::smart_holder::from_unique_ptr(std::move(unq_ptr));
}
template <typename T>
static smart_holder smart_holder_from_shared_ptr(std::shared_ptr<T> shd_ptr) {
return pybindit::memory::smart_holder::from_shared_ptr(shd_ptr);
}
};
template <typename T>
......@@ -668,6 +682,7 @@ struct smart_holder_type_caster<std::unique_ptr<T const, D>>
};
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
# define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T) \
namespace pybind11 { \
namespace detail { \
......@@ -687,6 +702,29 @@ struct smart_holder_type_caster<std::unique_ptr<T const, D>>
: public smart_holder_type_caster<std::unique_ptr<T const, D>> {}; \
} \
}
#else
# define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T)
template <typename T>
class type_caster_for_class_ : public smart_holder_type_caster<T> {};
template <typename T>
class type_caster_for_class_<std::shared_ptr<T>>
: public smart_holder_type_caster<std::shared_ptr<T>> {};
template <typename T>
class type_caster_for_class_<std::shared_ptr<T const>>
: public smart_holder_type_caster<std::shared_ptr<T const>> {};
template <typename T, typename D>
class type_caster_for_class_<std::unique_ptr<T, D>>
: public smart_holder_type_caster<std::unique_ptr<T, D>> {};
template <typename T, typename D>
class type_caster_for_class_<std::unique_ptr<T const, D>>
: public smart_holder_type_caster<std::unique_ptr<T const, D>> {};
#endif
PYBIND11_NAMESPACE_END(detail)
......
......@@ -1245,9 +1245,27 @@ auto method_adaptor(Return (Class::*pmf)(Args...) const) -> Return (Derived::*)(
// clang-format on
template <typename T>
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
using default_holder_type = std::unique_ptr<T>;
# define PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(T, ...)
#else
using default_holder_type = smart_holder;
// This define could be hidden away inside detail/smart_holder_type_casters.h, but is kept here
// for clarity.
# define PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(T, ...) \
namespace pybind11 { \
namespace detail { \
template <> \
class type_caster<T> : public type_caster_base<T> {}; \
template <> \
class type_caster<__VA_ARGS__> : public type_caster_holder<T, __VA_ARGS__> {}; \
} \
}
#endif
// clang-format off
......@@ -1294,7 +1312,7 @@ public:
// clang-format on
static constexpr bool holder_is_smart_holder
= std::is_same<holder_type, smart_holder>::value;
= detail::is_smart_holder_type<holder_type>::value;
static constexpr bool type_caster_type_is_smart_holder_type_caster
= detail::is_smart_holder_type_caster<type>::value;
static constexpr bool type_caster_type_is_type_caster_base_subtype
......
......@@ -4,6 +4,7 @@
#pragma once
#include "detail/smart_holder_type_casters.h"
#include "pybind11.h"
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
......
#include <pybind11/pybind11.h>
#include <pybind11/smart_holder.h>
#include <string>
......
// Identical to class_sh_module_local_2.cpp, except 2 replaced with 1.
#include <pybind11/pybind11.h>
#include <pybind11/smart_holder.h>
#include <string>
......
// Identical to class_sh_module_local_1.cpp, except 1 replaced with 2.
#include <pybind11/pybind11.h>
#include <pybind11/smart_holder.h>
#include <string>
......
......@@ -45,7 +45,7 @@ detail_headers = {
"include/pybind11/detail/internals.h",
"include/pybind11/detail/smart_holder_init_inline_include.h",
"include/pybind11/detail/smart_holder_poc.h",
"include/pybind11/detail/smart_holder_type_casters_inline_include.h",
"include/pybind11/detail/smart_holder_type_casters.h",
"include/pybind11/detail/typeid.h",
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment