/* pybind11/functional.h: std::function<> support Copyright (c) 2016 Wenzel Jakob All rights reserved. Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. */ #pragma once #include "pybind11.h" #include NAMESPACE_BEGIN(PYBIND11_NAMESPACE) NAMESPACE_BEGIN(detail) template struct type_caster> { using type = std::function; using retval_type = conditional_t::value, void_type, Return>; using function_type = Return (*) (Args...); public: bool load(handle src, bool convert) { if (src.is_none()) { // Defer accepting None to other overloads (if we aren't in convert mode): if (!convert) return false; return true; } if (!isinstance(src)) return false; auto func = reinterpret_borrow(src); /* When passing a C++ function as an argument to another C++ function via Python, every function call would normally involve a full C++ -> Python -> C++ roundtrip, which can be prohibitive. Here, we try to at least detect the case where the function is stateless (i.e. function pointer or lambda function without captured variables), in which case the roundtrip can be avoided. */ if (auto cfunc = func.cpp_function()) { auto c = reinterpret_borrow(PyCFunction_GET_SELF(cfunc.ptr())); auto rec = (function_record *) c; if (rec && rec->is_stateless && same_type(typeid(function_type), *reinterpret_cast(rec->data[1]))) { struct capture { function_type f; }; value = ((capture *) &rec->data)->f; return true; } } value = [func](Args... args) -> Return { gil_scoped_acquire acq; object retval(func(std::forward(args)...)); /* Visual studio 2015 parser issue: need parentheses around this expression */ return (retval.template cast()); }; return true; } template static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) { if (!f_) return none().inc_ref(); auto result = f_.template target(); if (result) return cpp_function(*result, policy).release(); else return cpp_function(std::forward(f_), policy).release(); } PYBIND11_TYPE_CASTER(type, _("Callable[[") + argument_loader::arg_names() + _("], ") + make_caster::name() + _("]")); }; NAMESPACE_END(detail) NAMESPACE_END(PYBIND11_NAMESPACE)