blob: ab1b48294b242549ea0ffe0a7bc55b180be936b9 [file] [log] [blame]
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +02001/*
2 example/example12.cpp -- overriding virtual functions from Python
3
4 Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.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"
Wenzel Jakob8f4eb002015-10-15 18:13:33 +020011#include <pybind11/functional.h>
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020012
13/* This is an example class that we'll want to be able to extend from Python */
14class Example12 {
15public:
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
jmabille9cfa71f2016-02-23 22:41:07 +010030 virtual bool run_bool() = 0;
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020031 virtual void pure_virtual() = 0;
32private:
33 int state;
34};
35
36/* This is a wrapper class that must be generated */
37class PyExample12 : public Example12 {
38public:
39 using Example12::Example12; /* Inherit constructors */
40
41 virtual int run(int value) {
42 /* Generate wrapping code that enables native function overloading */
Wenzel Jakobb1b71402015-10-18 16:48:30 +020043 PYBIND11_OVERLOAD(
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020044 int, /* Return type */
45 Example12, /* Parent class */
46 run, /* Name of function */
47 value /* Argument(s) */
48 );
49 }
50
jmabille9cfa71f2016-02-23 22:41:07 +010051 virtual bool run_bool() {
52 PYBIND11_OVERLOAD_PURE(
53 bool,
54 Example12,
55 run_bool
56 );
Wenzel Jakob4a50fa52016-02-23 23:50:21 +010057 throw std::runtime_error("this will never be reached");
jmabille9cfa71f2016-02-23 22:41:07 +010058 }
59
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020060 virtual void pure_virtual() {
Wenzel Jakobb1b71402015-10-18 16:48:30 +020061 PYBIND11_OVERLOAD_PURE(
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020062 void, /* Return type */
63 Example12, /* Parent class */
64 pure_virtual /* Name of function */
65 /* This function has no arguments */
66 );
67 }
68};
69
70int runExample12(Example12 *ex, int value) {
71 return ex->run(value);
72}
73
jmabille9cfa71f2016-02-23 22:41:07 +010074bool runExample12Bool(Example12* ex) {
75 return ex->run_bool();
76}
77
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020078void runExample12Virtual(Example12 *ex) {
79 ex->pure_virtual();
80}
81
82void init_ex12(py::module &m) {
83 /* Important: use the wrapper type as a template
84 argument to class_<>, but use the original name
85 to denote the type */
86 py::class_<PyExample12>(m, "Example12")
87 /* Declare that 'PyExample12' is really an alias for the original type 'Example12' */
88 .alias<Example12>()
89 .def(py::init<int>())
90 /* Reference original class in function definitions */
91 .def("run", &Example12::run)
jmabille9cfa71f2016-02-23 22:41:07 +010092 .def("run_bool", &Example12::run_bool)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020093 .def("pure_virtual", &Example12::pure_virtual);
94
95 m.def("runExample12", &runExample12);
jmabille9cfa71f2016-02-23 22:41:07 +010096 m.def("runExample12Bool", &runExample12Bool);
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020097 m.def("runExample12Virtual", &runExample12Virtual);
98}