blob: cabd84795a071c609565032f0d9b821a1aca5b24 [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Wenzel Jakoba576e6a2015-07-29 17:51:54 +02002 example/example5.cpp -- inheritance, callbacks, acquiring and releasing the
3 global interpreter lock
Wenzel Jakob38bd7112015-07-05 20:05:44 +02004
5 Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
6
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"
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020012#include <pybind11/functional.h>
Wenzel Jakob38bd7112015-07-05 20:05:44 +020013
14
15class Pet {
16public:
17 Pet(const std::string &name, const std::string &species)
18 : m_name(name), m_species(species) {}
19 std::string name() const { return m_name; }
20 std::string species() const { return m_species; }
21private:
22 std::string m_name;
23 std::string m_species;
24};
25
26class Dog : public Pet {
27public:
28 Dog(const std::string &name) : Pet(name, "dog") {}
29 void bark() const { std::cout << "Woof!" << std::endl; }
30};
31
32void pet_print(const Pet &pet) {
33 std::cout << pet.name() + " is a " + pet.species() << std::endl;
34}
35
36void dog_bark(const Dog &dog) {
37 dog.bark();
38}
39
Wenzel Jakob38bd7112015-07-05 20:05:44 +020040bool test_callback1(py::object func) {
41 func.call();
42 return false;
43}
44
45int test_callback2(py::object func) {
Wenzel Jakob7b8e0322015-08-28 17:49:15 +020046 py::object result = func.call("Hello", 'x', true, 5);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020047 return result.cast<int>();
48}
49
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020050void test_callback3(const std::function<int(int)> &func) {
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020051 cout << "func(43) = " << func(43)<< std::endl;
52}
53
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020054std::function<int(int)> test_callback4() {
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020055 return [](int i) { return i+1; };
56}
57
Wenzel Jakob38bd7112015-07-05 20:05:44 +020058void init_ex5(py::module &m) {
59 py::class_<Pet> pet_class(m, "Pet");
60 pet_class
61 .def(py::init<std::string, std::string>())
62 .def("name", &Pet::name)
63 .def("species", &Pet::species);
64
65 py::class_<Dog>(m, "Dog", pet_class)
66 .def(py::init<std::string>());
67
68 m.def("pet_print", pet_print);
69 m.def("dog_bark", dog_bark);
70
71 m.def("test_callback1", &test_callback1);
72 m.def("test_callback2", &test_callback2);
73 m.def("test_callback3", &test_callback3);
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020074 m.def("test_callback4", &test_callback4);
Wenzel Jakob19208fe2015-10-13 17:37:25 +020075
76 /* Test cleanup of lambda closure */
77
78 struct Payload {
79 Payload() {
80 std::cout << "Payload constructor" << std::endl;
81 }
82 ~Payload() {
83 std::cout << "Payload destructor" << std::endl;
84 }
85 Payload(const Payload &) {
86 std::cout << "Payload copy constructor" << std::endl;
87 }
88 Payload(Payload &&) {
89 std::cout << "Payload move constructor" << std::endl;
90 }
91 };
92
93 m.def("test_cleanup", []() -> std::function<void(void)> {
94 Payload p;
95
96 return [p]() {
97 /* p should be cleaned up when the returned function is garbage collected */
98 };
99 });
Wenzel Jakob38bd7112015-07-05 20:05:44 +0200100}