Skip to content

Commit 95425f1

Browse files
committed
Introducing PYBIND11_USE_SMART_HOLDER_AS_DEFAULT macro (tested only undefined; there are many errors with the macro defined).
1 parent 4fd458b commit 95425f1

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

include/pybind11/cast.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,6 +1560,7 @@ struct smart_holder_type_caster<std::unique_ptr<T const>> : smart_holder_type_ca
15601560
operator std::unique_ptr<T const>() { return this->loaded_as_unique_ptr(); }
15611561
};
15621562

1563+
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
15631564
#define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T) \
15641565
namespace pybind11 { \
15651566
namespace detail { \
@@ -1579,10 +1580,36 @@ struct smart_holder_type_caster<std::unique_ptr<T const>> : smart_holder_type_ca
15791580
: public smart_holder_type_caster<std::unique_ptr<T const>> {}; \
15801581
} \
15811582
}
1583+
#endif
15821584

15831585
//DETAIL/SMART_HOLDER_TYPE_CASTERS_H///////////////////////////////////////////////////////////////
15841586

1587+
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
1588+
15851589
template <typename type, typename SFINAE = void> class type_caster : public type_caster_base<type> { };
1590+
1591+
#else
1592+
1593+
template <typename type, typename SFINAE = void> class type_caster : public smart_holder_type_caster<type> {};
1594+
1595+
template <typename T>
1596+
class type_caster<std::shared_ptr<T>> : public smart_holder_type_caster<std::shared_ptr<T>> {};
1597+
1598+
template <typename T>
1599+
class type_caster<std::shared_ptr<T const>>
1600+
: public smart_holder_type_caster<std::shared_ptr<T const>> {};
1601+
1602+
template <typename T>
1603+
class type_caster<std::unique_ptr<T>> : public smart_holder_type_caster<std::unique_ptr<T>> {};
1604+
1605+
template <typename T>
1606+
class type_caster<std::unique_ptr<T const>>
1607+
: public smart_holder_type_caster<std::unique_ptr<T const>> {};
1608+
1609+
#define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T)
1610+
1611+
#endif
1612+
15861613
template <typename type> using make_caster = type_caster<intrinsic_t<type>>;
15871614

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

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

22342263
template <typename type, typename holder_type>
22352264
struct move_only_holder_caster {
@@ -2243,9 +2272,11 @@ struct move_only_holder_caster {
22432272
static constexpr auto name = type_caster_base<type>::name;
22442273
};
22452274

2275+
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
22462276
template <typename type, typename deleter>
22472277
class type_caster<std::unique_ptr<type, deleter>>
22482278
: public move_only_holder_caster<type, std::unique_ptr<type, deleter>> { };
2279+
#endif
22492280

22502281
template <typename type, typename holder_type>
22512282
using type_caster_holder = conditional_t<is_copy_constructible<holder_type>::value,

include/pybind11/pybind11.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,13 @@ class class_ : public detail::generic_type {
12571257
using type = type_;
12581258
using type_alias = detail::exactly_one_t<is_subtype, void, options...>;
12591259
constexpr static bool has_alias = !std::is_void<type_alias>::value;
1260-
using holder_type = detail::exactly_one_t<is_holder, std::unique_ptr<type>, options...>;
1260+
using holder_type = detail::exactly_one_t<is_holder,
1261+
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
1262+
std::unique_ptr<type>
1263+
#else
1264+
smart_holder
1265+
#endif
1266+
, options...>;
12611267

12621268
static_assert(detail::all_of<is_valid_class_option<options>...>::value,
12631269
"Unknown/invalid class_ template parameters provided");

tests/test_class.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,14 @@ CHECK_BASE(1); CHECK_BASE(2); CHECK_BASE(3); CHECK_BASE(4); CHECK_BASE(5); CHECK
505505
CHECK_ALIAS(1); CHECK_ALIAS(2); CHECK_NOALIAS(3); CHECK_ALIAS(4); CHECK_NOALIAS(5); CHECK_ALIAS(6); CHECK_ALIAS(7); CHECK_NOALIAS(8);
506506
#define CHECK_HOLDER(N, TYPE) static_assert(std::is_same<typename DoesntBreak##N::holder_type, std::TYPE##_ptr<BreaksBase<N>>>::value, \
507507
"DoesntBreak" #N " has wrong holder_type!")
508-
CHECK_HOLDER(1, unique); CHECK_HOLDER(2, unique); CHECK_HOLDER(3, unique); CHECK_HOLDER(4, unique); CHECK_HOLDER(5, unique);
508+
#define CHECK_SMART_HOLDER(N) static_assert(std::is_same<typename DoesntBreak##N::holder_type, smart_holder, \
509+
"DoesntBreak" #N " has wrong holder_type!")
510+
CHECK_HOLDER(1, unique); CHECK_HOLDER(2, unique); CHECK_HOLDER(3, unique);
511+
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
512+
CHECK_HOLDER(4, unique); CHECK_HOLDER(5, unique);
513+
#else
514+
CHECK_SMART_HOLDER(4); CHECK_SMART_HOLDER(5);
515+
#endif
509516
CHECK_HOLDER(6, shared); CHECK_HOLDER(7, shared); CHECK_HOLDER(8, shared);
510517

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

0 commit comments

Comments
 (0)