|
| 1 | +// KEEP IN SYNC WITH test_holder_unique_ptr.cpp |
| 2 | + |
| 3 | +#include "pybind11_tests.h" |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <memory> |
| 7 | + |
| 8 | +namespace pybind11_tests { |
| 9 | +namespace holder_shared_ptr { |
| 10 | + |
| 11 | +inline void to_cout(std::string text) { std::cout << text << std::endl; } |
| 12 | + |
| 13 | +class pointee { // NOT copyable. |
| 14 | + public: |
| 15 | + pointee() { to_cout("pointee::pointee()"); } |
| 16 | + |
| 17 | + int get_int() const { |
| 18 | + to_cout("pointee::get_int()"); |
| 19 | + return 213; |
| 20 | + } |
| 21 | + |
| 22 | + ~pointee() { to_cout("~pointee()"); } |
| 23 | + |
| 24 | + private: |
| 25 | + pointee(const pointee &) = delete; |
| 26 | + pointee(pointee &&) = delete; |
| 27 | + pointee &operator=(const pointee &) = delete; |
| 28 | + pointee &operator=(pointee &&) = delete; |
| 29 | +}; |
| 30 | + |
| 31 | +inline std::unique_ptr<pointee> make_unique_pointee() { |
| 32 | + return std::unique_ptr<pointee>(new pointee); |
| 33 | +} |
| 34 | + |
| 35 | +inline std::shared_ptr<pointee> make_shared_pointee() { |
| 36 | + return std::unique_ptr<pointee>(new pointee); |
| 37 | +} |
| 38 | + |
| 39 | +inline int pass_unique_pointee(std::unique_ptr<pointee> ptr) { |
| 40 | + return 4000 + ptr->get_int(); |
| 41 | +} |
| 42 | + |
| 43 | +inline int pass_shared_pointee(std::shared_ptr<pointee> ptr) { |
| 44 | + return 5000 + ptr->get_int(); |
| 45 | +} |
| 46 | + |
| 47 | +inline pointee* get_static_pointee() { |
| 48 | + static pointee cpp_instance; |
| 49 | + return &cpp_instance; |
| 50 | +} |
| 51 | + |
| 52 | +TEST_SUBMODULE(holder_shared_ptr, m) { |
| 53 | + m.def("to_cout", to_cout); |
| 54 | + |
| 55 | + py::class_<pointee, std::shared_ptr<pointee>>(m, "pointee") |
| 56 | + .def(py::init<>()) |
| 57 | + .def("get_int", &pointee::get_int); |
| 58 | + |
| 59 | + m.def("make_unique_pointee", make_unique_pointee); |
| 60 | + m.def("make_shared_pointee", make_shared_pointee); |
| 61 | + // m.def("pass_unique_pointee", pass_unique_pointee); |
| 62 | + m.def("pass_shared_pointee", pass_shared_pointee); |
| 63 | + |
| 64 | + m.def("get_static_pointee", |
| 65 | + get_static_pointee, py::return_value_policy::reference); |
| 66 | +} |
| 67 | + |
| 68 | +} // namespace holder_shared_ptr |
| 69 | +} // namespace pybind11_tests |
0 commit comments