mirror of
https://github.com/pybind/pybind11.git
synced 2025-02-07 17:32:00 +00:00
* Adding google-explicit-constructor to .clang-tidy * clang-tidy explicit attr.h (all automatic) * clang-tidy explicit cast.h (all automatic) * clang-tidy detail/init.h (1 NOLINT) * clang-tidy detail/type_caster_base.h (2 NOLINT) * clang-tidy pybind11.h (7 NOLINT) * clang-tidy detail/common.h (3 NOLINT) * clang-tidy detail/descr.h (2 NOLINT) * clang-tidy pytypes.h (23 NOLINT, only 1 explicit) * clang-tidy eigen.h (7 NOLINT, 0 explicit) * Adding 2 explicit in functional.h * Adding 4 explicit in iostream.h * clang-tidy numpy.h (1 NOLINT, 1 explicit) * clang-tidy embed.h (0 NOLINT, 1 explicit) * clang-tidy tests/local_bindings.h (0 NOLINT, 4 explicit) * clang-tidy tests/pybind11_cross_module_tests.cpp (0 NOLINT, 1 explicit) * clang-tidy tests/pybind11_tests.h (0 NOLINT, 2 explicit) * clang-tidy tests/test_buffers.cpp (0 NOLINT, 2 explicit) * clang-tidy tests/test_builtin_casters.cpp (0 NOLINT, 4 explicit) * clang-tidy tests/test_class.cpp (0 NOLINT, 6 explicit) * clang-tidy tests/test_copy_move.cpp (0 NOLINT, 7 explicit) * clang-tidy tests/test_embed/external_module.cpp (0 NOLINT, 1 explicit) * clang-tidy tests/test_embed/test_interpreter.cpp (0 NOLINT, 1 explicit) * clang-tidy tests/object.h (0 NOLINT, 2 explicit) * clang-tidy batch of fully automatic fixes. * Workaround for MSVC 19.16.27045.0 C++17 Python 2 C++ syntax error.
122 lines
4.5 KiB
C++
122 lines
4.5 KiB
C++
/*
|
|
pybind11/functional.h: std::function<> support
|
|
|
|
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.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 "pybind11.h"
|
|
#include <functional>
|
|
|
|
PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
|
|
PYBIND11_NAMESPACE_BEGIN(detail)
|
|
|
|
template <typename Return, typename... Args>
|
|
struct type_caster<std::function<Return(Args...)>> {
|
|
using type = std::function<Return(Args...)>;
|
|
using retval_type = conditional_t<std::is_same<Return, void>::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<function>(src))
|
|
return false;
|
|
|
|
auto func = reinterpret_borrow<function>(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 cfunc_self = PyCFunction_GET_SELF(cfunc.ptr());
|
|
if (isinstance<capsule>(cfunc_self)) {
|
|
auto c = reinterpret_borrow<capsule>(cfunc_self);
|
|
auto rec = (function_record *) c;
|
|
|
|
while (rec != nullptr) {
|
|
if (rec->is_stateless
|
|
&& same_type(typeid(function_type),
|
|
*reinterpret_cast<const std::type_info *>(rec->data[1]))) {
|
|
struct capture {
|
|
function_type f;
|
|
};
|
|
value = ((capture *) &rec->data)->f;
|
|
return true;
|
|
}
|
|
rec = rec->next;
|
|
}
|
|
}
|
|
// PYPY segfaults here when passing builtin function like sum.
|
|
// Raising an fail exception here works to prevent the segfault, but only on gcc.
|
|
// See PR #1413 for full details
|
|
}
|
|
|
|
// ensure GIL is held during functor destruction
|
|
struct func_handle {
|
|
function f;
|
|
#if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17) && PY_MAJOR_VERSION < 3)
|
|
// This triggers a syntax error under very special conditions (very weird indeed).
|
|
explicit
|
|
#endif
|
|
func_handle(function &&f_) noexcept : f(std::move(f_)) {}
|
|
func_handle(const func_handle &f_) { operator=(f_); }
|
|
func_handle &operator=(const func_handle &f_) {
|
|
gil_scoped_acquire acq;
|
|
f = f_.f;
|
|
return *this;
|
|
}
|
|
~func_handle() {
|
|
gil_scoped_acquire acq;
|
|
function kill_f(std::move(f));
|
|
}
|
|
};
|
|
|
|
// to emulate 'move initialization capture' in C++11
|
|
struct func_wrapper {
|
|
func_handle hfunc;
|
|
explicit func_wrapper(func_handle &&hf) noexcept : hfunc(std::move(hf)) {}
|
|
Return operator()(Args... args) const {
|
|
gil_scoped_acquire acq;
|
|
object retval(hfunc.f(std::forward<Args>(args)...));
|
|
/* Visual studio 2015 parser issue: need parentheses around this expression */
|
|
return (retval.template cast<Return>());
|
|
}
|
|
};
|
|
|
|
value = func_wrapper(func_handle(std::move(func)));
|
|
return true;
|
|
}
|
|
|
|
template <typename Func>
|
|
static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) {
|
|
if (!f_)
|
|
return none().inc_ref();
|
|
|
|
auto result = f_.template target<function_type>();
|
|
if (result)
|
|
return cpp_function(*result, policy).release();
|
|
return cpp_function(std::forward<Func>(f_), policy).release();
|
|
}
|
|
|
|
PYBIND11_TYPE_CASTER(type, _("Callable[[") + concat(make_caster<Args>::name...) + _("], ")
|
|
+ make_caster<retval_type>::name + _("]"));
|
|
};
|
|
|
|
PYBIND11_NAMESPACE_END(detail)
|
|
PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)
|