blob: 4d1e77e328e38bdbc95b4c7c073e022dcde9decb [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Dean Moldovana0c1ccf2016-08-12 13:50:00 +02002 tests/test_smart_ptr.cpp -- binding classes with custom reference counting,
Wenzel Jakoba576e6a2015-07-29 17:51:54 +02003 implicit conversions between types
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02005 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02006
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
Dean Moldovana0c1ccf2016-08-12 13:50:00 +020011#include "pybind11_tests.h"
Wenzel Jakob38bd7112015-07-05 20:05:44 +020012#include "object.h"
13
Wenzel Jakobb2c2c792016-01-17 22:36:40 +010014/// Custom object with builtin reference counting (see 'object.h' for the implementation)
15class MyObject1 : public Object {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020016public:
Wenzel Jakobb2c2c792016-01-17 22:36:40 +010017 MyObject1(int value) : value(value) {
Jason Rhinelander3f589372016-08-07 13:05:26 -040018 print_created(this, toString());
Wenzel Jakob38bd7112015-07-05 20:05:44 +020019 }
20
21 std::string toString() const {
Wenzel Jakobb2c2c792016-01-17 22:36:40 +010022 return "MyObject1[" + std::to_string(value) + "]";
Wenzel Jakob38bd7112015-07-05 20:05:44 +020023 }
24
25protected:
Wenzel Jakobb2c2c792016-01-17 22:36:40 +010026 virtual ~MyObject1() {
Jason Rhinelander3f589372016-08-07 13:05:26 -040027 print_destroyed(this);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020028 }
29
30private:
31 int value;
32};
33
Wenzel Jakobb2c2c792016-01-17 22:36:40 +010034/// Object managed by a std::shared_ptr<>
35class MyObject2 {
Wenzel Jakob6e213c92015-11-24 23:05:58 +010036public:
37 MyObject2(int value) : value(value) {
Jason Rhinelander3f589372016-08-07 13:05:26 -040038 print_created(this, toString());
Wenzel Jakob6e213c92015-11-24 23:05:58 +010039 }
40
41 std::string toString() const {
42 return "MyObject2[" + std::to_string(value) + "]";
43 }
44
45 virtual ~MyObject2() {
Jason Rhinelander3f589372016-08-07 13:05:26 -040046 print_destroyed(this);
Wenzel Jakob6e213c92015-11-24 23:05:58 +010047 }
48
49private:
50 int value;
51};
52
Wenzel Jakobb2c2c792016-01-17 22:36:40 +010053/// Object managed by a std::shared_ptr<>, additionally derives from std::enable_shared_from_this<>
54class MyObject3 : public std::enable_shared_from_this<MyObject3> {
55public:
56 MyObject3(int value) : value(value) {
Jason Rhinelander3f589372016-08-07 13:05:26 -040057 print_created(this, toString());
Wenzel Jakobb2c2c792016-01-17 22:36:40 +010058 }
59
60 std::string toString() const {
61 return "MyObject3[" + std::to_string(value) + "]";
62 }
63
64 virtual ~MyObject3() {
Jason Rhinelander3f589372016-08-07 13:05:26 -040065 print_destroyed(this);
Wenzel Jakobb2c2c792016-01-17 22:36:40 +010066 }
67
68private:
69 int value;
70};
71
Jason Rhinelandera6495af2016-09-04 18:23:55 -040072class MyObject4 {
73public:
74 MyObject4(int value) : value{value} {
75 print_created(this);
76 }
77 int value;
78private:
79 ~MyObject4() {
80 print_destroyed(this);
81 }
82};
83
Wenzel Jakobb2c2c792016-01-17 22:36:40 +010084/// Make pybind aware of the ref-counted wrapper type (s)
Wenzel Jakob20291712016-12-15 23:44:23 +010085
86// ref<T> is a wrapper for 'Object' which uses intrusive reference counting
87// It is always possible to construct a ref<T> from an Object* pointer without
88// possible incosistencies, hence the 'true' argument at the end.
89PYBIND11_DECLARE_HOLDER_TYPE(T, ref<T>, true);
Dean Moldovan5d28dd12016-10-18 13:56:33 +020090PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>); // Not required any more for std::shared_ptr,
91 // but it should compile without error
Wenzel Jakob38bd7112015-07-05 20:05:44 +020092
Wenzel Jakobb2c2c792016-01-17 22:36:40 +010093Object *make_object_1() { return new MyObject1(1); }
94ref<Object> make_object_2() { return new MyObject1(2); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +020095
Wenzel Jakobb2c2c792016-01-17 22:36:40 +010096MyObject1 *make_myobject1_1() { return new MyObject1(4); }
97ref<MyObject1> make_myobject1_2() { return new MyObject1(5); }
98
99MyObject2 *make_myobject2_1() { return new MyObject2(6); }
100std::shared_ptr<MyObject2> make_myobject2_2() { return std::make_shared<MyObject2>(7); }
101
102MyObject3 *make_myobject3_1() { return new MyObject3(8); }
103std::shared_ptr<MyObject3> make_myobject3_2() { return std::make_shared<MyObject3>(9); }
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100104
Dean Moldovan81511be2016-09-07 00:50:10 +0200105void print_object_1(const Object *obj) { py::print(obj->toString()); }
106void print_object_2(ref<Object> obj) { py::print(obj->toString()); }
107void print_object_3(const ref<Object> &obj) { py::print(obj->toString()); }
108void print_object_4(const ref<Object> *obj) { py::print((*obj)->toString()); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200109
Dean Moldovan81511be2016-09-07 00:50:10 +0200110void print_myobject1_1(const MyObject1 *obj) { py::print(obj->toString()); }
111void print_myobject1_2(ref<MyObject1> obj) { py::print(obj->toString()); }
112void print_myobject1_3(const ref<MyObject1> &obj) { py::print(obj->toString()); }
113void print_myobject1_4(const ref<MyObject1> *obj) { py::print((*obj)->toString()); }
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200114
Dean Moldovan81511be2016-09-07 00:50:10 +0200115void print_myobject2_1(const MyObject2 *obj) { py::print(obj->toString()); }
116void print_myobject2_2(std::shared_ptr<MyObject2> obj) { py::print(obj->toString()); }
117void print_myobject2_3(const std::shared_ptr<MyObject2> &obj) { py::print(obj->toString()); }
118void print_myobject2_4(const std::shared_ptr<MyObject2> *obj) { py::print((*obj)->toString()); }
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100119
Dean Moldovan81511be2016-09-07 00:50:10 +0200120void print_myobject3_1(const MyObject3 *obj) { py::print(obj->toString()); }
121void print_myobject3_2(std::shared_ptr<MyObject3> obj) { py::print(obj->toString()); }
122void print_myobject3_3(const std::shared_ptr<MyObject3> &obj) { py::print(obj->toString()); }
123void print_myobject3_4(const std::shared_ptr<MyObject3> *obj) { py::print((*obj)->toString()); }
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100124
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400125test_initializer smart_ptr([](py::module &m) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200126 py::class_<Object, ref<Object>> obj(m, "Object");
127 obj.def("getRefCount", &Object::getRefCount);
128
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100129 py::class_<MyObject1, ref<MyObject1>>(m, "MyObject1", obj)
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200130 .def(py::init<int>());
131
Wenzel Jakob20291712016-12-15 23:44:23 +0100132 m.def("test_object1_refcounting",
133 []() -> bool {
134 ref<MyObject1> o = new MyObject1(0);
135 bool good = o->getRefCount() == 1;
136 py::object o2 = py::cast(o, py::return_value_policy::reference);
137 // always request (partial) ownership for objects with intrusive
138 // reference counting even when using the 'reference' RVP
139 good &= o->getRefCount() == 2;
140 return good;
141 }
142 );
143
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200144 m.def("make_object_1", &make_object_1);
145 m.def("make_object_2", &make_object_2);
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100146 m.def("make_myobject1_1", &make_myobject1_1);
147 m.def("make_myobject1_2", &make_myobject1_2);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200148 m.def("print_object_1", &print_object_1);
149 m.def("print_object_2", &print_object_2);
150 m.def("print_object_3", &print_object_3);
151 m.def("print_object_4", &print_object_4);
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100152 m.def("print_myobject1_1", &print_myobject1_1);
153 m.def("print_myobject1_2", &print_myobject1_2);
154 m.def("print_myobject1_3", &print_myobject1_3);
155 m.def("print_myobject1_4", &print_myobject1_4);
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200156
Wenzel Jakob6e213c92015-11-24 23:05:58 +0100157 py::class_<MyObject2, std::shared_ptr<MyObject2>>(m, "MyObject2")
158 .def(py::init<int>());
159 m.def("make_myobject2_1", &make_myobject2_1);
160 m.def("make_myobject2_2", &make_myobject2_2);
161 m.def("print_myobject2_1", &print_myobject2_1);
162 m.def("print_myobject2_2", &print_myobject2_2);
163 m.def("print_myobject2_3", &print_myobject2_3);
164 m.def("print_myobject2_4", &print_myobject2_4);
165
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100166 py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, "MyObject3")
167 .def(py::init<int>());
168 m.def("make_myobject3_1", &make_myobject3_1);
169 m.def("make_myobject3_2", &make_myobject3_2);
170 m.def("print_myobject3_1", &print_myobject3_1);
171 m.def("print_myobject3_2", &print_myobject3_2);
172 m.def("print_myobject3_3", &print_myobject3_3);
173 m.def("print_myobject3_4", &print_myobject3_4);
174
Jason Rhinelandera6495af2016-09-04 18:23:55 -0400175 py::class_<MyObject4, std::unique_ptr<MyObject4, py::nodelete>>(m, "MyObject4")
176 .def(py::init<int>())
177 .def_readwrite("value", &MyObject4::value);
178
Wenzel Jakobb2c2c792016-01-17 22:36:40 +0100179 py::implicitly_convertible<py::int_, MyObject1>();
Jason Rhinelander3f589372016-08-07 13:05:26 -0400180
181 // Expose constructor stats for the ref type
182 m.def("cstats_ref", &ConstructorStats::get<ref_tag>);
Jason Rhinelander52f4be82016-09-03 14:54:22 -0400183});
Dean Moldovanab90ec62016-12-07 02:36:44 +0100184
185struct SharedPtrRef {
186 struct A {
187 A() { print_created(this); }
188 A(const A &) { print_copy_created(this); }
189 A(A &&) { print_move_created(this); }
190 ~A() { print_destroyed(this); }
191 };
192
193 A value = {};
194 std::shared_ptr<A> shared = std::make_shared<A>();
195};
196
197struct SharedFromThisRef {
198 struct B : std::enable_shared_from_this<B> {
199 B() { print_created(this); }
200 B(const B &) : std::enable_shared_from_this<B>() { print_copy_created(this); }
201 B(B &&) : std::enable_shared_from_this<B>() { print_move_created(this); }
202 ~B() { print_destroyed(this); }
203 };
204
205 B value = {};
206 std::shared_ptr<B> shared = std::make_shared<B>();
207};
208
209test_initializer smart_ptr_and_references([](py::module &pm) {
210 auto m = pm.def_submodule("smart_ptr");
211
212 using A = SharedPtrRef::A;
213 py::class_<A, std::shared_ptr<A>>(m, "A");
214
215 py::class_<SharedPtrRef>(m, "SharedPtrRef")
216 .def(py::init<>())
217 .def_readonly("ref", &SharedPtrRef::value)
218 .def_property_readonly("copy", [](const SharedPtrRef &s) { return s.value; },
219 py::return_value_policy::copy)
220 .def_readonly("holder_ref", &SharedPtrRef::shared)
221 .def_property_readonly("holder_copy", [](const SharedPtrRef &s) { return s.shared; },
222 py::return_value_policy::copy)
223 .def("set_ref", [](SharedPtrRef &, const A &) { return true; })
224 .def("set_holder", [](SharedPtrRef &, std::shared_ptr<A>) { return true; });
225
226 using B = SharedFromThisRef::B;
227 py::class_<B, std::shared_ptr<B>>(m, "B");
228
229 py::class_<SharedFromThisRef>(m, "SharedFromThisRef")
230 .def(py::init<>())
231 .def_readonly("bad_wp", &SharedFromThisRef::value)
232 .def_property_readonly("ref", [](const SharedFromThisRef &s) -> const B & { return *s.shared; })
233 .def_property_readonly("copy", [](const SharedFromThisRef &s) { return s.value; },
234 py::return_value_policy::copy)
235 .def_readonly("holder_ref", &SharedFromThisRef::shared)
236 .def_property_readonly("holder_copy", [](const SharedFromThisRef &s) { return s.shared; },
237 py::return_value_policy::copy)
238 .def("set_ref", [](SharedFromThisRef &, const B &) { return true; })
239 .def("set_holder", [](SharedFromThisRef &, std::shared_ptr<B>) { return true; });
240});