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 | |
| 34 | /// Make pybind aware of the ref-counted wrapper type |
| 35 | namespace pybind { namespace detail { |
| 36 | template <typename T> class type_caster<ref<T>> |
| 37 | : public type_caster_holder<T, ref<T>> { }; |
| 38 | }} |
| 39 | |
| 40 | Object *make_object_1() { return new MyObject(1); } |
| 41 | ref<Object> make_object_2() { return new MyObject(2); } |
| 42 | MyObject *make_myobject_4() { return new MyObject(4); } |
| 43 | ref<MyObject> make_myobject_5() { return new MyObject(5); } |
| 44 | |
| 45 | void print_object_1(const Object *obj) { std::cout << obj->toString() << std::endl; } |
| 46 | void print_object_2(ref<Object> obj) { std::cout << obj->toString() << std::endl; } |
| 47 | void print_object_3(const ref<Object> &obj) { std::cout << obj->toString() << std::endl; } |
| 48 | void print_object_4(const ref<Object> *obj) { std::cout << (*obj)->toString() << std::endl; } |
| 49 | |
| 50 | void print_myobject_1(const MyObject *obj) { std::cout << obj->toString() << std::endl; } |
| 51 | void print_myobject_2(ref<MyObject> obj) { std::cout << obj->toString() << std::endl; } |
| 52 | void print_myobject_3(const ref<MyObject> &obj) { std::cout << obj->toString() << std::endl; } |
| 53 | void print_myobject_4(const ref<MyObject> *obj) { std::cout << (*obj)->toString() << std::endl; } |
| 54 | |
| 55 | void init_ex8(py::module &m) { |
| 56 | py::class_<Object, ref<Object>> obj(m, "Object"); |
| 57 | obj.def("getRefCount", &Object::getRefCount); |
| 58 | |
| 59 | py::class_<MyObject, ref<MyObject>>(m, "MyObject", obj) |
| 60 | .def(py::init<int>()); |
| 61 | |
| 62 | m.def("make_object_1", &make_object_1); |
| 63 | m.def("make_object_2", &make_object_2); |
| 64 | m.def("make_myobject_4", &make_myobject_4); |
| 65 | m.def("make_myobject_5", &make_myobject_5); |
| 66 | m.def("print_object_1", &print_object_1); |
| 67 | m.def("print_object_2", &print_object_2); |
| 68 | m.def("print_object_3", &print_object_3); |
| 69 | m.def("print_object_4", &print_object_4); |
| 70 | m.def("print_myobject_1", &print_myobject_1); |
| 71 | m.def("print_myobject_2", &print_myobject_2); |
| 72 | m.def("print_myobject_3", &print_myobject_3); |
| 73 | m.def("print_myobject_4", &print_myobject_4); |
| 74 | |
| 75 | py::implicitly_convertible<py::int_, MyObject>(); |
| 76 | } |