Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 1 | /* |
Wenzel Jakob | a576e6a | 2015-07-29 17:51:54 +0200 | [diff] [blame] | 2 | example/example5.cpp -- inheritance, callbacks, acquiring and releasing the |
| 3 | global interpreter lock |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 4 | |
| 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 Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 12 | #include <pybind/functional.h> |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 13 | |
| 14 | |
| 15 | class Pet { |
| 16 | public: |
| 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; } |
| 21 | private: |
| 22 | std::string m_name; |
| 23 | std::string m_species; |
| 24 | }; |
| 25 | |
| 26 | class Dog : public Pet { |
| 27 | public: |
| 28 | Dog(const std::string &name) : Pet(name, "dog") {} |
| 29 | void bark() const { std::cout << "Woof!" << std::endl; } |
| 30 | }; |
| 31 | |
| 32 | void pet_print(const Pet &pet) { |
| 33 | std::cout << pet.name() + " is a " + pet.species() << std::endl; |
| 34 | } |
| 35 | |
| 36 | void dog_bark(const Dog &dog) { |
| 37 | dog.bark(); |
| 38 | } |
| 39 | |
| 40 | class Example5 { |
| 41 | public: |
| 42 | Example5(py::handle self, int state) |
| 43 | : self(self), state(state) { |
| 44 | cout << "Constructing Example5.." << endl; |
| 45 | } |
| 46 | |
| 47 | ~Example5() { |
| 48 | cout << "Destructing Example5.." << endl; |
| 49 | } |
| 50 | |
| 51 | void callback(int value) { |
| 52 | py::gil_scoped_acquire gil; |
| 53 | cout << "In Example5::callback() " << endl; |
| 54 | py::object method = self.attr("callback"); |
| 55 | method.call(state, value); |
| 56 | } |
| 57 | private: |
| 58 | py::handle self; |
| 59 | int state; |
| 60 | }; |
| 61 | |
| 62 | bool test_callback1(py::object func) { |
| 63 | func.call(); |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | int test_callback2(py::object func) { |
Wenzel Jakob | 7b8e032 | 2015-08-28 17:49:15 +0200 | [diff] [blame^] | 68 | py::object result = func.call("Hello", 'x', true, 5); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 69 | return result.cast<int>(); |
| 70 | } |
| 71 | |
| 72 | void test_callback3(Example5 *ex, int value) { |
| 73 | py::gil_scoped_release gil; |
| 74 | ex->callback(value); |
| 75 | } |
| 76 | |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 77 | void test_callback4(const std::function<int(int)> &func) { |
| 78 | cout << "func(43) = " << func(43)<< std::endl; |
| 79 | } |
| 80 | |
| 81 | std::function<int(int)> test_callback5() { |
| 82 | return [](int i) { return i+1; }; |
| 83 | } |
| 84 | |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 85 | void init_ex5(py::module &m) { |
| 86 | py::class_<Pet> pet_class(m, "Pet"); |
| 87 | pet_class |
| 88 | .def(py::init<std::string, std::string>()) |
| 89 | .def("name", &Pet::name) |
| 90 | .def("species", &Pet::species); |
| 91 | |
| 92 | py::class_<Dog>(m, "Dog", pet_class) |
| 93 | .def(py::init<std::string>()); |
| 94 | |
| 95 | m.def("pet_print", pet_print); |
| 96 | m.def("dog_bark", dog_bark); |
| 97 | |
| 98 | m.def("test_callback1", &test_callback1); |
| 99 | m.def("test_callback2", &test_callback2); |
| 100 | m.def("test_callback3", &test_callback3); |
Wenzel Jakob | 281aa0e | 2015-07-30 15:29:00 +0200 | [diff] [blame] | 101 | m.def("test_callback4", &test_callback4); |
| 102 | m.def("test_callback5", &test_callback5); |
Wenzel Jakob | 38bd711 | 2015-07-05 20:05:44 +0200 | [diff] [blame] | 103 | |
| 104 | py::class_<Example5>(m, "Example5") |
| 105 | .def(py::init<py::object, int>()); |
| 106 | } |