Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | example/example13.cpp -- keep_alive modifier (pybind11's version |
| 3 | of Boost.Python's with_custodian_and_ward / with_custodian_and_ward_postcall) |
| 4 | |
Wenzel Jakob | 8cb6cb3 | 2016-04-17 20:21:41 +0200 | [diff] [blame] | 5 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
Wenzel Jakob | 5f218b3 | 2016-01-17 22:36:39 +0100 | [diff] [blame] | 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 | |
| 13 | class Child { |
| 14 | public: |
| 15 | Child() { std::cout << "Allocating child." << std::endl; } |
| 16 | ~Child() { std::cout << "Releasing child." << std::endl; } |
| 17 | }; |
| 18 | |
| 19 | class Parent { |
| 20 | public: |
| 21 | Parent() { std::cout << "Allocating parent." << std::endl; } |
| 22 | ~Parent() { std::cout << "Releasing parent." << std::endl; } |
| 23 | void addChild(Child *) { } |
| 24 | Child *returnChild() { return new Child(); } |
| 25 | }; |
| 26 | |
| 27 | void init_ex13(py::module &m) { |
| 28 | py::class_<Parent>(m, "Parent") |
| 29 | .def(py::init<>()) |
| 30 | .def("addChild", &Parent::addChild) |
| 31 | .def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>()) |
| 32 | .def("returnChild", &Parent::returnChild) |
| 33 | .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>()); |
| 34 | |
| 35 | py::class_<Child>(m, "Child") |
| 36 | .def(py::init<>()); |
| 37 | } |