test_smart_ptr.cpp
Go to the documentation of this file.
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 #include "object.h"
12 #include "pybind11_tests.h"
13 
14 namespace {
15 
16 // This is just a wrapper around unique_ptr, but with extra fields to deliberately bloat up the
17 // holder size to trigger the non-simple-layout internal instance layout for single inheritance
18 // with large holder type:
19 template <typename T>
20 class huge_unique_ptr {
21  std::unique_ptr<T> ptr;
22  uint64_t padding[10];
23 
24 public:
25  explicit huge_unique_ptr(T *p) : ptr(p) {}
26  T *get() { return ptr.get(); }
27 };
28 
29 // Simple custom holder that works like unique_ptr
30 template <typename T>
31 class custom_unique_ptr {
32  std::unique_ptr<T> impl;
33 
34 public:
35  explicit custom_unique_ptr(T *p) : impl(p) {}
36  T *get() const { return impl.get(); }
37  T *release_ptr() { return impl.release(); }
38 };
39 
40 // Simple custom holder that works like shared_ptr and has operator& overload
41 // To obtain address of an instance of this holder pybind should use std::addressof
42 // Attempt to get address via operator& may leads to segmentation fault
43 template <typename T>
44 class shared_ptr_with_addressof_operator {
45  std::shared_ptr<T> impl;
46 
47 public:
48  shared_ptr_with_addressof_operator() = default;
49  explicit shared_ptr_with_addressof_operator(T *p) : impl(p) {}
50  T *get() const { return impl.get(); }
51  T **operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); }
52 };
53 
54 // Simple custom holder that works like unique_ptr and has operator& overload
55 // To obtain address of an instance of this holder pybind should use std::addressof
56 // Attempt to get address via operator& may leads to segmentation fault
57 template <typename T>
58 class unique_ptr_with_addressof_operator {
59  std::unique_ptr<T> impl;
60 
61 public:
62  unique_ptr_with_addressof_operator() = default;
63  explicit unique_ptr_with_addressof_operator(T *p) : impl(p) {}
64  T *get() const { return impl.get(); }
65  T *release_ptr() { return impl.release(); }
66  T **operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); }
67 };
68 
69 // Custom object with builtin reference counting (see 'object.h' for the implementation)
70 class MyObject1 : public Object {
71 public:
72  explicit MyObject1(int value) : value(value) { print_created(this, toString()); }
73  std::string toString() const override { return "MyObject1[" + std::to_string(value) + "]"; }
74 
75 protected:
76  ~MyObject1() override { print_destroyed(this); }
77 
78 private:
79  int value;
80 };
81 
82 // Object managed by a std::shared_ptr<>
83 class MyObject2 {
84 public:
85  MyObject2(const MyObject2 &) = default;
86  explicit MyObject2(int value) : value(value) { print_created(this, toString()); }
87  std::string toString() const { return "MyObject2[" + std::to_string(value) + "]"; }
88  virtual ~MyObject2() { print_destroyed(this); }
89 
90 private:
91  int value;
92 };
93 
94 // Object managed by a std::shared_ptr<>, additionally derives from std::enable_shared_from_this<>
95 class MyObject3 : public std::enable_shared_from_this<MyObject3> {
96 public:
97  MyObject3(const MyObject3 &) = default;
98  explicit MyObject3(int value) : value(value) { print_created(this, toString()); }
99  std::string toString() const { return "MyObject3[" + std::to_string(value) + "]"; }
100  virtual ~MyObject3() { print_destroyed(this); }
101 
102 private:
103  int value;
104 };
105 
106 template <typename T>
107 std::unordered_set<T *> &pointer_set() {
108  // https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables
109  static auto singleton = new std::unordered_set<T *>();
110  return *singleton;
111 }
112 
113 // test_unique_nodelete
114 // Object with a private destructor
115 class MyObject4 {
116 public:
117  explicit MyObject4(int value) : value{value} {
118  print_created(this);
119  pointer_set<MyObject4>().insert(this);
120  }
121  int value;
122 
123  static void cleanupAllInstances() {
124  auto tmp = std::move(pointer_set<MyObject4>());
125  pointer_set<MyObject4>().clear();
126  for (auto *o : tmp) {
127  delete o;
128  }
129  }
130 
131 private:
132  ~MyObject4() {
133  pointer_set<MyObject4>().erase(this);
134  print_destroyed(this);
135  }
136 };
137 
138 // test_unique_deleter
139 // Object with std::unique_ptr<T, D> where D is not matching the base class
140 // Object with a protected destructor
141 class MyObject4a {
142 public:
143  explicit MyObject4a(int i) : value{i} {
144  print_created(this);
145  pointer_set<MyObject4a>().insert(this);
146  };
147  int value;
148 
149  static void cleanupAllInstances() {
150  auto tmp = std::move(pointer_set<MyObject4a>());
151  pointer_set<MyObject4a>().clear();
152  for (auto *o : tmp) {
153  delete o;
154  }
155  }
156 
157 protected:
158  virtual ~MyObject4a() {
159  pointer_set<MyObject4a>().erase(this);
160  print_destroyed(this);
161  }
162 };
163 
164 // Object derived but with public destructor and no Deleter in default holder
165 class MyObject4b : public MyObject4a {
166 public:
167  explicit MyObject4b(int i) : MyObject4a(i) { print_created(this); }
168  ~MyObject4b() override { print_destroyed(this); }
169 };
170 
171 // test_large_holder
172 class MyObject5 { // managed by huge_unique_ptr
173 public:
174  explicit MyObject5(int value) : value{value} { print_created(this); }
175  ~MyObject5() { print_destroyed(this); }
176  int value;
177 };
178 
179 // test_shared_ptr_and_references
180 struct SharedPtrRef {
181  struct A {
182  A() { print_created(this); }
183  A(const A &) { print_copy_created(this); }
184  A(A &&) noexcept { print_move_created(this); }
185  ~A() { print_destroyed(this); }
186  };
187 
188  A value = {};
189  std::shared_ptr<A> shared = std::make_shared<A>();
190 };
191 
192 // test_shared_ptr_from_this_and_references
193 struct SharedFromThisRef {
194  struct B : std::enable_shared_from_this<B> {
195  B() { print_created(this); }
196  // NOLINTNEXTLINE(bugprone-copy-constructor-init)
197  B(const B &) : std::enable_shared_from_this<B>() { print_copy_created(this); }
198  B(B &&) noexcept : std::enable_shared_from_this<B>() { print_move_created(this); }
199  ~B() { print_destroyed(this); }
200  };
201 
202  B value = {};
203  std::shared_ptr<B> shared = std::make_shared<B>();
204 };
205 
206 // Issue #865: shared_from_this doesn't work with virtual inheritance
207 struct SharedFromThisVBase : std::enable_shared_from_this<SharedFromThisVBase> {
208  SharedFromThisVBase() = default;
209  SharedFromThisVBase(const SharedFromThisVBase &) = default;
210  virtual ~SharedFromThisVBase() = default;
211 };
212 struct SharedFromThisVirt : virtual SharedFromThisVBase {};
213 
214 // test_move_only_holder
215 struct C {
216  C() { print_created(this); }
217  ~C() { print_destroyed(this); }
218 };
219 
220 // test_holder_with_addressof_operator
221 struct TypeForHolderWithAddressOf {
222  TypeForHolderWithAddressOf() { print_created(this); }
223  TypeForHolderWithAddressOf(const TypeForHolderWithAddressOf &) { print_copy_created(this); }
224  TypeForHolderWithAddressOf(TypeForHolderWithAddressOf &&) noexcept {
225  print_move_created(this);
226  }
227  ~TypeForHolderWithAddressOf() { print_destroyed(this); }
228  std::string toString() const {
229  return "TypeForHolderWithAddressOf[" + std::to_string(value) + "]";
230  }
231  int value = 42;
232 };
233 
234 // test_move_only_holder_with_addressof_operator
235 struct TypeForMoveOnlyHolderWithAddressOf {
236  explicit TypeForMoveOnlyHolderWithAddressOf(int value) : value{value} { print_created(this); }
237  ~TypeForMoveOnlyHolderWithAddressOf() { print_destroyed(this); }
238  std::string toString() const {
239  return "MoveOnlyHolderWithAddressOf[" + std::to_string(value) + "]";
240  }
241  int value;
242 };
243 
244 // test_smart_ptr_from_default
245 struct HeldByDefaultHolder {};
246 
247 // test_shared_ptr_gc
248 // #187: issue involving std::shared_ptr<> return value policy & garbage collection
249 struct ElementBase {
250  virtual ~ElementBase() = default; /* Force creation of virtual table */
251  ElementBase() = default;
252  ElementBase(const ElementBase &) = delete;
253 };
254 
255 struct ElementA : ElementBase {
256  explicit ElementA(int v) : v(v) {}
257  int value() const { return v; }
258  int v;
259 };
260 
261 struct ElementList {
262  void add(const std::shared_ptr<ElementBase> &e) { l.push_back(e); }
263  std::vector<std::shared_ptr<ElementBase>> l;
264 };
265 
266 } // namespace
267 
268 // ref<T> is a wrapper for 'Object' which uses intrusive reference counting
269 // It is always possible to construct a ref<T> from an Object* pointer without
270 // possible inconsistencies, hence the 'true' argument at the end.
271 // Make pybind11 aware of the non-standard getter member function
272 namespace PYBIND11_NAMESPACE {
273 namespace detail {
274 template <typename T>
275 struct holder_helper<ref<T>> {
276  static const T *get(const ref<T> &p) { return p.get_ptr(); }
277 };
278 } // namespace detail
279 } // namespace PYBIND11_NAMESPACE
280 
281 // Make pybind aware of the ref-counted wrapper type (s):
283 // The following is not required anymore for std::shared_ptr, but it should compile without error:
284 PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
285 PYBIND11_DECLARE_HOLDER_TYPE(T, huge_unique_ptr<T>);
286 PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr<T>);
287 PYBIND11_DECLARE_HOLDER_TYPE(T, shared_ptr_with_addressof_operator<T>);
288 PYBIND11_DECLARE_HOLDER_TYPE(T, unique_ptr_with_addressof_operator<T>);
289 
290 TEST_SUBMODULE(smart_ptr, m) {
291  // Please do not interleave `struct` and `class` definitions with bindings code,
292  // but implement `struct`s and `class`es in the anonymous namespace above.
293  // This helps keeping the smart_holder branch in sync with master.
294 
295  // test_smart_ptr
296 
297  // Object implementation in `object.h`
298  py::class_<Object, ref<Object>> obj(m, "Object");
299  obj.def("getRefCount", &Object::getRefCount);
300 
301  py::class_<MyObject1, ref<MyObject1>>(m, "MyObject1", obj).def(py::init<int>());
302  py::implicitly_convertible<py::int_, MyObject1>();
303 
304  m.def("make_object_1", []() -> Object * { return new MyObject1(1); });
305  m.def("make_object_2", []() -> ref<Object> { return ref<Object>(new MyObject1(2)); });
306  m.def("make_myobject1_1", []() -> MyObject1 * { return new MyObject1(4); });
307  m.def("make_myobject1_2", []() -> ref<MyObject1> { return ref<MyObject1>(new MyObject1(5)); });
308  m.def("print_object_1", [](const Object *obj) { py::print(obj->toString()); });
309  m.def("print_object_2", [](ref<Object> obj) { py::print(obj->toString()); });
310  m.def("print_object_3", [](const ref<Object> &obj) { py::print(obj->toString()); });
311  m.def("print_object_4", [](const ref<Object> *obj) { py::print((*obj)->toString()); });
312  m.def("print_myobject1_1", [](const MyObject1 *obj) { py::print(obj->toString()); });
313  m.def("print_myobject1_2", [](ref<MyObject1> obj) { py::print(obj->toString()); });
314  m.def("print_myobject1_3", [](const ref<MyObject1> &obj) { py::print(obj->toString()); });
315  m.def("print_myobject1_4", [](const ref<MyObject1> *obj) { py::print((*obj)->toString()); });
316 
317  // Expose constructor stats for the ref type
318  m.def("cstats_ref", &ConstructorStats::get<ref_tag>);
319 
320  py::class_<MyObject2, std::shared_ptr<MyObject2>>(m, "MyObject2").def(py::init<int>());
321  m.def("make_myobject2_1", []() { return new MyObject2(6); });
322  m.def("make_myobject2_2", []() { return std::make_shared<MyObject2>(7); });
323  m.def("print_myobject2_1", [](const MyObject2 *obj) { py::print(obj->toString()); });
324  // NOLINTNEXTLINE(performance-unnecessary-value-param)
325  m.def("print_myobject2_2", [](std::shared_ptr<MyObject2> obj) { py::print(obj->toString()); });
326  m.def("print_myobject2_3",
327  [](const std::shared_ptr<MyObject2> &obj) { py::print(obj->toString()); });
328  m.def("print_myobject2_4",
329  [](const std::shared_ptr<MyObject2> *obj) { py::print((*obj)->toString()); });
330 
331  py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, "MyObject3").def(py::init<int>());
332  m.def("make_myobject3_1", []() { return new MyObject3(8); });
333  m.def("make_myobject3_2", []() { return std::make_shared<MyObject3>(9); });
334  m.def("print_myobject3_1", [](const MyObject3 *obj) { py::print(obj->toString()); });
335  // NOLINTNEXTLINE(performance-unnecessary-value-param)
336  m.def("print_myobject3_2", [](std::shared_ptr<MyObject3> obj) { py::print(obj->toString()); });
337  m.def("print_myobject3_3",
338  [](const std::shared_ptr<MyObject3> &obj) { py::print(obj->toString()); });
339  m.def("print_myobject3_4",
340  [](const std::shared_ptr<MyObject3> *obj) { py::print((*obj)->toString()); });
341 
342  // test_smart_ptr_refcounting
343  m.def("test_object1_refcounting", []() {
344  auto o = ref<MyObject1>(new MyObject1(0));
345  bool good = o->getRefCount() == 1;
346  py::object o2 = py::cast(o, py::return_value_policy::reference);
347  // always request (partial) ownership for objects with intrusive
348  // reference counting even when using the 'reference' RVP
349  good &= o->getRefCount() == 2;
350  return good;
351  });
352 
353  // test_unique_nodelete
354  py::class_<MyObject4, std::unique_ptr<MyObject4, py::nodelete>>(m, "MyObject4")
355  .def(py::init<int>())
356  .def_readwrite("value", &MyObject4::value)
357  .def_static("cleanup_all_instances", &MyObject4::cleanupAllInstances);
358 
359  // test_unique_deleter
360  py::class_<MyObject4a, std::unique_ptr<MyObject4a, py::nodelete>>(m, "MyObject4a")
361  .def(py::init<int>())
362  .def_readwrite("value", &MyObject4a::value)
363  .def_static("cleanup_all_instances", &MyObject4a::cleanupAllInstances);
364 
365  py::class_<MyObject4b, MyObject4a, std::unique_ptr<MyObject4b>>(m, "MyObject4b")
366  .def(py::init<int>());
367 
368  // test_large_holder
369  py::class_<MyObject5, huge_unique_ptr<MyObject5>>(m, "MyObject5")
370  .def(py::init<int>())
371  .def_readwrite("value", &MyObject5::value);
372 
373  // test_shared_ptr_and_references
374  using A = SharedPtrRef::A;
375  py::class_<A, std::shared_ptr<A>>(m, "A");
376  py::class_<SharedPtrRef, std::unique_ptr<SharedPtrRef>>(m, "SharedPtrRef")
377  .def(py::init<>())
378  .def_readonly("ref", &SharedPtrRef::value)
379  .def_property_readonly(
380  "copy", [](const SharedPtrRef &s) { return s.value; }, py::return_value_policy::copy)
381  .def_readonly("holder_ref", &SharedPtrRef::shared)
382  .def_property_readonly(
383  "holder_copy",
384  [](const SharedPtrRef &s) { return s.shared; },
386  .def("set_ref", [](SharedPtrRef &, const A &) { return true; })
387  // NOLINTNEXTLINE(performance-unnecessary-value-param)
388  .def("set_holder", [](SharedPtrRef &, std::shared_ptr<A>) { return true; });
389 
390  // test_shared_ptr_from_this_and_references
391  using B = SharedFromThisRef::B;
392  py::class_<B, std::shared_ptr<B>>(m, "B");
393  py::class_<SharedFromThisRef, std::unique_ptr<SharedFromThisRef>>(m, "SharedFromThisRef")
394  .def(py::init<>())
395  .def_readonly("bad_wp", &SharedFromThisRef::value)
396  .def_property_readonly("ref",
397  [](const SharedFromThisRef &s) -> const B & { return *s.shared; })
398  .def_property_readonly(
399  "copy",
400  [](const SharedFromThisRef &s) { return s.value; },
402  .def_readonly("holder_ref", &SharedFromThisRef::shared)
403  .def_property_readonly(
404  "holder_copy",
405  [](const SharedFromThisRef &s) { return s.shared; },
407  .def("set_ref", [](SharedFromThisRef &, const B &) { return true; })
408  // NOLINTNEXTLINE(performance-unnecessary-value-param)
409  .def("set_holder", [](SharedFromThisRef &, std::shared_ptr<B>) { return true; });
410 
411  // Issue #865: shared_from_this doesn't work with virtual inheritance
412  static std::shared_ptr<SharedFromThisVirt> sft(new SharedFromThisVirt());
413  py::class_<SharedFromThisVirt, std::shared_ptr<SharedFromThisVirt>>(m, "SharedFromThisVirt")
414  .def_static("get", []() { return sft.get(); });
415 
416  // test_move_only_holder
417  py::class_<C, custom_unique_ptr<C>>(m, "TypeWithMoveOnlyHolder")
418  .def_static("make", []() { return custom_unique_ptr<C>(new C); })
419  .def_static("make_as_object", []() { return py::cast(custom_unique_ptr<C>(new C)); });
420 
421  // test_holder_with_addressof_operator
422  using HolderWithAddressOf = shared_ptr_with_addressof_operator<TypeForHolderWithAddressOf>;
423  py::class_<TypeForHolderWithAddressOf, HolderWithAddressOf>(m, "TypeForHolderWithAddressOf")
424  .def_static("make", []() { return HolderWithAddressOf(new TypeForHolderWithAddressOf); })
425  .def("get", [](const HolderWithAddressOf &self) { return self.get(); })
426  .def("print_object_1",
427  [](const TypeForHolderWithAddressOf *obj) { py::print(obj->toString()); })
428  // NOLINTNEXTLINE(performance-unnecessary-value-param)
429  .def("print_object_2", [](HolderWithAddressOf obj) { py::print(obj.get()->toString()); })
430  .def("print_object_3",
431  [](const HolderWithAddressOf &obj) { py::print(obj.get()->toString()); })
432  .def("print_object_4",
433  [](const HolderWithAddressOf *obj) { py::print((*obj).get()->toString()); });
434 
435  // test_move_only_holder_with_addressof_operator
436  using MoveOnlyHolderWithAddressOf
437  = unique_ptr_with_addressof_operator<TypeForMoveOnlyHolderWithAddressOf>;
438  py::class_<TypeForMoveOnlyHolderWithAddressOf, MoveOnlyHolderWithAddressOf>(
439  m, "TypeForMoveOnlyHolderWithAddressOf")
440  .def_static("make",
441  []() {
442  return MoveOnlyHolderWithAddressOf(
443  new TypeForMoveOnlyHolderWithAddressOf(0));
444  })
445  .def_readwrite("value", &TypeForMoveOnlyHolderWithAddressOf::value)
446  .def("print_object",
447  [](const TypeForMoveOnlyHolderWithAddressOf *obj) { py::print(obj->toString()); });
448 
449  // test_smart_ptr_from_default
450  py::class_<HeldByDefaultHolder, std::unique_ptr<HeldByDefaultHolder>>(m, "HeldByDefaultHolder")
451  .def(py::init<>())
452  // NOLINTNEXTLINE(performance-unnecessary-value-param)
453  .def_static("load_shared_ptr", [](std::shared_ptr<HeldByDefaultHolder>) {});
454 
455  // test_shared_ptr_gc
456  // #187: issue involving std::shared_ptr<> return value policy & garbage collection
457  py::class_<ElementBase, std::shared_ptr<ElementBase>>(m, "ElementBase");
458 
459  py::class_<ElementA, ElementBase, std::shared_ptr<ElementA>>(m, "ElementA")
460  .def(py::init<int>())
461  .def("value", &ElementA::value);
462 
463  py::class_<ElementList, std::shared_ptr<ElementList>>(m, "ElementList")
464  .def(py::init<>())
465  .def("add", &ElementList::add)
466  .def("get", [](ElementList &el) {
467  py::list list;
468  for (auto &e : el.l) {
469  list.append(py::cast(e));
470  }
471  return list;
472  });
473 }
Eigen::internal::print
EIGEN_STRONG_INLINE Packet4f print(const Packet4f &a)
Definition: NEON/PacketMath.h:3115
B
Matrix< SCALARB, Dynamic, Dynamic, opt_B > B
Definition: bench_gemm.cpp:49
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
copy
int EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:29
E1::B
@ B
detail
Definition: testSerializationNonlinear.cpp:70
A
Matrix< SCALARA, Dynamic, Dynamic, opt_A > A
Definition: bench_gemm.cpp:48
print_copy_created
void print_copy_created(T *inst, Values &&...values)
Definition: constructor_stats.h:282
Object
Reference counted object base class.
Definition: object.h:9
PYBIND11_DECLARE_HOLDER_TYPE
PYBIND11_DECLARE_HOLDER_TYPE(T, ref< T >, true)
add
graph add(PriorFactor< Pose2 >(1, priorMean, priorNoise))
l
static const Line3 l(Rot3(), 1, 1)
TEST_SUBMODULE
TEST_SUBMODULE(smart_ptr, m)
Definition: test_smart_ptr.cpp:290
holder_helper
Definition: cast.h:746
PYBIND11_NAMESPACE
Definition: test_custom_type_casters.cpp:24
m
Matrix3f m
Definition: AngleAxis_mimic_euler.cpp:1
Eigen::Triplet< double >
E1::A
@ A
PYBIND11_NAMESPACE::detail::holder_helper< ref< T > >::get
static const T * get(const ref< T > &p)
Definition: test_smart_ptr.cpp:276
Object::toString
virtual std::string toString() const =0
C
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:50
print_move_created
void print_move_created(T *inst, Values &&...values)
Definition: constructor_stats.h:288
pybind11_tests.h
std
Definition: BFloat16.h:88
print_destroyed
void print_destroyed(T *inst, Values &&...values)
Definition: constructor_stats.h:314
p
float * p
Definition: Tutorial_Map_using.cpp:9
ref
Reference counting helper.
Definition: object.h:67
v
Array< int, Dynamic, 1 > v
Definition: Array_initializer_list_vector_cxx11.cpp:1
Object::getRefCount
int getRefCount() const
Return the current reference count.
Definition: object.h:18
uint64_t
unsigned __int64 uint64_t
Definition: ms_stdint.h:95
get
Container::iterator get(Container &c, Position position)
Definition: stdlist_overload.cpp:29
gtsam::operator&
DiscreteKeys operator&(const DiscreteKey &key1, const DiscreteKey &key2)
Create a list from two keys.
Definition: DiscreteKey.cpp:45
test_callbacks.value
value
Definition: test_callbacks.py:160
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Eigen::internal::cast
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
Definition: Eigen/src/Core/MathFunctions.h:460
print_created
void print_created(T *inst, Values &&...values)
Definition: constructor_stats.h:309
object.h


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:08