functional.h revision 11986:c12e4625ab56
1/* 2 pybind11/functional.h: std::function<> support 3 4 Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch> 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#include <functional> 14 15NAMESPACE_BEGIN(pybind11) 16NAMESPACE_BEGIN(detail) 17 18template <typename Return, typename... Args> struct type_caster<std::function<Return(Args...)>> { 19 typedef std::function<Return(Args...)> type; 20 typedef typename std::conditional<std::is_same<Return, void>::value, void_type, Return>::type retval_type; 21public: 22 bool load(handle src_, bool) { 23 if (src_.is_none()) 24 return true; 25 26 src_ = detail::get_function(src_); 27 if (!src_ || !PyCallable_Check(src_.ptr())) 28 return false; 29 30 /* 31 When passing a C++ function as an argument to another C++ 32 function via Python, every function call would normally involve 33 a full C++ -> Python -> C++ roundtrip, which can be prohibitive. 34 Here, we try to at least detect the case where the function is 35 stateless (i.e. function pointer or lambda function without 36 captured variables), in which case the roundtrip can be avoided. 37 */ 38 if (PyCFunction_Check(src_.ptr())) { 39 auto c = reinterpret_borrow<capsule>(PyCFunction_GetSelf(src_.ptr())); 40 auto rec = (function_record *) c; 41 using FunctionType = Return (*) (Args...); 42 43 if (rec && rec->is_stateless && rec->data[1] == &typeid(FunctionType)) { 44 struct capture { FunctionType f; }; 45 value = ((capture *) &rec->data)->f; 46 return true; 47 } 48 } 49 50 auto src = reinterpret_borrow<object>(src_); 51 value = [src](Args... args) -> Return { 52 gil_scoped_acquire acq; 53 object retval(src(std::move(args)...)); 54 /* Visual studio 2015 parser issue: need parentheses around this expression */ 55 return (retval.template cast<Return>()); 56 }; 57 return true; 58 } 59 60 template <typename Func> 61 static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) { 62 if (!f_) 63 return none().inc_ref(); 64 65 auto result = f_.template target<Return (*)(Args...)>(); 66 if (result) 67 return cpp_function(*result, policy).release(); 68 else 69 return cpp_function(std::forward<Func>(f_), policy).release(); 70 } 71 72 PYBIND11_TYPE_CASTER(type, _("Callable[[") + 73 argument_loader<Args...>::arg_names() + _("], ") + 74 type_caster<retval_type>::name() + 75 _("]")); 76}; 77 78NAMESPACE_END(detail) 79NAMESPACE_END(pybind11) 80