From de10541b1218d4f1747f0d5e3ed9dcfbd0fbb995 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Sun, 10 Jan 2021 12:22:23 -0800 Subject: [PATCH] Adding type_caster_bare_interface_demo. --- .../test_type_caster_bare_interface_demo.cpp | 115 ++++++++++++++++++ tests/test_type_caster_bare_interface_demo.py | 22 ++++ 2 files changed, 137 insertions(+) create mode 100644 tests/test_type_caster_bare_interface_demo.cpp create mode 100644 tests/test_type_caster_bare_interface_demo.py diff --git a/tests/test_type_caster_bare_interface_demo.cpp b/tests/test_type_caster_bare_interface_demo.cpp new file mode 100644 index 000000000..35ebaf88b --- /dev/null +++ b/tests/test_type_caster_bare_interface_demo.cpp @@ -0,0 +1,115 @@ +#include "pybind11_tests.h" + +namespace pybind11_tests { +namespace type_caster_bare_interface_demo { + +struct mpty {}; + +mpty rtrn_mpty_valu() { mpty obj; return obj; } +mpty&& rtrn_mpty_rref() { 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() { static mpty obj; return &obj; } +mpty* rtrn_mpty_mptr() { static mpty obj; return &obj; } + +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"; } + +} // namespace type_caster_bare_interface_demo +} // namespace pybind11_tests + +namespace pybind11 { +namespace detail { + +using namespace pybind11_tests::type_caster_bare_interface_demo; + +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*/) { + return str("cast_cptr").release(); + } + + static handle cast(mpty* /*src*/, + return_value_policy /*policy*/, + handle /*parent*/) { + return str("cast_mptr").release(); + } + + template + using cast_op_type = conditional_t< + std::is_same, const mpty*>::value, const mpty*, + conditional_t< + std::is_same, mpty*>::value, mpty*, + conditional_t< + std::is_same::value, const mpty&, + conditional_t< + std::is_same::value, mpty&, + conditional_t< + std::is_same::value, mpty&&, + mpty>>>>>; + + 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*() { return rtrn_mpty_cptr(); } + operator mpty*() { return rtrn_mpty_mptr(); } + + bool load(handle /*src*/, bool /*convert*/) { + return true; + } +}; + +} // namespace detail +} // namespace pybind11 + +namespace pybind11_tests { +namespace type_caster_bare_interface_demo { + +TEST_SUBMODULE(type_caster_bare_interface_demo, 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); +} + +} // namespace type_caster_bare_interface_demo +} // namespace pybind11_tests diff --git a/tests/test_type_caster_bare_interface_demo.py b/tests/test_type_caster_bare_interface_demo.py new file mode 100644 index 000000000..a166b2684 --- /dev/null +++ b/tests/test_type_caster_bare_interface_demo.py @@ -0,0 +1,22 @@ +# -*- coding: utf-8 -*- +import pytest + +from pybind11_tests import type_caster_bare_interface_demo 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"