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 #if defined(_MSC_VER) && _MSC_VER < 1910
12 # pragma warning(disable: 4702) // unreachable code in system header
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>> {
28  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:
43  huge_unique_ptr(T *p) : ptr(p) {};
44  T *get() { return ptr.get(); }
45 };
47 
48 // Simple custom holder that works like unique_ptr
49 template <typename T>
51  std::unique_ptr<T> impl;
52 public:
53  custom_unique_ptr(T* p) : impl(p) { }
54  T* get() const { return impl.get(); }
55  T* release_ptr() { return impl.release(); }
56 };
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>
64  std::shared_ptr<T> impl;
65 public:
68  T* get() const { return impl.get(); }
69  T** operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); }
70 };
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>
78  std::unique_ptr<T> impl;
79 public:
82  T* get() const { return impl.get(); }
83  T* release_ptr() { return impl.release(); }
84  T** operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); }
85 };
87 
88 
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  public:
181  MyObject4(int value) : value{value} { print_created(this); }
182  int value;
183  private:
184  ~MyObject4() { print_destroyed(this); }
185  };
186  py::class_<MyObject4, std::unique_ptr<MyObject4, py::nodelete>>(m, "MyObject4")
187  .def(py::init<int>())
188  .def_readwrite("value", &MyObject4::value);
189 
190  // test_unique_deleter
191  // Object with std::unique_ptr<T, D> where D is not matching the base class
192  // Object with a protected destructor
193  class MyObject4a {
194  public:
195  MyObject4a(int i) {
196  value = i;
197  print_created(this);
198  };
199  int value;
200  protected:
201  virtual ~MyObject4a() { print_destroyed(this); }
202  };
203  py::class_<MyObject4a, std::unique_ptr<MyObject4a, py::nodelete>>(m, "MyObject4a")
204  .def(py::init<int>())
205  .def_readwrite("value", &MyObject4a::value);
206 
207  // Object derived but with public destructor and no Deleter in default holder
208  class MyObject4b : public MyObject4a {
209  public:
210  MyObject4b(int i) : MyObject4a(i) { print_created(this); }
211  ~MyObject4b() override { print_destroyed(this); }
212  };
213  py::class_<MyObject4b, MyObject4a>(m, "MyObject4b")
214  .def(py::init<int>());
215 
216  // test_large_holder
217  class MyObject5 { // managed by huge_unique_ptr
218  public:
219  MyObject5(int value) : value{value} { print_created(this); }
220  ~MyObject5() { print_destroyed(this); }
221  int value;
222  };
223  py::class_<MyObject5, huge_unique_ptr<MyObject5>>(m, "MyObject5")
224  .def(py::init<int>())
225  .def_readwrite("value", &MyObject5::value);
226 
227  // test_shared_ptr_and_references
228  struct SharedPtrRef {
229  struct A {
230  A() { print_created(this); }
231  A(const A &) { print_copy_created(this); }
232  A(A &&) { print_move_created(this); }
233  ~A() { print_destroyed(this); }
234  };
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; },
246  .def_readonly("holder_ref", &SharedPtrRef::shared)
247  .def_property_readonly("holder_copy", [](const SharedPtrRef &s) { return s.shared; },
249  .def("set_ref", [](SharedPtrRef &, const A &) { return true; })
250  .def("set_holder", [](SharedPtrRef &, std::shared_ptr<A>) { return true; });
251 
252  // test_shared_ptr_from_this_and_references
253  struct SharedFromThisRef {
254  struct B : std::enable_shared_from_this<B> {
255  B() { print_created(this); }
256  B(const B &) : std::enable_shared_from_this<B>() { print_copy_created(this); }
257  B(B &&) : std::enable_shared_from_this<B>() { print_move_created(this); }
258  ~B() { print_destroyed(this); }
259  };
260 
261  B value = {};
262  std::shared_ptr<B> shared = std::make_shared<B>();
263  };
264  using B = SharedFromThisRef::B;
265  py::class_<B, std::shared_ptr<B>>(m, "B");
266  py::class_<SharedFromThisRef>(m, "SharedFromThisRef")
267  .def(py::init<>())
268  .def_readonly("bad_wp", &SharedFromThisRef::value)
269  .def_property_readonly("ref", [](const SharedFromThisRef &s) -> const B & { return *s.shared; })
270  .def_property_readonly("copy", [](const SharedFromThisRef &s) { return s.value; },
272  .def_readonly("holder_ref", &SharedFromThisRef::shared)
273  .def_property_readonly("holder_copy", [](const SharedFromThisRef &s) { return s.shared; },
275  .def("set_ref", [](SharedFromThisRef &, const B &) { return true; })
276  .def("set_holder", [](SharedFromThisRef &, std::shared_ptr<B>) { return true; });
277 
278  // Issue #865: shared_from_this doesn't work with virtual inheritance
279  struct SharedFromThisVBase : std::enable_shared_from_this<SharedFromThisVBase> {
280  SharedFromThisVBase() = default;
281  SharedFromThisVBase(const SharedFromThisVBase &) = default;
282  virtual ~SharedFromThisVBase() = default;
283  };
284  struct SharedFromThisVirt : virtual SharedFromThisVBase {};
285  static std::shared_ptr<SharedFromThisVirt> sft(new SharedFromThisVirt());
286  py::class_<SharedFromThisVirt, std::shared_ptr<SharedFromThisVirt>>(m, "SharedFromThisVirt")
287  .def_static("get", []() { return sft.get(); });
288 
289  // test_move_only_holder
290  struct C {
291  C() { print_created(this); }
292  ~C() { print_destroyed(this); }
293  };
294  py::class_<C, custom_unique_ptr<C>>(m, "TypeWithMoveOnlyHolder")
295  .def_static("make", []() { return custom_unique_ptr<C>(new C); })
296  .def_static("make_as_object", []() { return py::cast(custom_unique_ptr<C>(new C)); });
297 
298  // test_holder_with_addressof_operator
299  struct TypeForHolderWithAddressOf {
300  TypeForHolderWithAddressOf() { print_created(this); }
301  TypeForHolderWithAddressOf(const TypeForHolderWithAddressOf &) { print_copy_created(this); }
302  TypeForHolderWithAddressOf(TypeForHolderWithAddressOf &&) { print_move_created(this); }
303  ~TypeForHolderWithAddressOf() { print_destroyed(this); }
304  std::string toString() const {
305  return "TypeForHolderWithAddressOf[" + std::to_string(value) + "]";
306  }
307  int value = 42;
308  };
310  py::class_<TypeForHolderWithAddressOf, HolderWithAddressOf>(m, "TypeForHolderWithAddressOf")
311  .def_static("make", []() { return HolderWithAddressOf(new TypeForHolderWithAddressOf); })
312  .def("get", [](const HolderWithAddressOf &self) { return self.get(); })
313  .def("print_object_1", [](const TypeForHolderWithAddressOf *obj) { py::print(obj->toString()); })
314  .def("print_object_2", [](HolderWithAddressOf obj) { py::print(obj.get()->toString()); })
315  .def("print_object_3", [](const HolderWithAddressOf &obj) { py::print(obj.get()->toString()); })
316  .def("print_object_4", [](const HolderWithAddressOf *obj) { py::print((*obj).get()->toString()); });
317 
318  // test_move_only_holder_with_addressof_operator
319  struct TypeForMoveOnlyHolderWithAddressOf {
320  TypeForMoveOnlyHolderWithAddressOf(int value) : value{value} { print_created(this); }
321  ~TypeForMoveOnlyHolderWithAddressOf() { print_destroyed(this); }
322  std::string toString() const {
323  return "MoveOnlyHolderWithAddressOf[" + std::to_string(value) + "]";
324  }
325  int value;
326  };
328  py::class_<TypeForMoveOnlyHolderWithAddressOf, MoveOnlyHolderWithAddressOf>(m, "TypeForMoveOnlyHolderWithAddressOf")
329  .def_static("make", []() { return MoveOnlyHolderWithAddressOf(new TypeForMoveOnlyHolderWithAddressOf(0)); })
330  .def_readwrite("value", &TypeForMoveOnlyHolderWithAddressOf::value)
331  .def("print_object", [](const TypeForMoveOnlyHolderWithAddressOf *obj) { py::print(obj->toString()); });
332 
333  // test_smart_ptr_from_default
334  struct HeldByDefaultHolder { };
335  py::class_<HeldByDefaultHolder>(m, "HeldByDefaultHolder")
336  .def(py::init<>())
337  .def_static("load_shared_ptr", [](std::shared_ptr<HeldByDefaultHolder>) {});
338 
339  // test_shared_ptr_gc
340  // #187: issue involving std::shared_ptr<> return value policy & garbage collection
341  struct ElementBase {
342  virtual ~ElementBase() = default; /* Force creation of virtual table */
343  ElementBase() = default;
344  ElementBase(const ElementBase&) = delete;
345  };
346  py::class_<ElementBase, std::shared_ptr<ElementBase>>(m, "ElementBase");
347 
348  struct ElementA : ElementBase {
349  ElementA(int v) : v(v) { }
350  int value() { return v; }
351  int v;
352  };
353  py::class_<ElementA, ElementBase, std::shared_ptr<ElementA>>(m, "ElementA")
354  .def(py::init<int>())
355  .def("value", &ElementA::value);
356 
357  struct ElementList {
358  void add(std::shared_ptr<ElementBase> e) { l.push_back(e); }
359  std::vector<std::shared_ptr<ElementBase>> l;
360  };
361  py::class_<ElementList, std::shared_ptr<ElementList>>(m, "ElementList")
362  .def(py::init<>())
363  .def("add", &ElementList::add)
364  .def("get", [](ElementList &el) {
365  py::list list;
366  for (auto &e : el.l)
367  list.append(py::cast(e));
368  return list;
369  });
370 }
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
Reference counted object base class.
Definition: object.h:8
Matrix3f m
std::unique_ptr< T > ptr
int getRefCount() const
Return the current reference count.
Definition: object.h:17
TEST_SUBMODULE(smart_ptr, m)
ArrayXcf v
Definition: Cwise_arg.cpp:1
void print_destroyed(T *inst, Values &&...values)
std::string toString(T t, std::ios_base &(*f)(std::ios_base &))
Definition: mpreal.h:1726
void print_copy_created(T *inst, Values &&...values)
Matrix< SCALARA, Dynamic, Dynamic > A
Definition: bench_gemm.cpp:35
Matrix< SCALARB, Dynamic, Dynamic > B
Definition: bench_gemm.cpp:36
graph add(boost::make_shared< UnaryFactor >(1, 0.0, 0.0, unaryNoise))
static const Line3 l(Rot3(), 1, 1)
unsigned __int64 uint64_t
Definition: ms_stdint.h:95
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
Array< double, 1, 3 > e(1./3., 0.5, 2.)
RealScalar s
Matrix< Scalar, Dynamic, Dynamic > C
Definition: bench_gemm.cpp:37
virtual std::string toString() const =0
void print_created(T *inst, Values &&...values)
float * p
int EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
Definition: level1_impl.h:29
void print_move_created(T *inst, Values &&...values)
PYBIND11_DECLARE_HOLDER_TYPE(T, ref< T >, true)
std::unique_ptr< T > impl


gtsam
Author(s):
autogenerated on Sat May 8 2021 02:46:04