15 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
16 PYBIND11_NAMESPACE_BEGIN(detail)
18 template <typename Return, typename... Args>
20 using type = std::function<Return(Args...)>;
21 using retval_type = conditional_t<std::is_same<Return, void>::value,
void_type, Return>;
22 using function_type = Return (*) (Args...);
25 bool load(
handle src,
bool convert) {
28 if (!convert)
return false;
32 if (!isinstance<function>(src))
35 auto func = reinterpret_borrow<function>(src);
45 if (
auto cfunc = func.cpp_function()) {
46 auto c = reinterpret_borrow<capsule>(PyCFunction_GET_SELF(cfunc.ptr()));
49 if (rec && rec->is_stateless &&
50 same_type(
typeid(function_type), *reinterpret_cast<const std::type_info *>(rec->data[1]))) {
51 struct capture { function_type f; };
52 value = ((capture *) &rec->data)->f;
60 func_handle(
function&& f_) : f(std::move(f_)) {}
61 func_handle(
const func_handle& f_) {
67 function kill_f(std::move(f));
74 func_wrapper(func_handle&& hf): hfunc(std::move(hf)) {}
75 Return operator()(Args...
args)
const {
77 object retval(hfunc.f(std::forward<Args>(
args)...));
79 return (retval.template cast<Return>());
83 value = func_wrapper(func_handle(std::move(func)));
87 template <
typename Func>
88 static handle cast(Func &&f_, return_value_policy policy,
handle ) {
92 auto result = f_.template target<function_type>();
103 PYBIND11_NAMESPACE_END(detail)
104 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
const handle & inc_ref() const &
Helper type to replace 'void' in some expressions.
Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object.
Internal data structure which holds metadata about a bound function (signature, overloads, etc.)