Jason Rhinelander | b3f3d79 | 2016-07-18 16:43:18 -0400 | [diff] [blame] | 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 | All rights reserved. Use of this source code is governed by a |
| 7 | BSD-style license that can be found in the LICENSE file. |
| 8 | */ |
| 9 | |
| 10 | #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 | ExampleVirt(int state) : state(state) { |
| 17 | cout << "Constructing ExampleVirt.." << endl; |
| 18 | } |
| 19 | |
| 20 | ~ExampleVirt() { |
| 21 | cout << "Destructing ExampleVirt.." << endl; |
| 22 | } |
| 23 | |
| 24 | virtual int run(int value) { |
| 25 | std::cout << "Original implementation of ExampleVirt::run(state=" << state |
| 26 | << ", value=" << value << ")" << std::endl; |
| 27 | return state + value; |
| 28 | } |
| 29 | |
| 30 | virtual bool run_bool() = 0; |
| 31 | virtual void pure_virtual() = 0; |
| 32 | private: |
| 33 | int state; |
| 34 | }; |
| 35 | |
| 36 | /* This is a wrapper class that must be generated */ |
| 37 | class PyExampleVirt : public ExampleVirt { |
| 38 | public: |
| 39 | using ExampleVirt::ExampleVirt; /* Inherit constructors */ |
| 40 | |
| 41 | virtual int run(int value) { |
| 42 | /* Generate wrapping code that enables native function overloading */ |
| 43 | PYBIND11_OVERLOAD( |
| 44 | int, /* Return type */ |
| 45 | ExampleVirt, /* Parent class */ |
| 46 | run, /* Name of function */ |
| 47 | value /* Argument(s) */ |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | virtual bool run_bool() { |
| 52 | PYBIND11_OVERLOAD_PURE( |
| 53 | bool, /* Return type */ |
| 54 | ExampleVirt, /* Parent class */ |
| 55 | run_bool, /* Name of function */ |
| 56 | /* This function has no arguments. The trailing comma |
| 57 | 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 | pure_virtual, /* Name of function */ |
| 66 | /* This function has no arguments. The trailing comma |
| 67 | in the previous line is needed for some compilers */ |
| 68 | ); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | int runExampleVirt(ExampleVirt *ex, int value) { |
| 73 | return ex->run(value); |
| 74 | } |
| 75 | |
| 76 | bool runExampleVirtBool(ExampleVirt* ex) { |
| 77 | return ex->run_bool(); |
| 78 | } |
| 79 | |
| 80 | void runExampleVirtVirtual(ExampleVirt *ex) { |
| 81 | ex->pure_virtual(); |
| 82 | } |
| 83 | |
| 84 | void init_ex_virtual_functions(py::module &m) { |
| 85 | /* Important: indicate the trampoline class PyExampleVirt using the third |
| 86 | argument to py::class_. The second argument with the unique pointer |
| 87 | is simply the default holder type used by pybind11. */ |
| 88 | py::class_<ExampleVirt, std::unique_ptr<ExampleVirt>, PyExampleVirt>(m, "ExampleVirt") |
| 89 | .def(py::init<int>()) |
| 90 | /* Reference original class in function definitions */ |
| 91 | .def("run", &ExampleVirt::run) |
| 92 | .def("run_bool", &ExampleVirt::run_bool) |
| 93 | .def("pure_virtual", &ExampleVirt::pure_virtual); |
| 94 | |
| 95 | m.def("runExampleVirt", &runExampleVirt); |
| 96 | m.def("runExampleVirtBool", &runExampleVirtBool); |
| 97 | m.def("runExampleVirtVirtual", &runExampleVirtVirtual); |
| 98 | } |