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.

116 lines
3.1 KiB

  1. /*
  2. example/example5.cpp -- inheritance, callbacks, acquiring and releasing the
  3. global interpreter lock
  4. Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.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();
  37. return false;
  38. }
  39. int test_callback2(py::object func) {
  40. py::object result = func("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. py::cpp_function test_callback5() {
  50. return py::cpp_function([](int i) { return i+1; },
  51. py::arg("number"));
  52. }
  53. void init_ex5(py::module &m) {
  54. py::class_<Pet> pet_class(m, "Pet");
  55. pet_class
  56. .def(py::init<std::string, std::string>())
  57. .def("name", &Pet::name)
  58. .def("species", &Pet::species);
  59. /* One way of declaring a subclass relationship: reference parent's class_ object */
  60. py::class_<Dog>(m, "Dog", pet_class)
  61. .def(py::init<std::string>());
  62. /* Another way of declaring a subclass relationship: reference parent's C++ type */
  63. py::class_<Rabbit>(m, "Rabbit", py::base<Pet>())
  64. .def(py::init<std::string>());
  65. m.def("pet_print", pet_print);
  66. m.def("dog_bark", dog_bark);
  67. m.def("test_callback1", &test_callback1);
  68. m.def("test_callback2", &test_callback2);
  69. m.def("test_callback3", &test_callback3);
  70. m.def("test_callback4", &test_callback4);
  71. m.def("test_callback5", &test_callback5);
  72. /* Test cleanup of lambda closure */
  73. struct Payload {
  74. Payload() {
  75. std::cout << "Payload constructor" << std::endl;
  76. }
  77. ~Payload() {
  78. std::cout << "Payload destructor" << std::endl;
  79. }
  80. Payload(const Payload &) {
  81. std::cout << "Payload copy constructor" << std::endl;
  82. }
  83. Payload(Payload &&) {
  84. std::cout << "Payload move constructor" << std::endl;
  85. }
  86. };
  87. m.def("test_cleanup", []() -> std::function<void(void)> {
  88. Payload p;
  89. return [p]() {
  90. /* p should be cleaned up when the returned function is garbage collected */
  91. };
  92. });
  93. }