You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

224 lines
8.2 KiB

  1. /*
  2. tests/test_smart_ptr.cpp -- binding classes with custom reference counting,
  3. implicit conversions between types
  4. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #include "pybind11_tests.h"
  9. #include "object.h"
  10. /// Custom object with builtin reference counting (see 'object.h' for the implementation)
  11. class MyObject1 : public Object {
  12. public:
  13. MyObject1(int value) : value(value) {
  14. print_created(this, toString());
  15. }
  16. std::string toString() const {
  17. return "MyObject1[" + std::to_string(value) + "]";
  18. }
  19. protected:
  20. virtual ~MyObject1() {
  21. print_destroyed(this);
  22. }
  23. private:
  24. int value;
  25. };
  26. /// Object managed by a std::shared_ptr<>
  27. class MyObject2 {
  28. public:
  29. MyObject2(int value) : value(value) {
  30. print_created(this, toString());
  31. }
  32. std::string toString() const {
  33. return "MyObject2[" + std::to_string(value) + "]";
  34. }
  35. virtual ~MyObject2() {
  36. print_destroyed(this);
  37. }
  38. private:
  39. int value;
  40. };
  41. /// Object managed by a std::shared_ptr<>, additionally derives from std::enable_shared_from_this<>
  42. class MyObject3 : public std::enable_shared_from_this<MyObject3> {
  43. public:
  44. MyObject3(int value) : value(value) {
  45. print_created(this, toString());
  46. }
  47. std::string toString() const {
  48. return "MyObject3[" + std::to_string(value) + "]";
  49. }
  50. virtual ~MyObject3() {
  51. print_destroyed(this);
  52. }
  53. private:
  54. int value;
  55. };
  56. class MyObject4 {
  57. public:
  58. MyObject4(int value) : value{value} {
  59. print_created(this);
  60. }
  61. int value;
  62. private:
  63. ~MyObject4() {
  64. print_destroyed(this);
  65. }
  66. };
  67. /// Make pybind aware of the ref-counted wrapper type (s)
  68. PYBIND11_DECLARE_HOLDER_TYPE(T, ref<T>); // Required for custom holder type
  69. PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>); // Not required any more for std::shared_ptr,
  70. // but it should compile without error
  71. Object *make_object_1() { return new MyObject1(1); }
  72. ref<Object> make_object_2() { return new MyObject1(2); }
  73. MyObject1 *make_myobject1_1() { return new MyObject1(4); }
  74. ref<MyObject1> make_myobject1_2() { return new MyObject1(5); }
  75. MyObject2 *make_myobject2_1() { return new MyObject2(6); }
  76. std::shared_ptr<MyObject2> make_myobject2_2() { return std::make_shared<MyObject2>(7); }
  77. MyObject3 *make_myobject3_1() { return new MyObject3(8); }
  78. std::shared_ptr<MyObject3> make_myobject3_2() { return std::make_shared<MyObject3>(9); }
  79. void print_object_1(const Object *obj) { py::print(obj->toString()); }
  80. void print_object_2(ref<Object> obj) { py::print(obj->toString()); }
  81. void print_object_3(const ref<Object> &obj) { py::print(obj->toString()); }
  82. void print_object_4(const ref<Object> *obj) { py::print((*obj)->toString()); }
  83. void print_myobject1_1(const MyObject1 *obj) { py::print(obj->toString()); }
  84. void print_myobject1_2(ref<MyObject1> obj) { py::print(obj->toString()); }
  85. void print_myobject1_3(const ref<MyObject1> &obj) { py::print(obj->toString()); }
  86. void print_myobject1_4(const ref<MyObject1> *obj) { py::print((*obj)->toString()); }
  87. void print_myobject2_1(const MyObject2 *obj) { py::print(obj->toString()); }
  88. void print_myobject2_2(std::shared_ptr<MyObject2> obj) { py::print(obj->toString()); }
  89. void print_myobject2_3(const std::shared_ptr<MyObject2> &obj) { py::print(obj->toString()); }
  90. void print_myobject2_4(const std::shared_ptr<MyObject2> *obj) { py::print((*obj)->toString()); }
  91. void print_myobject3_1(const MyObject3 *obj) { py::print(obj->toString()); }
  92. void print_myobject3_2(std::shared_ptr<MyObject3> obj) { py::print(obj->toString()); }
  93. void print_myobject3_3(const std::shared_ptr<MyObject3> &obj) { py::print(obj->toString()); }
  94. void print_myobject3_4(const std::shared_ptr<MyObject3> *obj) { py::print((*obj)->toString()); }
  95. test_initializer smart_ptr([](py::module &m) {
  96. py::class_<Object, ref<Object>> obj(m, "Object");
  97. obj.def("getRefCount", &Object::getRefCount);
  98. py::class_<MyObject1, ref<MyObject1>>(m, "MyObject1", obj)
  99. .def(py::init<int>());
  100. m.def("make_object_1", &make_object_1);
  101. m.def("make_object_2", &make_object_2);
  102. m.def("make_myobject1_1", &make_myobject1_1);
  103. m.def("make_myobject1_2", &make_myobject1_2);
  104. m.def("print_object_1", &print_object_1);
  105. m.def("print_object_2", &print_object_2);
  106. m.def("print_object_3", &print_object_3);
  107. m.def("print_object_4", &print_object_4);
  108. m.def("print_myobject1_1", &print_myobject1_1);
  109. m.def("print_myobject1_2", &print_myobject1_2);
  110. m.def("print_myobject1_3", &print_myobject1_3);
  111. m.def("print_myobject1_4", &print_myobject1_4);
  112. py::class_<MyObject2, std::shared_ptr<MyObject2>>(m, "MyObject2")
  113. .def(py::init<int>());
  114. m.def("make_myobject2_1", &make_myobject2_1);
  115. m.def("make_myobject2_2", &make_myobject2_2);
  116. m.def("print_myobject2_1", &print_myobject2_1);
  117. m.def("print_myobject2_2", &print_myobject2_2);
  118. m.def("print_myobject2_3", &print_myobject2_3);
  119. m.def("print_myobject2_4", &print_myobject2_4);
  120. py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, "MyObject3")
  121. .def(py::init<int>());
  122. m.def("make_myobject3_1", &make_myobject3_1);
  123. m.def("make_myobject3_2", &make_myobject3_2);
  124. m.def("print_myobject3_1", &print_myobject3_1);
  125. m.def("print_myobject3_2", &print_myobject3_2);
  126. m.def("print_myobject3_3", &print_myobject3_3);
  127. m.def("print_myobject3_4", &print_myobject3_4);
  128. py::class_<MyObject4, std::unique_ptr<MyObject4, py::nodelete>>(m, "MyObject4")
  129. .def(py::init<int>())
  130. .def_readwrite("value", &MyObject4::value);
  131. py::implicitly_convertible<py::int_, MyObject1>();
  132. // Expose constructor stats for the ref type
  133. m.def("cstats_ref", &ConstructorStats::get<ref_tag>);
  134. });
  135. struct SharedPtrRef {
  136. struct A {
  137. A() { print_created(this); }
  138. A(const A &) { print_copy_created(this); }
  139. A(A &&) { print_move_created(this); }
  140. ~A() { print_destroyed(this); }
  141. };
  142. A value = {};
  143. std::shared_ptr<A> shared = std::make_shared<A>();
  144. };
  145. struct SharedFromThisRef {
  146. struct B : std::enable_shared_from_this<B> {
  147. B() { print_created(this); }
  148. B(const B &) : std::enable_shared_from_this<B>() { print_copy_created(this); }
  149. B(B &&) : std::enable_shared_from_this<B>() { print_move_created(this); }
  150. ~B() { print_destroyed(this); }
  151. };
  152. B value = {};
  153. std::shared_ptr<B> shared = std::make_shared<B>();
  154. };
  155. test_initializer smart_ptr_and_references([](py::module &pm) {
  156. auto m = pm.def_submodule("smart_ptr");
  157. using A = SharedPtrRef::A;
  158. py::class_<A, std::shared_ptr<A>>(m, "A");
  159. py::class_<SharedPtrRef>(m, "SharedPtrRef")
  160. .def(py::init<>())
  161. .def_readonly("ref", &SharedPtrRef::value)
  162. .def_property_readonly("copy", [](const SharedPtrRef &s) { return s.value; },
  163. py::return_value_policy::copy)
  164. .def_readonly("holder_ref", &SharedPtrRef::shared)
  165. .def_property_readonly("holder_copy", [](const SharedPtrRef &s) { return s.shared; },
  166. py::return_value_policy::copy)
  167. .def("set_ref", [](SharedPtrRef &, const A &) { return true; })
  168. .def("set_holder", [](SharedPtrRef &, std::shared_ptr<A>) { return true; });
  169. using B = SharedFromThisRef::B;
  170. py::class_<B, std::shared_ptr<B>>(m, "B");
  171. py::class_<SharedFromThisRef>(m, "SharedFromThisRef")
  172. .def(py::init<>())
  173. .def_readonly("bad_wp", &SharedFromThisRef::value)
  174. .def_property_readonly("ref", [](const SharedFromThisRef &s) -> const B & { return *s.shared; })
  175. .def_property_readonly("copy", [](const SharedFromThisRef &s) { return s.value; },
  176. py::return_value_policy::copy)
  177. .def_readonly("holder_ref", &SharedFromThisRef::shared)
  178. .def_property_readonly("holder_copy", [](const SharedFromThisRef &s) { return s.shared; },
  179. py::return_value_policy::copy)
  180. .def("set_ref", [](SharedFromThisRef &, const B &) { return true; })
  181. .def("set_holder", [](SharedFromThisRef &, std::shared_ptr<B>) { return true; });
  182. });