Moving factored-out make_constructor to test_classh_wip.cpp, restoring previous…

Moving factored-out make_constructor to test_classh_wip.cpp, restoring previous version of cast.h. This is currently the most practical approach. See PR #2798 for background.
parent bad7199a
...@@ -824,30 +824,6 @@ template <typename Container> struct is_copy_assignable<Container, enable_if_t<a ...@@ -824,30 +824,6 @@ template <typename Container> struct is_copy_assignable<Container, enable_if_t<a
template <typename T1, typename T2> struct is_copy_assignable<std::pair<T1, T2>> template <typename T1, typename T2> struct is_copy_assignable<std::pair<T1, T2>>
: all_of<is_copy_assignable<T1>, is_copy_assignable<T2>> {}; : all_of<is_copy_assignable<T1>, is_copy_assignable<T2>> {};
// Helper for type_caster_base.
struct make_constructor {
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; }
};
PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(detail)
// polymorphic_type_hook<itype>::get(src, tinfo) determines whether the object pointed // polymorphic_type_hook<itype>::get(src, tinfo) determines whether the object pointed
...@@ -890,8 +866,7 @@ struct polymorphic_type_hook : public polymorphic_type_hook_base<itype> {}; ...@@ -890,8 +866,7 @@ struct polymorphic_type_hook : public polymorphic_type_hook_base<itype> {};
PYBIND11_NAMESPACE_BEGIN(detail) PYBIND11_NAMESPACE_BEGIN(detail)
/// Generic type caster for objects stored on the heap /// Generic type caster for objects stored on the heap
template <typename type> class type_caster_base : public type_caster_generic, template <typename type> class type_caster_base : public type_caster_generic {
protected make_constructor {
using itype = intrinsic_t<type>; using itype = intrinsic_t<type>;
public: public:
...@@ -952,6 +927,28 @@ public: ...@@ -952,6 +927,28 @@ public:
operator itype*() { return (type *) value; } operator itype*() { return (type *) value; }
operator itype&() { if (!value) throw reference_cast_error(); return *((itype *) value); } operator itype&() { if (!value) throw reference_cast_error(); return *((itype *) value); }
protected:
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; }
}; };
template <typename type, typename SFINAE = void> class type_caster : public type_caster_base<type> { }; template <typename type, typename SFINAE = void> class type_caster : public type_caster_base<type> { };
......
...@@ -102,6 +102,34 @@ protected: ...@@ -102,6 +102,34 @@ protected:
holder_type *loaded_smhldr_ptr = nullptr; holder_type *loaded_smhldr_ptr = nullptr;
}; };
// type_caster_base BEGIN
// clang-format off
// Helper factored out of type_caster_base.
struct make_constructor {
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
template <> template <>
struct type_caster<mpty> : smart_holder_type_caster_load<mpty> { struct type_caster<mpty> : smart_holder_type_caster_load<mpty> {
static constexpr auto name = _<mpty>(); static constexpr auto name = _<mpty>();
......
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