mirror of
https://github.com/pybind/pybind11.git
synced 2024-11-26 15:12:01 +00:00
b3f3d79f4c
This renames example files from `exampleN` to `example-description`. Specifically, the following renaming is applied: example1 -> example-methods-and-attributes example2 -> example-python-types example3 -> example-operator-overloading example4 -> example-constants-and-functions example5 -> example-callbacks (*) example6 -> example-sequence-and-iterators example7 -> example-buffers example8 -> example-custom-ref-counting example9 -> example-modules example10 -> example-numpy-vectorize example11 -> example-arg-keywords-and-defaults example12 -> example-virtual-functions example13 -> example-keep-alive example14 -> example-opaque-types example15 -> example-pickling example16 -> example-inheritance example17 -> example-stl-binders example18 -> example-eval example19 -> example-custom-exceptions * the inheritance parts of example5 are moved into example-inheritance (previously example16), and the remainder is left as example-callbacks. This commit also renames the internal variables ("Example1", "Example2", "Example4", etc.) into non-numeric names ("ExampleMandA", "ExamplePythonTypes", "ExampleWithEnum", etc.) to correspond to the file renaming. The order of tests is preserved, but this can easily be changed if there is some more natural ordering by updating the list in examples/CMakeLists.txt.
148 lines
5.6 KiB
C++
148 lines
5.6 KiB
C++
/*
|
|
example/example-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 "example.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) {
|
|
std::cout << toString() << " constructor" << std::endl;
|
|
}
|
|
|
|
std::string toString() const {
|
|
return "MyObject1[" + std::to_string(value) + "]";
|
|
}
|
|
|
|
protected:
|
|
virtual ~MyObject1() {
|
|
std::cout << toString() << " destructor" << std::endl;
|
|
}
|
|
|
|
private:
|
|
int value;
|
|
};
|
|
|
|
/// Object managed by a std::shared_ptr<>
|
|
class MyObject2 {
|
|
public:
|
|
MyObject2(int value) : value(value) {
|
|
std::cout << toString() << " constructor" << std::endl;
|
|
}
|
|
|
|
std::string toString() const {
|
|
return "MyObject2[" + std::to_string(value) + "]";
|
|
}
|
|
|
|
virtual ~MyObject2() {
|
|
std::cout << toString() << " destructor" << std::endl;
|
|
}
|
|
|
|
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) {
|
|
std::cout << toString() << " constructor" << std::endl;
|
|
}
|
|
|
|
std::string toString() const {
|
|
return "MyObject3[" + std::to_string(value) + "]";
|
|
}
|
|
|
|
virtual ~MyObject3() {
|
|
std::cout << toString() << " destructor" << std::endl;
|
|
}
|
|
|
|
private:
|
|
int value;
|
|
};
|
|
|
|
/// 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; }
|
|
|
|
void init_ex_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::implicitly_convertible<py::int_, MyObject1>();
|
|
}
|