2020-11-17 20:19:19 +00:00
|
|
|
#include "pybind11_tests.h"
|
|
|
|
|
2021-01-25 06:15:58 +00:00
|
|
|
#include <pybind11/smart_holder.h>
|
2020-11-20 19:24:40 +00:00
|
|
|
|
2020-11-17 20:19:19 +00:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
namespace pybind11_tests {
|
2021-01-25 07:06:33 +00:00
|
|
|
namespace class_sh_unique_ptr_member {
|
2020-11-17 20:19:19 +00:00
|
|
|
|
|
|
|
class pointee { // NOT copyable.
|
2021-01-24 06:47:53 +00:00
|
|
|
public:
|
2020-11-17 20:19:19 +00:00
|
|
|
pointee() = default;
|
|
|
|
|
2021-01-24 06:47:53 +00:00
|
|
|
int get_int() const { return 213; }
|
2020-11-17 20:19:19 +00:00
|
|
|
|
2021-01-24 06:47:53 +00:00
|
|
|
private:
|
2020-11-17 20:19:19 +00:00
|
|
|
pointee(const pointee &) = delete;
|
2021-01-24 06:47:53 +00:00
|
|
|
pointee(pointee &&) = delete;
|
2020-11-17 20:19:19 +00:00
|
|
|
pointee &operator=(const pointee &) = delete;
|
|
|
|
pointee &operator=(pointee &&) = delete;
|
|
|
|
};
|
|
|
|
|
2020-11-20 02:33:37 +00:00
|
|
|
inline std::unique_ptr<pointee> make_unique_pointee() {
|
|
|
|
return std::unique_ptr<pointee>(new pointee);
|
|
|
|
}
|
|
|
|
|
2020-11-17 20:19:19 +00:00
|
|
|
class ptr_owner {
|
2021-01-24 06:47:53 +00:00
|
|
|
public:
|
2020-11-17 20:19:19 +00:00
|
|
|
explicit ptr_owner(std::unique_ptr<pointee> ptr) : ptr_(std::move(ptr)) {}
|
|
|
|
|
2020-11-20 02:33:37 +00:00
|
|
|
bool is_owner() const { return bool(ptr_); }
|
|
|
|
|
2021-01-24 06:47:53 +00:00
|
|
|
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_); }
|
2020-11-20 02:33:37 +00:00
|
|
|
|
2021-01-24 06:47:53 +00:00
|
|
|
private:
|
2020-11-17 20:19:19 +00:00
|
|
|
std::unique_ptr<pointee> ptr_;
|
|
|
|
};
|
|
|
|
|
2021-01-25 07:06:33 +00:00
|
|
|
} // namespace class_sh_unique_ptr_member
|
2020-11-20 19:24:40 +00:00
|
|
|
} // namespace pybind11_tests
|
|
|
|
|
2021-01-25 07:06:33 +00:00
|
|
|
PYBIND11_SMART_HOLDER_TYPE_CASTERS(pybind11_tests::class_sh_unique_ptr_member::pointee)
|
2021-02-05 05:13:54 +00:00
|
|
|
PYBIND11_SMART_POINTER_HOLDER_TYPE_CASTERS(
|
|
|
|
pybind11_tests::class_sh_unique_ptr_member::ptr_owner,
|
|
|
|
std::unique_ptr<pybind11_tests::class_sh_unique_ptr_member::ptr_owner>)
|
2020-11-20 19:24:40 +00:00
|
|
|
|
|
|
|
namespace pybind11_tests {
|
2021-01-25 07:06:33 +00:00
|
|
|
namespace class_sh_unique_ptr_member {
|
2020-11-20 19:24:40 +00:00
|
|
|
|
2021-01-25 07:06:33 +00:00
|
|
|
TEST_SUBMODULE(class_sh_unique_ptr_member, m) {
|
2021-01-28 15:31:40 +00:00
|
|
|
py::classh<pointee>(m, "pointee").def(py::init<>()).def("get_int", &pointee::get_int);
|
2020-11-17 20:19:19 +00:00
|
|
|
|
2020-11-20 02:33:37 +00:00
|
|
|
m.def("make_unique_pointee", make_unique_pointee);
|
|
|
|
|
2021-01-29 02:34:59 +00:00
|
|
|
// Could also be class_, but can conveniently be used for testing classu.
|
|
|
|
py::classu<ptr_owner>(m, "ptr_owner")
|
2020-11-20 19:24:40 +00:00
|
|
|
.def(py::init<std::unique_ptr<pointee>>(), py::arg("ptr"))
|
2020-11-20 02:33:37 +00:00
|
|
|
.def("is_owner", &ptr_owner::is_owner)
|
2021-01-24 06:47:53 +00:00
|
|
|
.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);
|
2020-11-17 20:19:19 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 07:06:33 +00:00
|
|
|
} // namespace class_sh_unique_ptr_member
|
2020-11-17 20:19:19 +00:00
|
|
|
} // namespace pybind11_tests
|