static cast handle for rtrn_cref works by simply dropping in code from type_caster_base (marked with comments). rtrn_mref and rtrn_mptr work via const_cast (to add const).

This commit is contained in:
Ralf W. Grosse-Kunstleve 2021-01-12 13:45:48 -08:00
parent e70a882718
commit 6dea65fe64
2 changed files with 15 additions and 9 deletions

View File

@ -78,12 +78,18 @@ struct type_caster<mpty> : smart_holder_type_caster_load<mpty> {
return str("cast_rref").release(); return str("cast_rref").release();
} }
static handle cast(mpty const & /*src*/, return_value_policy /*policy*/, handle /*parent*/) { static handle cast(mpty const &src, return_value_policy policy, handle parent) {
return str("cast_cref").release(); // type_caster_base BEGIN
// clang-format off
if (policy == return_value_policy::automatic || policy == return_value_policy::automatic_reference)
policy = return_value_policy::copy;
return cast(&src, policy, parent);
// clang-format on
// type_caster_base END
} }
static handle cast(mpty & /*src*/, return_value_policy /*policy*/, handle /*parent*/) { static handle cast(mpty &src, return_value_policy policy, handle parent) {
return str("cast_mref").release(); return cast(const_cast<mpty const &>(src), policy, parent); // Mtbl2Const
} }
static handle cast(mpty const *src, return_value_policy policy, handle parent) { static handle cast(mpty const *src, return_value_policy policy, handle parent) {
@ -97,8 +103,8 @@ struct type_caster<mpty> : smart_holder_type_caster_load<mpty> {
// type_caster_base END // type_caster_base END
} }
static handle cast(mpty * /*src*/, return_value_policy /*policy*/, handle /*parent*/) { static handle cast(mpty *src, return_value_policy policy, handle parent) {
return str("cast_mptr").release(); return cast(const_cast<mpty const *>(src), policy, parent); // Mtbl2Const
} }
template <typename T_> template <typename T_>

View File

@ -16,10 +16,10 @@ def test_mpty_constructors():
def test_cast(): def test_cast():
assert m.rtrn_mpty_valu() == "cast_rref" assert m.rtrn_mpty_valu() == "cast_rref"
assert m.rtrn_mpty_rref() == "cast_rref" assert m.rtrn_mpty_rref() == "cast_rref"
assert m.rtrn_mpty_cref() == "cast_cref" assert m.get_mtxt(m.rtrn_mpty_cref()) == "rtrn_cref"
assert m.rtrn_mpty_mref() == "cast_mref" assert m.get_mtxt(m.rtrn_mpty_mref()) == "rtrn_mref"
assert m.get_mtxt(m.rtrn_mpty_cptr()) == "rtrn_cptr" assert m.get_mtxt(m.rtrn_mpty_cptr()) == "rtrn_cptr"
assert m.rtrn_mpty_mptr() == "cast_mptr" assert m.get_mtxt(m.rtrn_mpty_mptr()) == "rtrn_mptr"
def test_load(): def test_load():