/* pybind/functional.h: std::function<> support Copyright (c) 2015 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 #include NAMESPACE_BEGIN(pybind) NAMESPACE_BEGIN(detail) template struct type_caster> { typedef std::function type; public: bool load(PyObject *src_, bool) { if (!PyFunction_Check(src_)) return false; object src(src_, true); value = [src](Args... args) -> Return { object retval(pybind::handle(src).call(std::move(args)...)); /* Visual studio 2015 parser issue: need parentheses around this expression */ return (retval.template cast()); }; return true; } template static PyObject *cast(Func &&f_, return_value_policy policy, PyObject *) { cpp_function f(std::forward(f_), policy); f.inc_ref(); return f.ptr(); } PYBIND_TYPE_CASTER(type, detail::descr("function<") + type_caster>::descr() + detail::descr(" -> ") + type_caster::type>::descr() + detail::descr(">")); }; NAMESPACE_END(detail) NAMESPACE_END(pybind)