2016-01-17 21:36:39 +00:00
|
|
|
/*
|
2016-08-12 11:50:00 +00:00
|
|
|
tests/test_keep_alive.cpp -- keep_alive modifier (pybind11's version
|
2016-01-17 21:36:39 +00:00
|
|
|
of Boost.Python's with_custodian_and_ward / with_custodian_and_ward_postcall)
|
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2016-01-17 21:36:39 +00:00
|
|
|
|
|
|
|
All rights reserved. Use of this source code is governed by a
|
|
|
|
BSD-style license that can be found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2016-08-12 11:50:00 +00:00
|
|
|
#include "pybind11_tests.h"
|
2016-01-17 21:36:39 +00:00
|
|
|
|
|
|
|
class Child {
|
|
|
|
public:
|
2016-09-06 22:50:10 +00:00
|
|
|
Child() { py::print("Allocating child."); }
|
|
|
|
~Child() { py::print("Releasing child."); }
|
2016-01-17 21:36:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class Parent {
|
|
|
|
public:
|
2016-09-06 22:50:10 +00:00
|
|
|
Parent() { py::print("Allocating parent."); }
|
|
|
|
~Parent() { py::print("Releasing parent."); }
|
2016-01-17 21:36:39 +00:00
|
|
|
void addChild(Child *) { }
|
|
|
|
Child *returnChild() { return new Child(); }
|
2016-08-16 05:50:43 +00:00
|
|
|
Child *returnNullChild() { return nullptr; }
|
2016-01-17 21:36:39 +00:00
|
|
|
};
|
|
|
|
|
2016-09-03 18:54:22 +00:00
|
|
|
test_initializer keep_alive([](py::module &m) {
|
2016-01-17 21:36:39 +00:00
|
|
|
py::class_<Parent>(m, "Parent")
|
|
|
|
.def(py::init<>())
|
|
|
|
.def("addChild", &Parent::addChild)
|
|
|
|
.def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>())
|
|
|
|
.def("returnChild", &Parent::returnChild)
|
2016-08-16 05:50:43 +00:00
|
|
|
.def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>())
|
|
|
|
.def("returnNullChildKeepAliveChild", &Parent::returnNullChild, py::keep_alive<1, 0>())
|
|
|
|
.def("returnNullChildKeepAliveParent", &Parent::returnNullChild, py::keep_alive<0, 1>());
|
2016-01-17 21:36:39 +00:00
|
|
|
|
|
|
|
py::class_<Child>(m, "Child")
|
|
|
|
.def(py::init<>());
|
2016-09-03 18:54:22 +00:00
|
|
|
});
|