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.

110 lines
2.9 KiB

  1. /*
  2. example/example5.cpp -- inheritance, callbacks, acquiring and releasing the
  3. global interpreter lock
  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. #include <pybind11/functional.h>
  10. class Pet {
  11. public:
  12. Pet(const std::string &name, const std::string &species)
  13. : m_name(name), m_species(species) {}
  14. std::string name() const { return m_name; }
  15. std::string species() const { return m_species; }
  16. private:
  17. std::string m_name;
  18. std::string m_species;
  19. };
  20. class Dog : public Pet {
  21. public:
  22. Dog(const std::string &name) : Pet(name, "dog") {}
  23. void bark() const { std::cout << "Woof!" << std::endl; }
  24. };
  25. class Rabbit : public Pet {
  26. public:
  27. Rabbit(const std::string &name) : Pet(name, "parrot") {}
  28. };
  29. void pet_print(const Pet &pet) {
  30. std::cout << pet.name() + " is a " + pet.species() << std::endl;
  31. }
  32. void dog_bark(const Dog &dog) {
  33. dog.bark();
  34. }
  35. bool test_callback1(py::object func) {
  36. func.call();
  37. return false;
  38. }
  39. int test_callback2(py::object func) {
  40. py::object result = func.call("Hello", 'x', true, 5);
  41. return result.cast<int>();
  42. }
  43. void test_callback3(const std::function<int(int)> &func) {
  44. cout << "func(43) = " << func(43)<< std::endl;
  45. }
  46. std::function<int(int)> test_callback4() {
  47. return [](int i) { return i+1; };
  48. }
  49. void init_ex5(py::module &m) {
  50. py::class_<Pet> pet_class(m, "Pet");
  51. pet_class
  52. .def(py::init<std::string, std::string>())
  53. .def("name", &Pet::name)
  54. .def("species", &Pet::species);
  55. /* One way of declaring a subclass relationship: reference parent's class_ object */
  56. py::class_<Dog>(m, "Dog", pet_class)
  57. .def(py::init<std::string>());
  58. /* Another way of declaring a subclass relationship: reference parent's C++ type */
  59. py::class_<Rabbit>(m, "Rabbit", py::base<Pet>())
  60. .def(py::init<std::string>());
  61. m.def("pet_print", pet_print);
  62. m.def("dog_bark", dog_bark);
  63. m.def("test_callback1", &test_callback1);
  64. m.def("test_callback2", &test_callback2);
  65. m.def("test_callback3", &test_callback3);
  66. m.def("test_callback4", &test_callback4);
  67. /* Test cleanup of lambda closure */
  68. struct Payload {
  69. Payload() {
  70. std::cout << "Payload constructor" << std::endl;
  71. }
  72. ~Payload() {
  73. std::cout << "Payload destructor" << std::endl;
  74. }
  75. Payload(const Payload &) {
  76. std::cout << "Payload copy constructor" << std::endl;
  77. }
  78. Payload(Payload &&) {
  79. std::cout << "Payload move constructor" << std::endl;
  80. }
  81. };
  82. m.def("test_cleanup", []() -> std::function<void(void)> {
  83. Payload p;
  84. return [p]() {
  85. /* p should be cleaned up when the returned function is garbage collected */
  86. };
  87. });
  88. }