unique_ptr or shared_ptr return

This commit is contained in:
Ralf W. Grosse-Kunstleve 2020-11-19 18:33:37 -08:00
parent 0a92391128
commit 01e437a2da
2 changed files with 73 additions and 16 deletions

View File

@ -1,15 +1,23 @@
#include "pybind11_tests.h" #include "pybind11_tests.h"
#include <iostream>
#include <memory> #include <memory>
namespace pybind11_tests { namespace pybind11_tests {
namespace unique_ptr_member { namespace unique_ptr_member {
inline void to_cout(std::string text) { std::cout << text << std::endl; }
class pointee { // NOT copyable. class pointee { // NOT copyable.
public: public:
pointee() = default; pointee() = default;
int get_int() const { return 213; } int get_int() const {
to_cout("pointee::get_int()");
return 213;
}
~pointee() { to_cout("~pointee()"); }
private: private:
pointee(const pointee &) = delete; pointee(const pointee &) = delete;
@ -18,35 +26,64 @@ class pointee { // NOT copyable.
pointee &operator=(pointee &&) = delete; pointee &operator=(pointee &&) = delete;
}; };
inline std::unique_ptr<pointee> make_unique_pointee() {
return std::unique_ptr<pointee>(new pointee);
}
class ptr_owner { class ptr_owner {
public: public:
explicit ptr_owner(std::unique_ptr<pointee> ptr) : ptr_(std::move(ptr)) {} explicit ptr_owner(std::unique_ptr<pointee> ptr) : ptr_(std::move(ptr)) {}
bool is_owner() const { return bool(ptr_); }
std::unique_ptr<pointee> give_up_ownership_via_unique_ptr() {
return std::move(ptr_);
}
std::shared_ptr<pointee> give_up_ownership_via_shared_ptr() {
return std::move(ptr_);
}
private: private:
std::unique_ptr<pointee> ptr_; std::unique_ptr<pointee> ptr_;
}; };
// Just to have a minimal example of a typical C++ pattern. // Just to have a minimal example of a typical C++ pattern.
inline int cpp_pattern() { inline int cpp_pattern() {
auto obj = std::unique_ptr<pointee>(new pointee); auto obj = make_unique_pointee();
int result = (obj ? 10 : 0); int result = (obj ? 1 : 8);
obj->get_int();
ptr_owner owner(std::move(obj)); ptr_owner owner(std::move(obj));
result += (obj ? 1 : 0); result = result * 10 + (obj ? 8 : 1);
result = result * 10 + (owner.is_owner() ? 1 : 8);
to_cout("before give up");
auto reclaimed = owner.give_up_ownership_via_shared_ptr();
to_cout("after give up");
result = result * 10 + (owner.is_owner() ? 8 : 1);
result = result * 10 + (reclaimed ? 1 : 8);
reclaimed.reset();
to_cout("after del");
result = result * 10 + (reclaimed ? 8 : 1);
return result; return result;
} }
TEST_SUBMODULE(unique_ptr_member, m) { TEST_SUBMODULE(unique_ptr_member, m) {
m.def("cpp_pattern", cpp_pattern); m.def("to_cout", to_cout);
py::class_<pointee>(m, "pointee") py::class_<pointee, std::shared_ptr<pointee>>(m, "pointee")
.def(py::init<>()) .def(py::init<>())
.def("get_int", &pointee::get_int); .def("get_int", &pointee::get_int);
m.def("make_unique_pointee", make_unique_pointee);
py::class_<ptr_owner>(m, "ptr_owner") py::class_<ptr_owner>(m, "ptr_owner")
#ifdef FEAT_UNIQUE_PTR_ARG //.def(py::init<std::unique_ptr<pointee>>(), py::arg("ptr"))
.def(py::init<std::unique_ptr<pointee>>(), py::arg("ptr")) .def("is_owner", &ptr_owner::is_owner)
#endif .def("give_up_ownership_via_unique_ptr",
; &ptr_owner::give_up_ownership_via_unique_ptr)
.def("give_up_ownership_via_shared_ptr",
&ptr_owner::give_up_ownership_via_shared_ptr);
m.def("cpp_pattern", cpp_pattern);
} }
} // namespace unique_ptr_member } // namespace unique_ptr_member

View File

@ -4,15 +4,35 @@ import pytest
from pybind11_tests import unique_ptr_member as m from pybind11_tests import unique_ptr_member as m
def test_cpp_pattern(): def test_make_unique_pointee():
res = m.cpp_pattern() m.to_cout("")
assert res == 10 obj = m.make_unique_pointee()
assert obj.get_int() == 213
m.to_cout("")
def test_pointee_and_ptr_owner(): def test_pointee_and_ptr_owner():
m.to_cout("")
obj = m.pointee() obj = m.pointee()
assert obj.get_int() == 213 assert obj.get_int() == 213
m.ptr_owner(obj) owner = m.ptr_owner(obj)
with pytest.raises(ValueError) as exc_info: with pytest.raises(RuntimeError) as exc_info:
obj.get_int() obj.get_int()
assert str(exc_info.value).startswith("Missing value for wrapped C++ type ") assert str(exc_info.value) == "Invalid object instance"
assert owner.is_owner()
m.to_cout("before give up")
reclaimed = owner.give_up_ownership_via_shared_ptr()
m.to_cout("after give up")
assert not owner.is_owner()
# assert reclaimed.get_int() == 213
del reclaimed
m.to_cout("after del")
m.to_cout("3")
m.to_cout("")
def test_cpp_pattern():
m.to_cout("")
res = m.cpp_pattern()
assert res == 111111
m.to_cout("")