Introducing PYBIND11_USE_SMART_HOLDER_AS_DEFAULT macro (tested only undefined; there are many errors with the macro defined).

This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-01-28 21:16:09 -08:00
parent 4fd458b64f
commit 95425f13d6
3 changed files with 46 additions and 2 deletions

View File

@ -1560,6 +1560,7 @@ struct smart_holder_type_caster<std::unique_ptr<T const>> : smart_holder_type_ca
operator std::unique_ptr<T const>() { return this->loaded_as_unique_ptr(); } operator std::unique_ptr<T const>() { return this->loaded_as_unique_ptr(); }
}; };
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
#define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T) \ #define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T) \
namespace pybind11 { \ namespace pybind11 { \
namespace detail { \ namespace detail { \
@ -1579,10 +1580,36 @@ struct smart_holder_type_caster<std::unique_ptr<T const>> : smart_holder_type_ca
: public smart_holder_type_caster<std::unique_ptr<T const>> {}; \ : public smart_holder_type_caster<std::unique_ptr<T const>> {}; \
} \ } \
} }
#endif
//DETAIL/SMART_HOLDER_TYPE_CASTERS_H/////////////////////////////////////////////////////////////// //DETAIL/SMART_HOLDER_TYPE_CASTERS_H///////////////////////////////////////////////////////////////
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
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> { };
#else
template <typename type, typename SFINAE = void> class type_caster : public smart_holder_type_caster<type> {};
template <typename T>
class type_caster<std::shared_ptr<T>> : public smart_holder_type_caster<std::shared_ptr<T>> {};
template <typename T>
class type_caster<std::shared_ptr<T const>>
: public smart_holder_type_caster<std::shared_ptr<T const>> {};
template <typename T>
class type_caster<std::unique_ptr<T>> : public smart_holder_type_caster<std::unique_ptr<T>> {};
template <typename T>
class type_caster<std::unique_ptr<T const>>
: public smart_holder_type_caster<std::unique_ptr<T const>> {};
#define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T)
#endif
template <typename type> using make_caster = type_caster<intrinsic_t<type>>; template <typename type> using make_caster = type_caster<intrinsic_t<type>>;
// Shortcut for calling a caster's `cast_op_type` cast operator for casting a type_caster to a T // Shortcut for calling a caster's `cast_op_type` cast operator for casting a type_caster to a T
@ -2227,9 +2254,11 @@ protected:
holder_type holder; holder_type holder;
}; };
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
/// Specialize for the common std::shared_ptr, so users don't need to /// Specialize for the common std::shared_ptr, so users don't need to
template <typename T> template <typename T>
class type_caster<std::shared_ptr<T>> : public copyable_holder_caster<T, std::shared_ptr<T>> { }; class type_caster<std::shared_ptr<T>> : public copyable_holder_caster<T, std::shared_ptr<T>> { };
#endif
template <typename type, typename holder_type> template <typename type, typename holder_type>
struct move_only_holder_caster { struct move_only_holder_caster {
@ -2243,9 +2272,11 @@ struct move_only_holder_caster {
static constexpr auto name = type_caster_base<type>::name; static constexpr auto name = type_caster_base<type>::name;
}; };
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
template <typename type, typename deleter> template <typename type, typename deleter>
class type_caster<std::unique_ptr<type, deleter>> class type_caster<std::unique_ptr<type, deleter>>
: public move_only_holder_caster<type, std::unique_ptr<type, deleter>> { }; : public move_only_holder_caster<type, std::unique_ptr<type, deleter>> { };
#endif
template <typename type, typename holder_type> template <typename type, typename holder_type>
using type_caster_holder = conditional_t<is_copy_constructible<holder_type>::value, using type_caster_holder = conditional_t<is_copy_constructible<holder_type>::value,

View File

@ -1257,7 +1257,13 @@ public:
using type = type_; using type = type_;
using type_alias = detail::exactly_one_t<is_subtype, void, options...>; using type_alias = detail::exactly_one_t<is_subtype, void, options...>;
constexpr static bool has_alias = !std::is_void<type_alias>::value; constexpr static bool has_alias = !std::is_void<type_alias>::value;
using holder_type = detail::exactly_one_t<is_holder, std::unique_ptr<type>, options...>; using holder_type = detail::exactly_one_t<is_holder,
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
std::unique_ptr<type>
#else
smart_holder
#endif
, options...>;
static_assert(detail::all_of<is_valid_class_option<options>...>::value, static_assert(detail::all_of<is_valid_class_option<options>...>::value,
"Unknown/invalid class_ template parameters provided"); "Unknown/invalid class_ template parameters provided");

View File

@ -505,7 +505,14 @@ CHECK_BASE(1); CHECK_BASE(2); CHECK_BASE(3); CHECK_BASE(4); CHECK_BASE(5); CHECK
CHECK_ALIAS(1); CHECK_ALIAS(2); CHECK_NOALIAS(3); CHECK_ALIAS(4); CHECK_NOALIAS(5); CHECK_ALIAS(6); CHECK_ALIAS(7); CHECK_NOALIAS(8); CHECK_ALIAS(1); CHECK_ALIAS(2); CHECK_NOALIAS(3); CHECK_ALIAS(4); CHECK_NOALIAS(5); CHECK_ALIAS(6); CHECK_ALIAS(7); CHECK_NOALIAS(8);
#define CHECK_HOLDER(N, TYPE) static_assert(std::is_same<typename DoesntBreak##N::holder_type, std::TYPE##_ptr<BreaksBase<N>>>::value, \ #define CHECK_HOLDER(N, TYPE) static_assert(std::is_same<typename DoesntBreak##N::holder_type, std::TYPE##_ptr<BreaksBase<N>>>::value, \
"DoesntBreak" #N " has wrong holder_type!") "DoesntBreak" #N " has wrong holder_type!")
CHECK_HOLDER(1, unique); CHECK_HOLDER(2, unique); CHECK_HOLDER(3, unique); CHECK_HOLDER(4, unique); CHECK_HOLDER(5, unique); #define CHECK_SMART_HOLDER(N) static_assert(std::is_same<typename DoesntBreak##N::holder_type, smart_holder, \
"DoesntBreak" #N " has wrong holder_type!")
CHECK_HOLDER(1, unique); CHECK_HOLDER(2, unique); CHECK_HOLDER(3, unique);
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
CHECK_HOLDER(4, unique); CHECK_HOLDER(5, unique);
#else
CHECK_SMART_HOLDER(4); CHECK_SMART_HOLDER(5);
#endif
CHECK_HOLDER(6, shared); CHECK_HOLDER(7, shared); CHECK_HOLDER(8, shared); CHECK_HOLDER(6, shared); CHECK_HOLDER(7, shared); CHECK_HOLDER(8, shared);
// There's no nice way to test that these fail because they fail to compile; leave them here, // There's no nice way to test that these fail because they fail to compile; leave them here,