From 6dea65fe64d4e11f78be6fc025bfb86b4a7eab2f Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Tue, 12 Jan 2021 13:45:48 -0800 Subject: [PATCH] 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). --- tests/test_classh_wip.cpp | 18 ++++++++++++------ tests/test_classh_wip.py | 6 +++--- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/tests/test_classh_wip.cpp b/tests/test_classh_wip.cpp index 58d991bd8..d974452da 100644 --- a/tests/test_classh_wip.cpp +++ b/tests/test_classh_wip.cpp @@ -78,12 +78,18 @@ struct type_caster : smart_holder_type_caster_load { return str("cast_rref").release(); } - static handle cast(mpty const & /*src*/, return_value_policy /*policy*/, handle /*parent*/) { - return str("cast_cref").release(); + static handle cast(mpty const &src, return_value_policy policy, handle parent) { + // 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*/) { - return str("cast_mref").release(); + static handle cast(mpty &src, return_value_policy policy, handle parent) { + return cast(const_cast(src), policy, parent); // Mtbl2Const } static handle cast(mpty const *src, return_value_policy policy, handle parent) { @@ -97,8 +103,8 @@ struct type_caster : smart_holder_type_caster_load { // type_caster_base END } - static handle cast(mpty * /*src*/, return_value_policy /*policy*/, handle /*parent*/) { - return str("cast_mptr").release(); + static handle cast(mpty *src, return_value_policy policy, handle parent) { + return cast(const_cast(src), policy, parent); // Mtbl2Const } template diff --git a/tests/test_classh_wip.py b/tests/test_classh_wip.py index 719b303a8..10a42ae9d 100644 --- a/tests/test_classh_wip.py +++ b/tests/test_classh_wip.py @@ -16,10 +16,10 @@ def test_mpty_constructors(): def test_cast(): assert m.rtrn_mpty_valu() == "cast_rref" assert m.rtrn_mpty_rref() == "cast_rref" - assert m.rtrn_mpty_cref() == "cast_cref" - assert m.rtrn_mpty_mref() == "cast_mref" + assert m.get_mtxt(m.rtrn_mpty_cref()) == "rtrn_cref" + assert m.get_mtxt(m.rtrn_mpty_mref()) == "rtrn_mref" 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():