mirror of
https://github.com/pybind/pybind11.git
synced 2026-04-19 22:39:09 +00:00
nicer code separation, cleanup logic, std::function type caster
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
*/
|
||||
|
||||
#include "example.h"
|
||||
#include <pybind/stl.h>
|
||||
|
||||
class Example2 {
|
||||
public:
|
||||
|
||||
@@ -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>());
|
||||
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user