diff --git a/tests/test_type_caster_bare_interface_demo.cpp b/tests/test_type_caster_bare_interface_demo.cpp index 12e98f5d2..ea6041a65 100644 --- a/tests/test_type_caster_bare_interface_demo.cpp +++ b/tests/test_type_caster_bare_interface_demo.cpp @@ -7,12 +7,14 @@ namespace type_caster_bare_interface_demo { struct mpty {}; +// clang-format off + mpty rtrn_mpty_valu() { mpty obj; return obj; } -mpty&& rtrn_mpty_rref() { mpty obj; return std::move(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() { static mpty obj; return &obj; } -mpty* rtrn_mpty_mptr() { 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"; } @@ -21,20 +23,22 @@ 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_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_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"; } -} // namespace type_caster_bare_interface_demo -} // namespace pybind11_tests +// clang-format on + +} // namespace type_caster_bare_interface_demo +} // namespace pybind11_tests namespace pybind11 { namespace detail { @@ -48,139 +52,129 @@ struct type_caster { // static handle cast(mpty, ...) // is redundant (leads to ambiguous overloads). - 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_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(); } - 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(); } - static handle cast(mpty const* /*src*/, - return_value_policy /*policy*/, - handle /*parent*/) { + 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*/) { + 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*, + std::is_same, mpty const *>::value, + mpty const *, conditional_t< - std::is_same, mpty*>::value, mpty*, + std::is_same, mpty *>::value, + mpty *, conditional_t< - std::is_same::value, mpty const&, - conditional_t< - std::is_same::value, mpty&, - conditional_t< - std::is_same::value, mpty&&, - mpty>>>>>; + 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*() { return rtrn_mpty_cptr(); } - operator mpty*() { return rtrn_mpty_mptr(); } + operator mpty const*() { static mpty obj; return &obj; } + operator mpty*() { static mpty obj; return &obj; } - bool load(handle /*src*/, bool /*convert*/) { - return true; - } + // 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*/, + 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; + template + using cast_op_type = std::shared_ptr; operator std::shared_ptr() { return rtrn_mpty_shmp(); } - bool load(handle /*src*/, bool /*convert*/) { - return true; - } + bool load(handle /*src*/, bool /*convert*/) { return true; } }; template <> struct type_caster> { static constexpr auto name = _>(); - static handle cast(const std::shared_ptr& /*src*/, + 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; + template + using cast_op_type = std::shared_ptr; operator std::shared_ptr() { return rtrn_mpty_shcp(); } - bool load(handle /*src*/, bool /*convert*/) { - return true; - } + 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*/) { + 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; + template + using cast_op_type = std::unique_ptr; operator std::unique_ptr() { return rtrn_mpty_uqmp(); } - bool load(handle /*src*/, bool /*convert*/) { - return true; - } + bool load(handle /*src*/, bool /*convert*/) { return true; } }; template <> struct type_caster> { static constexpr auto name = _>(); - static handle cast(std::unique_ptr&& /*src*/, + 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; + template + using cast_op_type = std::unique_ptr; operator std::unique_ptr() { return rtrn_mpty_uqcp(); } - bool load(handle /*src*/, bool /*convert*/) { - return true; - } + bool load(handle /*src*/, bool /*convert*/) { return true; } }; -} // namespace detail -} // namespace pybind11 +} // namespace detail +} // namespace pybind11 namespace pybind11_tests { namespace type_caster_bare_interface_demo { @@ -213,5 +207,5 @@ TEST_SUBMODULE(type_caster_bare_interface_demo, m) { m.def("pass_mpty_uqcp", pass_mpty_uqcp); } -} // namespace type_caster_bare_interface_demo -} // namespace pybind11_tests +} // namespace type_caster_bare_interface_demo +} // namespace pybind11_tests