From d4fc392ebe4f8e59d42831ae3a78b4d8d90b0bbd Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 3 Jul 2024 14:37:39 -0700 Subject: [PATCH] `move_only_holder_caster>`: inherit from `type_caster_base` CRUDE INTERMEDIATE STATE, just to see if this builds on all platforms. --- include/pybind11/cast.h | 48 +++++++++++++++++++++++++++++++---- tests/test_class_sh_basic.cpp | 2 +- 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/include/pybind11/cast.h b/include/pybind11/cast.h index 0aa75929e..58a606463 100644 --- a/include/pybind11/cast.h +++ b/include/pybind11/cast.h @@ -855,14 +855,14 @@ public: explicit operator type *() { if (typeinfo->default_holder) { - throw std::runtime_error("BAKEIN_WIP: operator type *()"); + throw std::runtime_error("BAKEIN_WIP: operator type *() shared_ptr"); } return this->value; } explicit operator type &() { if (typeinfo->default_holder) { - throw std::runtime_error("BAKEIN_WIP: operator type &()"); + throw std::runtime_error("BAKEIN_WIP: operator type &() shared_ptr"); } // static_cast works around compiler error with MSVC 17 and CUDA 10.2 // see issue #2180 @@ -971,16 +971,54 @@ struct move_only_holder_caster { // BAKEIN_WIP template -struct move_only_holder_caster> { - static_assert(std::is_base_of, type_caster>::value, +struct move_only_holder_caster> + : public type_caster_base { +public: + using base = type_caster_base; + static_assert(std::is_base_of>::value, "Holder classes are only supported for custom types"); + using base::base; + using base::cast; + using base::typeinfo; + using base::value; static handle cast(std::unique_ptr &&src, return_value_policy policy, handle parent) { return smart_holder_type_caster_support::unique_ptr_to_python( std::move(src), policy, parent); } - static constexpr auto name = type_caster_base::name; + + bool load(handle src, bool convert) { + return base::template load_impl< + move_only_holder_caster>>(src, convert); + } + + bool load_value(value_and_holder &&v_h) { + if (typeinfo->default_holder) { + sh_load_helper.loaded_v_h = v_h; + return true; + } + return false; // BAKEIN_WIP: What is the best behavior here? + } + + explicit operator type *() { + throw std::runtime_error("BAKEIN_WIP: operator type *() unique_ptr"); + } + + explicit operator type &() { + throw std::runtime_error("BAKEIN_WIP: operator type &() unique_ptr"); + } + + template + using cast_op_type = std::unique_ptr; + + explicit operator std::unique_ptr() { + throw std::runtime_error("WIP operator std::unique_ptr ()"); + } + + static bool try_direct_conversions(handle) { return false; } + + smart_holder_type_caster_support::load_helper> sh_load_helper; // Const2Mutbl }; template diff --git a/tests/test_class_sh_basic.cpp b/tests/test_class_sh_basic.cpp index 53b7a0f43..030dc2cf7 100644 --- a/tests/test_class_sh_basic.cpp +++ b/tests/test_class_sh_basic.cpp @@ -175,7 +175,7 @@ TEST_SUBMODULE(class_sh_basic, m) { m.def("rtrn_uqmp", rtrn_uqmp); m.def("rtrn_uqcp", rtrn_uqcp); - // BAKEIN_BREAK m.def("pass_uqmp", pass_uqmp); + m.def("pass_uqmp", pass_uqmp); // BAKEIN_BREAK m.def("pass_uqcp", pass_uqcp); m.def("rtrn_udmp", rtrn_udmp);