You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
1.1 KiB

  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. Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
  5. All rights reserved. Use of this source code is governed by a
  6. BSD-style license that can be found in the LICENSE file.
  7. */
  8. #include "example.h"
  9. class Child {
  10. public:
  11. Child() { std::cout << "Allocating child." << std::endl; }
  12. ~Child() { std::cout << "Releasing child." << std::endl; }
  13. };
  14. class Parent {
  15. public:
  16. Parent() { std::cout << "Allocating parent." << std::endl; }
  17. ~Parent() { std::cout << "Releasing parent." << std::endl; }
  18. void addChild(Child *) { }
  19. Child *returnChild() { return new Child(); }
  20. };
  21. void init_ex13(py::module &m) {
  22. py::class_<Parent>(m, "Parent")
  23. .def(py::init<>())
  24. .def("addChild", &Parent::addChild)
  25. .def("addChildKeepAlive", &Parent::addChild, py::keep_alive<1, 2>())
  26. .def("returnChild", &Parent::returnChild)
  27. .def("returnChildKeepAlive", &Parent::returnChild, py::keep_alive<1, 0>());
  28. py::class_<Child>(m, "Child")
  29. .def(py::init<>());
  30. }