Skip to content

Commit c8eb29c

Browse files
committed
Fixing up cast.h and smart_holder.h after rebase.
1 parent b9eb2a4 commit c8eb29c

File tree

2 files changed

+38
-31
lines changed

2 files changed

+38
-31
lines changed

include/pybind11/cast.h

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "detail/typeid.h"
1515
#include "detail/descr.h"
1616
#include "detail/internals.h"
17+
#include "detail/smart_holder_poc.h"
1718
#include <array>
1819
#include <limits>
1920
#include <tuple>
@@ -37,6 +38,9 @@
3738
#endif
3839

3940
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
41+
42+
using pybindit::memory::smart_holder;
43+
4044
PYBIND11_NAMESPACE_BEGIN(detail)
4145

4246
/// A life support system for temporary objects created by `type_caster::load()`.
@@ -1231,16 +1235,18 @@ struct smart_holder_type_caster_load {
12311235
return std::shared_ptr<T>(void_ptr, convert_type(void_ptr.get()));
12321236
}
12331237

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);
12361242
auto raw_void_ptr = holder().template as_raw_ptr_unowned<void>();
12371243
// MISSING: Safety checks for type conversions
12381244
// (T must be polymorphic or meet certain other conditions).
12391245
T *raw_type_ptr = convert_type(raw_void_ptr);
12401246

12411247
// Critical transfer-of-ownership section. This must stay together.
12421248
holder().release_ownership();
1243-
auto result = std::unique_ptr<T>(raw_type_ptr);
1249+
auto result = std::unique_ptr<T, D>(raw_type_ptr);
12441250

12451251
void *value_void_ptr
12461252
= 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
14951501
operator std::shared_ptr<T const>() { return this->loaded_as_shared_ptr(); } // Mutbl2Const
14961502
};
14971503

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>>();
15021508

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) {
15041510
if (policy != return_value_policy::automatic
15051511
&& policy != return_value_policy::reference_internal) {
15061512
// IMPROVABLE: Error message.
@@ -1533,27 +1539,28 @@ struct smart_holder_type_caster<std::unique_ptr<T>> : smart_holder_type_caster_l
15331539
}
15341540

15351541
template <typename>
1536-
using cast_op_type = std::unique_ptr<T>;
1542+
using cast_op_type = std::unique_ptr<T, D>;
15371543

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>(); }
15391545
};
15401546

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>>();
15451551

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
15491556
policy,
15501557
parent);
15511558
}
15521559

15531560
template <typename>
1554-
using cast_op_type = std::unique_ptr<T const>;
1561+
using cast_op_type = std::unique_ptr<T const, D>;
15551562

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>(); }
15571564
};
15581565

15591566
#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
15681575
template <> \
15691576
class type_caster<std::shared_ptr<T const>> \
15701577
: 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>> {}; \
15771584
} \
15781585
}
15791586
#endif
@@ -1595,12 +1602,13 @@ template <typename T>
15951602
class type_caster<std::shared_ptr<T const>>
15961603
: public smart_holder_type_caster<std::shared_ptr<T const>> {};
15971604

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

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

16051613
#define PYBIND11_SMART_HOLDER_TYPE_CASTERS(T)
16061614

include/pybind11/smart_holder.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
#pragma once
66

7-
#include "detail/smart_holder_type_casters.h"
87
#include "pybind11.h"
98

109
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)

0 commit comments

Comments
 (0)