blob: c099aa81f3bd9d3f1e70234728b76e550d866ddd [file] [log] [blame]
Wenzel Jakob5f218b32016-01-17 22:36:39 +01001/*
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04002 example/example-keep-alive.cpp -- keep_alive modifier (pybind11's version
Wenzel Jakob5f218b32016-01-17 22:36:39 +01003 of Boost.Python's with_custodian_and_ward / with_custodian_and_ward_postcall)
4
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02005 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob5f218b32016-01-17 22:36:39 +01006
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
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040027void init_ex_keep_alive(py::module &m) {
Wenzel Jakob5f218b32016-01-17 22:36:39 +010028 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}