blob: 782db3109e2aafbb10f3d145fefd9843a4a0149a [file] [log] [blame]
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001/*
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
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
13class Child {
14public:
15 Child() { std::cout << "Allocating child." << std::endl; }
16 ~Child() { std::cout << "Releasing child." << std::endl; }
17};
18
19class Parent {
20public:
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
27void 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}