functional.h
Go to the documentation of this file.
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 
14 #include <functional>
15 
18 
19 template <typename Return, typename... Args>
20 struct type_caster<std::function<Return(Args...)>> {
21  using type = std::function<Return(Args...)>;
23  using function_type = Return (*)(Args...);
24 
25 public:
26  bool load(handle src, bool convert) {
27  if (src.is_none()) {
28  // Defer accepting None to other overloads (if we aren't in convert mode):
29  if (!convert) {
30  return false;
31  }
32  return true;
33  }
34 
35  if (!isinstance<function>(src)) {
36  return false;
37  }
38 
39  auto func = reinterpret_borrow<function>(src);
40 
41  /*
42  When passing a C++ function as an argument to another C++
43  function via Python, every function call would normally involve
44  a full C++ -> Python -> C++ roundtrip, which can be prohibitive.
45  Here, we try to at least detect the case where the function is
46  stateless (i.e. function pointer or lambda function without
47  captured variables), in which case the roundtrip can be avoided.
48  */
49  if (auto cfunc = func.cpp_function()) {
50  auto *cfunc_self = PyCFunction_GET_SELF(cfunc.ptr());
51  if (isinstance<capsule>(cfunc_self)) {
52  auto c = reinterpret_borrow<capsule>(cfunc_self);
53  auto *rec = (function_record *) c;
54 
55  while (rec != nullptr) {
56  if (rec->is_stateless
57  && same_type(typeid(function_type),
58  *reinterpret_cast<const std::type_info *>(rec->data[1]))) {
59  struct capture {
61  };
62  value = ((capture *) &rec->data)->f;
63  return true;
64  }
65  rec = rec->next;
66  }
67  }
68  // PYPY segfaults here when passing builtin function like sum.
69  // Raising an fail exception here works to prevent the segfault, but only on gcc.
70  // See PR #1413 for full details
71  }
72 
73  // ensure GIL is held during functor destruction
74  struct func_handle {
75  function f;
76 #if !(defined(_MSC_VER) && _MSC_VER == 1916 && defined(PYBIND11_CPP17))
77  // This triggers a syntax error under very special conditions (very weird indeed).
78  explicit
79 #endif
80  func_handle(function &&f_) noexcept
81  : f(std::move(f_)) {
82  }
83  func_handle(const func_handle &f_) { operator=(f_); }
84  func_handle &operator=(const func_handle &f_) {
86  f = f_.f;
87  return *this;
88  }
89  ~func_handle() {
91  function kill_f(std::move(f));
92  }
93  };
94 
95  // to emulate 'move initialization capture' in C++11
96  struct func_wrapper {
97  func_handle hfunc;
98  explicit func_wrapper(func_handle &&hf) noexcept : hfunc(std::move(hf)) {}
99  Return operator()(Args... args) const {
100  gil_scoped_acquire acq;
101  // casts the returned object as a rvalue to the return type
102  return hfunc.f(std::forward<Args>(args)...).template cast<Return>();
103  }
104  };
105 
106  value = func_wrapper(func_handle(std::move(func)));
107  return true;
108  }
109 
110  template <typename Func>
111  static handle cast(Func &&f_, return_value_policy policy, handle /* parent */) {
112  if (!f_) {
113  return none().inc_ref();
114  }
115 
116  auto result = f_.template target<function_type>();
117  if (result) {
118  return cpp_function(*result, policy).release();
119  }
120  return cpp_function(std::forward<Func>(f_), policy).release();
121  }
122 
124  const_name("Callable[[") + concat(make_caster<Args>::name...)
126  + const_name("]"));
127 };
128 
typename std::conditional< B, T, F >::type conditional_t
static handle cast(Func &&f_, return_value_policy policy, handle)
Definition: functional.h:111
Definition: pytypes.h:2012
conditional_t< std::is_same< Return, void >::value, void_type, Return > retval_type
Definition: functional.h:22
Scalar Scalar * c
Definition: benchVecAdd.cpp:17
Definition: BFloat16.h:88
bool load(handle src, bool convert)
Definition: functional.h:26
Definition: pytypes.h:1614
bool same_type(const std::type_info &lhs, const std::type_info &rhs)
Definition: internals.h:114
const handle & inc_ref() const &
Definition: pytypes.h:246
Values result
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
constexpr descr< 0 > concat()
Definition: descr.h:139
Helper type to replace &#39;void&#39; in some expressions.
handle release()
Definition: pytypes.h:330
Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object.
Definition: pybind11.h:81
def capture(capsys)
Definition: conftest.py:118
internal::enable_if< internal::valid_indexed_view_overload< RowIndices, ColIndices >::value &&internal::traits< typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::ReturnAsIndexedView, typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST
constexpr descr< N - 1 > const_name(char const (&text)[N])
Definition: descr.h:60
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
#define PYBIND11_TYPE_CASTER(type, py_name)
Definition: cast.h:82
#define PYBIND11_NAMESPACE_END(name)
static BinaryMeasurement< Rot3 > convert(const BetweenFactor< Pose3 >::shared_ptr &f)
Definition: pytypes.h:1370
#define PYBIND11_NAMESPACE_BEGIN(name)


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:34:15