From 4a879cfdcbe38ec67281e53dd0433413f02e8c4e Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Mon, 11 Jan 2021 21:27:23 -0800 Subject: [PATCH] Retrieving smart_holder pointer in type_caster>::load, and using it cast_op operators. --- tests/test_classh_wip.cpp | 22 ++++++---------------- tests/test_classh_wip.py | 4 ++-- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/tests/test_classh_wip.cpp b/tests/test_classh_wip.cpp index a6e20e633..3de45a438 100644 --- a/tests/test_classh_wip.cpp +++ b/tests/test_classh_wip.cpp @@ -27,8 +27,8 @@ std::string pass_mpty_mptr(mpty* obj) { return "pass_mptr:" + obj->mtxt; } std::shared_ptr rtrn_mpty_shmp() { return std::shared_ptr(new mpty); } std::shared_ptr rtrn_mpty_shcp() { return std::shared_ptr(new mpty); } -const char* pass_mpty_shmp(std::shared_ptr) { return "load_shmp"; } -const char* pass_mpty_shcp(std::shared_ptr) { return "load_shcp"; } +std::string pass_mpty_shmp(std::shared_ptr obj) { return "pass_shmp:" + obj->mtxt; } +std::string pass_mpty_shcp(std::shared_ptr obj) { return "pass_shcp:" + obj->mtxt; } std::unique_ptr rtrn_mpty_uqmp() { return std::unique_ptr(new mpty); } std::unique_ptr rtrn_mpty_uqcp() { return std::unique_ptr(new mpty); } @@ -117,7 +117,7 @@ struct type_caster : smart_holder_type_caster_load { }; template <> -struct type_caster> { +struct type_caster> : smart_holder_type_caster_load { static constexpr auto name = _>(); static handle cast(const std::shared_ptr& /*src*/, @@ -128,16 +128,11 @@ struct type_caster> { template using cast_op_type = std::shared_ptr; - operator std::shared_ptr() { return rtrn_mpty_shmp(); } - - bool load(handle src, bool /*convert*/) { - if (!isinstance(src)) return false; - return true; - } + operator std::shared_ptr() { return smhldr_ptr->as_shared_ptr(); } }; template <> -struct type_caster> { +struct type_caster> : smart_holder_type_caster_load { static constexpr auto name = _>(); static handle cast(const std::shared_ptr& /*src*/, @@ -148,12 +143,7 @@ struct type_caster> { template using cast_op_type = std::shared_ptr; - operator std::shared_ptr() { return rtrn_mpty_shcp(); } - - bool load(handle src, bool /*convert*/) { - if (!isinstance(src)) return false; - return true; - } + operator std::shared_ptr() { return smhldr_ptr->as_shared_ptr(); } }; template <> diff --git a/tests/test_classh_wip.py b/tests/test_classh_wip.py index d93889704..7d6d0bb0f 100644 --- a/tests/test_classh_wip.py +++ b/tests/test_classh_wip.py @@ -37,8 +37,8 @@ def test_cast_shared_ptr(): def test_load_shared_ptr(): - assert m.pass_mpty_shmp(m.mpty()) == "load_shmp" - assert m.pass_mpty_shcp(m.mpty()) == "load_shcp" + assert m.pass_mpty_shmp(m.mpty("Shmp")) == "pass_shmp:Shmp" + assert m.pass_mpty_shcp(m.mpty("Shcp")) == "pass_shcp:Shcp" def test_cast_unique_ptr():