File indexing completed on 2025-01-18 10:17:57
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include "object.h"
0012 #include "pybind11_tests.h"
0013
0014 namespace {
0015
0016
0017
0018
0019 template <typename T>
0020 class huge_unique_ptr {
0021 std::unique_ptr<T> ptr;
0022 uint64_t padding[10];
0023
0024 public:
0025 explicit huge_unique_ptr(T *p) : ptr(p) {}
0026 T *get() { return ptr.get(); }
0027 };
0028
0029
0030 template <typename T>
0031 class custom_unique_ptr {
0032 std::unique_ptr<T> impl;
0033
0034 public:
0035 explicit custom_unique_ptr(T *p) : impl(p) {}
0036 T *get() const { return impl.get(); }
0037 T *release_ptr() { return impl.release(); }
0038 };
0039
0040
0041
0042
0043 template <typename T>
0044 class shared_ptr_with_addressof_operator {
0045 std::shared_ptr<T> impl;
0046
0047 public:
0048 shared_ptr_with_addressof_operator() = default;
0049 explicit shared_ptr_with_addressof_operator(T *p) : impl(p) {}
0050 T *get() const { return impl.get(); }
0051 T **operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); }
0052 };
0053
0054
0055
0056
0057 template <typename T>
0058 class unique_ptr_with_addressof_operator {
0059 std::unique_ptr<T> impl;
0060
0061 public:
0062 unique_ptr_with_addressof_operator() = default;
0063 explicit unique_ptr_with_addressof_operator(T *p) : impl(p) {}
0064 T *get() const { return impl.get(); }
0065 T *release_ptr() { return impl.release(); }
0066 T **operator&() { throw std::logic_error("Call of overloaded operator& is not expected"); }
0067 };
0068
0069
0070 class MyObject1 : public Object {
0071 public:
0072 explicit MyObject1(int value) : value(value) { print_created(this, toString()); }
0073 std::string toString() const override { return "MyObject1[" + std::to_string(value) + "]"; }
0074
0075 protected:
0076 ~MyObject1() override { print_destroyed(this); }
0077
0078 private:
0079 int value;
0080 };
0081
0082
0083 class MyObject2 {
0084 public:
0085 MyObject2(const MyObject2 &) = default;
0086 explicit MyObject2(int value) : value(value) { print_created(this, toString()); }
0087 std::string toString() const { return "MyObject2[" + std::to_string(value) + "]"; }
0088 virtual ~MyObject2() { print_destroyed(this); }
0089
0090 private:
0091 int value;
0092 };
0093
0094
0095 class MyObject3 : public std::enable_shared_from_this<MyObject3> {
0096 public:
0097 MyObject3(const MyObject3 &) = default;
0098 explicit MyObject3(int value) : value(value) { print_created(this, toString()); }
0099 std::string toString() const { return "MyObject3[" + std::to_string(value) + "]"; }
0100 virtual ~MyObject3() { print_destroyed(this); }
0101
0102 private:
0103 int value;
0104 };
0105
0106
0107
0108 class MyObject4;
0109 std::unordered_set<MyObject4 *> myobject4_instances;
0110 class MyObject4 {
0111 public:
0112 explicit MyObject4(int value) : value{value} {
0113 print_created(this);
0114 myobject4_instances.insert(this);
0115 }
0116 int value;
0117
0118 static void cleanupAllInstances() {
0119 auto tmp = std::move(myobject4_instances);
0120 myobject4_instances.clear();
0121 for (auto *o : tmp) {
0122 delete o;
0123 }
0124 }
0125
0126 private:
0127 ~MyObject4() {
0128 myobject4_instances.erase(this);
0129 print_destroyed(this);
0130 }
0131 };
0132
0133
0134
0135
0136 class MyObject4a;
0137 std::unordered_set<MyObject4a *> myobject4a_instances;
0138 class MyObject4a {
0139 public:
0140 explicit MyObject4a(int i) : value{i} {
0141 print_created(this);
0142 myobject4a_instances.insert(this);
0143 };
0144 int value;
0145
0146 static void cleanupAllInstances() {
0147 auto tmp = std::move(myobject4a_instances);
0148 myobject4a_instances.clear();
0149 for (auto *o : tmp) {
0150 delete o;
0151 }
0152 }
0153
0154 protected:
0155 virtual ~MyObject4a() {
0156 myobject4a_instances.erase(this);
0157 print_destroyed(this);
0158 }
0159 };
0160
0161
0162 class MyObject4b : public MyObject4a {
0163 public:
0164 explicit MyObject4b(int i) : MyObject4a(i) { print_created(this); }
0165 ~MyObject4b() override { print_destroyed(this); }
0166 };
0167
0168
0169 class MyObject5 {
0170 public:
0171 explicit MyObject5(int value) : value{value} { print_created(this); }
0172 ~MyObject5() { print_destroyed(this); }
0173 int value;
0174 };
0175
0176
0177 struct SharedPtrRef {
0178 struct A {
0179 A() { print_created(this); }
0180 A(const A &) { print_copy_created(this); }
0181 A(A &&) noexcept { print_move_created(this); }
0182 ~A() { print_destroyed(this); }
0183 };
0184
0185 A value = {};
0186 std::shared_ptr<A> shared = std::make_shared<A>();
0187 };
0188
0189
0190 struct SharedFromThisRef {
0191 struct B : std::enable_shared_from_this<B> {
0192 B() { print_created(this); }
0193
0194 B(const B &) : std::enable_shared_from_this<B>() { print_copy_created(this); }
0195 B(B &&) noexcept : std::enable_shared_from_this<B>() { print_move_created(this); }
0196 ~B() { print_destroyed(this); }
0197 };
0198
0199 B value = {};
0200 std::shared_ptr<B> shared = std::make_shared<B>();
0201 };
0202
0203
0204 struct SharedFromThisVBase : std::enable_shared_from_this<SharedFromThisVBase> {
0205 SharedFromThisVBase() = default;
0206 SharedFromThisVBase(const SharedFromThisVBase &) = default;
0207 virtual ~SharedFromThisVBase() = default;
0208 };
0209 struct SharedFromThisVirt : virtual SharedFromThisVBase {};
0210
0211
0212 struct C {
0213 C() { print_created(this); }
0214 ~C() { print_destroyed(this); }
0215 };
0216
0217
0218 struct TypeForHolderWithAddressOf {
0219 TypeForHolderWithAddressOf() { print_created(this); }
0220 TypeForHolderWithAddressOf(const TypeForHolderWithAddressOf &) { print_copy_created(this); }
0221 TypeForHolderWithAddressOf(TypeForHolderWithAddressOf &&) noexcept {
0222 print_move_created(this);
0223 }
0224 ~TypeForHolderWithAddressOf() { print_destroyed(this); }
0225 std::string toString() const {
0226 return "TypeForHolderWithAddressOf[" + std::to_string(value) + "]";
0227 }
0228 int value = 42;
0229 };
0230
0231
0232 struct TypeForMoveOnlyHolderWithAddressOf {
0233 explicit TypeForMoveOnlyHolderWithAddressOf(int value) : value{value} { print_created(this); }
0234 ~TypeForMoveOnlyHolderWithAddressOf() { print_destroyed(this); }
0235 std::string toString() const {
0236 return "MoveOnlyHolderWithAddressOf[" + std::to_string(value) + "]";
0237 }
0238 int value;
0239 };
0240
0241
0242 struct HeldByDefaultHolder {};
0243
0244
0245
0246 struct ElementBase {
0247 virtual ~ElementBase() = default;
0248 ElementBase() = default;
0249 ElementBase(const ElementBase &) = delete;
0250 };
0251
0252 struct ElementA : ElementBase {
0253 explicit ElementA(int v) : v(v) {}
0254 int value() const { return v; }
0255 int v;
0256 };
0257
0258 struct ElementList {
0259 void add(const std::shared_ptr<ElementBase> &e) { l.push_back(e); }
0260 std::vector<std::shared_ptr<ElementBase>> l;
0261 };
0262
0263 }
0264
0265
0266
0267
0268
0269 namespace PYBIND11_NAMESPACE {
0270 namespace detail {
0271 template <typename T>
0272 struct holder_helper<ref<T>> {
0273 static const T *get(const ref<T> &p) { return p.get_ptr(); }
0274 };
0275 }
0276 }
0277
0278
0279 PYBIND11_DECLARE_HOLDER_TYPE(T, ref<T>, true);
0280
0281 PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
0282 PYBIND11_DECLARE_HOLDER_TYPE(T, huge_unique_ptr<T>);
0283 PYBIND11_DECLARE_HOLDER_TYPE(T, custom_unique_ptr<T>);
0284 PYBIND11_DECLARE_HOLDER_TYPE(T, shared_ptr_with_addressof_operator<T>);
0285 PYBIND11_DECLARE_HOLDER_TYPE(T, unique_ptr_with_addressof_operator<T>);
0286
0287 TEST_SUBMODULE(smart_ptr, m) {
0288
0289
0290
0291
0292
0293
0294
0295 py::class_<Object, ref<Object>> obj(m, "Object");
0296 obj.def("getRefCount", &Object::getRefCount);
0297
0298 py::class_<MyObject1, ref<MyObject1>>(m, "MyObject1", obj).def(py::init<int>());
0299 py::implicitly_convertible<py::int_, MyObject1>();
0300
0301 m.def("make_object_1", []() -> Object * { return new MyObject1(1); });
0302 m.def("make_object_2", []() -> ref<Object> { return ref<Object>(new MyObject1(2)); });
0303 m.def("make_myobject1_1", []() -> MyObject1 * { return new MyObject1(4); });
0304 m.def("make_myobject1_2", []() -> ref<MyObject1> { return ref<MyObject1>(new MyObject1(5)); });
0305 m.def("print_object_1", [](const Object *obj) { py::print(obj->toString()); });
0306 m.def("print_object_2", [](ref<Object> obj) { py::print(obj->toString()); });
0307 m.def("print_object_3", [](const ref<Object> &obj) { py::print(obj->toString()); });
0308 m.def("print_object_4", [](const ref<Object> *obj) { py::print((*obj)->toString()); });
0309 m.def("print_myobject1_1", [](const MyObject1 *obj) { py::print(obj->toString()); });
0310 m.def("print_myobject1_2", [](ref<MyObject1> obj) { py::print(obj->toString()); });
0311 m.def("print_myobject1_3", [](const ref<MyObject1> &obj) { py::print(obj->toString()); });
0312 m.def("print_myobject1_4", [](const ref<MyObject1> *obj) { py::print((*obj)->toString()); });
0313
0314
0315 m.def("cstats_ref", &ConstructorStats::get<ref_tag>);
0316
0317 py::class_<MyObject2, std::shared_ptr<MyObject2>>(m, "MyObject2").def(py::init<int>());
0318 m.def("make_myobject2_1", []() { return new MyObject2(6); });
0319 m.def("make_myobject2_2", []() { return std::make_shared<MyObject2>(7); });
0320 m.def("print_myobject2_1", [](const MyObject2 *obj) { py::print(obj->toString()); });
0321
0322 m.def("print_myobject2_2", [](std::shared_ptr<MyObject2> obj) { py::print(obj->toString()); });
0323 m.def("print_myobject2_3",
0324 [](const std::shared_ptr<MyObject2> &obj) { py::print(obj->toString()); });
0325 m.def("print_myobject2_4",
0326 [](const std::shared_ptr<MyObject2> *obj) { py::print((*obj)->toString()); });
0327
0328 py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, "MyObject3").def(py::init<int>());
0329 m.def("make_myobject3_1", []() { return new MyObject3(8); });
0330 m.def("make_myobject3_2", []() { return std::make_shared<MyObject3>(9); });
0331 m.def("print_myobject3_1", [](const MyObject3 *obj) { py::print(obj->toString()); });
0332
0333 m.def("print_myobject3_2", [](std::shared_ptr<MyObject3> obj) { py::print(obj->toString()); });
0334 m.def("print_myobject3_3",
0335 [](const std::shared_ptr<MyObject3> &obj) { py::print(obj->toString()); });
0336 m.def("print_myobject3_4",
0337 [](const std::shared_ptr<MyObject3> *obj) { py::print((*obj)->toString()); });
0338
0339
0340 m.def("test_object1_refcounting", []() {
0341 auto o = ref<MyObject1>(new MyObject1(0));
0342 bool good = o->getRefCount() == 1;
0343 py::object o2 = py::cast(o, py::return_value_policy::reference);
0344
0345
0346 good &= o->getRefCount() == 2;
0347 return good;
0348 });
0349
0350
0351 py::class_<MyObject4, std::unique_ptr<MyObject4, py::nodelete>>(m, "MyObject4")
0352 .def(py::init<int>())
0353 .def_readwrite("value", &MyObject4::value)
0354 .def_static("cleanup_all_instances", &MyObject4::cleanupAllInstances);
0355
0356
0357 py::class_<MyObject4a, std::unique_ptr<MyObject4a, py::nodelete>>(m, "MyObject4a")
0358 .def(py::init<int>())
0359 .def_readwrite("value", &MyObject4a::value)
0360 .def_static("cleanup_all_instances", &MyObject4a::cleanupAllInstances);
0361
0362 py::class_<MyObject4b, MyObject4a, std::unique_ptr<MyObject4b>>(m, "MyObject4b")
0363 .def(py::init<int>());
0364
0365
0366 py::class_<MyObject5, huge_unique_ptr<MyObject5>>(m, "MyObject5")
0367 .def(py::init<int>())
0368 .def_readwrite("value", &MyObject5::value);
0369
0370
0371 using A = SharedPtrRef::A;
0372 py::class_<A, std::shared_ptr<A>>(m, "A");
0373 py::class_<SharedPtrRef, std::unique_ptr<SharedPtrRef>>(m, "SharedPtrRef")
0374 .def(py::init<>())
0375 .def_readonly("ref", &SharedPtrRef::value)
0376 .def_property_readonly(
0377 "copy", [](const SharedPtrRef &s) { return s.value; }, py::return_value_policy::copy)
0378 .def_readonly("holder_ref", &SharedPtrRef::shared)
0379 .def_property_readonly(
0380 "holder_copy",
0381 [](const SharedPtrRef &s) { return s.shared; },
0382 py::return_value_policy::copy)
0383 .def("set_ref", [](SharedPtrRef &, const A &) { return true; })
0384
0385 .def("set_holder", [](SharedPtrRef &, std::shared_ptr<A>) { return true; });
0386
0387
0388 using B = SharedFromThisRef::B;
0389 py::class_<B, std::shared_ptr<B>>(m, "B");
0390 py::class_<SharedFromThisRef, std::unique_ptr<SharedFromThisRef>>(m, "SharedFromThisRef")
0391 .def(py::init<>())
0392 .def_readonly("bad_wp", &SharedFromThisRef::value)
0393 .def_property_readonly("ref",
0394 [](const SharedFromThisRef &s) -> const B & { return *s.shared; })
0395 .def_property_readonly(
0396 "copy",
0397 [](const SharedFromThisRef &s) { return s.value; },
0398 py::return_value_policy::copy)
0399 .def_readonly("holder_ref", &SharedFromThisRef::shared)
0400 .def_property_readonly(
0401 "holder_copy",
0402 [](const SharedFromThisRef &s) { return s.shared; },
0403 py::return_value_policy::copy)
0404 .def("set_ref", [](SharedFromThisRef &, const B &) { return true; })
0405
0406 .def("set_holder", [](SharedFromThisRef &, std::shared_ptr<B>) { return true; });
0407
0408
0409 static std::shared_ptr<SharedFromThisVirt> sft(new SharedFromThisVirt());
0410 py::class_<SharedFromThisVirt, std::shared_ptr<SharedFromThisVirt>>(m, "SharedFromThisVirt")
0411 .def_static("get", []() { return sft.get(); });
0412
0413
0414 py::class_<C, custom_unique_ptr<C>>(m, "TypeWithMoveOnlyHolder")
0415 .def_static("make", []() { return custom_unique_ptr<C>(new C); })
0416 .def_static("make_as_object", []() { return py::cast(custom_unique_ptr<C>(new C)); });
0417
0418
0419 using HolderWithAddressOf = shared_ptr_with_addressof_operator<TypeForHolderWithAddressOf>;
0420 py::class_<TypeForHolderWithAddressOf, HolderWithAddressOf>(m, "TypeForHolderWithAddressOf")
0421 .def_static("make", []() { return HolderWithAddressOf(new TypeForHolderWithAddressOf); })
0422 .def("get", [](const HolderWithAddressOf &self) { return self.get(); })
0423 .def("print_object_1",
0424 [](const TypeForHolderWithAddressOf *obj) { py::print(obj->toString()); })
0425
0426 .def("print_object_2", [](HolderWithAddressOf obj) { py::print(obj.get()->toString()); })
0427 .def("print_object_3",
0428 [](const HolderWithAddressOf &obj) { py::print(obj.get()->toString()); })
0429 .def("print_object_4",
0430 [](const HolderWithAddressOf *obj) { py::print((*obj).get()->toString()); });
0431
0432
0433 using MoveOnlyHolderWithAddressOf
0434 = unique_ptr_with_addressof_operator<TypeForMoveOnlyHolderWithAddressOf>;
0435 py::class_<TypeForMoveOnlyHolderWithAddressOf, MoveOnlyHolderWithAddressOf>(
0436 m, "TypeForMoveOnlyHolderWithAddressOf")
0437 .def_static("make",
0438 []() {
0439 return MoveOnlyHolderWithAddressOf(
0440 new TypeForMoveOnlyHolderWithAddressOf(0));
0441 })
0442 .def_readwrite("value", &TypeForMoveOnlyHolderWithAddressOf::value)
0443 .def("print_object",
0444 [](const TypeForMoveOnlyHolderWithAddressOf *obj) { py::print(obj->toString()); });
0445
0446
0447 py::class_<HeldByDefaultHolder, std::unique_ptr<HeldByDefaultHolder>>(m, "HeldByDefaultHolder")
0448 .def(py::init<>())
0449
0450 .def_static("load_shared_ptr", [](std::shared_ptr<HeldByDefaultHolder>) {});
0451
0452
0453
0454 py::class_<ElementBase, std::shared_ptr<ElementBase>>(m, "ElementBase");
0455
0456 py::class_<ElementA, ElementBase, std::shared_ptr<ElementA>>(m, "ElementA")
0457 .def(py::init<int>())
0458 .def("value", &ElementA::value);
0459
0460 py::class_<ElementList, std::shared_ptr<ElementList>>(m, "ElementList")
0461 .def(py::init<>())
0462 .def("add", &ElementList::add)
0463 .def("get", [](ElementList &el) {
0464 py::list list;
0465 for (auto &e : el.l) {
0466 list.append(py::cast(e));
0467 }
0468 return list;
0469 });
0470 }