2015-10-01 14:46:03 +00:00
|
|
|
/*
|
|
|
|
example/example12.cpp -- overriding virtual functions from Python
|
|
|
|
|
2016-04-17 18:21:41 +00:00
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
|
2015-10-01 14:46:03 +00:00
|
|
|
|
|
|
|
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"
|
2015-10-15 16:13:33 +00:00
|
|
|
#include <pybind11/functional.h>
|
2015-10-01 14:46:03 +00:00
|
|
|
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
|
2016-02-23 21:41:07 +00:00
|
|
|
virtual bool run_bool() = 0;
|
2015-10-01 14:46:03 +00:00
|
|
|
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 */
|
2015-10-18 14:48:30 +00:00
|
|
|
PYBIND11_OVERLOAD(
|
2015-10-01 14:46:03 +00:00
|
|
|
int, /* Return type */
|
|
|
|
Example12, /* Parent class */
|
|
|
|
run, /* Name of function */
|
|
|
|
value /* Argument(s) */
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-02-23 21:41:07 +00:00
|
|
|
virtual bool run_bool() {
|
|
|
|
PYBIND11_OVERLOAD_PURE(
|
2016-04-18 08:34:27 +00:00
|
|
|
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 */
|
2016-02-23 21:41:07 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-01 14:46:03 +00:00
|
|
|
virtual void pure_virtual() {
|
2015-10-18 14:48:30 +00:00
|
|
|
PYBIND11_OVERLOAD_PURE(
|
2015-10-01 14:46:03 +00:00
|
|
|
void, /* Return type */
|
|
|
|
Example12, /* Parent class */
|
2016-04-18 08:34:27 +00:00
|
|
|
pure_virtual, /* Name of function */
|
|
|
|
/* This function has no arguments. The trailing comma
|
|
|
|
in the previous line is needed for some compilers */
|
2015-10-01 14:46:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
int runExample12(Example12 *ex, int value) {
|
|
|
|
return ex->run(value);
|
|
|
|
}
|
|
|
|
|
2016-02-23 21:41:07 +00:00
|
|
|
bool runExample12Bool(Example12* ex) {
|
|
|
|
return ex->run_bool();
|
|
|
|
}
|
|
|
|
|
2015-10-01 14:46:03 +00:00
|
|
|
void runExample12Virtual(Example12 *ex) {
|
|
|
|
ex->pure_virtual();
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_ex12(py::module &m) {
|
2016-05-26 11:19:27 +00:00
|
|
|
/* Important: indicate the trampoline class PyExample12 using the third
|
|
|
|
argument to py::class_. The second argument with the unique pointer
|
|
|
|
is simply the default holder type used by pybind11. */
|
|
|
|
py::class_<Example12, std::unique_ptr<Example12>, PyExample12>(m, "Example12")
|
2015-10-01 14:46:03 +00:00
|
|
|
.def(py::init<int>())
|
|
|
|
/* Reference original class in function definitions */
|
|
|
|
.def("run", &Example12::run)
|
2016-02-23 21:41:07 +00:00
|
|
|
.def("run_bool", &Example12::run_bool)
|
2015-10-01 14:46:03 +00:00
|
|
|
.def("pure_virtual", &Example12::pure_virtual);
|
|
|
|
|
|
|
|
m.def("runExample12", &runExample12);
|
2016-02-23 21:41:07 +00:00
|
|
|
m.def("runExample12Bool", &runExample12Bool);
|
2015-10-01 14:46:03 +00:00
|
|
|
m.def("runExample12Virtual", &runExample12Virtual);
|
|
|
|
}
|