Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 1 | /* |
| 2 | example/example12.cpp -- overriding virtual functions from Python |
| 3 | |
Wenzel Jakob | 8cb6cb3 | 2016-04-17 20:21:41 +0200 | [diff] [blame] | 4 | Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 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" |
Wenzel Jakob | 8f4eb00 | 2015-10-15 18:13:33 +0200 | [diff] [blame] | 11 | #include <pybind11/functional.h> |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 12 | |
| 13 | /* This is an example class that we'll want to be able to extend from Python */ |
| 14 | class Example12 { |
| 15 | public: |
| 16 | Example12(int state) : state(state) { |
| 17 | cout << "Constructing Example12.." << endl; |
| 18 | } |
| 19 | |
| 20 | ~Example12() { |
| 21 | cout << "Destructing Example12.." << endl; |
| 22 | } |
| 23 | |
| 24 | virtual int run(int value) { |
| 25 | std::cout << "Original implementation of Example12::run(state=" << state |
| 26 | << ", value=" << value << ")" << std::endl; |
| 27 | return state + value; |
| 28 | } |
| 29 | |
jmabille | 9cfa71f | 2016-02-23 22:41:07 +0100 | [diff] [blame] | 30 | virtual bool run_bool() = 0; |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 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 PyExample12 : public Example12 { |
| 38 | public: |
| 39 | using Example12::Example12; /* Inherit constructors */ |
| 40 | |
| 41 | virtual int run(int value) { |
| 42 | /* Generate wrapping code that enables native function overloading */ |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 43 | PYBIND11_OVERLOAD( |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 44 | int, /* Return type */ |
| 45 | Example12, /* Parent class */ |
| 46 | run, /* Name of function */ |
| 47 | value /* Argument(s) */ |
| 48 | ); |
| 49 | } |
| 50 | |
jmabille | 9cfa71f | 2016-02-23 22:41:07 +0100 | [diff] [blame] | 51 | virtual bool run_bool() { |
| 52 | PYBIND11_OVERLOAD_PURE( |
Wenzel Jakob | 1dc940d | 2016-04-18 10:34:27 +0200 | [diff] [blame] | 53 | bool, /* Return type */ |
| 54 | Example12, /* 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 */ |
jmabille | 9cfa71f | 2016-02-23 22:41:07 +0100 | [diff] [blame] | 58 | ); |
| 59 | } |
| 60 | |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 61 | virtual void pure_virtual() { |
Wenzel Jakob | b1b7140 | 2015-10-18 16:48:30 +0200 | [diff] [blame] | 62 | PYBIND11_OVERLOAD_PURE( |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 63 | void, /* Return type */ |
| 64 | Example12, /* Parent class */ |
Wenzel Jakob | 1dc940d | 2016-04-18 10:34:27 +0200 | [diff] [blame] | 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 */ |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 68 | ); |
| 69 | } |
| 70 | }; |
| 71 | |
| 72 | int runExample12(Example12 *ex, int value) { |
| 73 | return ex->run(value); |
| 74 | } |
| 75 | |
jmabille | 9cfa71f | 2016-02-23 22:41:07 +0100 | [diff] [blame] | 76 | bool runExample12Bool(Example12* ex) { |
| 77 | return ex->run_bool(); |
| 78 | } |
| 79 | |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 80 | void runExample12Virtual(Example12 *ex) { |
| 81 | ex->pure_virtual(); |
| 82 | } |
| 83 | |
| 84 | void init_ex12(py::module &m) { |
| 85 | /* Important: use the wrapper type as a template |
| 86 | argument to class_<>, but use the original name |
| 87 | to denote the type */ |
| 88 | py::class_<PyExample12>(m, "Example12") |
| 89 | /* Declare that 'PyExample12' is really an alias for the original type 'Example12' */ |
| 90 | .alias<Example12>() |
| 91 | .def(py::init<int>()) |
Wenzel Jakob | 43b09af | 2016-05-21 01:11:11 +0200 | [diff] [blame] | 92 | /* Copy constructor (not needed in this case, but should generally be declared in this way) */ |
| 93 | .def(py::init<const PyExample12 &>()) |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 94 | /* Reference original class in function definitions */ |
| 95 | .def("run", &Example12::run) |
jmabille | 9cfa71f | 2016-02-23 22:41:07 +0100 | [diff] [blame] | 96 | .def("run_bool", &Example12::run_bool) |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 97 | .def("pure_virtual", &Example12::pure_virtual); |
| 98 | |
| 99 | m.def("runExample12", &runExample12); |
jmabille | 9cfa71f | 2016-02-23 22:41:07 +0100 | [diff] [blame] | 100 | m.def("runExample12Bool", &runExample12Bool); |
Wenzel Jakob | a2f6fde | 2015-10-01 16:46:03 +0200 | [diff] [blame] | 101 | m.def("runExample12Virtual", &runExample12Virtual); |
| 102 | } |