nicer code separation, cleanup logic, std::function type caster

This commit is contained in:
Wenzel Jakob
2015-07-30 15:29:00 +02:00
parent a576e6a8ca
commit 281aa0e668
13 changed files with 296 additions and 132 deletions

View File

@@ -9,6 +9,7 @@
*/
#include "example.h"
#include <pybind/stl.h>
class Example2 {
public:

View File

@@ -9,6 +9,7 @@
*/
#include "example.h"
#include <pybind/functional.h>
class Pet {
@@ -73,6 +74,14 @@ void test_callback3(Example5 *ex, int value) {
ex->callback(value);
}
void test_callback4(const std::function<int(int)> &func) {
cout << "func(43) = " << func(43)<< std::endl;
}
std::function<int(int)> test_callback5() {
return [](int i) { return i+1; };
}
void init_ex5(py::module &m) {
py::class_<Pet> pet_class(m, "Pet");
pet_class
@@ -89,6 +98,8 @@ void init_ex5(py::module &m) {
m.def("test_callback1", &test_callback1);
m.def("test_callback2", &test_callback2);
m.def("test_callback3", &test_callback3);
m.def("test_callback4", &test_callback4);
m.def("test_callback5", &test_callback5);
py::class_<Example5>(m, "Example5")
.def(py::init<py::object, int>());

View File

@@ -22,6 +22,8 @@ except Exception as e:
from example import test_callback1
from example import test_callback2
from example import test_callback3
from example import test_callback4
from example import test_callback5
from example import Example5
def func1():
@@ -43,3 +45,7 @@ print(test_callback2(func2))
callback = MyCallback(3)
test_callback3(callback, 4)
test_callback4(lambda i: i+1)
f = test_callback5()
print("func(43) = %i" % f(43))