example-virtual-functions.cpp 3.12 KB
Newer Older
1
/*
2
    example/example-virtual-functions.cpp -- overriding virtual functions from Python
3

4
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
5
6
7
8
9
10

    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"
11
#include <pybind11/functional.h>
12
13

/* This is an example class that we'll want to be able to extend from Python */
14
class ExampleVirt  {
15
public:
16
17
    ExampleVirt(int state) : state(state) {
        cout << "Constructing ExampleVirt.." << endl;
18
19
    }

20
21
    ~ExampleVirt() {
        cout << "Destructing ExampleVirt.." << endl;
22
23
24
    }

    virtual int run(int value) {
25
        std::cout << "Original implementation of ExampleVirt::run(state=" << state
26
27
28
29
                  << ", value=" << value << ")" << std::endl;
        return state + value;
    }

30
    virtual bool run_bool() = 0;
31
32
33
34
35
36
    virtual void pure_virtual() = 0;
private:
    int state;
};

/* This is a wrapper class that must be generated */
37
class PyExampleVirt : public ExampleVirt {
38
public:
39
    using ExampleVirt::ExampleVirt; /* Inherit constructors */
40
41
42

    virtual int run(int value) {
        /* Generate wrapping code that enables native function overloading */
43
        PYBIND11_OVERLOAD(
44
45
46
47
            int,         /* Return type */
            ExampleVirt, /* Parent class */
            run,         /* Name of function */
            value        /* Argument(s) */
48
49
50
        );
    }

51
52
    virtual bool run_bool() {
        PYBIND11_OVERLOAD_PURE(
53
            bool,         /* Return type */
54
            ExampleVirt,  /* Parent class */
55
56
57
            run_bool,     /* Name of function */
                          /* This function has no arguments. The trailing comma
                             in the previous line is needed for some compilers */
58
59
60
        );
    }

61
    virtual void pure_virtual() {
62
        PYBIND11_OVERLOAD_PURE(
63
            void,         /* Return type */
64
            ExampleVirt,  /* Parent class */
65
66
67
            pure_virtual, /* Name of function */
                          /* This function has no arguments. The trailing comma
                             in the previous line is needed for some compilers */
68
69
70
71
        );
    }
};

72
int runExampleVirt(ExampleVirt *ex, int value) {
73
74
75
    return ex->run(value);
}

76
bool runExampleVirtBool(ExampleVirt* ex) {
77
78
79
    return ex->run_bool();
}

80
void runExampleVirtVirtual(ExampleVirt *ex) {
81
82
83
    ex->pure_virtual();
}

84
85
void init_ex_virtual_functions(py::module &m) {
    /* Important: indicate the trampoline class PyExampleVirt using the third
86
87
       argument to py::class_. The second argument with the unique pointer
       is simply the default holder type used by pybind11. */
88
    py::class_<ExampleVirt, std::unique_ptr<ExampleVirt>, PyExampleVirt>(m, "ExampleVirt")
89
90
        .def(py::init<int>())
        /* Reference original class in function definitions */
91
92
93
        .def("run", &ExampleVirt::run)
        .def("run_bool", &ExampleVirt::run_bool)
        .def("pure_virtual", &ExampleVirt::pure_virtual);
94

95
96
97
    m.def("runExampleVirt", &runExampleVirt);
    m.def("runExampleVirtBool", &runExampleVirtBool);
    m.def("runExampleVirtVirtual", &runExampleVirtVirtual);
98
}