mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-11 08:03:55 +00:00
SH, update shared_ptr copy tests
This commit is contained in:
parent
1f98d74761
commit
fc548c067f
@ -106,6 +106,7 @@ set(PYBIND11_TEST_FILES
|
||||
test_class_sh_disowning_mi.cpp
|
||||
test_class_sh_factory_constructors.cpp
|
||||
test_class_sh_inheritance.cpp
|
||||
test_class_sh_shared_ptr_copy.cpp
|
||||
test_class_sh_trampoline_basic.cpp
|
||||
test_class_sh_trampoline_self_life_support.cpp
|
||||
test_class_sh_trampoline_shared_ptr_cpp_arg.cpp
|
||||
|
51
tests/test_class_sh_shared_ptr_copy.cpp
Normal file
51
tests/test_class_sh_shared_ptr_copy.cpp
Normal file
@ -0,0 +1,51 @@
|
||||
#include "pybind11_tests.h"
|
||||
|
||||
#include <pybind11/smart_holder.h>
|
||||
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace pybind11_tests {
|
||||
namespace class_sh_shared_ptr_copy {
|
||||
|
||||
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>;
|
||||
|
||||
} // namespace class_sh_shared_ptr_copy
|
||||
} // namespace pybind11_tests
|
||||
|
||||
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_shared_ptr_copy::FooAVL)
|
||||
|
||||
namespace pybind11_tests {
|
||||
namespace class_sh_shared_ptr_copy {
|
||||
|
||||
TEST_SUBMODULE(class_sh_shared_ptr_copy, m) {
|
||||
namespace py = pybind11;
|
||||
|
||||
py::class_<FooAVL, PYBIND11_SH_AVL(FooAVL)>(m, "FooAVL");
|
||||
py::class_<FooDEF, std::shared_ptr<FooDEF>>(m, "FooDEF");
|
||||
|
||||
m.def("test_avl", []() {
|
||||
auto o = std::make_shared<FooAVL>("AVL");
|
||||
auto l = py::list();
|
||||
l.append(o);
|
||||
});
|
||||
m.def("test_def", []() {
|
||||
auto o = std::make_shared<FooDEF>("DEF");
|
||||
auto l = py::list();
|
||||
l.append(o);
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace class_sh_shared_ptr_copy
|
||||
} // namespace pybind11_tests
|
11
tests/test_class_sh_shared_ptr_copy.py
Normal file
11
tests/test_class_sh_shared_ptr_copy.py
Normal file
@ -0,0 +1,11 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from pybind11_tests import class_sh_shared_ptr_copy as m
|
||||
|
||||
|
||||
def test_avl():
|
||||
m.test_avl()
|
||||
|
||||
|
||||
def test_def():
|
||||
m.test_def()
|
@ -98,16 +98,6 @@ private:
|
||||
int value;
|
||||
};
|
||||
|
||||
// an uncopyable object managed by a std::shared_ptr<>
|
||||
class MyObject3a {
|
||||
public:
|
||||
MyObject3a(int value) : value(value) { print_created(this, toString()); }
|
||||
std::string toString() const { return "MyObject3a[" + std::to_string(value) + "]"; }
|
||||
virtual ~MyObject3a() { print_destroyed(this); }
|
||||
private:
|
||||
int value;
|
||||
};
|
||||
|
||||
// test_unique_nodelete
|
||||
// Object with a private destructor
|
||||
class MyObject4;
|
||||
@ -367,15 +357,6 @@ TEST_SUBMODULE(smart_ptr, m) {
|
||||
m.def("print_myobject3_3", [](const std::shared_ptr<MyObject3> &obj) { py::print(obj->toString()); });
|
||||
m.def("print_myobject3_4", [](const std::shared_ptr<MyObject3> *obj) { py::print((*obj)->toString()); });
|
||||
|
||||
py::class_<MyObject3a>(m, "MyObject3a");
|
||||
m.def("make_myobject3_1", []() { return new MyObject3a(8); });
|
||||
m.def("make_myobject3_2", []() { return std::make_shared<MyObject3a>(9); });
|
||||
m.def("print_myobject3a_1", [](const MyObject3a *obj) { py::print(obj->toString()); });
|
||||
m.def("print_myobject3a_2", [](std::shared_ptr<MyObject3a> obj) { py::print(obj->toString()); });
|
||||
m.def("print_myobject3a_3", [](const std::shared_ptr<MyObject3a> &obj) { py::print(obj->toString()); });
|
||||
// this doesn't compile, should it?
|
||||
//m.def("print_myobject3a_4", [](const std::shared_ptr<MyObject3a> *obj) { py::print((*obj)->toString()); });
|
||||
|
||||
// test_smart_ptr_refcounting
|
||||
m.def("test_object1_refcounting", []() {
|
||||
ref<MyObject1> o = new MyObject1(0);
|
||||
@ -499,10 +480,4 @@ TEST_SUBMODULE(smart_ptr, m) {
|
||||
list.append(py::cast(e));
|
||||
return list;
|
||||
});
|
||||
|
||||
m.def("test_3011_shared_ptr", []() {
|
||||
auto o = std::make_shared<MyObject3a>(42);
|
||||
auto l = py::list();
|
||||
l.append(o);
|
||||
});
|
||||
}
|
||||
|
@ -316,7 +316,3 @@ def test_shared_ptr_gc():
|
||||
pytest.gc_collect()
|
||||
for i, v in enumerate(el.get()):
|
||||
assert i == v.value()
|
||||
|
||||
|
||||
def test_3011_shared_ptr():
|
||||
m.test_3011_shared_ptr()
|
Loading…
Reference in New Issue
Block a user