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 15 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE) 16 PYBIND11_NAMESPACE_BEGIN(detail) 17 18 template <typename Return, typename... Args> 19 struct type_caster<std::function<Return(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...); 23 24 public: 25 bool load(handle src, bool convert) { 26 if (src.is_none()) { 27 // Defer accepting None to other overloads (if we aren't in convert mode): 28 if (!convert) return false; 29 return true; 30 } 31 32 if (!isinstance<function>(src)) 33 return false; 34 35 auto func = reinterpret_borrow<function>(src); 36 37 /* 38 When passing a C++ function as an argument to another C++ 39 function via Python, every function call would normally involve 40 a full C++ -> Python -> C++ roundtrip, which can be prohibitive. 41 Here, we try to at least detect the case where the function is 42 stateless (i.e. function pointer or lambda function without 43 captured variables), in which case the roundtrip can be avoided. 44 */ 45 if (auto cfunc = func.cpp_function()) { 46 auto c = reinterpret_borrow<capsule>(PyCFunction_GET_SELF(cfunc.ptr())); 47 auto rec = (function_record *) c; 48 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; 53 return true; 54 } 55 } 56 57 // ensure GIL is held during functor destruction 58 struct func_handle { 59 function f; 60 func_handle(function&& f_) : f(std::move(f_)) {} 61 func_handle(const func_handle& f_) { 62 gil_scoped_acquire acq; 63 f = f_.f; 64 } 65 ~func_handle() { 66 gil_scoped_acquire acq; 67 function kill_f(std::move(f)); 68 } 69 }; 70 71 // to emulate 'move initialization capture' in C++11 72 struct func_wrapper { 73 func_handle hfunc; 74 func_wrapper(func_handle&& hf): hfunc(std::move(hf)) {} 75 Return operator()(Args... args) const { 76 gil_scoped_acquire acq; 77 object retval(hfunc.f(std::forward<Args>(args)...)); 78 /* Visual studio 2015 parser issue: need parentheses around this expression */ 79 return (retval.template cast<Return>()); 80 } 81 }; 82 83 value = func_wrapper(func_handle(std::move(func))); 84 return true; 85 } 86 87 template <typename Func> 88 static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) { 89 if (!f_) 90 return none().inc_ref(); 91 92 auto result = f_.template target<function_type>(); 93 if (result) 94 return cpp_function(*result, policy).release(); 95 else 96 return cpp_function(std::forward<Func>(f_), policy).release(); 97 } 98 99 PYBIND11_TYPE_CASTER(type, _("Callable[[") + concat(make_caster<Args>::name...) + _("], ") 100 + make_caster<retval_type>::name + _("]")); 101 }; 102 103 PYBIND11_NAMESPACE_END(detail) 104 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE) 105