From 95425f13d6c14fcb6ee479b62b602dc8a605ec49 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Thu, 28 Jan 2021 21:16:09 -0800 Subject: [PATCH] Introducing PYBIND11_USE_SMART_HOLDER_AS_DEFAULT macro (tested only undefined; there are many errors with the macro defined). --- include/pybind11/cast.h | 31 +++++++++++++++++++++++++++++++ include/pybind11/pybind11.h | 8 +++++++- tests/test_class.cpp | 9 ++++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 4ad558527..5c8d4b875 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -1560,6 +1560,7 @@ struct smart_holder_type_caster> : smart_holder_type_ca operator std::unique_ptr() { return this->loaded_as_unique_ptr(); } }; +#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT #define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T) \ namespace pybind11 { \ namespace detail { \ @@ -1579,10 +1580,36 @@ struct smart_holder_type_caster> : smart_holder_type_ca : public smart_holder_type_caster> {}; \ } \ } +#endif //DETAIL/SMART_HOLDER_TYPE_CASTERS_H/////////////////////////////////////////////////////////////// +#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT + template class type_caster : public type_caster_base { }; + +#else + +template class type_caster : public smart_holder_type_caster {}; + +template +class type_caster> : public smart_holder_type_caster> {}; + +template +class type_caster> + : public smart_holder_type_caster> {}; + +template +class type_caster> : public smart_holder_type_caster> {}; + +template +class type_caster> + : public smart_holder_type_caster> {}; + +#define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T) + +#endif + template using make_caster = type_caster>; // 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; }; +#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT /// Specialize for the common std::shared_ptr, so users don't need to template class type_caster> : public copyable_holder_caster> { }; +#endif template struct move_only_holder_caster { @@ -2243,9 +2272,11 @@ struct move_only_holder_caster { static constexpr auto name = type_caster_base::name; }; +#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT template class type_caster> : public move_only_holder_caster> { }; +#endif template using type_caster_holder = conditional_t::value, diff --git a/include/pybind11/pybind11.h b/include/pybind11/pybind11.h index 5ad52e89b..fb1967cea 100644 --- a/include/pybind11/pybind11.h +++ b/include/pybind11/pybind11.h @@ -1257,7 +1257,13 @@ public: using type = type_; using type_alias = detail::exactly_one_t; constexpr static bool has_alias = !std::is_void::value; - using holder_type = detail::exactly_one_t, options...>; + using holder_type = detail::exactly_one_t +#else + smart_holder +#endif + , options...>; static_assert(detail::all_of...>::value, "Unknown/invalid class_ template parameters provided"); diff --git a/tests/test_class.cpp b/tests/test_class.cpp index 6ce928ca9..b786332e7 100644 --- a/tests/test_class.cpp +++ b/tests/test_class.cpp @@ -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); #define CHECK_HOLDER(N, TYPE) static_assert(std::is_same>>::value, \ "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