Introducing 1. type_caster_for_class_, used in PYBIND11_MAKE_OPAQUE, and 2. default_holder_type, used in stl_bind.h.

This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-02-04 21:13:54 -08:00
parent 99e9595a9c
commit 75f7eca26e
4 changed files with 28 additions and 21 deletions

View File

@ -1591,7 +1591,7 @@ struct smart_holder_type_caster<std::unique_ptr<T const, D>>
#define PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(T, H) #define PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(T, H)
template <typename type, typename SFINAE = void> class type_caster : public type_caster_base<type> { }; template <typename T> class type_caster_for_class_ : public type_caster_base<T> {};
#else #else
@ -1605,27 +1605,29 @@ template <typename type, typename SFINAE = void> class type_caster : public type
} \ } \
} }
template <typename type, typename SFINAE = void> class type_caster : public smart_holder_type_caster<type> {}; template <typename T> class type_caster_for_class_ : public smart_holder_type_caster<T> {};
template <typename T> template <typename T>
class type_caster<std::shared_ptr<T>> : public smart_holder_type_caster<std::shared_ptr<T>> {}; class type_caster_for_class_<std::shared_ptr<T>> : public smart_holder_type_caster<std::shared_ptr<T>> {};
template <typename T> template <typename T>
class type_caster<std::shared_ptr<T const>> class type_caster_for_class_<std::shared_ptr<T const>>
: public smart_holder_type_caster<std::shared_ptr<T const>> {}; : public smart_holder_type_caster<std::shared_ptr<T const>> {};
template <typename T, typename D> template <typename T, typename D>
class type_caster<std::unique_ptr<T, D>> class type_caster_for_class_<std::unique_ptr<T, D>>
: public smart_holder_type_caster<std::unique_ptr<T, D>> {}; : public smart_holder_type_caster<std::unique_ptr<T, D>> {};
template <typename T, typename D> template <typename T, typename D>
class type_caster<std::unique_ptr<T const, D>> class type_caster_for_class_<std::unique_ptr<T const, D>>
: public smart_holder_type_caster<std::unique_ptr<T const, D>> {}; : public smart_holder_type_caster<std::unique_ptr<T const, D>> {};
#define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T) #define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T)
#endif #endif
template <typename type, typename SFINAE = void> class type_caster : public type_caster_for_class_<type> { };
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
@ -2963,7 +2965,7 @@ handle type::handle_of() {
#define PYBIND11_MAKE_OPAQUE(...) \ #define PYBIND11_MAKE_OPAQUE(...) \
namespace pybind11 { namespace detail { \ namespace pybind11 { namespace detail { \
template<> class type_caster<__VA_ARGS__> : public type_caster_base<__VA_ARGS__> { }; \ template<> class type_caster<__VA_ARGS__> : public type_caster_for_class_<__VA_ARGS__> { }; \
}} }}
/// Lets you pass a type containing a `,` through a macro parameter without needing a separate /// Lets you pass a type containing a `,` through a macro parameter without needing a separate

View File

@ -1240,6 +1240,13 @@ auto method_adaptor(Return (Class::*pmf)(Args...) const) -> Return (Derived::*)(
return pmf; return pmf;
} }
template <typename T>
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
using default_holder_type = std::unique_ptr<T>;
#else
using default_holder_type = smart_holder;
#endif
template <typename type_, typename... options> template <typename type_, typename... options>
class class_ : public detail::generic_type { class class_ : public detail::generic_type {
template <typename T> using is_subtype = detail::is_strict_base_of<type_, T>; template <typename T> using is_subtype = detail::is_strict_base_of<type_, T>;
@ -1257,13 +1264,7 @@ 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, using holder_type = detail::exactly_one_t<is_holder, default_holder_type<type>, options...>;
#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");
@ -1297,14 +1298,15 @@ public:
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT #ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
record.default_holder = detail::is_instantiation<std::unique_ptr, holder_type>::value; record.default_holder = detail::is_instantiation<std::unique_ptr, holder_type>::value;
#else #else
record.default_holder = std::is_same<holder_type, smart_holder>::value; static constexpr bool holder_is_smart_holder = std::is_same<holder_type, smart_holder>::value;
record.default_holder = holder_is_smart_holder;
#if 0 #if 0
static_assert(!(detail::is_instantiation<std::unique_ptr, holder_type>::value && detail::is_smart_holder_type_caster<type>::value)); static constexpr bool type_caster_type_is_smart_holder_type_caster = detail::is_smart_holder_type_caster<type>::value;
static_assert(!(detail::is_instantiation<std::shared_ptr, holder_type>::value && detail::is_smart_holder_type_caster<type>::value)); static_assert(!(detail::is_instantiation<std::unique_ptr, holder_type>::value && type_caster_type_is_smart_holder_type_caster));
static_assert(detail::is_smart_holder_type_caster<type>::value == std::is_same<holder_type, smart_holder>::value); static_assert(!(detail::is_instantiation<std::shared_ptr, holder_type>::value && type_caster_type_is_smart_holder_type_caster));
static_assert(holder_is_smart_holder == type_caster_type_is_smart_holder_type_caster);
#endif #endif
#endif #endif
set_operator_new<type>(&record); set_operator_new<type>(&record);
/* Register base classes specified via template arguments to class_, if any */ /* Register base classes specified via template arguments to class_, if any */

View File

@ -438,7 +438,7 @@ PYBIND11_NAMESPACE_END(detail)
// //
// std::vector // std::vector
// //
template <typename Vector, typename holder_type = std::unique_ptr<Vector>, typename... Args> template <typename Vector, typename holder_type = default_holder_type<Vector>, typename... Args>
class_<Vector, holder_type> bind_vector(handle scope, std::string const &name, Args&&... args) { class_<Vector, holder_type> bind_vector(handle scope, std::string const &name, Args&&... args) {
using Class_ = class_<Vector, holder_type>; using Class_ = class_<Vector, holder_type>;
@ -599,7 +599,7 @@ template <typename Map, typename Class_> auto map_if_insertion_operator(Class_ &
PYBIND11_NAMESPACE_END(detail) PYBIND11_NAMESPACE_END(detail)
template <typename Map, typename holder_type = std::unique_ptr<Map>, typename... Args> template <typename Map, typename holder_type = default_holder_type<Map>, typename... Args>
class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&... args) { class_<Map, holder_type> bind_map(handle scope, const std::string &name, Args&&... args) {
using KeyType = typename Map::key_type; using KeyType = typename Map::key_type;
using MappedType = typename Map::mapped_type; using MappedType = typename Map::mapped_type;

View File

@ -41,6 +41,9 @@ private:
} // namespace pybind11_tests } // namespace pybind11_tests
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_unique_ptr_member::pointee) PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_unique_ptr_member::pointee)
PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(
pybind11_tests::class_sh_unique_ptr_member::ptr_owner,
std::unique_ptr<pybind11_tests::class_sh_unique_ptr_member::ptr_owner>)
namespace pybind11_tests { namespace pybind11_tests {
namespace class_sh_unique_ptr_member { namespace class_sh_unique_ptr_member {