blob: a7a583e6768b94252561f3a252d2ea7b80ed205a [file] [log] [blame]
Wenzel Jakob38bd7112015-07-05 20:05:44 +02001/*
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -04002 example/example-callbacks.cpp -- callbacks
Wenzel Jakob38bd7112015-07-05 20:05:44 +02003
Wenzel Jakob8cb6cb32016-04-17 20:21:41 +02004 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob38bd7112015-07-05 20:05:44 +02005
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 Jakob38bd7112015-07-05 20:05:44 +020012
13
Wenzel Jakob38bd7112015-07-05 20:05:44 +020014bool test_callback1(py::object func) {
Wenzel Jakob6c03beb2016-05-08 14:34:09 +020015 func();
Wenzel Jakob38bd7112015-07-05 20:05:44 +020016 return false;
17}
18
19int test_callback2(py::object func) {
Wenzel Jakob6c03beb2016-05-08 14:34:09 +020020 py::object result = func("Hello", 'x', true, 5);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020021 return result.cast<int>();
22}
23
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020024void test_callback3(const std::function<int(int)> &func) {
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020025 cout << "func(43) = " << func(43)<< std::endl;
26}
27
Wenzel Jakoba2f6fde2015-10-01 16:46:03 +020028std::function<int(int)> test_callback4() {
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020029 return [](int i) { return i+1; };
30}
31
Brad Harmon835fc062016-06-16 13:19:15 -050032py::cpp_function test_callback5() {
33 return py::cpp_function([](int i) { return i+1; },
34 py::arg("number"));
35}
36
Wenzel Jakob954b7932016-07-10 10:13:18 +020037int dummy_function(int i) { return i + 1; }
38int dummy_function2(int i, int j) { return i + j; }
39std::function<int(int)> roundtrip(std::function<int(int)> f) {
40 std::cout << "roundtrip.." << std::endl;
41 return f;
42}
43
44void test_dummy_function(const std::function<int(int)> &f) {
45 using fn_type = int (*)(int);
46 auto result = f.target<fn_type>();
47 if (!result) {
48 std::cout << "could not convert to a function pointer." << std::endl;
49 auto r = f(1);
50 std::cout << "eval(1) = " << r << std::endl;
51 } else if (*result == dummy_function) {
52 std::cout << "argument matches dummy_function" << std::endl;
53 auto r = (*result)(1);
54 std::cout << "eval(1) = " << r << std::endl;
55 } else {
56 std::cout << "argument does NOT match dummy_function. This should never happen!" << std::endl;
57 }
58}
59
Jason Rhinelanderb3f3d792016-07-18 16:43:18 -040060void init_ex_callbacks(py::module &m) {
Wenzel Jakob38bd7112015-07-05 20:05:44 +020061 m.def("test_callback1", &test_callback1);
62 m.def("test_callback2", &test_callback2);
63 m.def("test_callback3", &test_callback3);
Wenzel Jakob281aa0e2015-07-30 15:29:00 +020064 m.def("test_callback4", &test_callback4);
Brad Harmon835fc062016-06-16 13:19:15 -050065 m.def("test_callback5", &test_callback5);
Wenzel Jakob19208fe2015-10-13 17:37:25 +020066
67 /* Test cleanup of lambda closure */
68
69 struct Payload {
70 Payload() {
71 std::cout << "Payload constructor" << std::endl;
72 }
73 ~Payload() {
74 std::cout << "Payload destructor" << std::endl;
75 }
76 Payload(const Payload &) {
77 std::cout << "Payload copy constructor" << std::endl;
78 }
79 Payload(Payload &&) {
80 std::cout << "Payload move constructor" << std::endl;
81 }
82 };
83
84 m.def("test_cleanup", []() -> std::function<void(void)> {
85 Payload p;
86
87 return [p]() {
88 /* p should be cleaned up when the returned function is garbage collected */
89 };
90 });
Wenzel Jakob954b7932016-07-10 10:13:18 +020091
92 /* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */
93 m.def("dummy_function", &dummy_function);
94 m.def("dummy_function2", &dummy_function2);
95 m.def("roundtrip", &roundtrip);
96 m.def("test_dummy_function", &test_dummy_function);
Wenzel Jakob38bd7112015-07-05 20:05:44 +020097}