pybind11/tests/test_class_sh_shared_ptr_copy_move.cpp

63 lines
1.6 KiB
C++
Raw Normal View History

2021-06-18 19:44:20 +00:00
#include "pybind11_tests.h"
#include <pybind11/smart_holder.h>
#include <memory>
#include <string>
#include <vector>
namespace pybind11_tests {
2021-06-19 14:35:20 +00:00
namespace class_sh_shared_ptr_copy_move {
2021-06-18 19:44:20 +00:00
template<int SerNo>
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"; }
};
using FooAVL = Foo<0>;
using FooDEF = Foo<1>;
2021-06-19 14:35:20 +00:00
} // namespace class_sh_shared_ptr_copy_move
2021-06-18 19:44:20 +00:00
} // namespace pybind11_tests
2021-06-19 14:35:20 +00:00
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_shared_ptr_copy_move::FooAVL)
2021-06-18 19:44:20 +00:00
namespace pybind11_tests {
2021-06-19 14:35:20 +00:00
namespace class_sh_shared_ptr_copy_move {
2021-06-18 19:44:20 +00:00
2021-06-19 14:35:20 +00:00
TEST_SUBMODULE(class_sh_shared_ptr_copy_move, m) {
2021-06-18 19:44:20 +00:00
namespace py = pybind11;
py::class_<FooAVL, PYBIND11_SH_AVL(FooAVL)>(m, "FooAVL");
2021-06-19 08:45:32 +00:00
py::class_<FooDEF, PYBIND11_SH_DEF(FooDEF)>(m, "FooDEF");
2021-06-18 19:44:20 +00:00
2021-06-19 14:35:20 +00:00
m.def("test_avl_copy", []() {
2021-06-18 19:44:20 +00:00
auto o = std::make_shared<FooAVL>("AVL");
auto l = py::list();
l.append(o);
});
2021-06-19 14:35:20 +00:00
m.def("test_def_copy", []() {
2021-06-18 19:44:20 +00:00
auto o = std::make_shared<FooDEF>("DEF");
auto l = py::list();
l.append(o);
});
2021-06-19 14:35:20 +00:00
m.def("test_avl_move", []() {
auto o = std::make_shared<FooAVL>("AVL");
auto l = py::list();
l.append(std::move(o));
});
m.def("test_def_move", []() {
auto o = std::make_shared<FooDEF>("DEF");
auto l = py::list();
l.append(std::move(o));
});
2021-06-18 19:44:20 +00:00
}
2021-06-19 14:35:20 +00:00
} // namespace class_sh_shared_ptr_copy_move
2021-06-18 19:44:20 +00:00
} // namespace pybind11_tests