Skip to content

Commit c7b7799

Browse files
committed
xetter_cpp_function implementation for std::unique_ptr
1 parent 65310a6 commit c7b7799

File tree

2 files changed

+35
-20
lines changed

2 files changed

+35
-20
lines changed

include/pybind11/pybind11.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,6 +1445,7 @@ struct xetter_cpp_function<
14451445
&& !std::is_pointer<D>::value //
14461446
&& !is_std_unique_ptr<D>::value //
14471447
&& !is_std_shared_ptr<D>::value>> {
1448+
14481449
template <typename PM>
14491450
static cpp_function readonly(PM pm, const handle &hdl) {
14501451
return cpp_function(
@@ -1472,6 +1473,36 @@ struct xetter_cpp_function<
14721473
}
14731474
};
14741475

1476+
template <typename T, typename D>
1477+
struct xetter_cpp_function<
1478+
T,
1479+
D,
1480+
detail::enable_if_t<
1481+
detail::type_uses_smart_holder_type_caster<T>::value //
1482+
&& is_std_unique_ptr<D>::value
1483+
&& detail::type_uses_smart_holder_type_caster<typename D::element_type>::value>> {
1484+
1485+
template <typename PM>
1486+
static cpp_function readonly(PM, const handle &) {
1487+
static_assert(!is_std_unique_ptr<D>::value,
1488+
"def_readonly cannot be used for std::unique_ptr members.");
1489+
return cpp_function{}; // Unreachable.
1490+
}
1491+
1492+
template <typename PM>
1493+
static cpp_function read(PM pm, const handle &hdl) {
1494+
return cpp_function(
1495+
[pm](const std::shared_ptr<T> &c_sp) -> D { return D{std::move(c_sp.get()->*pm)}; },
1496+
is_method(hdl));
1497+
}
1498+
1499+
template <typename PM>
1500+
static cpp_function write(PM pm, const handle &hdl) {
1501+
return cpp_function([pm](T &c, D &&value) { (c.*pm).reset(value.release()); },
1502+
is_method(hdl));
1503+
}
1504+
};
1505+
14751506
// clang-format off
14761507

14771508
template <typename type_, typename... options>

tests/test_class_sh_property.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -73,26 +73,10 @@ TEST_SUBMODULE(class_sh_property, m) {
7373
.def_readonly("m_cptr_readonly", &Outer::m_cptr)
7474
.def_readwrite("m_cptr_readwrite", &Outer::m_cptr)
7575

76-
.def_property( //
77-
"m_uqmp_readwrite",
78-
[](const std::shared_ptr<Outer> &self) {
79-
return std::unique_ptr<Field>(std::move(self->m_uqmp));
80-
},
81-
[](Outer &self, std::unique_ptr<Field> uqmp) {
82-
self.m_uqmp = std::move(uqmp); //
83-
})
84-
.def_property( //
85-
"m_uqcp_readwrite",
86-
[](const std::shared_ptr<Outer> &self) {
87-
return std::unique_ptr<const Field>(std::move(self->m_uqcp));
88-
},
89-
[](Outer &self, std::unique_ptr<const Field> uqcp) {
90-
self.m_uqcp = std::move(uqcp); //
91-
})
92-
// .def_readonly("m_uqmp_readonly", &Outer::m_uqmp) // Compilation Error.
93-
// .def_readwrite("m_uqmp_readwrite", &Outer::m_uqmp)
94-
// .def_readonly("m_uqcp_readonly", &Outer::m_uqcp) // Compilation Error.
95-
// .def_readwrite("m_uqcp_readwrite", &Outer::m_uqcp)
76+
// .def_readonly("m_uqmp_readonly", &Outer::m_uqmp) // Custom compilation Error.
77+
.def_readwrite("m_uqmp_readwrite", &Outer::m_uqmp)
78+
// .def_readonly("m_uqcp_readonly", &Outer::m_uqcp) // Custom compilation Error.
79+
.def_readwrite("m_uqcp_readwrite", &Outer::m_uqcp)
9680

9781
.def_readwrite("m_shmp_readonly", &Outer::m_shmp)
9882
.def_readwrite("m_shmp_readwrite", &Outer::m_shmp)

0 commit comments

Comments
 (0)