pybind11/tests/test_smart_ptr.cpp
Jason Rhinelander a6495af87a Make unique_ptr's with non-default deleters work
Currently pybind11 only supports std::unique_ptr<T> holders by default
(other holders can, of course, be declared using the macro).  PR #368
added a `py::nodelete` that is intended to be used as:

    py::class_<Type, std::unique_ptr<Type, py::nodelete>> c("Type");

but this doesn't work out of the box.  (You could add an explicit
holder type declaration, but this doesn't appear to have been the
intention of the commit).

This commit fixes it by generalizing the unique_ptr type_caster to take
both the type and deleter as template arguments, so that *any*
unique_ptr instances are now automatically handled by pybind.  It also
adds a test to test_smart_ptr, testing both that py::nodelete (now)
works, and that the object is indeed not deleted as intended.
2016-09-04 18:23:55 -04:00

167 lines
5.9 KiB
C++

/*
tests/test_smart_ptr.cpp -- binding classes with custom reference counting,
implicit conversions between types
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
All rights reserved. Use of this source code is governed by a
BSD-style license that can be found in the LICENSE file.
*/
#include "pybind11_tests.h"
#include "object.h"
/// Custom object with builtin reference counting (see 'object.h' for the implementation)
class MyObject1 : public Object {
public:
MyObject1(int value) : value(value) {
print_created(this, toString());
}
std::string toString() const {
return "MyObject1[" + std::to_string(value) + "]";
}
protected:
virtual ~MyObject1() {
print_destroyed(this);
}
private:
int value;
};
/// Object managed by a std::shared_ptr<>
class MyObject2 {
public:
MyObject2(int value) : value(value) {
print_created(this, toString());
}
std::string toString() const {
return "MyObject2[" + std::to_string(value) + "]";
}
virtual ~MyObject2() {
print_destroyed(this);
}
private:
int value;
};
/// Object managed by a std::shared_ptr<>, additionally derives from std::enable_shared_from_this<>
class MyObject3 : public std::enable_shared_from_this<MyObject3> {
public:
MyObject3(int value) : value(value) {
print_created(this, toString());
}
std::string toString() const {
return "MyObject3[" + std::to_string(value) + "]";
}
virtual ~MyObject3() {
print_destroyed(this);
}
private:
int value;
};
class MyObject4 {
public:
MyObject4(int value) : value{value} {
print_created(this);
}
int value;
private:
~MyObject4() {
print_destroyed(this);
}
};
/// Make pybind aware of the ref-counted wrapper type (s)
PYBIND11_DECLARE_HOLDER_TYPE(T, ref<T>);
PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>);
Object *make_object_1() { return new MyObject1(1); }
ref<Object> make_object_2() { return new MyObject1(2); }
MyObject1 *make_myobject1_1() { return new MyObject1(4); }
ref<MyObject1> make_myobject1_2() { return new MyObject1(5); }
MyObject2 *make_myobject2_1() { return new MyObject2(6); }
std::shared_ptr<MyObject2> make_myobject2_2() { return std::make_shared<MyObject2>(7); }
MyObject3 *make_myobject3_1() { return new MyObject3(8); }
std::shared_ptr<MyObject3> make_myobject3_2() { return std::make_shared<MyObject3>(9); }
void print_object_1(const Object *obj) { std::cout << obj->toString() << std::endl; }
void print_object_2(ref<Object> obj) { std::cout << obj->toString() << std::endl; }
void print_object_3(const ref<Object> &obj) { std::cout << obj->toString() << std::endl; }
void print_object_4(const ref<Object> *obj) { std::cout << (*obj)->toString() << std::endl; }
void print_myobject1_1(const MyObject1 *obj) { std::cout << obj->toString() << std::endl; }
void print_myobject1_2(ref<MyObject1> obj) { std::cout << obj->toString() << std::endl; }
void print_myobject1_3(const ref<MyObject1> &obj) { std::cout << obj->toString() << std::endl; }
void print_myobject1_4(const ref<MyObject1> *obj) { std::cout << (*obj)->toString() << std::endl; }
void print_myobject2_1(const MyObject2 *obj) { std::cout << obj->toString() << std::endl; }
void print_myobject2_2(std::shared_ptr<MyObject2> obj) { std::cout << obj->toString() << std::endl; }
void print_myobject2_3(const std::shared_ptr<MyObject2> &obj) { std::cout << obj->toString() << std::endl; }
void print_myobject2_4(const std::shared_ptr<MyObject2> *obj) { std::cout << (*obj)->toString() << std::endl; }
void print_myobject3_1(const MyObject3 *obj) { std::cout << obj->toString() << std::endl; }
void print_myobject3_2(std::shared_ptr<MyObject3> obj) { std::cout << obj->toString() << std::endl; }
void print_myobject3_3(const std::shared_ptr<MyObject3> &obj) { std::cout << obj->toString() << std::endl; }
void print_myobject3_4(const std::shared_ptr<MyObject3> *obj) { std::cout << (*obj)->toString() << std::endl; }
test_initializer smart_ptr([](py::module &m) {
py::class_<Object, ref<Object>> obj(m, "Object");
obj.def("getRefCount", &Object::getRefCount);
py::class_<MyObject1, ref<MyObject1>>(m, "MyObject1", obj)
.def(py::init<int>());
m.def("make_object_1", &make_object_1);
m.def("make_object_2", &make_object_2);
m.def("make_myobject1_1", &make_myobject1_1);
m.def("make_myobject1_2", &make_myobject1_2);
m.def("print_object_1", &print_object_1);
m.def("print_object_2", &print_object_2);
m.def("print_object_3", &print_object_3);
m.def("print_object_4", &print_object_4);
m.def("print_myobject1_1", &print_myobject1_1);
m.def("print_myobject1_2", &print_myobject1_2);
m.def("print_myobject1_3", &print_myobject1_3);
m.def("print_myobject1_4", &print_myobject1_4);
py::class_<MyObject2, std::shared_ptr<MyObject2>>(m, "MyObject2")
.def(py::init<int>());
m.def("make_myobject2_1", &make_myobject2_1);
m.def("make_myobject2_2", &make_myobject2_2);
m.def("print_myobject2_1", &print_myobject2_1);
m.def("print_myobject2_2", &print_myobject2_2);
m.def("print_myobject2_3", &print_myobject2_3);
m.def("print_myobject2_4", &print_myobject2_4);
py::class_<MyObject3, std::shared_ptr<MyObject3>>(m, "MyObject3")
.def(py::init<int>());
m.def("make_myobject3_1", &make_myobject3_1);
m.def("make_myobject3_2", &make_myobject3_2);
m.def("print_myobject3_1", &print_myobject3_1);
m.def("print_myobject3_2", &print_myobject3_2);
m.def("print_myobject3_3", &print_myobject3_3);
m.def("print_myobject3_4", &print_myobject3_4);
py::class_<MyObject4, std::unique_ptr<MyObject4, py::nodelete>>(m, "MyObject4")
.def(py::init<int>())
.def_readwrite("value", &MyObject4::value);
py::implicitly_convertible<py::int_, MyObject1>();
// Expose constructor stats for the ref type
m.def("cstats_ref", &ConstructorStats::get<ref_tag>);
});