2015-07-30 13:29:00 +00:00
|
|
|
/*
|
|
|
|
pybind/functional.h: std::function<> support
|
|
|
|
|
|
|
|
Copyright (c) 2015 Wenzel Jakob <wenzel@inf.ethz.ch>
|
|
|
|
|
|
|
|
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 <pybind/pybind.h>
|
|
|
|
#include <functional>
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(pybind)
|
|
|
|
NAMESPACE_BEGIN(detail)
|
|
|
|
|
|
|
|
template <typename Return, typename... Args> struct type_caster<std::function<Return(Args...)>> {
|
|
|
|
typedef std::function<Return(Args...)> type;
|
|
|
|
public:
|
|
|
|
|
|
|
|
bool load(PyObject *src_, bool) {
|
|
|
|
if (!PyFunction_Check(src_))
|
|
|
|
return false;
|
|
|
|
object src(src_, true);
|
|
|
|
value = [src](Args... args) -> Return {
|
2015-08-28 15:53:31 +00:00
|
|
|
object retval(pybind::handle(src).call(std::move(args)...));
|
2015-07-30 13:29:00 +00:00
|
|
|
/* Visual studio 2015 parser issue: need parentheses around this expression */
|
|
|
|
return (retval.template cast<Return>());
|
|
|
|
};
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename Func>
|
|
|
|
static PyObject *cast(Func &&f_, return_value_policy policy, PyObject *) {
|
|
|
|
cpp_function f(std::forward<Func>(f_), policy);
|
|
|
|
f.inc_ref();
|
|
|
|
return f.ptr();
|
|
|
|
}
|
|
|
|
|
2015-08-24 13:31:24 +00:00
|
|
|
|
|
|
|
PYBIND_TYPE_CASTER(type, detail::descr("function<") +
|
2015-08-26 15:23:23 +00:00
|
|
|
type_caster<std::tuple<Args...>>::name() + detail::descr(" -> ") +
|
|
|
|
type_caster<typename decay<Return>::type>::name() +
|
2015-08-24 13:31:24 +00:00
|
|
|
detail::descr(">"));
|
2015-07-30 13:29:00 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
NAMESPACE_END(detail)
|
|
|
|
NAMESPACE_END(pybind)
|