mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-11 16:13:53 +00:00
Retrieving smart_holder pointer in type_caster<std::unique_ptr<mpty[ const]>>::load, and using it cast_op operators.
This commit is contained in:
parent
6277910abd
commit
cfc848a137
@ -33,8 +33,8 @@ std::string pass_mpty_shcp(std::shared_ptr<mpty const> obj) { return "pass_shcp:
|
|||||||
std::unique_ptr<mpty> rtrn_mpty_uqmp() { return std::unique_ptr<mpty>(new mpty); }
|
std::unique_ptr<mpty> rtrn_mpty_uqmp() { return std::unique_ptr<mpty>(new mpty); }
|
||||||
std::unique_ptr<mpty const> rtrn_mpty_uqcp() { return std::unique_ptr<mpty const>(new mpty); }
|
std::unique_ptr<mpty const> rtrn_mpty_uqcp() { return std::unique_ptr<mpty const>(new mpty); }
|
||||||
|
|
||||||
const char* pass_mpty_uqmp(std::unique_ptr<mpty>) { return "load_uqmp"; }
|
std::string pass_mpty_uqmp(std::unique_ptr<mpty> obj) { return "pass_uqmp:" + obj->mtxt; }
|
||||||
const char* pass_mpty_uqcp(std::unique_ptr<mpty const>) { return "load_uqcp"; }
|
std::string pass_mpty_uqcp(std::unique_ptr<mpty const> obj) { return "pass_uqcp:" + obj->mtxt; }
|
||||||
|
|
||||||
} // namespace classh_wip
|
} // namespace classh_wip
|
||||||
} // namespace pybind11_tests
|
} // namespace pybind11_tests
|
||||||
@ -147,7 +147,7 @@ struct type_caster<std::shared_ptr<mpty const>> : smart_holder_type_caster_load<
|
|||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct type_caster<std::unique_ptr<mpty>> {
|
struct type_caster<std::unique_ptr<mpty>> : smart_holder_type_caster_load<mpty> {
|
||||||
static constexpr auto name = _<std::unique_ptr<mpty>>();
|
static constexpr auto name = _<std::unique_ptr<mpty>>();
|
||||||
|
|
||||||
static handle cast(std::unique_ptr<mpty>&& /*src*/,
|
static handle cast(std::unique_ptr<mpty>&& /*src*/,
|
||||||
@ -158,16 +158,11 @@ struct type_caster<std::unique_ptr<mpty>> {
|
|||||||
|
|
||||||
template <typename> using cast_op_type = std::unique_ptr<mpty>;
|
template <typename> using cast_op_type = std::unique_ptr<mpty>;
|
||||||
|
|
||||||
operator std::unique_ptr<mpty>() { return rtrn_mpty_uqmp(); }
|
operator std::unique_ptr<mpty>() { return smhldr_ptr->as_unique_ptr<mpty>(); }
|
||||||
|
|
||||||
bool load(handle src, bool /*convert*/) {
|
|
||||||
if (!isinstance<mpty>(src)) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
struct type_caster<std::unique_ptr<mpty const>> {
|
struct type_caster<std::unique_ptr<mpty const>> : smart_holder_type_caster_load<mpty> {
|
||||||
static constexpr auto name = _<std::unique_ptr<mpty const>>();
|
static constexpr auto name = _<std::unique_ptr<mpty const>>();
|
||||||
|
|
||||||
static handle cast(std::unique_ptr<mpty const>&& /*src*/,
|
static handle cast(std::unique_ptr<mpty const>&& /*src*/,
|
||||||
@ -178,12 +173,7 @@ struct type_caster<std::unique_ptr<mpty const>> {
|
|||||||
|
|
||||||
template <typename> using cast_op_type = std::unique_ptr<mpty const>;
|
template <typename> using cast_op_type = std::unique_ptr<mpty const>;
|
||||||
|
|
||||||
operator std::unique_ptr<mpty const>() { return rtrn_mpty_uqcp(); }
|
operator std::unique_ptr<mpty const>() { return smhldr_ptr->as_unique_ptr<mpty>(); }
|
||||||
|
|
||||||
bool load(handle src, bool /*convert*/) {
|
|
||||||
if (!isinstance<mpty>(src)) return false;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace detail
|
} // namespace detail
|
||||||
|
@ -47,5 +47,20 @@ def test_cast_unique_ptr():
|
|||||||
|
|
||||||
|
|
||||||
def test_load_unique_ptr():
|
def test_load_unique_ptr():
|
||||||
assert m.pass_mpty_uqmp(m.mpty()) == "load_uqmp"
|
assert m.pass_mpty_uqmp(m.mpty("Uqmp")) == "pass_uqmp:Uqmp"
|
||||||
assert m.pass_mpty_uqcp(m.mpty()) == "load_uqcp"
|
assert m.pass_mpty_uqcp(m.mpty("Uqcp")) == "pass_uqcp:Uqcp"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"pass_mpty, argm, rtrn",
|
||||||
|
[
|
||||||
|
(m.pass_mpty_uqmp, "Uqmp", "pass_uqmp:Uqmp"),
|
||||||
|
(m.pass_mpty_uqcp, "Uqcp", "pass_uqcp:Uqcp"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_pass_unique_ptr_disowns(pass_mpty, argm, rtrn):
|
||||||
|
obj = m.mpty(argm)
|
||||||
|
assert pass_mpty(obj) == rtrn
|
||||||
|
with pytest.raises(RuntimeError) as exc_info:
|
||||||
|
m.pass_mpty_uqmp(obj)
|
||||||
|
assert str(exc_info.value) == "Cannot disown nullptr (as_unique_ptr)."
|
||||||
|
Loading…
Reference in New Issue
Block a user