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