SH, shared_ptr copy/move, update after review

This commit is contained in:
Jakob Lykke Andersen 2021-06-20 09:44:25 +02:00 committed by Ralf W. Grosse-Kunstleve
parent 7312e624b2
commit abf11b9d9a
2 changed files with 53 additions and 35 deletions

View File

@ -7,56 +7,70 @@
#include <vector> #include <vector>
namespace pybind11_tests { namespace pybind11_tests {
namespace class_sh_shared_ptr_copy_move { namespace {
template<int SerNo> template <int SerNo>
struct Foo { struct Foo {
std::string mtxt; std::string mtxt;
Foo() : mtxt("DefaultConstructor") {}
Foo(const std::string &mtxt_) : mtxt(mtxt_) {} Foo(const std::string &mtxt_) : mtxt(mtxt_) {}
Foo(const Foo &other) { mtxt = other.mtxt + "_CpCtor"; } Foo(const Foo &other) = delete;
Foo(Foo &&other) { mtxt = other.mtxt + "_MvCtor"; } 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 FooShPtr = Foo<0>;
using FooDEF = Foo<1>; using FooSmHld = Foo<1>;
} // namespace class_sh_shared_ptr_copy_move } // namespace
} // namespace pybind11_tests } // 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 pybind11_tests {
namespace class_sh_shared_ptr_copy_move { namespace {
TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) { TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
namespace py = pybind11; namespace py = pybind11;
py::class_<FooAVL, PYBIND11_SH_AVL(FooAVL)>(m, "FooAVL"); py::class_<FooShPtr, std::shared_ptr<FooShPtr>>(m, "FooShPtr")
py::class_<FooDEF, PYBIND11_SH_DEF(FooDEF)>(m, "FooDEF"); .def("get_text", &FooShPtr::get_text);
py::classh<FooSmHld>(m, "FooSmHld")
.def("get_text", &FooSmHld::get_text);
m.def("test_avl_copy", []() { m.def("test_ShPtr_copy", []() {
auto o = std::make_shared<FooAVL>("AVL"); auto o = std::make_shared<FooShPtr>("copy");
auto l = py::list(); auto l = py::list();
l.append(o); l.append(o);
return l;
}); });
m.def("test_def_copy", []() { m.def("test_SmHld_copy", []() {
auto o = std::make_shared<FooDEF>("DEF"); auto o = std::make_shared<FooSmHld>("copy");
auto l = py::list(); auto l = py::list();
l.append(o); l.append(o);
return l;
}); });
m.def("test_avl_move", []() { m.def("test_ShPtr_move", []() {
auto o = std::make_shared<FooAVL>("AVL"); auto o = std::make_shared<FooShPtr>("move");
auto l = py::list(); auto l = py::list();
l.append(std::move(o)); l.append(std::move(o));
return l;
}); });
m.def("test_def_move", []() { m.def("test_SmHld_move", []() {
auto o = std::make_shared<FooDEF>("DEF"); auto o = std::make_shared<FooSmHld>("move");
auto l = py::list(); auto l = py::list();
l.append(std::move(o)); l.append(std::move(o));
return l;
}); });
} }
} // namespace class_sh_shared_ptr_copy_move } // namespace
} // namespace pybind11_tests } // namespace pybind11_tests

View File

@ -3,17 +3,21 @@
from pybind11_tests import class_sh_shared_ptr_copy_move as m from pybind11_tests import class_sh_shared_ptr_copy_move as m
def test_avl_copy(): def test_shptr_copy():
m.test_avl_copy() txt = m.test_ShPtr_copy()[0].get_text()
assert txt == "FooShPtr_copy"
def test_def_copy(): def test_smhld_copy():
m.test_def_copy() txt = m.test_SmHld_copy()[0].get_text()
assert txt == "FooSmHld_copy"
def test_avl_move(): def test_shptr_move():
m.test_avl_move() txt = m.test_ShPtr_move()[0].get_text()
assert txt == "FooShPtr_move"
def test_def_move(): def test_smhld_move():
m.test_def_move() txt = m.test_SmHld_move()[0].get_text()
assert txt == "FooSmHld_move"