test_smart_ptr.cpp 18 KB

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