1 | /* |
2 | pybind11/functional.h: std::function<> support |
3 | |
4 | Copyright (c) 2016 Wenzel Jakob <[email protected]> |
5 | |
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 | #pragma once |
11 | |
12 | #include "pybind11.h" |
13 | |
14 | #include <functional> |
15 | |
16 | PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) |
17 | PYBIND11_NAMESPACE_BEGIN(detail) |
18 | |
19 | template <typename Return, typename... Args> |
20 | struct type_caster<std::function<Return(Args...)>> { |
21 | using type = std::function<Return(Args...)>; |
22 | using retval_type = conditional_t<std::is_same<Return, void>::value, void_type, Return>; |
23 | using function_type = Return (*)(Args...); |
24 | |
25 | public: |
26 | bool load(handle src, bool convert) { |
27 | if (src.is_none()) { |
28 | // Defer accepting None to other overloads (if we aren't in convert mode): |
29 | if (!convert) { |
30 | return false; |
31 | } |
32 | return true; |
33 | } |
34 | |
35 | if (!isinstance<function>(src)) { |
36 | return false; |
37 | } |
38 | |
39 | auto func = reinterpret_borrow<function>(src); |
40 | |
41 | /* |
42 | When passing a C++ function as an argument to another C++ |
43 | function via Python, every function call would normally involve |
44 | a full C++ -> Python -> C++ roundtrip, which can be prohibitive. |
45 | Here, we try to at least detect the case where the function is |
46 | stateless (i.e. function pointer or lambda function without |
47 | captured variables), in which case the roundtrip can be avoided. |
48 | */ |
49 | if (auto cfunc = func.cpp_function()) { |
50 | auto *cfunc_self = PyCFunction_GET_SELF(cfunc.ptr()); |
51 | if (isinstance<capsule>(cfunc_self)) { |
52 | auto c = reinterpret_borrow<capsule>(cfunc_self); |
53 | auto *rec = (function_record *) c; |
54 | |
55 | while (rec != nullptr) { |
56 | if (rec->is_stateless |
57 | && same_type(typeid(function_type), |
58 | *reinterpret_cast<const std::type_info *>(rec->data[1]))) { |
59 | struct capture { |
60 | function_type f; |
61 | }; |
62 | value = ((capture *) &rec->data)->f; |
63 | return true; |
64 | } |
65 | rec = rec->next; |
66 | } |
67 | } |
68 | // PYPY segfaults here when passing builtin function like sum. |
69 | // Raising an fail exception here works to prevent the segfault, but only on gcc. |
70 | // See PR #1413 for full details |
71 | } |
72 | |
73 | // ensure GIL is held during functor destruction |
74 | struct func_handle { |
75 | function f; |
76 | #if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17)) |
77 | // This triggers a syntax error under very special conditions (very weird indeed). |
78 | explicit |
79 | #endif |
80 | func_handle(function &&f_) noexcept |
81 | : f(std::move(f_)) { |
82 | } |
83 | func_handle(const func_handle &f_) { operator=(f_); } |
84 | func_handle &operator=(const func_handle &f_) { |
85 | gil_scoped_acquire acq; |
86 | f = f_.f; |
87 | return *this; |
88 | } |
89 | ~func_handle() { |
90 | gil_scoped_acquire acq; |
91 | function kill_f(std::move(f)); |
92 | } |
93 | }; |
94 | |
95 | // to emulate 'move initialization capture' in C++11 |
96 | struct func_wrapper { |
97 | func_handle hfunc; |
98 | explicit func_wrapper(func_handle &&hf) noexcept : hfunc(std::move(hf)) {} |
99 | Return operator()(Args... args) const { |
100 | gil_scoped_acquire acq; |
101 | // casts the returned object as a rvalue to the return type |
102 | return hfunc.f(std::forward<Args>(args)...).template cast<Return>(); |
103 | } |
104 | }; |
105 | |
106 | value = func_wrapper(func_handle(std::move(func))); |
107 | return true; |
108 | } |
109 | |
110 | template <typename Func> |
111 | static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) { |
112 | if (!f_) { |
113 | return none().release(); |
114 | } |
115 | |
116 | auto result = f_.template target<function_type>(); |
117 | if (result) { |
118 | return cpp_function(*result, policy).release(); |
119 | } |
120 | return cpp_function(std::forward<Func>(f_), policy).release(); |
121 | } |
122 | |
123 | PYBIND11_TYPE_CASTER(type, |
124 | const_name("Callable[[" ) + concat(make_caster<Args>::name...) |
125 | + const_name("], " ) + make_caster<retval_type>::name |
126 | + const_name("]" )); |
127 | }; |
128 | |
129 | PYBIND11_NAMESPACE_END(detail) |
130 | PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) |
131 | |