blob: 7bba2c8f9cc173213c6f913f423efb024c16433a [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 );
57 }
58
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020059 virtual void pure_virtual() {
Wenzel Jakobb1b71402015-10-18 16:48:30 +020060 PYBIND11_OVERLOAD_PURE(
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020061 void, /* Return type */
62 Example12, /* Parent class */
63 pure_virtual /* Name of function */
64 /* This function has no arguments */
65 );
66 }
67};
68
69int runExample12(Example12 *ex, int value) {
70 return ex->run(value);
71}
72
jmabille9cfa71f2016-02-23 22:41:07 +010073bool runExample12Bool(Example12* ex) {
74 return ex->run_bool();
75}
76
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020077void runExample12Virtual(Example12 *ex) {
78 ex->pure_virtual();
79}
80
81void init_ex12(py::module &m) {
82 /* Important: use the wrapper type as a template
83 argument to class_<>, but use the original name
84 to denote the type */
85 py::class_<PyExample12>(m, "Example12")
86 /* Declare that 'PyExample12' is really an alias for the original type 'Example12' */
87 .alias<Example12>()
88 .def(py::init<int>())
89 /* Reference original class in function definitions */
90 .def("run", &Example12::run)
jmabille9cfa71f2016-02-23 22:41:07 +010091 .def("run_bool", &Example12::run_bool)
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020092 .def("pure_virtual", &Example12::pure_virtual);
93
94 m.def("runExample12", &runExample12);
jmabille9cfa71f2016-02-23 22:41:07 +010095 m.def("runExample12Bool", &runExample12Bool);
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020096 m.def("runExample12Virtual", &runExample12Virtual);
97}