2021-01-11 19:55:03 +00:00
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
|
|
|
#include <pybind11/classh.h>
|
|
|
|
|
|
|
|
#include <memory>
|
2021-01-11 23:58:14 +00:00
|
|
|
#include <string>
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
namespace pybind11_tests {
|
|
|
|
namespace classh_wip {
|
|
|
|
|
2021-01-12 18:53:42 +00:00
|
|
|
struct mpty {
|
|
|
|
std::string mtxt;
|
|
|
|
};
|
|
|
|
|
|
|
|
// clang-format off
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-12 20:01:26 +00:00
|
|
|
mpty rtrn_mpty_valu() { mpty obj{"rtrn_valu"}; return obj; }
|
2021-01-14 18:29:23 +00:00
|
|
|
mpty&& rtrn_mpty_rref() { static mpty obj; obj.mtxt = "rtrn_rref"; return std::move(obj); }
|
2021-01-12 20:01:26 +00:00
|
|
|
mpty const& rtrn_mpty_cref() { static mpty obj; obj.mtxt = "rtrn_cref"; return obj; }
|
|
|
|
mpty& rtrn_mpty_mref() { static mpty obj; obj.mtxt = "rtrn_mref"; return obj; }
|
2021-01-14 18:29:23 +00:00
|
|
|
mpty const* rtrn_mpty_cptr() { return new mpty{"rtrn_cptr"}; }
|
|
|
|
mpty* rtrn_mpty_mptr() { return new mpty{"rtrn_mptr"}; }
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-12 04:38:11 +00:00
|
|
|
std::string pass_mpty_valu(mpty obj) { return "pass_valu:" + obj.mtxt; }
|
|
|
|
std::string pass_mpty_rref(mpty&& obj) { return "pass_rref:" + obj.mtxt; }
|
|
|
|
std::string pass_mpty_cref(mpty const& obj) { return "pass_cref:" + obj.mtxt; }
|
|
|
|
std::string pass_mpty_mref(mpty& obj) { return "pass_mref:" + obj.mtxt; }
|
|
|
|
std::string pass_mpty_cptr(mpty const* obj) { return "pass_cptr:" + obj->mtxt; }
|
|
|
|
std::string pass_mpty_mptr(mpty* obj) { return "pass_mptr:" + obj->mtxt; }
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-12 20:01:26 +00:00
|
|
|
std::shared_ptr<mpty> rtrn_mpty_shmp() { return std::shared_ptr<mpty >(new mpty{"rtrn_shmp"}); }
|
|
|
|
std::shared_ptr<mpty const> rtrn_mpty_shcp() { return std::shared_ptr<mpty const>(new mpty{"rtrn_shcp"}); }
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-12 20:01:26 +00:00
|
|
|
std::string pass_mpty_shmp(std::shared_ptr<mpty> obj) { return "pass_shmp:" + obj->mtxt; }
|
2021-01-12 05:27:23 +00:00
|
|
|
std::string pass_mpty_shcp(std::shared_ptr<mpty const> obj) { return "pass_shcp:" + obj->mtxt; }
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-12 20:01:26 +00:00
|
|
|
std::unique_ptr<mpty> rtrn_mpty_uqmp() { return std::unique_ptr<mpty >(new mpty{"rtrn_uqmp"}); }
|
2021-01-13 20:00:26 +00:00
|
|
|
std::unique_ptr<mpty const> rtrn_mpty_uqcp() { return std::unique_ptr<mpty const>(new mpty{"rtrn_uqcp"}); }
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-12 20:01:26 +00:00
|
|
|
std::string pass_mpty_uqmp(std::unique_ptr<mpty > obj) { return "pass_uqmp:" + obj->mtxt; }
|
2021-01-12 06:06:18 +00:00
|
|
|
std::string pass_mpty_uqcp(std::unique_ptr<mpty const> obj) { return "pass_uqcp:" + obj->mtxt; }
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-12 18:53:42 +00:00
|
|
|
// clang-format on
|
|
|
|
|
2021-01-15 19:44:07 +00:00
|
|
|
// Helpers for testing.
|
2021-01-12 21:26:22 +00:00
|
|
|
std::string get_mtxt(mpty const &obj) { return obj.mtxt; }
|
2021-01-15 19:44:07 +00:00
|
|
|
std::unique_ptr<mpty> unique_ptr_roundtrip(std::unique_ptr<mpty> obj) { return obj; }
|
2021-01-12 21:26:22 +00:00
|
|
|
|
2021-01-12 18:53:42 +00:00
|
|
|
} // namespace classh_wip
|
|
|
|
} // namespace pybind11_tests
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
namespace pybind11 {
|
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
using namespace pybind11_tests::classh_wip;
|
|
|
|
|
2021-01-12 05:15:07 +00:00
|
|
|
template <typename T>
|
|
|
|
struct smart_holder_type_caster_load {
|
2021-01-15 06:23:07 +00:00
|
|
|
using holder_type = pybindit::memory::smart_holder;
|
|
|
|
|
2021-01-12 05:15:07 +00:00
|
|
|
bool load(handle src, bool /*convert*/) {
|
2021-01-12 18:53:42 +00:00
|
|
|
if (!isinstance<T>(src))
|
|
|
|
return false;
|
|
|
|
auto inst = reinterpret_cast<instance *>(src.ptr());
|
2021-01-15 06:23:07 +00:00
|
|
|
loaded_v_h = inst->get_value_and_holder(get_type_info(typeid(T)));
|
|
|
|
if (!loaded_v_h.holder_constructed()) {
|
|
|
|
// IMPROVEABLE: Error message.
|
|
|
|
throw std::runtime_error("Missing value for wrapped C++ type:"
|
|
|
|
" Python instance is uninitialized or was disowned.");
|
|
|
|
}
|
|
|
|
loaded_smhldr_ptr = &loaded_v_h.holder<holder_type>();
|
2021-01-12 05:15:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-01-15 06:23:07 +00:00
|
|
|
std::unique_ptr<T> loaded_as_unique_ptr() {
|
|
|
|
void *value_void_ptr = loaded_v_h.value_ptr();
|
|
|
|
auto unq_ptr = loaded_smhldr_ptr->as_unique_ptr<mpty>();
|
|
|
|
loaded_v_h.holder<holder_type>().~holder_type();
|
|
|
|
loaded_v_h.set_holder_constructed(false);
|
|
|
|
loaded_v_h.value_ptr() = nullptr;
|
|
|
|
deregister_instance(loaded_v_h.inst, value_void_ptr, loaded_v_h.type);
|
|
|
|
return unq_ptr;
|
|
|
|
}
|
|
|
|
|
2021-01-12 18:53:42 +00:00
|
|
|
protected:
|
2021-01-15 06:23:07 +00:00
|
|
|
value_and_holder loaded_v_h;
|
|
|
|
holder_type *loaded_smhldr_ptr = nullptr;
|
2021-01-12 05:15:07 +00:00
|
|
|
};
|
|
|
|
|
2021-01-11 19:55:03 +00:00
|
|
|
template <>
|
2021-01-12 05:15:07 +00:00
|
|
|
struct type_caster<mpty> : smart_holder_type_caster_load<mpty> {
|
2021-01-11 19:55:03 +00:00
|
|
|
static constexpr auto name = _<mpty>();
|
|
|
|
|
|
|
|
// static handle cast(mpty, ...)
|
|
|
|
// is redundant (leads to ambiguous overloads).
|
|
|
|
|
2021-01-12 22:24:31 +00:00
|
|
|
static handle cast(mpty &&src, return_value_policy /*policy*/, handle parent) {
|
|
|
|
// type_caster_base BEGIN
|
|
|
|
// clang-format off
|
|
|
|
return cast(&src, return_value_policy::move, parent);
|
|
|
|
// clang-format on
|
|
|
|
// type_caster_base END
|
2021-01-11 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 21:45:48 +00:00
|
|
|
static handle cast(mpty const &src, return_value_policy policy, handle parent) {
|
|
|
|
// type_caster_base BEGIN
|
|
|
|
// clang-format off
|
|
|
|
if (policy == return_value_policy::automatic || policy == return_value_policy::automatic_reference)
|
|
|
|
policy = return_value_policy::copy;
|
|
|
|
return cast(&src, policy, parent);
|
|
|
|
// clang-format on
|
|
|
|
// type_caster_base END
|
2021-01-11 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 21:45:48 +00:00
|
|
|
static handle cast(mpty &src, return_value_policy policy, handle parent) {
|
2021-01-13 18:48:08 +00:00
|
|
|
return cast(const_cast<mpty const &>(src), policy, parent); // Mutbl2Const
|
2021-01-11 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 21:26:22 +00:00
|
|
|
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);
|
2021-01-13 04:01:12 +00:00
|
|
|
return cast( // Originally type_caster_generic::cast.
|
2021-01-12 21:26:22 +00:00
|
|
|
st.first, policy, parent, st.second,
|
|
|
|
make_copy_constructor(src), make_move_constructor(src));
|
|
|
|
// clang-format on
|
|
|
|
// type_caster_base END
|
2021-01-11 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 21:45:48 +00:00
|
|
|
static handle cast(mpty *src, return_value_policy policy, handle parent) {
|
2021-01-13 18:48:08 +00:00
|
|
|
return cast(const_cast<mpty const *>(src), policy, parent); // Mutbl2Const
|
2021-01-11 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T_>
|
|
|
|
using cast_op_type = conditional_t<
|
2021-01-12 18:53:42 +00:00
|
|
|
std::is_same<remove_reference_t<T_>, mpty const *>::value,
|
|
|
|
mpty const *,
|
2021-01-11 19:55:03 +00:00
|
|
|
conditional_t<
|
2021-01-12 18:53:42 +00:00
|
|
|
std::is_same<remove_reference_t<T_>, mpty *>::value,
|
|
|
|
mpty *,
|
2021-01-11 19:55:03 +00:00
|
|
|
conditional_t<
|
2021-01-12 18:53:42 +00:00
|
|
|
std::is_same<T_, mpty const &>::value,
|
|
|
|
mpty const &,
|
|
|
|
conditional_t<std::is_same<T_, mpty &>::value,
|
|
|
|
mpty &,
|
|
|
|
conditional_t<std::is_same<T_, mpty &&>::value, mpty &&, mpty>>>>>;
|
|
|
|
|
|
|
|
// clang-format off
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-15 06:23:07 +00:00
|
|
|
operator mpty() { return loaded_smhldr_ptr->lvalue_ref<mpty>(); }
|
|
|
|
operator mpty&&() && { return loaded_smhldr_ptr->rvalue_ref<mpty>(); }
|
|
|
|
operator mpty const&() { return loaded_smhldr_ptr->lvalue_ref<mpty>(); }
|
|
|
|
operator mpty&() { return loaded_smhldr_ptr->lvalue_ref<mpty>(); }
|
|
|
|
operator mpty const*() { return loaded_smhldr_ptr->as_raw_ptr_unowned<mpty>(); }
|
|
|
|
operator mpty*() { return loaded_smhldr_ptr->as_raw_ptr_unowned<mpty>(); }
|
2021-01-12 18:53:42 +00:00
|
|
|
|
|
|
|
// clang-format on
|
2021-01-12 21:26:22 +00:00
|
|
|
|
|
|
|
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<const void *, const type_info *> src_and_type(const itype *src) {
|
|
|
|
auto &cast_type = typeid(itype);
|
|
|
|
const std::type_info *instance_type = nullptr;
|
|
|
|
const void *vsrc = polymorphic_type_hook<itype>::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
|
|
|
|
does not have a private operator new implementation. */
|
|
|
|
template <typename T, typename = enable_if_t<is_copy_constructible<T>::value>>
|
|
|
|
static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) {
|
|
|
|
return [](const void *arg) -> void * {
|
|
|
|
return new T(*reinterpret_cast<const T *>(arg));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T, typename = enable_if_t<std::is_move_constructible<T>::value>>
|
|
|
|
static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast<T *>(x))), Constructor{}) {
|
|
|
|
return [](const void *arg) -> void * {
|
|
|
|
return new T(std::move(*const_cast<T *>(reinterpret_cast<const T *>(arg))));
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
static Constructor make_copy_constructor(...) { return nullptr; }
|
|
|
|
static Constructor make_move_constructor(...) { return nullptr; }
|
|
|
|
|
|
|
|
// clang-format on
|
|
|
|
// type_caster_base END
|
2021-01-13 04:01:12 +00:00
|
|
|
|
|
|
|
// Originally type_caster_generic::cast.
|
2021-01-13 04:05:20 +00:00
|
|
|
PYBIND11_NOINLINE static handle cast(const void *_src,
|
|
|
|
return_value_policy policy,
|
|
|
|
handle parent,
|
2021-01-13 04:01:12 +00:00
|
|
|
const detail::type_info *tinfo,
|
|
|
|
void *(*copy_constructor)(const void *),
|
|
|
|
void *(*move_constructor)(const void *),
|
|
|
|
const void *existing_holder = nullptr) {
|
|
|
|
if (!tinfo) // no type info: error will be set already
|
|
|
|
return handle();
|
|
|
|
|
|
|
|
void *src = const_cast<void *>(_src);
|
|
|
|
if (src == nullptr)
|
|
|
|
return none().release();
|
|
|
|
|
|
|
|
auto it_instances = get_internals().registered_instances.equal_range(src);
|
|
|
|
for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
|
|
|
|
for (auto instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
|
|
|
|
if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype))
|
|
|
|
return handle((PyObject *) it_i->second).inc_ref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-13 04:05:20 +00:00
|
|
|
auto inst = reinterpret_steal<object>(make_new_instance(tinfo->type));
|
|
|
|
auto wrapper = reinterpret_cast<instance *>(inst.ptr());
|
|
|
|
wrapper->owned = false;
|
2021-01-13 04:01:12 +00:00
|
|
|
void *&valueptr = values_and_holders(wrapper).begin()->value_ptr();
|
|
|
|
|
|
|
|
switch (policy) {
|
|
|
|
case return_value_policy::automatic:
|
|
|
|
case return_value_policy::take_ownership:
|
2021-01-13 04:05:20 +00:00
|
|
|
valueptr = src;
|
2021-01-13 04:01:12 +00:00
|
|
|
wrapper->owned = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case return_value_policy::automatic_reference:
|
|
|
|
case return_value_policy::reference:
|
2021-01-13 04:05:20 +00:00
|
|
|
valueptr = src;
|
2021-01-13 04:01:12 +00:00
|
|
|
wrapper->owned = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case return_value_policy::copy:
|
|
|
|
if (copy_constructor)
|
|
|
|
valueptr = copy_constructor(src);
|
|
|
|
else {
|
|
|
|
#if defined(NDEBUG)
|
|
|
|
throw cast_error("return_value_policy = copy, but type is "
|
|
|
|
"non-copyable! (compile in debug mode for details)");
|
|
|
|
#else
|
|
|
|
std::string type_name(tinfo->cpptype->name());
|
|
|
|
detail::clean_type_id(type_name);
|
2021-01-13 04:05:20 +00:00
|
|
|
throw cast_error("return_value_policy = copy, but type " + type_name
|
|
|
|
+ " is non-copyable!");
|
2021-01-13 04:01:12 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
wrapper->owned = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case return_value_policy::move:
|
|
|
|
if (move_constructor)
|
|
|
|
valueptr = move_constructor(src);
|
|
|
|
else if (copy_constructor)
|
|
|
|
valueptr = copy_constructor(src);
|
|
|
|
else {
|
|
|
|
#if defined(NDEBUG)
|
|
|
|
throw cast_error("return_value_policy = move, but type is neither "
|
|
|
|
"movable nor copyable! "
|
|
|
|
"(compile in debug mode for details)");
|
|
|
|
#else
|
|
|
|
std::string type_name(tinfo->cpptype->name());
|
|
|
|
detail::clean_type_id(type_name);
|
2021-01-13 04:05:20 +00:00
|
|
|
throw cast_error("return_value_policy = move, but type " + type_name
|
|
|
|
+ " is neither movable nor copyable!");
|
2021-01-13 04:01:12 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
wrapper->owned = true;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case return_value_policy::reference_internal:
|
2021-01-13 04:05:20 +00:00
|
|
|
valueptr = src;
|
2021-01-13 04:01:12 +00:00
|
|
|
wrapper->owned = false;
|
|
|
|
keep_alive_impl(inst, parent);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw cast_error("unhandled return_value_policy: should not happen!");
|
|
|
|
}
|
|
|
|
|
|
|
|
tinfo->init_instance(wrapper, existing_holder);
|
|
|
|
|
|
|
|
return inst.release();
|
|
|
|
}
|
2021-01-11 19:55:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2021-01-12 05:27:23 +00:00
|
|
|
struct type_caster<std::shared_ptr<mpty>> : smart_holder_type_caster_load<mpty> {
|
2021-01-11 19:55:03 +00:00
|
|
|
static constexpr auto name = _<std::shared_ptr<mpty>>();
|
|
|
|
|
2021-01-13 18:48:08 +00:00
|
|
|
static handle
|
|
|
|
cast(const std::shared_ptr<mpty> &src, return_value_policy policy, handle parent) {
|
|
|
|
if (policy != return_value_policy::automatic
|
|
|
|
&& policy != return_value_policy::reference_internal) {
|
|
|
|
// IMPROVEABLE: Error message.
|
|
|
|
throw cast_error("Invalid return_value_policy for shared_ptr.");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto src_raw_ptr = src.get();
|
|
|
|
auto st = type_caster<mpty>::src_and_type(src_raw_ptr);
|
|
|
|
if (st.first == nullptr)
|
|
|
|
return none().release(); // PyErr was set already.
|
|
|
|
|
|
|
|
void *src_raw_void_ptr = static_cast<void *>(src_raw_ptr);
|
|
|
|
const detail::type_info *tinfo = st.second;
|
|
|
|
auto it_instances = get_internals().registered_instances.equal_range(src_raw_void_ptr);
|
|
|
|
// Loop copied from type_caster_generic::cast.
|
|
|
|
for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
|
|
|
|
for (auto instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
|
|
|
|
if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype))
|
|
|
|
// MISSING: Enforcement of consistency with existing smart_holder.
|
|
|
|
// MISSING: keep_alive.
|
|
|
|
return handle((PyObject *) it_i->second).inc_ref();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object inst = reinterpret_steal<object>(make_new_instance(tinfo->type));
|
|
|
|
instance *inst_raw_ptr = reinterpret_cast<instance *>(inst.ptr());
|
2021-01-14 18:29:23 +00:00
|
|
|
inst_raw_ptr->owned = true;
|
|
|
|
void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr();
|
|
|
|
valueptr = src_raw_void_ptr;
|
2021-01-13 18:48:08 +00:00
|
|
|
|
|
|
|
auto smhldr = pybindit::memory::smart_holder::from_shared_ptr(src);
|
|
|
|
tinfo->init_instance(inst_raw_ptr, static_cast<const void *>(&smhldr));
|
|
|
|
|
|
|
|
if (policy == return_value_policy::reference_internal)
|
|
|
|
keep_alive_impl(inst, parent);
|
|
|
|
|
|
|
|
return inst.release();
|
2021-01-11 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:53:42 +00:00
|
|
|
template <typename>
|
|
|
|
using cast_op_type = std::shared_ptr<mpty>;
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-15 06:23:07 +00:00
|
|
|
operator std::shared_ptr<mpty>() { return loaded_smhldr_ptr->as_shared_ptr<mpty>(); }
|
2021-01-11 19:55:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2021-01-12 05:27:23 +00:00
|
|
|
struct type_caster<std::shared_ptr<mpty const>> : smart_holder_type_caster_load<mpty> {
|
2021-01-11 19:55:03 +00:00
|
|
|
static constexpr auto name = _<std::shared_ptr<mpty const>>();
|
|
|
|
|
2021-01-13 18:48:08 +00:00
|
|
|
static handle
|
|
|
|
cast(const std::shared_ptr<mpty const> &src, return_value_policy policy, handle parent) {
|
|
|
|
return type_caster<std::shared_ptr<mpty>>::cast(
|
|
|
|
std::const_pointer_cast<mpty>(src), // Const2Mutbl
|
|
|
|
policy,
|
|
|
|
parent);
|
2021-01-11 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:53:42 +00:00
|
|
|
template <typename>
|
|
|
|
using cast_op_type = std::shared_ptr<mpty const>;
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-15 06:23:07 +00:00
|
|
|
operator std::shared_ptr<mpty const>() { return loaded_smhldr_ptr->as_shared_ptr<mpty>(); }
|
2021-01-11 19:55:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2021-01-12 06:06:18 +00:00
|
|
|
struct type_caster<std::unique_ptr<mpty>> : smart_holder_type_caster_load<mpty> {
|
2021-01-11 19:55:03 +00:00
|
|
|
static constexpr auto name = _<std::unique_ptr<mpty>>();
|
|
|
|
|
2021-01-13 20:00:26 +00:00
|
|
|
static handle cast(std::unique_ptr<mpty> &&src, return_value_policy policy, handle parent) {
|
|
|
|
if (policy != return_value_policy::automatic
|
|
|
|
&& policy != return_value_policy::reference_internal) {
|
|
|
|
// IMPROVEABLE: Error message.
|
|
|
|
throw cast_error("Invalid return_value_policy for unique_ptr.");
|
|
|
|
}
|
|
|
|
|
|
|
|
auto src_raw_ptr = src.get();
|
|
|
|
auto st = type_caster<mpty>::src_and_type(src_raw_ptr);
|
|
|
|
if (st.first == nullptr)
|
|
|
|
return none().release(); // PyErr was set already.
|
|
|
|
|
|
|
|
void *src_raw_void_ptr = static_cast<void *>(src_raw_ptr);
|
|
|
|
const detail::type_info *tinfo = st.second;
|
|
|
|
auto it_instances = get_internals().registered_instances.equal_range(src_raw_void_ptr);
|
|
|
|
// Loop copied from type_caster_generic::cast.
|
|
|
|
for (auto it_i = it_instances.first; it_i != it_instances.second; ++it_i) {
|
|
|
|
for (auto instance_type : detail::all_type_info(Py_TYPE(it_i->second))) {
|
|
|
|
if (instance_type && same_type(*instance_type->cpptype, *tinfo->cpptype))
|
|
|
|
throw cast_error(
|
|
|
|
"Invalid unique_ptr: another instance owns this pointer already.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
object inst = reinterpret_steal<object>(make_new_instance(tinfo->type));
|
|
|
|
instance *inst_raw_ptr = reinterpret_cast<instance *>(inst.ptr());
|
2021-01-14 18:29:23 +00:00
|
|
|
inst_raw_ptr->owned = true;
|
|
|
|
void *&valueptr = values_and_holders(inst_raw_ptr).begin()->value_ptr();
|
|
|
|
valueptr = src_raw_void_ptr;
|
2021-01-13 20:00:26 +00:00
|
|
|
|
|
|
|
auto smhldr = pybindit::memory::smart_holder::from_unique_ptr(std::move(src));
|
|
|
|
tinfo->init_instance(inst_raw_ptr, static_cast<const void *>(&smhldr));
|
|
|
|
|
|
|
|
if (policy == return_value_policy::reference_internal)
|
|
|
|
keep_alive_impl(inst, parent);
|
|
|
|
|
|
|
|
return inst.release();
|
2021-01-11 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:53:42 +00:00
|
|
|
template <typename>
|
|
|
|
using cast_op_type = std::unique_ptr<mpty>;
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-15 06:23:07 +00:00
|
|
|
operator std::unique_ptr<mpty>() { return loaded_as_unique_ptr(); }
|
2021-01-11 19:55:03 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
2021-01-12 06:06:18 +00:00
|
|
|
struct type_caster<std::unique_ptr<mpty const>> : smart_holder_type_caster_load<mpty> {
|
2021-01-11 19:55:03 +00:00
|
|
|
static constexpr auto name = _<std::unique_ptr<mpty const>>();
|
|
|
|
|
2021-01-13 20:00:26 +00:00
|
|
|
static handle
|
|
|
|
cast(std::unique_ptr<mpty const> &&src, return_value_policy policy, handle parent) {
|
|
|
|
return type_caster<std::unique_ptr<mpty>>::cast(
|
|
|
|
std::unique_ptr<mpty>(const_cast<mpty *>(src.release())), // Const2Mutbl
|
|
|
|
policy,
|
|
|
|
parent);
|
2021-01-11 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:53:42 +00:00
|
|
|
template <typename>
|
|
|
|
using cast_op_type = std::unique_ptr<mpty const>;
|
2021-01-11 19:55:03 +00:00
|
|
|
|
2021-01-15 06:23:07 +00:00
|
|
|
operator std::unique_ptr<mpty const>() { return loaded_as_unique_ptr(); }
|
2021-01-11 19:55:03 +00:00
|
|
|
};
|
|
|
|
|
2021-01-12 18:53:42 +00:00
|
|
|
} // namespace detail
|
|
|
|
} // namespace pybind11
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
namespace pybind11_tests {
|
|
|
|
namespace classh_wip {
|
|
|
|
|
|
|
|
TEST_SUBMODULE(classh_wip, m) {
|
|
|
|
namespace py = pybind11;
|
|
|
|
|
2021-01-12 18:53:42 +00:00
|
|
|
py::classh<mpty>(m, "mpty").def(py::init<>()).def(py::init([](const std::string &mtxt) {
|
|
|
|
mpty obj;
|
|
|
|
obj.mtxt = mtxt;
|
|
|
|
return obj;
|
|
|
|
}));
|
2021-01-11 19:55:03 +00:00
|
|
|
|
|
|
|
m.def("rtrn_mpty_valu", rtrn_mpty_valu);
|
|
|
|
m.def("rtrn_mpty_rref", rtrn_mpty_rref);
|
|
|
|
m.def("rtrn_mpty_cref", rtrn_mpty_cref);
|
|
|
|
m.def("rtrn_mpty_mref", rtrn_mpty_mref);
|
|
|
|
m.def("rtrn_mpty_cptr", rtrn_mpty_cptr);
|
|
|
|
m.def("rtrn_mpty_mptr", rtrn_mpty_mptr);
|
|
|
|
|
|
|
|
m.def("pass_mpty_valu", pass_mpty_valu);
|
|
|
|
m.def("pass_mpty_rref", pass_mpty_rref);
|
|
|
|
m.def("pass_mpty_cref", pass_mpty_cref);
|
|
|
|
m.def("pass_mpty_mref", pass_mpty_mref);
|
|
|
|
m.def("pass_mpty_cptr", pass_mpty_cptr);
|
|
|
|
m.def("pass_mpty_mptr", pass_mpty_mptr);
|
|
|
|
|
|
|
|
m.def("rtrn_mpty_shmp", rtrn_mpty_shmp);
|
|
|
|
m.def("rtrn_mpty_shcp", rtrn_mpty_shcp);
|
|
|
|
|
|
|
|
m.def("pass_mpty_shmp", pass_mpty_shmp);
|
|
|
|
m.def("pass_mpty_shcp", pass_mpty_shcp);
|
|
|
|
|
|
|
|
m.def("rtrn_mpty_uqmp", rtrn_mpty_uqmp);
|
|
|
|
m.def("rtrn_mpty_uqcp", rtrn_mpty_uqcp);
|
|
|
|
|
|
|
|
m.def("pass_mpty_uqmp", pass_mpty_uqmp);
|
|
|
|
m.def("pass_mpty_uqcp", pass_mpty_uqcp);
|
2021-01-12 21:26:22 +00:00
|
|
|
|
2021-01-15 19:44:07 +00:00
|
|
|
// Helpers for testing.
|
|
|
|
// These require selected functions above to work first, as indicated:
|
|
|
|
m.def("get_mtxt", get_mtxt); // pass_mpty_cref
|
|
|
|
m.def("unique_ptr_roundtrip", unique_ptr_roundtrip); // pass_mpty_uqmp, rtrn_mpty_uqmp
|
2021-01-11 19:55:03 +00:00
|
|
|
}
|
|
|
|
|
2021-01-12 18:53:42 +00:00
|
|
|
} // namespace classh_wip
|
|
|
|
} // namespace pybind11_tests
|