"example/example-virtual-functions.cpp" did not exist on "86d825f3302701d81414ddd3d38bcd09433076bc"
example5.cpp 2.57 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
/*
2
3
    example/example5.cpp -- inheritance, callbacks, acquiring and releasing the
    global interpreter lock
Wenzel Jakob's avatar
Wenzel Jakob committed
4
5
6
7
8
9
10
11

    Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>

    All rights reserved. Use of this source code is governed by a
    BSD-style license that can be found in the LICENSE file.
*/

#include "example.h"
12
#include <pybind/functional.h>
Wenzel Jakob's avatar
Wenzel Jakob committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67


class Pet {
public:
    Pet(const std::string &name, const std::string &species)
        : m_name(name), m_species(species) {}
    std::string name() const { return m_name; }
    std::string species() const { return m_species; }
private:
    std::string m_name;
    std::string m_species;
};

class Dog : public Pet {
public:
    Dog(const std::string &name) : Pet(name, "dog") {}
    void bark() const { std::cout << "Woof!" << std::endl; }
};

void pet_print(const Pet &pet) {
    std::cout << pet.name() + " is a " + pet.species() << std::endl;
}

void dog_bark(const Dog &dog) {
    dog.bark();
}

class Example5  {
public:
    Example5(py::handle self, int state)
        : self(self), state(state) {
        cout << "Constructing Example5.." << endl;
    }

    ~Example5() {
        cout << "Destructing Example5.." << endl;
    }

    void callback(int value) {
		py::gil_scoped_acquire gil;
        cout << "In Example5::callback() " << endl;
        py::object method = self.attr("callback");
        method.call(state, value);
    }
private:
    py::handle self;
    int state;
};

bool test_callback1(py::object func) {
    func.call();
    return false;
}

int test_callback2(py::object func) {
Wenzel Jakob's avatar
Wenzel Jakob committed
68
    py::object result = func.call("Hello", 'x', true, 5);
Wenzel Jakob's avatar
Wenzel Jakob committed
69
70
71
72
73
74
75
76
    return result.cast<int>();
}

void test_callback3(Example5 *ex, int value) {
	py::gil_scoped_release gil;
    ex->callback(value);
}

77
78
79
80
81
82
83
84
void test_callback4(const std::function<int(int)> &func) {
    cout << "func(43) = " << func(43)<< std::endl;
}

std::function<int(int)> test_callback5() {
    return [](int i) { return i+1; };
}

Wenzel Jakob's avatar
Wenzel Jakob committed
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
void init_ex5(py::module &m) {
    py::class_<Pet> pet_class(m, "Pet");
    pet_class
        .def(py::init<std::string, std::string>())
        .def("name", &Pet::name)
        .def("species", &Pet::species);

    py::class_<Dog>(m, "Dog", pet_class)
        .def(py::init<std::string>());

    m.def("pet_print", pet_print);
    m.def("dog_bark", dog_bark);

    m.def("test_callback1", &test_callback1);
    m.def("test_callback2", &test_callback2);
    m.def("test_callback3", &test_callback3);
101
102
    m.def("test_callback4", &test_callback4);
    m.def("test_callback5", &test_callback5);
Wenzel Jakob's avatar
Wenzel Jakob committed
103
104
105
106

    py::class_<Example5>(m, "Example5")
        .def(py::init<py::object, int>());
}