Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1 | /* |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 2 | example/example8.cpp -- binding classes with custom reference counting, |
| 3 | implicit conversions between types |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 4 | |
| 5 | Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch> |
| 6 | |
| 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 | |
| 11 | #include "example.h" |
| 12 | #include "object.h" |
| 13 | |
| 14 | /// Object subclass |
| 15 | class MyObject : public Object { |
| 16 | public: |
| 17 | MyObject(int value) : value(value) { |
| 18 | std::cout << toString() << " constructor" << std::endl; |
| 19 | } |
| 20 | |
| 21 | std::string toString() const { |
| 22 | return "MyObject[" + std::to_string(value) + "]"; |
| 23 | } |
| 24 | |
| 25 | protected: |
| 26 | virtual ~MyObject() { |
| 27 | std::cout << toString() << " destructor" << std::endl; |
| 28 | } |
| 29 | |
| 30 | private: |
| 31 | int value; |
| 32 | }; |
| 33 | |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 34 | class MyObject2 : public std::enable_shared_from_this<MyObject2> { |
| 35 | public: |
| 36 | MyObject2(int value) : value(value) { |
| 37 | std::cout << toString() << " constructor" << std::endl; |
| 38 | } |
| 39 | |
| 40 | std::string toString() const { |
| 41 | return "MyObject2[" + std::to_string(value) + "]"; |
| 42 | } |
| 43 | |
| 44 | virtual ~MyObject2() { |
| 45 | std::cout << toString() << " destructor" << std::endl; |
| 46 | } |
| 47 | |
| 48 | private: |
| 49 | int value; |
| 50 | }; |
| 51 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 52 | /// Make pybind aware of the ref-counted wrapper type |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 53 | PYBIND11_DECLARE_HOLDER_TYPE(T, ref<T>); |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 54 | PYBIND11_DECLARE_HOLDER_TYPE(T, std::shared_ptr<T>); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 55 | |
| 56 | Object *make_object_1() { return new MyObject(1); } |
| 57 | ref<Object> make_object_2() { return new MyObject(2); } |
| 58 | MyObject *make_myobject_4() { return new MyObject(4); } |
| 59 | ref<MyObject> make_myobject_5() { return new MyObject(5); } |
| 60 | |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 61 | MyObject2 *make_myobject2_1() { return new MyObject2(1); } |
| 62 | std::shared_ptr<MyObject2> make_myobject2_2() { return std::make_shared<MyObject2>(2); } |
| 63 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 64 | void print_object_1(const Object *obj) { std::cout << obj->toString() << std::endl; } |
| 65 | void print_object_2(ref<Object> obj) { std::cout << obj->toString() << std::endl; } |
| 66 | void print_object_3(const ref<Object> &obj) { std::cout << obj->toString() << std::endl; } |
| 67 | void print_object_4(const ref<Object> *obj) { std::cout << (*obj)->toString() << std::endl; } |
| 68 | |
| 69 | void print_myobject_1(const MyObject *obj) { std::cout << obj->toString() << std::endl; } |
| 70 | void print_myobject_2(ref<MyObject> obj) { std::cout << obj->toString() << std::endl; } |
| 71 | void print_myobject_3(const ref<MyObject> &obj) { std::cout << obj->toString() << std::endl; } |
| 72 | void print_myobject_4(const ref<MyObject> *obj) { std::cout << (*obj)->toString() << std::endl; } |
| 73 | |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 74 | void print_myobject2_1(const MyObject2 *obj) { std::cout << obj->toString() << std::endl; } |
| 75 | void print_myobject2_2(std::shared_ptr<MyObject2> obj) { std::cout << obj->toString() << std::endl; } |
| 76 | void print_myobject2_3(const std::shared_ptr<MyObject2> &obj) { std::cout << obj->toString() << std::endl; } |
| 77 | void print_myobject2_4(const std::shared_ptr<MyObject2> *obj) { std::cout << (*obj)->toString() << std::endl; } |
| 78 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 79 | void init_ex8(py::module &m) { |
| 80 | py::class_<Object, ref<Object>> obj(m, "Object"); |
| 81 | obj.def("getRefCount", &Object::getRefCount); |
| 82 | |
| 83 | py::class_<MyObject, ref<MyObject>>(m, "MyObject", obj) |
| 84 | .def(py::init<int>()); |
| 85 | |
| 86 | m.def("make_object_1", &make_object_1); |
| 87 | m.def("make_object_2", &make_object_2); |
| 88 | m.def("make_myobject_4", &make_myobject_4); |
| 89 | m.def("make_myobject_5", &make_myobject_5); |
| 90 | m.def("print_object_1", &print_object_1); |
| 91 | m.def("print_object_2", &print_object_2); |
| 92 | m.def("print_object_3", &print_object_3); |
| 93 | m.def("print_object_4", &print_object_4); |
| 94 | m.def("print_myobject_1", &print_myobject_1); |
| 95 | m.def("print_myobject_2", &print_myobject_2); |
| 96 | m.def("print_myobject_3", &print_myobject_3); |
| 97 | m.def("print_myobject_4", &print_myobject_4); |
| 98 | |
Wenzel Jakob | 6e213c9 | 2015-11-24 23:05:58 +0100 | [diff] [blame] | 99 | py::class_<MyObject2, std::shared_ptr<MyObject2>>(m, "MyObject2") |
| 100 | .def(py::init<int>()); |
| 101 | m.def("make_myobject2_1", &make_myobject2_1); |
| 102 | m.def("make_myobject2_2", &make_myobject2_2); |
| 103 | m.def("print_myobject2_1", &print_myobject2_1); |
| 104 | m.def("print_myobject2_2", &print_myobject2_2); |
| 105 | m.def("print_myobject2_3", &print_myobject2_3); |
| 106 | m.def("print_myobject2_4", &print_myobject2_4); |
| 107 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 108 | py::implicitly_convertible<py::int_, MyObject>(); |
| 109 | } |