11 #if defined(_MSC_VER) && _MSC_VER < 1910 12 # pragma warning(disable: 4702) // unreachable code in system header 28 static const T *
get(
const ref<T> &
p) {
return p.get_ptr(); }
40 std::unique_ptr<T>
ptr;
44 T *
get() {
return ptr.get(); }
54 T*
get()
const {
return impl.get(); }
68 T*
get()
const {
return impl.get(); }
69 T**
operator&() {
throw std::logic_error(
"Call of overloaded operator& is not expected"); }
82 T*
get()
const {
return impl.get(); }
84 T**
operator&() {
throw std::logic_error(
"Call of overloaded operator& is not expected"); }
94 py::class_<Object, ref<Object>> obj(
m,
"Object");
98 class MyObject1 :
public Object {
101 std::string
toString()
const override {
return "MyObject1[" + std::to_string(value) +
"]"; }
107 py::class_<MyObject1, ref<MyObject1>>(
m,
"MyObject1", obj)
108 .def(py::init<int>());
109 py::implicitly_convertible<py::int_, MyObject1>();
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); });
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()); });
125 m.def(
"cstats_ref", &ConstructorStats::get<ref_tag>);
131 MyObject2(
const MyObject2 &) =
default;
133 std::string
toString()
const {
return "MyObject2[" + std::to_string(value) +
"]"; }
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()); });
148 class MyObject3 :
public std::enable_shared_from_this<MyObject3> {
150 MyObject3(
const MyObject3 &) =
default;
152 std::string
toString()
const {
return "MyObject3[" + std::to_string(value) +
"]"; }
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()); });
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);
173 good &= o->getRefCount() == 2;
186 py::class_<MyObject4, std::unique_ptr<MyObject4, py::nodelete>>(
m,
"MyObject4")
187 .def(py::init<int>())
203 py::class_<MyObject4a, std::unique_ptr<MyObject4a, py::nodelete>>(
m,
"MyObject4a")
204 .def(py::init<int>())
208 class MyObject4b :
public MyObject4a {
213 py::class_<MyObject4b, MyObject4a>(
m,
"MyObject4b")
214 .def(py::init<int>());
223 py::class_<MyObject5, huge_unique_ptr<MyObject5>>(
m,
"MyObject5")
224 .def(py::init<int>())
228 struct SharedPtrRef {
237 std::shared_ptr<A> shared = std::make_shared<A>();
240 py::class_<A, std::shared_ptr<A>>(
m,
"A");
241 py::class_<SharedPtrRef>(
m,
"SharedPtrRef")
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; });
253 struct SharedFromThisRef {
254 struct B : std::enable_shared_from_this<B> {
262 std::shared_ptr<B> shared = std::make_shared<B>();
265 py::class_<B, std::shared_ptr<B>>(
m,
"B");
266 py::class_<SharedFromThisRef>(
m,
"SharedFromThisRef")
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; });
279 struct SharedFromThisVBase : std::enable_shared_from_this<SharedFromThisVBase> {
280 SharedFromThisVBase() =
default;
281 SharedFromThisVBase(
const SharedFromThisVBase &) =
default;
282 virtual ~SharedFromThisVBase() =
default;
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(); });
294 py::class_<C, custom_unique_ptr<C>>(
m,
"TypeWithMoveOnlyHolder")
299 struct TypeForHolderWithAddressOf {
301 TypeForHolderWithAddressOf(
const TypeForHolderWithAddressOf &) {
print_copy_created(
this); }
302 TypeForHolderWithAddressOf(TypeForHolderWithAddressOf &&) {
print_move_created(
this); }
305 return "TypeForHolderWithAddressOf[" + std::to_string(
value) +
"]";
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()); });
319 struct TypeForMoveOnlyHolderWithAddressOf {
323 return "MoveOnlyHolderWithAddressOf[" + std::to_string(value) +
"]";
328 py::class_<TypeForMoveOnlyHolderWithAddressOf, MoveOnlyHolderWithAddressOf>(
m,
"TypeForMoveOnlyHolderWithAddressOf")
329 .def_static(
"make", []() {
return MoveOnlyHolderWithAddressOf(
new TypeForMoveOnlyHolderWithAddressOf(0)); })
331 .def(
"print_object", [](
const TypeForMoveOnlyHolderWithAddressOf *obj) {
py::print(obj->toString()); });
334 struct HeldByDefaultHolder { };
335 py::class_<HeldByDefaultHolder>(
m,
"HeldByDefaultHolder")
337 .def_static(
"load_shared_ptr", [](std::shared_ptr<HeldByDefaultHolder>) {});
342 virtual ~ElementBase() =
default;
343 ElementBase() =
default;
344 ElementBase(
const ElementBase&) =
delete;
346 py::class_<ElementBase, std::shared_ptr<ElementBase>>(
m,
"ElementBase");
348 struct ElementA : ElementBase {
349 ElementA(
int v) :
v(v) { }
353 py::class_<ElementA, ElementBase, std::shared_ptr<ElementA>>(
m,
"ElementA")
354 .def(py::init<int>())
358 void add(std::shared_ptr<ElementBase>
e) {
l.push_back(e); }
359 std::vector<std::shared_ptr<ElementBase>>
l;
361 py::class_<ElementList, std::shared_ptr<ElementList>>(
m,
"ElementList")
364 .def(
"get", [](ElementList &el) {
void print(const Matrix &A, const string &s, ostream &stream)
Reference counted object base class.
int getRefCount() const
Return the current reference count.
TEST_SUBMODULE(smart_ptr, m)
void print_destroyed(T *inst, Values &&...values)
std::string toString(T t, std::ios_base &(*f)(std::ios_base &))
void print_copy_created(T *inst, Values &&...values)
Matrix< SCALARA, Dynamic, Dynamic > A
Matrix< SCALARB, Dynamic, Dynamic > B
graph add(boost::make_shared< UnaryFactor >(1, 0.0, 0.0, unaryNoise))
static const Line3 l(Rot3(), 1, 1)
unique_ptr_with_addressof_operator(T *p)
unsigned __int64 uint64_t
EIGEN_DEVICE_FUNC NewType cast(const OldType &x)
std::shared_ptr< T > impl
Array< double, 1, 3 > e(1./3., 0.5, 2.)
std::unique_ptr< T > impl
shared_ptr_with_addressof_operator(T *p)
Matrix< Scalar, Dynamic, Dynamic > C
virtual std::string toString() const =0
void print_created(T *inst, Values &&...values)
int EIGEN_BLAS_FUNC() copy(int *n, RealScalar *px, int *incx, RealScalar *py, int *incy)
void print_move_created(T *inst, Values &&...values)
PYBIND11_DECLARE_HOLDER_TYPE(T, ref< T >, true)
std::unique_ptr< T > impl