From abf11b9d9a39151936823effece615e1a7398f22 Mon Sep 17 00:00:00 2001 From: Jakob Lykke Andersen Date: Sun, 20 Jun 2021 09:44:25 +0200 Subject: [PATCH] SH, shared_ptr copy/move, update after review --- tests/test_class_sh_shared_ptr_copy_move.cpp | 68 ++++++++++++-------- tests/test_class_sh_shared_ptr_copy_move.py | 20 +++--- 2 files changed, 53 insertions(+), 35 deletions(-) diff --git a/tests/test_class_sh_shared_ptr_copy_move.cpp b/tests/test_class_sh_shared_ptr_copy_move.cpp index 1cecc6a09..3c6872cec 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.cpp +++ b/tests/test_class_sh_shared_ptr_copy_move.cpp @@ -7,56 +7,70 @@ #include namespace pybind11_tests { -namespace class_sh_shared_ptr_copy_move { +namespace { -template +template struct Foo { std::string mtxt; - Foo() : mtxt("DefaultConstructor") {} Foo(const std::string &mtxt_) : mtxt(mtxt_) {} - Foo(const Foo &other) { mtxt = other.mtxt + "_CpCtor"; } - Foo(Foo &&other) { mtxt = other.mtxt + "_MvCtor"; } + Foo(const Foo &other) = delete; + Foo(Foo &&other) = delete; + std::string get_text() const { + std::string res = "Foo"; + if (SerNo == 0) + res += "ShPtr_"; + else if (SerNo == 1) + res += "SmHld_"; + return res + mtxt; + } }; -using FooAVL = Foo<0>; -using FooDEF = Foo<1>; +using FooShPtr = Foo<0>; +using FooSmHld = Foo<1>; -} // namespace class_sh_shared_ptr_copy_move +} // namespace } // namespace pybind11_tests -PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_shared_ptr_copy_move::FooAVL) +PYBIND11_TYPE_CASTER_BASE_HOLDER(pybind11_tests::FooShPtr) +PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::FooSmHld) namespace pybind11_tests { -namespace class_sh_shared_ptr_copy_move { +namespace { TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) { namespace py = pybind11; - py::class_(m, "FooAVL"); - py::class_(m, "FooDEF"); + py::class_>(m, "FooShPtr") + .def("get_text", &FooShPtr::get_text); + py::classh(m, "FooSmHld") + .def("get_text", &FooSmHld::get_text); - m.def("test_avl_copy", []() { - auto o = std::make_shared("AVL"); + m.def("test_ShPtr_copy", []() { + auto o = std::make_shared("copy"); auto l = py::list(); l.append(o); + return l; }); - m.def("test_def_copy", []() { - auto o = std::make_shared("DEF"); - auto l = py::list(); - l.append(o); + m.def("test_SmHld_copy", []() { + auto o = std::make_shared("copy"); + auto l = py::list(); + l.append(o); + return l; }); - m.def("test_avl_move", []() { - auto o = std::make_shared("AVL"); - auto l = py::list(); - l.append(std::move(o)); + m.def("test_ShPtr_move", []() { + auto o = std::make_shared("move"); + auto l = py::list(); + l.append(std::move(o)); + return l; }); - m.def("test_def_move", []() { - auto o = std::make_shared("DEF"); - auto l = py::list(); - l.append(std::move(o)); + m.def("test_SmHld_move", []() { + auto o = std::make_shared("move"); + auto l = py::list(); + l.append(std::move(o)); + return l; }); } -} // namespace class_sh_shared_ptr_copy_move +} // namespace } // namespace pybind11_tests diff --git a/tests/test_class_sh_shared_ptr_copy_move.py b/tests/test_class_sh_shared_ptr_copy_move.py index 4263013e8..9f3813434 100644 --- a/tests/test_class_sh_shared_ptr_copy_move.py +++ b/tests/test_class_sh_shared_ptr_copy_move.py @@ -3,17 +3,21 @@ from pybind11_tests import class_sh_shared_ptr_copy_move as m -def test_avl_copy(): - m.test_avl_copy() +def test_shptr_copy(): + txt = m.test_ShPtr_copy()[0].get_text() + assert txt == "FooShPtr_copy" -def test_def_copy(): - m.test_def_copy() +def test_smhld_copy(): + txt = m.test_SmHld_copy()[0].get_text() + assert txt == "FooSmHld_copy" -def test_avl_move(): - m.test_avl_move() +def test_shptr_move(): + txt = m.test_ShPtr_move()[0].get_text() + assert txt == "FooShPtr_move" -def test_def_move(): - m.test_def_move() +def test_smhld_move(): + txt = m.test_SmHld_move()[0].get_text() + assert txt == "FooSmHld_move"