Skip to content

Commit df13603

Browse files
committed
Using factored-out make_constructor (PR #2798), removing duplicate code.
1 parent bc6aba8 commit df13603

File tree

2 files changed

+28
-52
lines changed

2 files changed

+28
-52
lines changed

include/pybind11/cast.h

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -816,6 +816,30 @@ template <typename Container> struct is_copy_assignable<Container, enable_if_t<a
816816
template <typename T1, typename T2> struct is_copy_assignable<std::pair<T1, T2>>
817817
: all_of<is_copy_assignable<T1>, is_copy_assignable<T2>> {};
818818

819+
// Helper for type_caster_base.
820+
struct make_constructor {
821+
using Constructor = void *(*)(const void *);
822+
823+
/* Only enabled when the types are {copy,move}-constructible *and* when the type
824+
does not have a private operator new implementation. */
825+
template <typename T, typename = enable_if_t<is_copy_constructible<T>::value>>
826+
static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) {
827+
return [](const void *arg) -> void * {
828+
return new T(*reinterpret_cast<const T *>(arg));
829+
};
830+
}
831+
832+
template <typename T, typename = enable_if_t<std::is_move_constructible<T>::value>>
833+
static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast<T *>(x))), Constructor{}) {
834+
return [](const void *arg) -> void * {
835+
return new T(std::move(*const_cast<T *>(reinterpret_cast<const T *>(arg))));
836+
};
837+
}
838+
839+
static Constructor make_copy_constructor(...) { return nullptr; }
840+
static Constructor make_move_constructor(...) { return nullptr; }
841+
};
842+
819843
PYBIND11_NAMESPACE_END(detail)
820844

821845
// polymorphic_type_hook<itype>::get(src, tinfo) determines whether the object pointed
@@ -858,7 +882,8 @@ struct polymorphic_type_hook : public polymorphic_type_hook_base<itype> {};
858882
PYBIND11_NAMESPACE_BEGIN(detail)
859883

860884
/// Generic type caster for objects stored on the heap
861-
template <typename type> class type_caster_base : public type_caster_generic {
885+
template <typename type> class type_caster_base : public type_caster_generic,
886+
protected make_constructor {
862887
using itype = intrinsic_t<type>;
863888

864889
public:
@@ -919,28 +944,6 @@ template <typename type> class type_caster_base : public type_caster_generic {
919944

920945
operator itype*() { return (type *) value; }
921946
operator itype&() { if (!value) throw reference_cast_error(); return *((itype *) value); }
922-
923-
protected:
924-
using Constructor = void *(*)(const void *);
925-
926-
/* Only enabled when the types are {copy,move}-constructible *and* when the type
927-
does not have a private operator new implementation. */
928-
template <typename T, typename = enable_if_t<is_copy_constructible<T>::value>>
929-
static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) {
930-
return [](const void *arg) -> void * {
931-
return new T(*reinterpret_cast<const T *>(arg));
932-
};
933-
}
934-
935-
template <typename T, typename = enable_if_t<std::is_move_constructible<T>::value>>
936-
static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast<T *>(x))), Constructor{}) {
937-
return [](const void *arg) -> void * {
938-
return new T(std::move(*const_cast<T *>(reinterpret_cast<const T *>(arg))));
939-
};
940-
}
941-
942-
static Constructor make_copy_constructor(...) { return nullptr; }
943-
static Constructor make_move_constructor(...) { return nullptr; }
944947
};
945948

946949
template <typename type, typename SFINAE = void> class type_caster : public type_caster_base<type> { };

tests/test_classh_wip.cpp

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,8 @@ struct type_caster<mpty> : smart_holder_type_caster_load<mpty> {
123123
policy,
124124
parent,
125125
st.second,
126-
make_copy_constructor(src),
127-
make_move_constructor(src));
126+
make_constructor::make_copy_constructor(src),
127+
make_constructor::make_move_constructor(src));
128128
}
129129

130130
static handle cast(mpty *src, return_value_policy policy, handle parent) {
@@ -156,33 +156,6 @@ struct type_caster<mpty> : smart_holder_type_caster_load<mpty> {
156156

157157
// clang-format on
158158

159-
// type_caster_base BEGIN
160-
// clang-format off
161-
162-
using Constructor = void *(*)(const void *);
163-
164-
/* Only enabled when the types are {copy,move}-constructible *and* when the type
165-
does not have a private operator new implementation. */
166-
template <typename T, typename = enable_if_t<is_copy_constructible<T>::value>>
167-
static auto make_copy_constructor(const T *x) -> decltype(new T(*x), Constructor{}) {
168-
return [](const void *arg) -> void * {
169-
return new T(*reinterpret_cast<const T *>(arg));
170-
};
171-
}
172-
173-
template <typename T, typename = enable_if_t<std::is_move_constructible<T>::value>>
174-
static auto make_move_constructor(const T *x) -> decltype(new T(std::move(*const_cast<T *>(x))), Constructor{}) {
175-
return [](const void *arg) -> void * {
176-
return new T(std::move(*const_cast<T *>(reinterpret_cast<const T *>(arg))));
177-
};
178-
}
179-
180-
static Constructor make_copy_constructor(...) { return nullptr; }
181-
static Constructor make_move_constructor(...) { return nullptr; }
182-
183-
// clang-format on
184-
// type_caster_base END
185-
186159
// Originally type_caster_generic::cast.
187160
PYBIND11_NOINLINE static handle cast_const_raw_ptr(const void *_src,
188161
return_value_policy policy,

0 commit comments

Comments
 (0)