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.

274 lines
9.8 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  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. // ref<T> is a wrapper for 'Object' which uses intrusive reference counting
  69. // It is always possible to construct a ref<T> from an Object* pointer without
  70. // possible incosistencies, hence the 'true' argument at the end.
  71. PYBIND11_DECLARE_HOLDER_TYPE(T, ref<T>, true);
  72. PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>); // Not required any more for std::shared_ptr,
  73. // but it should compile without error
  74. // Make pybind11 aware of the non-standard getter member function
  75. namespace pybind11 { namespace detail {
  76. template <typename T>
  77. struct holder_helper<ref<T>> {
  78. static const T *get(const ref<T> &p) { return p.get_ptr(); }
  79. };
  80. }}
  81. Object *make_object_1() { return new MyObject1(1); }
  82. ref<Object> make_object_2() { return new MyObject1(2); }
  83. MyObject1 *make_myobject1_1() { return new MyObject1(4); }
  84. ref<MyObject1> make_myobject1_2() { return new MyObject1(5); }
  85. MyObject2 *make_myobject2_1() { return new MyObject2(6); }
  86. std::shared_ptr<MyObject2> make_myobject2_2() { return std::make_shared<MyObject2>(7); }
  87. MyObject3 *make_myobject3_1() { return new MyObject3(8); }
  88. std::shared_ptr<MyObject3> make_myobject3_2() { return std::make_shared<MyObject3>(9); }
  89. void print_object_1(const Object *obj) { py::print(obj->toString()); }
  90. void print_object_2(ref<Object> obj) { py::print(obj->toString()); }
  91. void print_object_3(const ref<Object> &obj) { py::print(obj->toString()); }
  92. void print_object_4(const ref<Object> *obj) { py::print((*obj)->toString()); }
  93. void print_myobject1_1(const MyObject1 *obj) { py::print(obj->toString()); }
  94. void print_myobject1_2(ref<MyObject1> obj) { py::print(obj->toString()); }
  95. void print_myobject1_3(const ref<MyObject1> &obj) { py::print(obj->toString()); }
  96. void print_myobject1_4(const ref<MyObject1> *obj) { py::print((*obj)->toString()); }
  97. void print_myobject2_1(const MyObject2 *obj) { py::print(obj->toString()); }
  98. void print_myobject2_2(std::shared_ptr<MyObject2> obj) { py::print(obj->toString()); }
  99. void print_myobject2_3(const std::shared_ptr<MyObject2> &obj) { py::print(obj->toString()); }
  100. void print_myobject2_4(const std::shared_ptr<MyObject2> *obj) { py::print((*obj)->toString()); }
  101. void print_myobject3_1(const MyObject3 *obj) { py::print(obj->toString()); }
  102. void print_myobject3_2(std::shared_ptr<MyObject3> obj) { py::print(obj->toString()); }
  103. void print_myobject3_3(const std::shared_ptr<MyObject3> &obj) { py::print(obj->toString()); }
  104. void print_myobject3_4(const std::shared_ptr<MyObject3> *obj) { py::print((*obj)->toString()); }
  105. test_initializer smart_ptr([](py::module &m) {
  106. py::class_<Object, ref<Object>> obj(m, "Object");
  107. obj.def("getRefCount", &Object::getRefCount);
  108. py::class_<MyObject1, ref<MyObject1>>(m, "MyObject1", obj)
  109. .def(py::init<int>());
  110. m.def("test_object1_refcounting",
  111. []() -> bool {
  112. ref<MyObject1> o = new MyObject1(0);
  113. bool good = o->getRefCount() == 1;
  114. py::object o2 = py::cast(o, py::return_value_policy::reference);
  115. // always request (partial) ownership for objects with intrusive
  116. // reference counting even when using the 'reference' RVP
  117. good &= o->getRefCount() == 2;
  118. return good;
  119. }
  120. );
  121. m.def("make_object_1", &make_object_1);
  122. m.def("make_object_2", &make_object_2);
  123. m.def("make_myobject1_1", &make_myobject1_1);
  124. m.def("make_myobject1_2", &make_myobject1_2);
  125. m.def("print_object_1", &print_object_1);
  126. m.def("print_object_2", &print_object_2);
  127. m.def("print_object_3", &print_object_3);
  128. m.def("print_object_4", &print_object_4);
  129. m.def("print_myobject1_1", &print_myobject1_1);
  130. m.def("print_myobject1_2", &print_myobject1_2);
  131. m.def("print_myobject1_3", &print_myobject1_3);
  132. m.def("print_myobject1_4", &print_myobject1_4);
  133. py::class_<MyObject2, std::shared_ptr<MyObject2>>(m, "MyObject2")
  134. .def(py::init<int>());
  135. m.def("make_myobject2_1", &make_myobject2_1);
  136. m.def("make_myobject2_2", &make_myobject2_2);
  137. m.def("print_myobject2_1", &print_myobject2_1);
  138. m.def("print_myobject2_2", &print_myobject2_2);
  139. m.def("print_myobject2_3", &print_myobject2_3);
  140. m.def("print_myobject2_4", &print_myobject2_4);
  141. py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, "MyObject3")
  142. .def(py::init<int>());
  143. m.def("make_myobject3_1", &make_myobject3_1);
  144. m.def("make_myobject3_2", &make_myobject3_2);
  145. m.def("print_myobject3_1", &print_myobject3_1);
  146. m.def("print_myobject3_2", &print_myobject3_2);
  147. m.def("print_myobject3_3", &print_myobject3_3);
  148. m.def("print_myobject3_4", &print_myobject3_4);
  149. py::class_<MyObject4, std::unique_ptr<MyObject4, py::nodelete>>(m, "MyObject4")
  150. .def(py::init<int>())
  151. .def_readwrite("value", &MyObject4::value);
  152. py::implicitly_convertible<py::int_, MyObject1>();
  153. // Expose constructor stats for the ref type
  154. m.def("cstats_ref", &ConstructorStats::get<ref_tag>);
  155. });
  156. struct SharedPtrRef {
  157. struct A {
  158. A() { print_created(this); }
  159. A(const A &) { print_copy_created(this); }
  160. A(A &&) { print_move_created(this); }
  161. ~A() { print_destroyed(this); }
  162. };
  163. A value = {};
  164. std::shared_ptr<A> shared = std::make_shared<A>();
  165. };
  166. struct SharedFromThisRef {
  167. struct B : std::enable_shared_from_this<B> {
  168. B() { print_created(this); }
  169. B(const B &) : std::enable_shared_from_this<B>() { print_copy_created(this); }
  170. B(B &&) : std::enable_shared_from_this<B>() { print_move_created(this); }
  171. ~B() { print_destroyed(this); }
  172. };
  173. B value = {};
  174. std::shared_ptr<B> shared = std::make_shared<B>();
  175. };
  176. template <typename T>
  177. class CustomUniquePtr {
  178. std::unique_ptr<T> impl;
  179. public:
  180. CustomUniquePtr(T* p) : impl(p) { }
  181. T* get() const { return impl.get(); }
  182. T* release_ptr() { return impl.release(); }
  183. };
  184. PYBIND11_DECLARE_HOLDER_TYPE(T, CustomUniquePtr<T>);
  185. test_initializer smart_ptr_and_references([](py::module &pm) {
  186. auto m = pm.def_submodule("smart_ptr");
  187. using A = SharedPtrRef::A;
  188. py::class_<A, std::shared_ptr<A>>(m, "A");
  189. py::class_<SharedPtrRef>(m, "SharedPtrRef")
  190. .def(py::init<>())
  191. .def_readonly("ref", &SharedPtrRef::value)
  192. .def_property_readonly("copy", [](const SharedPtrRef &s) { return s.value; },
  193. py::return_value_policy::copy)
  194. .def_readonly("holder_ref", &SharedPtrRef::shared)
  195. .def_property_readonly("holder_copy", [](const SharedPtrRef &s) { return s.shared; },
  196. py::return_value_policy::copy)
  197. .def("set_ref", [](SharedPtrRef &, const A &) { return true; })
  198. .def("set_holder", [](SharedPtrRef &, std::shared_ptr<A>) { return true; });
  199. using B = SharedFromThisRef::B;
  200. py::class_<B, std::shared_ptr<B>>(m, "B");
  201. py::class_<SharedFromThisRef>(m, "SharedFromThisRef")
  202. .def(py::init<>())
  203. .def_readonly("bad_wp", &SharedFromThisRef::value)
  204. .def_property_readonly("ref", [](const SharedFromThisRef &s) -> const B & { return *s.shared; })
  205. .def_property_readonly("copy", [](const SharedFromThisRef &s) { return s.value; },
  206. py::return_value_policy::copy)
  207. .def_readonly("holder_ref", &SharedFromThisRef::shared)
  208. .def_property_readonly("holder_copy", [](const SharedFromThisRef &s) { return s.shared; },
  209. py::return_value_policy::copy)
  210. .def("set_ref", [](SharedFromThisRef &, const B &) { return true; })
  211. .def("set_holder", [](SharedFromThisRef &, std::shared_ptr<B>) { return true; });
  212. struct C {
  213. C() { print_created(this); }
  214. ~C() { print_destroyed(this); }
  215. };
  216. py::class_<C, CustomUniquePtr<C>>(m, "TypeWithMoveOnlyHolder")
  217. .def_static("make", []() { return CustomUniquePtr<C>(new C); });
  218. struct HeldByDefaultHolder { };
  219. py::class_<HeldByDefaultHolder>(m, "HeldByDefaultHolder")
  220. .def(py::init<>())
  221. .def_static("load_shared_ptr", [](std::shared_ptr<HeldByDefaultHolder>) {});
  222. });