"git@developer.sourcefind.cn:gaoqiong/pybind11.git" did not exist on "9beaa925db49cf4751642a1529d34bef4a3682ce"
example-callbacks.cpp 2.85 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 "constructor-stats.h"
12
#include <pybind11/functional.h>
Wenzel Jakob's avatar
Wenzel Jakob committed
13
14
15


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

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

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

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

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

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
    }
}

61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
struct Payload {
    Payload() {
        print_default_created(this);
    }
    ~Payload() {
        print_destroyed(this);
    }
    Payload(const Payload &) {
        print_copy_created(this);
    }
    Payload(Payload &&) {
        print_move_created(this);
    }
};

76
void init_ex_callbacks(py::module &m) {
Wenzel Jakob's avatar
Wenzel Jakob committed
77
78
79
    m.def("test_callback1", &test_callback1);
    m.def("test_callback2", &test_callback2);
    m.def("test_callback3", &test_callback3);
80
    m.def("test_callback4", &test_callback4);
81
    m.def("test_callback5", &test_callback5);
82
83
84
85
86
87
88
89
90
91

    /* Test cleanup of lambda closure */

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

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

    /* 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);
98
99
    // Export the payload constructor statistics for testing purposes:
    m.def("payload_cstats", &ConstructorStats::get<Payload>);
Wenzel Jakob's avatar
Wenzel Jakob committed
100
}