"git@developer.sourcefind.cn:gaoqiong/pybind11.git" did not exist on "3f200fab2237ea478a71997a9864b935506bb96e"
example-callbacks.cpp 2.84 KB
Newer Older
Wenzel Jakob's avatar
Wenzel Jakob committed
1
/*
2
    example/example-callbacks.cpp -- callbacks
Wenzel Jakob's avatar
Wenzel Jakob committed
3

4
    Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Wenzel Jakob's avatar
Wenzel Jakob committed
5
6
7
8
9
10

    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"
11
#include <pybind11/functional.h>
Wenzel Jakob's avatar
Wenzel Jakob committed
12
13
14


bool test_callback1(py::object func) {
15
    func();
Wenzel Jakob's avatar
Wenzel Jakob committed
16
17
18
19
    return false;
}

int test_callback2(py::object func) {
20
    py::object result = func("Hello", 'x', true, 5);
Wenzel Jakob's avatar
Wenzel Jakob committed
21
22
23
    return result.cast<int>();
}

24
void test_callback3(const std::function<int(int)> &func) {
25
26
27
    cout << "func(43) = " << func(43)<< std::endl;
}

28
std::function<int(int)> test_callback4() {
29
30
31
    return [](int i) { return i+1; };
}

32
33
34
35
36
py::cpp_function test_callback5() {
    return py::cpp_function([](int i) { return i+1; },
       py::arg("number"));
}

37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
int dummy_function(int i) { return i + 1; }
int dummy_function2(int i, int j) { return i + j; }
std::function<int(int)> roundtrip(std::function<int(int)> f) { 
    std::cout << "roundtrip.." << std::endl;
    return f;
}

void test_dummy_function(const std::function<int(int)> &f) {
    using fn_type = int (*)(int);
    auto result = f.target<fn_type>();
    if (!result) {
        std::cout << "could not convert to a function pointer." << std::endl;
        auto r = f(1);
        std::cout << "eval(1) = " << r << std::endl;
    } else if (*result == dummy_function) {
        std::cout << "argument matches dummy_function" << std::endl;
        auto r = (*result)(1);
        std::cout << "eval(1) = " << r << std::endl;
    } else {
        std::cout << "argument does NOT match dummy_function. This should never happen!" << std::endl;
    }
}

60
void init_ex_callbacks(py::module &m) {
Wenzel Jakob's avatar
Wenzel Jakob committed
61
62
63
    m.def("test_callback1", &test_callback1);
    m.def("test_callback2", &test_callback2);
    m.def("test_callback3", &test_callback3);
64
    m.def("test_callback4", &test_callback4);
65
    m.def("test_callback5", &test_callback5);
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90

    /* Test cleanup of lambda closure */

    struct Payload {
        Payload() {
            std::cout << "Payload constructor" << std::endl;
        }
        ~Payload() {
            std::cout << "Payload destructor" << std::endl;
        }
        Payload(const Payload &) {
            std::cout << "Payload copy constructor" << std::endl;
        }
        Payload(Payload &&) {
            std::cout << "Payload move constructor" << std::endl;
        }
    };

    m.def("test_cleanup", []() -> std::function<void(void)> { 
        Payload p;

        return [p]() {
            /* p should be cleaned up when the returned function is garbage collected */
        };
    });
91
92
93
94
95
96

    /* Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer */
    m.def("dummy_function", &dummy_function);
    m.def("dummy_function2", &dummy_function2);
    m.def("roundtrip", &roundtrip);
    m.def("test_dummy_function", &test_dummy_function);
Wenzel Jakob's avatar
Wenzel Jakob committed
97
}