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

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
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29

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

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

    virtual int run(int value) {
        std::cout << "Original implementation of Example12::run(state=" << state
                  << ", value=" << value << ")" << std::endl;
        return state + value;
    }

30
    virtual bool run_bool() = 0;
31
32
33
34
35
36
37
38
39
40
41
42
    virtual void pure_virtual() = 0;
private:
    int state;
};

/* This is a wrapper class that must be generated */
class PyExample12 : public Example12 {
public:
    using Example12::Example12; /* Inherit constructors */

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

51
52
    virtual bool run_bool() {
        PYBIND11_OVERLOAD_PURE(
53
54
55
56
57
            bool,         /* Return type */
            Example12,    /* Parent class */
            run_bool,     /* Name of function */
                          /* This function has no arguments. The trailing comma
                             in the previous line is needed for some compilers */
58
        );
59
        throw std::runtime_error("this will never be reached");
60
61
    }

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

int runExample12(Example12 *ex, int value) {
    return ex->run(value);
}

77
78
79
80
bool runExample12Bool(Example12* ex) {
    return ex->run_bool();
}

81
82
83
84
85
86
87
88
89
90
91
92
93
94
void runExample12Virtual(Example12 *ex) {
    ex->pure_virtual();
}

void init_ex12(py::module &m) {
    /* Important: use the wrapper type as a template
       argument to class_<>, but use the original name
       to denote the type */
    py::class_<PyExample12>(m, "Example12")
        /* Declare that 'PyExample12' is really an alias for the original type 'Example12' */
        .alias<Example12>()
        .def(py::init<int>())
        /* Reference original class in function definitions */
        .def("run", &Example12::run)
95
        .def("run_bool", &Example12::run_bool)
96
97
98
        .def("pure_virtual", &Example12::pure_virtual);

    m.def("runExample12", &runExample12);
99
    m.def("runExample12Bool", &runExample12Bool);
100
101
    m.def("runExample12Virtual", &runExample12Virtual);
}