From 0dfd3a6b7d25616328fa759df7fb57e8f3e2ca30 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Fri, 29 Jan 2021 13:42:52 -0800 Subject: [PATCH] Copying files as-is from branch test_unique_ptr_member (PR #2672). --- tests/test_type_caster_bare_interface.cpp | 211 ++++++++++++++++++++++ tests/test_type_caster_bare_interface.py | 41 +++++ 2 files changed, 252 insertions(+) create mode 100644 tests/test_type_caster_bare_interface.cpp create mode 100644 tests/test_type_caster_bare_interface.py diff --git a/tests/test_type_caster_bare_interface.cpp b/tests/test_type_caster_bare_interface.cpp new file mode 100644 index 000000000..02b6281c2 --- /dev/null +++ b/tests/test_type_caster_bare_interface.cpp @@ -0,0 +1,211 @@ +#include "pybind11_tests.h" + +#include + +namespace pybind11_tests { +namespace type_caster_bare_interface { + +struct mpty {}; + +// clang-format off + +mpty rtrn_mpty_valu() { mpty obj; return obj; } +mpty&& rtrn_mpty_rref() { static mpty obj; return std::move(obj); } +mpty const& rtrn_mpty_cref() { static mpty obj; return obj; } +mpty& rtrn_mpty_mref() { static mpty obj; return obj; } +mpty const* rtrn_mpty_cptr() { return new mpty; } +mpty* rtrn_mpty_mptr() { return new mpty; } + +const char* pass_mpty_valu(mpty) { return "load_valu"; } +const char* pass_mpty_rref(mpty&&) { return "load_rref"; } +const char* pass_mpty_cref(mpty const&) { return "load_cref"; } +const char* pass_mpty_mref(mpty&) { return "load_mref"; } +const char* pass_mpty_cptr(mpty const*) { return "load_cptr"; } +const char* pass_mpty_mptr(mpty*) { return "load_mptr"; } + +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::unique_ptr rtrn_mpty_uqmp() { return std::unique_ptr(new mpty); } +std::unique_ptr rtrn_mpty_uqcp() { return std::unique_ptr(new mpty); } + +const char* pass_mpty_uqmp(std::unique_ptr) { return "load_uqmp"; } +const char* pass_mpty_uqcp(std::unique_ptr) { return "load_uqcp"; } + +// clang-format on + +} // namespace type_caster_bare_interface +} // namespace pybind11_tests + +namespace pybind11 { +namespace detail { + +using namespace pybind11_tests::type_caster_bare_interface; + +template <> +struct type_caster { + static constexpr auto name = _(); + + // static handle cast(mpty, ...) + // is redundant (leads to ambiguous overloads). + + static handle cast(mpty && /*src*/, return_value_policy /*policy*/, handle /*parent*/) { + 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 & /*src*/, return_value_policy /*policy*/, handle /*parent*/) { + return str("cast_mref").release(); + } + + static handle cast(mpty const *src, return_value_policy /*policy*/, handle /*parent*/) { + delete src; + return str("cast_cptr").release(); + } + + static handle cast(mpty *src, return_value_policy /*policy*/, handle /*parent*/) { + delete src; + return str("cast_mptr").release(); + } + + template + using cast_op_type = conditional_t< + std::is_same, mpty const *>::value, + mpty const *, + conditional_t< + std::is_same, mpty *>::value, + mpty *, + conditional_t< + std::is_same::value, + mpty const &, + conditional_t::value, + mpty &, + conditional_t::value, mpty &&, mpty>>>>>; + + // clang-format off + + operator mpty() { return rtrn_mpty_valu(); } + operator mpty&&() && { return rtrn_mpty_rref(); } + operator mpty const&() { return rtrn_mpty_cref(); } + operator mpty&() { return rtrn_mpty_mref(); } + operator mpty const*() { static mpty obj; return &obj; } + operator mpty*() { static mpty obj; return &obj; } + + // clang-format on + + bool load(handle /*src*/, bool /*convert*/) { return true; } +}; + +template <> +struct type_caster> { + static constexpr auto name = _>(); + + static handle cast(const std::shared_ptr & /*src*/, + return_value_policy /*policy*/, + handle /*parent*/) { + return str("cast_shmp").release(); + } + + template + using cast_op_type = std::shared_ptr; + + operator std::shared_ptr() { return rtrn_mpty_shmp(); } + + bool load(handle /*src*/, bool /*convert*/) { return true; } +}; + +template <> +struct type_caster> { + static constexpr auto name = _>(); + + static handle cast(const std::shared_ptr & /*src*/, + return_value_policy /*policy*/, + handle /*parent*/) { + return str("cast_shcp").release(); + } + + template + using cast_op_type = std::shared_ptr; + + operator std::shared_ptr() { return rtrn_mpty_shcp(); } + + bool load(handle /*src*/, bool /*convert*/) { return true; } +}; + +template <> +struct type_caster> { + static constexpr auto name = _>(); + + static handle + cast(std::unique_ptr && /*src*/, return_value_policy /*policy*/, handle /*parent*/) { + return str("cast_uqmp").release(); + } + + template + using cast_op_type = std::unique_ptr; + + operator std::unique_ptr() { return rtrn_mpty_uqmp(); } + + bool load(handle /*src*/, bool /*convert*/) { return true; } +}; + +template <> +struct type_caster> { + static constexpr auto name = _>(); + + static handle cast(std::unique_ptr && /*src*/, + return_value_policy /*policy*/, + handle /*parent*/) { + return str("cast_uqcp").release(); + } + + template + using cast_op_type = std::unique_ptr; + + operator std::unique_ptr() { return rtrn_mpty_uqcp(); } + + bool load(handle /*src*/, bool /*convert*/) { return true; } +}; + +} // namespace detail +} // namespace pybind11 + +namespace pybind11_tests { +namespace type_caster_bare_interface { + +TEST_SUBMODULE(type_caster_bare_interface, m) { + m.def("rtrn_mpty_valu", rtrn_mpty_valu); + m.def("rtrn_mpty_rref", rtrn_mpty_rref); + m.def("rtrn_mpty_cref", rtrn_mpty_cref); + m.def("rtrn_mpty_mref", rtrn_mpty_mref); + m.def("rtrn_mpty_cptr", rtrn_mpty_cptr); + m.def("rtrn_mpty_mptr", rtrn_mpty_mptr); + + m.def("pass_mpty_valu", pass_mpty_valu); + m.def("pass_mpty_rref", pass_mpty_rref); + m.def("pass_mpty_cref", pass_mpty_cref); + m.def("pass_mpty_mref", pass_mpty_mref); + m.def("pass_mpty_cptr", pass_mpty_cptr); + m.def("pass_mpty_mptr", pass_mpty_mptr); + + m.def("rtrn_mpty_shmp", rtrn_mpty_shmp); + m.def("rtrn_mpty_shcp", rtrn_mpty_shcp); + + m.def("pass_mpty_shmp", pass_mpty_shmp); + m.def("pass_mpty_shcp", pass_mpty_shcp); + + m.def("rtrn_mpty_uqmp", rtrn_mpty_uqmp); + m.def("rtrn_mpty_uqcp", rtrn_mpty_uqcp); + + m.def("pass_mpty_uqmp", pass_mpty_uqmp); + m.def("pass_mpty_uqcp", pass_mpty_uqcp); +} + +} // namespace type_caster_bare_interface +} // namespace pybind11_tests diff --git a/tests/test_type_caster_bare_interface.py b/tests/test_type_caster_bare_interface.py new file mode 100644 index 000000000..5a19e1abc --- /dev/null +++ b/tests/test_type_caster_bare_interface.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- + +from pybind11_tests import type_caster_bare_interface as m + + +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.rtrn_mpty_cptr() == "cast_cptr" + assert m.rtrn_mpty_mptr() == "cast_mptr" + + +def test_load(): + assert m.pass_mpty_valu(None) == "load_valu" + assert m.pass_mpty_rref(None) == "load_rref" + assert m.pass_mpty_cref(None) == "load_cref" + assert m.pass_mpty_mref(None) == "load_mref" + assert m.pass_mpty_cptr(None) == "load_cptr" + assert m.pass_mpty_mptr(None) == "load_mptr" + + +def test_cast_shared_ptr(): + assert m.rtrn_mpty_shmp() == "cast_shmp" + assert m.rtrn_mpty_shcp() == "cast_shcp" + + +def test_load_shared_ptr(): + assert m.pass_mpty_shmp(None) == "load_shmp" + assert m.pass_mpty_shcp(None) == "load_shcp" + + +def test_cast_unique_ptr(): + assert m.rtrn_mpty_uqmp() == "cast_uqmp" + assert m.rtrn_mpty_uqcp() == "cast_uqcp" + + +def test_load_unique_ptr(): + assert m.pass_mpty_uqmp(None) == "load_uqmp" + assert m.pass_mpty_uqcp(None) == "load_uqcp"