14
14
#include " detail/typeid.h"
15
15
#include " detail/descr.h"
16
16
#include " detail/internals.h"
17
+ #include " detail/smart_holder_poc.h"
17
18
#include < array>
18
19
#include < limits>
19
20
#include < tuple>
37
38
#endif
38
39
39
40
PYBIND11_NAMESPACE_BEGIN (PYBIND11_NAMESPACE)
41
+
42
+ using pybindit::memory::smart_holder;
43
+
40
44
PYBIND11_NAMESPACE_BEGIN (detail)
41
45
42
46
// / A life support system for temporary objects created by `type_caster::load()`.
@@ -1231,16 +1235,18 @@ struct smart_holder_type_caster_load {
1231
1235
return std::shared_ptr<T>(void_ptr, convert_type (void_ptr.get ()));
1232
1236
}
1233
1237
1234
- std::unique_ptr<T> loaded_as_unique_ptr () {
1235
- holder ().ensure_can_release_ownership ();
1238
+ template <typename D = std::default_delete<T>>
1239
+ std::unique_ptr<T, D> loaded_as_unique_ptr (const char *context = " loaded_as_unique_ptr" ) {
1240
+ holder ().template ensure_compatible_rtti_uqp_del <T, D>(context);
1241
+ holder ().ensure_use_count_1 (context);
1236
1242
auto raw_void_ptr = holder ().template as_raw_ptr_unowned <void >();
1237
1243
// MISSING: Safety checks for type conversions
1238
1244
// (T must be polymorphic or meet certain other conditions).
1239
1245
T *raw_type_ptr = convert_type (raw_void_ptr);
1240
1246
1241
1247
// Critical transfer-of-ownership section. This must stay together.
1242
1248
holder ().release_ownership ();
1243
- auto result = std::unique_ptr<T>(raw_type_ptr);
1249
+ auto result = std::unique_ptr<T, D >(raw_type_ptr);
1244
1250
1245
1251
void *value_void_ptr
1246
1252
= load_impl.loaded_v_h .value_ptr (); // Expected to be identical to raw_void_ptr.
@@ -1495,12 +1501,12 @@ struct smart_holder_type_caster<std::shared_ptr<T const>> : smart_holder_type_ca
1495
1501
operator std::shared_ptr<T const >() { return this ->loaded_as_shared_ptr (); } // Mutbl2Const
1496
1502
};
1497
1503
1498
- template <typename T>
1499
- struct smart_holder_type_caster <std::unique_ptr<T>> : smart_holder_type_caster_load<T>,
1500
- smart_holder_type_caster_class_hooks {
1501
- static constexpr auto name = _<std::unique_ptr<T>>();
1504
+ template <typename T, typename D >
1505
+ struct smart_holder_type_caster <std::unique_ptr<T, D >> : smart_holder_type_caster_load<T>,
1506
+ smart_holder_type_caster_class_hooks {
1507
+ static constexpr auto name = _<std::unique_ptr<T, D >>();
1502
1508
1503
- static handle cast (std::unique_ptr<T> &&src, return_value_policy policy, handle parent) {
1509
+ static handle cast (std::unique_ptr<T, D > &&src, return_value_policy policy, handle parent) {
1504
1510
if (policy != return_value_policy::automatic
1505
1511
&& policy != return_value_policy::reference_internal) {
1506
1512
// IMPROVABLE: Error message.
@@ -1533,27 +1539,28 @@ struct smart_holder_type_caster<std::unique_ptr<T>> : smart_holder_type_caster_l
1533
1539
}
1534
1540
1535
1541
template <typename >
1536
- using cast_op_type = std::unique_ptr<T>;
1542
+ using cast_op_type = std::unique_ptr<T, D >;
1537
1543
1538
- operator std::unique_ptr<T>() { return this ->loaded_as_unique_ptr (); }
1544
+ operator std::unique_ptr<T, D >() { return this ->template loaded_as_unique_ptr <D> (); }
1539
1545
};
1540
1546
1541
- template <typename T>
1542
- struct smart_holder_type_caster <std::unique_ptr<T const >> : smart_holder_type_caster_load<T>,
1543
- smart_holder_type_caster_class_hooks {
1544
- static constexpr auto name = _<std::unique_ptr<T const >>();
1547
+ template <typename T, typename D >
1548
+ struct smart_holder_type_caster <std::unique_ptr<T const , D>>
1549
+ : smart_holder_type_caster_load<T>, smart_holder_type_caster_class_hooks {
1550
+ static constexpr auto name = _<std::unique_ptr<T const , D >>();
1545
1551
1546
- static handle cast (std::unique_ptr<T const > &&src, return_value_policy policy, handle parent) {
1547
- return smart_holder_type_caster<std::unique_ptr<T>>::cast (
1548
- std::unique_ptr<T>(const_cast <T *>(src.release ())), // Const2Mutbl
1552
+ static handle
1553
+ cast (std::unique_ptr<T const , D> &&src, return_value_policy policy, handle parent) {
1554
+ return smart_holder_type_caster<std::unique_ptr<T, D>>::cast (
1555
+ std::unique_ptr<T, D>(const_cast <T *>(src.release ())), // Const2Mutbl
1549
1556
policy,
1550
1557
parent);
1551
1558
}
1552
1559
1553
1560
template <typename >
1554
- using cast_op_type = std::unique_ptr<T const >;
1561
+ using cast_op_type = std::unique_ptr<T const , D >;
1555
1562
1556
- operator std::unique_ptr<T const >() { return this ->loaded_as_unique_ptr (); }
1563
+ operator std::unique_ptr<T const , D >() { return this ->template loaded_as_unique_ptr <D> (); }
1557
1564
};
1558
1565
1559
1566
#ifndef PYBIND11_USE_SMART_HOLDER_AS_DEFAULT
@@ -1568,12 +1575,12 @@ struct smart_holder_type_caster<std::unique_ptr<T const>> : smart_holder_type_ca
1568
1575
template <> \
1569
1576
class type_caster <std::shared_ptr<T const >> \
1570
1577
: public smart_holder_type_caster<std::shared_ptr<T const >> {}; \
1571
- template <> \
1572
- class type_caster <std::unique_ptr<T>> : public smart_holder_type_caster<std::unique_ptr<T>> { \
1573
- }; \
1574
- template <> \
1575
- class type_caster <std::unique_ptr<T const >> \
1576
- : public smart_holder_type_caster<std::unique_ptr<T const >> {}; \
1578
+ template <typename D> \
1579
+ class type_caster <std::unique_ptr<T, D >> \
1580
+ : public smart_holder_type_caster<std::unique_ptr<T, D>> {}; \
1581
+ template <typename D> \
1582
+ class type_caster <std::unique_ptr<T const , D>> \
1583
+ : public smart_holder_type_caster<std::unique_ptr<T const , D >> {}; \
1577
1584
} \
1578
1585
}
1579
1586
#endif
@@ -1595,12 +1602,13 @@ template <typename T>
1595
1602
class type_caster <std::shared_ptr<T const >>
1596
1603
: public smart_holder_type_caster<std::shared_ptr<T const >> {};
1597
1604
1598
- template <typename T>
1599
- class type_caster <std::unique_ptr<T>> : public smart_holder_type_caster<std::unique_ptr<T>> {};
1605
+ template <typename T, typename D>
1606
+ class type_caster <std::unique_ptr<T, D>>
1607
+ : public smart_holder_type_caster<std::unique_ptr<T, D>> {};
1600
1608
1601
- template <typename T>
1602
- class type_caster <std::unique_ptr<T const >>
1603
- : public smart_holder_type_caster<std::unique_ptr<T const >> {};
1609
+ template <typename T, typename D >
1610
+ class type_caster <std::unique_ptr<T const , D >>
1611
+ : public smart_holder_type_caster<std::unique_ptr<T const , D >> {};
1604
1612
1605
1613
#define PYBIND11_SMART_HOLDER_TYPE_CASTERS (T )
1606
1614
0 commit comments