test_smart_ptr.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /*
  2. tests/test_smart_ptr.cpp -- binding classes with custom reference counting,
  3. implicit conversions between types
  4. Copyright (c) 2016 Wenzel Jakob <[email protected]>
  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. #if defined(_MSC_VER) && _MSC_VER < 1910 // VS 2015's MSVC
  9. # pragma warning(disable: 4702) // unreachable code in system header (xatomic.h(382))
  10. #endif
  11. #include "pybind11_tests.h"
  12. #include "object.h"
  13. // Make pybind aware of the ref-counted wrapper type (s):
  14. // ref<T> is a wrapper for 'Object' which uses intrusive reference counting
  15. // It is always possible to construct a ref<T> from an Object* pointer without
  16. // possible inconsistencies, hence the 'true' argument at the end.
  17. PYBIND11_DECLARE_HOLDER_TYPE(T, ref<T>, true);
  18. // Make pybind11 aware of the non-standard getter member function
  19. namespace pybind11 { namespace detail {
  20. template <typename T>
  21. struct holder_helper<ref<T>> {
  22. static const T *get(const ref<T> &p) { return p.get_ptr(); }
  23. };
  24. } // namespace detail
  25. } // namespace pybind11
  26. // The following is not required anymore for std::shared_ptr, but it should compile without error:
  27. PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
  28. // This is just a wrapper around unique_ptr, but with extra fields to deliberately bloat up the
  29. // holder size to trigger the non-simple-layout internal instance layout for single inheritance with
  30. // large holder type:
  31. template <typename T> class huge_unique_ptr {
  32. std::unique_ptr<T> ptr;
  33. uint64_t padding[10];
  34. public:
  35. huge_unique_ptr(T *p) : ptr(p) {};
  36. T *get() { return ptr.get(); }
  37. };
  38. PYBIND11_DECLARE_HOLDER_TYPE(T, huge_unique_ptr<T>);
  39. // Simple custom holder that works like unique_ptr
  40. template <typename T>
  41. class custom_unique_ptr {
  42. std::unique_ptr<T> impl;
  43. public:
  44. custom_unique_ptr(T* p) : impl(p) { }
  45. T* get() const { return impl.get(); }
  46. T* release_ptr() { return impl.release(); }
  47. };
  48. PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr<T>);
  49. // Simple custom holder that works like shared_ptr and has operator& overload
  50. // To obtain address of an instance of this holder pybind should use std::addressof
  51. // Attempt to get address via operator& may leads to segmentation fault
  52. template <typename T>
  53. class shared_ptr_with_addressof_operator {
  54. std::shared_ptr<T> impl;
  55. public:
  56. shared_ptr_with_addressof_operator( ) = default;
  57. shared_ptr_with_addressof_operator(T* p) : impl(p) { }
  58. T* get() const { return impl.get(); }
  59. T** operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); }
  60. };
  61. PYBIND11_DECLARE_HOLDER_TYPE(T, shared_ptr_with_addressof_operator<T>);
  62. // Simple custom holder that works like unique_ptr and has operator& overload
  63. // To obtain address of an instance of this holder pybind should use std::addressof
  64. // Attempt to get address via operator& may leads to segmentation fault
  65. template <typename T>
  66. class unique_ptr_with_addressof_operator {
  67. std::unique_ptr<T> impl;
  68. public:
  69. unique_ptr_with_addressof_operator() = default;
  70. unique_ptr_with_addressof_operator(T* p) : impl(p) { }
  71. T* get() const { return impl.get(); }
  72. T* release_ptr() { return impl.release(); }
  73. T** operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); }
  74. };
  75. PYBIND11_DECLARE_HOLDER_TYPE(T, unique_ptr_with_addressof_operator<T>);
  76. TEST_SUBMODULE(smart_ptr, m) {
  77. // test_smart_ptr
  78. // Object implementation in `object.h`
  79. py::class_<Object, ref<Object>> obj(m, "Object");
  80. obj.def("getRefCount", &Object::getRefCount);
  81. // Custom object with builtin reference counting (see 'object.h' for the implementation)
  82. class MyObject1 : public Object {
  83. public:
  84. MyObject1(int value) : value(value) { print_created(this, toString()); }
  85. std::string toString() const override { return "MyObject1[" + std::to_string(value) + "]"; }
  86. protected:
  87. ~MyObject1() override { print_destroyed(this); }
  88. private:
  89. int value;
  90. };
  91. py::class_<MyObject1, ref<MyObject1>>(m, "MyObject1", obj)
  92. .def(py::init<int>());
  93. py::implicitly_convertible<py::int_, MyObject1>();
  94. m.def("make_object_1", []() -> Object * { return new MyObject1(1); });
  95. m.def("make_object_2", []() -> ref<Object> { return new MyObject1(2); });
  96. m.def("make_myobject1_1", []() -> MyObject1 * { return new MyObject1(4); });
  97. m.def("make_myobject1_2", []() -> ref<MyObject1> { return new MyObject1(5); });
  98. m.def("print_object_1", [](const Object *obj) { py::print(obj->toString()); });
  99. m.def("print_object_2", [](ref<Object> obj) { py::print(obj->toString()); });
  100. m.def("print_object_3", [](const ref<Object> &obj) { py::print(obj->toString()); });
  101. m.def("print_object_4", [](const ref<Object> *obj) { py::print((*obj)->toString()); });
  102. m.def("print_myobject1_1", [](const MyObject1 *obj) { py::print(obj->toString()); });
  103. m.def("print_myobject1_2", [](ref<MyObject1> obj) { py::print(obj->toString()); });
  104. m.def("print_myobject1_3", [](const ref<MyObject1> &obj) { py::print(obj->toString()); });
  105. m.def("print_myobject1_4", [](const ref<MyObject1> *obj) { py::print((*obj)->toString()); });
  106. // Expose constructor stats for the ref type
  107. m.def("cstats_ref", &ConstructorStats::get<ref_tag>);
  108. // Object managed by a std::shared_ptr<>
  109. class MyObject2 {
  110. public:
  111. MyObject2(const MyObject2 &) = default;
  112. MyObject2(int value) : value(value) { print_created(this, toString()); }
  113. std::string toString() const { return "MyObject2[" + std::to_string(value) + "]"; }
  114. virtual ~MyObject2() { print_destroyed(this); }
  115. private:
  116. int value;
  117. };
  118. py::class_<MyObject2, std::shared_ptr<MyObject2>>(m, "MyObject2")
  119. .def(py::init<int>());
  120. m.def("make_myobject2_1", []() { return new MyObject2(6); });
  121. m.def("make_myobject2_2", []() { return std::make_shared<MyObject2>(7); });
  122. m.def("print_myobject2_1", [](const MyObject2 *obj) { py::print(obj->toString()); });
  123. m.def("print_myobject2_2", [](std::shared_ptr<MyObject2> obj) { py::print(obj->toString()); });
  124. m.def("print_myobject2_3", [](const std::shared_ptr<MyObject2> &obj) { py::print(obj->toString()); });
  125. m.def("print_myobject2_4", [](const std::shared_ptr<MyObject2> *obj) { py::print((*obj)->toString()); });
  126. // Object managed by a std::shared_ptr<>, additionally derives from std::enable_shared_from_this<>
  127. class MyObject3 : public std::enable_shared_from_this<MyObject3> {
  128. public:
  129. MyObject3(const MyObject3 &) = default;
  130. MyObject3(int value) : value(value) { print_created(this, toString()); }
  131. std::string toString() const { return "MyObject3[" + std::to_string(value) + "]"; }
  132. virtual ~MyObject3() { print_destroyed(this); }
  133. private:
  134. int value;
  135. };
  136. py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, "MyObject3")
  137. .def(py::init<int>());
  138. m.def("make_myobject3_1", []() { return new MyObject3(8); });
  139. m.def("make_myobject3_2", []() { return std::make_shared<MyObject3>(9); });
  140. m.def("print_myobject3_1", [](const MyObject3 *obj) { py::print(obj->toString()); });
  141. m.def("print_myobject3_2", [](std::shared_ptr<MyObject3> obj) { py::print(obj->toString()); });
  142. m.def("print_myobject3_3", [](const std::shared_ptr<MyObject3> &obj) { py::print(obj->toString()); });
  143. m.def("print_myobject3_4", [](const std::shared_ptr<MyObject3> *obj) { py::print((*obj)->toString()); });
  144. // test_smart_ptr_refcounting
  145. m.def("test_object1_refcounting", []() {
  146. ref<MyObject1> o = new MyObject1(0);
  147. bool good = o->getRefCount() == 1;
  148. py::object o2 = py::cast(o, py::return_value_policy::reference);
  149. // always request (partial) ownership for objects with intrusive
  150. // reference counting even when using the 'reference' RVP
  151. good &= o->getRefCount() == 2;
  152. return good;
  153. });
  154. // test_unique_nodelete
  155. // Object with a private destructor
  156. class MyObject4;
  157. static std::unordered_set<MyObject4 *> myobject4_instances;
  158. class MyObject4 {
  159. public:
  160. MyObject4(int value) : value{value} {
  161. print_created(this);
  162. myobject4_instances.insert(this);
  163. }
  164. int value;
  165. static void cleanupAllInstances() {
  166. auto tmp = std::move(myobject4_instances);
  167. myobject4_instances.clear();
  168. for (auto o : tmp)
  169. delete o;
  170. }
  171. private:
  172. ~MyObject4() {
  173. myobject4_instances.erase(this);
  174. print_destroyed(this);
  175. }
  176. };
  177. py::class_<MyObject4, std::unique_ptr<MyObject4, py::nodelete>>(m, "MyObject4")
  178. .def(py::init<int>())
  179. .def_readwrite("value", &MyObject4::value)
  180. .def_static("cleanup_all_instances", &MyObject4::cleanupAllInstances);
  181. // test_unique_deleter
  182. // Object with std::unique_ptr<T, D> where D is not matching the base class
  183. // Object with a protected destructor
  184. class MyObject4a;
  185. static std::unordered_set<MyObject4a *> myobject4a_instances;
  186. class MyObject4a {
  187. public:
  188. MyObject4a(int i) {
  189. value = i;
  190. print_created(this);
  191. myobject4a_instances.insert(this);
  192. };
  193. int value;
  194. static void cleanupAllInstances() {
  195. auto tmp = std::move(myobject4a_instances);
  196. myobject4a_instances.clear();
  197. for (auto o : tmp)
  198. delete o;
  199. }
  200. protected:
  201. virtual ~MyObject4a() {
  202. myobject4a_instances.erase(this);
  203. print_destroyed(this);
  204. }
  205. };
  206. py::class_<MyObject4a, std::unique_ptr<MyObject4a, py::nodelete>>(m, "MyObject4a")
  207. .def(py::init<int>())
  208. .def_readwrite("value", &MyObject4a::value)
  209. .def_static("cleanup_all_instances", &MyObject4a::cleanupAllInstances);
  210. // Object derived but with public destructor and no Deleter in default holder
  211. class MyObject4b : public MyObject4a {
  212. public:
  213. MyObject4b(int i) : MyObject4a(i) { print_created(this); }
  214. ~MyObject4b() override { print_destroyed(this); }
  215. };
  216. py::class_<MyObject4b, MyObject4a>(m, "MyObject4b")
  217. .def(py::init<int>());
  218. // test_large_holder
  219. class MyObject5 { // managed by huge_unique_ptr
  220. public:
  221. MyObject5(int value) : value{value} { print_created(this); }
  222. ~MyObject5() { print_destroyed(this); }
  223. int value;
  224. };
  225. py::class_<MyObject5, huge_unique_ptr<MyObject5>>(m, "MyObject5")
  226. .def(py::init<int>())
  227. .def_readwrite("value", &MyObject5::value);
  228. // test_shared_ptr_and_references
  229. struct SharedPtrRef {
  230. struct A {
  231. A() { print_created(this); }
  232. A(const A &) { print_copy_created(this); }
  233. A(A &&) { print_move_created(this); }
  234. ~A() { print_destroyed(this); }
  235. };
  236. A value = {};
  237. std::shared_ptr<A> shared = std::make_shared<A>();
  238. };
  239. using A = SharedPtrRef::A;
  240. py::class_<A, std::shared_ptr<A>>(m, "A");
  241. py::class_<SharedPtrRef>(m, "SharedPtrRef")
  242. .def(py::init<>())
  243. .def_readonly("ref", &SharedPtrRef::value)
  244. .def_property_readonly("copy", [](const SharedPtrRef &s) { return s.value; },
  245. py::return_value_policy::copy)
  246. .def_readonly("holder_ref", &SharedPtrRef::shared)
  247. .def_property_readonly("holder_copy", [](const SharedPtrRef &s) { return s.shared; },
  248. py::return_value_policy::copy)
  249. .def("set_ref", [](SharedPtrRef &, const A &) { return true; })
  250. .def("set_holder", [](SharedPtrRef &, std::shared_ptr<A>) { return true; });
  251. // test_shared_ptr_from_this_and_references
  252. struct SharedFromThisRef {
  253. struct B : std::enable_shared_from_this<B> {
  254. B() { print_created(this); }
  255. B(const B &) : std::enable_shared_from_this<B>() { print_copy_created(this); }
  256. B(B &&) : std::enable_shared_from_this<B>() { print_move_created(this); }
  257. ~B() { print_destroyed(this); }
  258. };
  259. B value = {};
  260. std::shared_ptr<B> shared = std::make_shared<B>();
  261. };
  262. using B = SharedFromThisRef::B;
  263. py::class_<B, std::shared_ptr<B>>(m, "B");
  264. py::class_<SharedFromThisRef>(m, "SharedFromThisRef")
  265. .def(py::init<>())
  266. .def_readonly("bad_wp", &SharedFromThisRef::value)
  267. .def_property_readonly("ref", [](const SharedFromThisRef &s) -> const B & { return *s.shared; })
  268. .def_property_readonly("copy", [](const SharedFromThisRef &s) { return s.value; },
  269. py::return_value_policy::copy)
  270. .def_readonly("holder_ref", &SharedFromThisRef::shared)
  271. .def_property_readonly("holder_copy", [](const SharedFromThisRef &s) { return s.shared; },
  272. py::return_value_policy::copy)
  273. .def("set_ref", [](SharedFromThisRef &, const B &) { return true; })
  274. .def("set_holder", [](SharedFromThisRef &, std::shared_ptr<B>) { return true; });
  275. // Issue #865: shared_from_this doesn't work with virtual inheritance
  276. struct SharedFromThisVBase : std::enable_shared_from_this<SharedFromThisVBase> {
  277. SharedFromThisVBase() = default;
  278. SharedFromThisVBase(const SharedFromThisVBase &) = default;
  279. virtual ~SharedFromThisVBase() = default;
  280. };
  281. struct SharedFromThisVirt : virtual SharedFromThisVBase {};
  282. static std::shared_ptr<SharedFromThisVirt> sft(new SharedFromThisVirt());
  283. py::class_<SharedFromThisVirt, std::shared_ptr<SharedFromThisVirt>>(m, "SharedFromThisVirt")
  284. .def_static("get", []() { return sft.get(); });
  285. // test_move_only_holder
  286. struct C {
  287. C() { print_created(this); }
  288. ~C() { print_destroyed(this); }
  289. };
  290. py::class_<C, custom_unique_ptr<C>>(m, "TypeWithMoveOnlyHolder")
  291. .def_static("make", []() { return custom_unique_ptr<C>(new C); })
  292. .def_static("make_as_object", []() { return py::cast(custom_unique_ptr<C>(new C)); });
  293. // test_holder_with_addressof_operator
  294. struct TypeForHolderWithAddressOf {
  295. TypeForHolderWithAddressOf() { print_created(this); }
  296. TypeForHolderWithAddressOf(const TypeForHolderWithAddressOf &) { print_copy_created(this); }
  297. TypeForHolderWithAddressOf(TypeForHolderWithAddressOf &&) { print_move_created(this); }
  298. ~TypeForHolderWithAddressOf() { print_destroyed(this); }
  299. std::string toString() const {
  300. return "TypeForHolderWithAddressOf[" + std::to_string(value) + "]";
  301. }
  302. int value = 42;
  303. };
  304. using HolderWithAddressOf = shared_ptr_with_addressof_operator<TypeForHolderWithAddressOf>;
  305. py::class_<TypeForHolderWithAddressOf, HolderWithAddressOf>(m, "TypeForHolderWithAddressOf")
  306. .def_static("make", []() { return HolderWithAddressOf(new TypeForHolderWithAddressOf); })
  307. .def("get", [](const HolderWithAddressOf &self) { return self.get(); })
  308. .def("print_object_1", [](const TypeForHolderWithAddressOf *obj) { py::print(obj->toString()); })
  309. .def("print_object_2", [](HolderWithAddressOf obj) { py::print(obj.get()->toString()); })
  310. .def("print_object_3", [](const HolderWithAddressOf &obj) { py::print(obj.get()->toString()); })
  311. .def("print_object_4", [](const HolderWithAddressOf *obj) { py::print((*obj).get()->toString()); });
  312. // test_move_only_holder_with_addressof_operator
  313. struct TypeForMoveOnlyHolderWithAddressOf {
  314. TypeForMoveOnlyHolderWithAddressOf(int value) : value{value} { print_created(this); }
  315. ~TypeForMoveOnlyHolderWithAddressOf() { print_destroyed(this); }
  316. std::string toString() const {
  317. return "MoveOnlyHolderWithAddressOf[" + std::to_string(value) + "]";
  318. }
  319. int value;
  320. };
  321. using MoveOnlyHolderWithAddressOf = unique_ptr_with_addressof_operator<TypeForMoveOnlyHolderWithAddressOf>;
  322. py::class_<TypeForMoveOnlyHolderWithAddressOf, MoveOnlyHolderWithAddressOf>(m, "TypeForMoveOnlyHolderWithAddressOf")
  323. .def_static("make", []() { return MoveOnlyHolderWithAddressOf(new TypeForMoveOnlyHolderWithAddressOf(0)); })
  324. .def_readwrite("value", &TypeForMoveOnlyHolderWithAddressOf::value)
  325. .def("print_object", [](const TypeForMoveOnlyHolderWithAddressOf *obj) { py::print(obj->toString()); });
  326. // test_smart_ptr_from_default
  327. struct HeldByDefaultHolder { };
  328. py::class_<HeldByDefaultHolder>(m, "HeldByDefaultHolder")
  329. .def(py::init<>())
  330. .def_static("load_shared_ptr", [](std::shared_ptr<HeldByDefaultHolder>) {});
  331. // test_shared_ptr_gc
  332. // #187: issue involving std::shared_ptr<> return value policy & garbage collection
  333. struct ElementBase {
  334. virtual ~ElementBase() = default; /* Force creation of virtual table */
  335. ElementBase() = default;
  336. ElementBase(const ElementBase&) = delete;
  337. };
  338. py::class_<ElementBase, std::shared_ptr<ElementBase>>(m, "ElementBase");
  339. struct ElementA : ElementBase {
  340. ElementA(int v) : v(v) { }
  341. int value() { return v; }
  342. int v;
  343. };
  344. py::class_<ElementA, ElementBase, std::shared_ptr<ElementA>>(m, "ElementA")
  345. .def(py::init<int>())
  346. .def("value", &ElementA::value);
  347. struct ElementList {
  348. void add(std::shared_ptr<ElementBase> e) { l.push_back(e); }
  349. std::vector<std::shared_ptr<ElementBase>> l;
  350. };
  351. py::class_<ElementList, std::shared_ptr<ElementList>>(m, "ElementList")
  352. .def(py::init<>())
  353. .def("add", &ElementList::add)
  354. .def("get", [](ElementList &el) {
  355. py::list list;
  356. for (auto &e : el.l)
  357. list.append(py::cast(e));
  358. return list;
  359. });
  360. }