Skip to content

Commit

Permalink
Introducing PYBIND11_USE_SMART_HOLDER_AS_DEFAULT macro (tested only u…
Browse files Browse the repository at this point in the history
…ndefined; there are many errors with the macro defined).
  • Loading branch information
Ralf W. Grosse-Kunstleve committed Jan 29, 2021
1 parent 4fd458b commit 95425f1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
31 changes: 31 additions & 0 deletions include/pybind11/cast.h
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
};

#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
#define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T) \
namespace pybind11 { \
namespace detail { \
Expand All @@ -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>> {}; \
} \
}
#endif

//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> { };

#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>>;

// Shortcut for calling a caster's `cast_op_type` cast operator for casting a type_caster to a T
Expand Down Expand Up @@ -2227,9 +2254,11 @@ struct copyable_holder_caster : public type_caster_base<type> {
holder_type holder;
};

#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
/// Specialize for the common std::shared_ptr, so users don't need to
template <typename T>
class type_caster<std::shared_ptr<T>> : public copyable_holder_caster<T, std::shared_ptr<T>> { };
#endif

template <typename type, typename holder_type>
struct move_only_holder_caster {
Expand All @@ -2243,9 +2272,11 @@ struct move_only_holder_caster {
static constexpr auto name = type_caster_base<type>::name;
};

#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
template <typename type, typename deleter>
class type_caster<std::unique_ptr<type, deleter>>
: public move_only_holder_caster<type, std::unique_ptr<type, deleter>> { };
#endif

template <typename type, typename holder_type>
using type_caster_holder = conditional_t<is_copy_constructible<holder_type>::value,
Expand Down
8 changes: 7 additions & 1 deletion include/pybind11/pybind11.h
Original file line number Diff line number Diff line change
Expand Up @@ -1257,7 +1257,13 @@ class class_ : public detail::generic_type {
using type = type_;
using type_alias = detail::exactly_one_t<is_subtype, void, options...>;
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,
"Unknown/invalid class_ template parameters provided");
Expand Down
9 changes: 8 additions & 1 deletion tests/test_class.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename DoesntBreak##N::holder_type, std::TYPE##_ptr<BreaksBase<N>>>::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<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);

// There's no nice way to test that these fail because they fail to compile; leave them here,
Expand Down

0 comments on commit 95425f1

Please sign in to comment.