cppyabm  1.0.17
An agent-based library to integrate C++ and Python
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 #include <functional>
14 
17 
18 template <typename Return, typename... Args>
19 struct type_caster<std::function<Return(Args...)>> {
20  using type = std::function<Return(Args...)>;
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_) {
63  f = f_.f;
64  }
65  ~func_handle() {
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 {
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 
101 };
102 
function_record
Internal data structure which holds metadata about a bound function (signature, overloads,...
Definition: attr.h:141
PYBIND11_NAMESPACE_BEGIN
#define PYBIND11_NAMESPACE_BEGIN(name)
Definition: common.h:16
PYBIND11_NAMESPACE_END
#define PYBIND11_NAMESPACE_END(name)
Definition: common.h:17
PYBIND11_NAMESPACE
#define PYBIND11_NAMESPACE
Definition: common.h:26
type_caster< std::function< Return(Args...)> >::load
bool load(handle src, bool convert)
Definition: functional.h:25
type_caster_generic::value
void * value
Definition: cast.h:767
handle::inc_ref
const handle & inc_ref() const &
Definition: pytypes.h:192
type
Definition: pytypes.h:915
_
constexpr descr< N - 1 > _(char const(&text)[N])
Definition: descr.h:54
type_caster< std::function< Return(Args...)> >::function_type
Return(*)(Args...) function_type
Definition: functional.h:22
type_caster< std::function< Return(Args...)> >::PYBIND11_TYPE_CASTER
PYBIND11_TYPE_CASTER(type, _("Callable[[")+concat(make_caster< Args >::name...)+_("], ")+make_caster< retval_type >::name+_("]"))
conditional_t
typename std::conditional< B, T, F >::type conditional_t
Definition: common.h:505
same_type
bool same_type(const std::type_info &lhs, const std::type_info &rhs)
Definition: internals.h:61
handle
Definition: pytypes.h:176
type_caster
Definition: cast.h:954
object::release
handle release()
Definition: pytypes.h:249
conftest.capture
def capture(capsys)
Definition: conftest.py:124
policy
Definition: policy.py:1
concat
constexpr descr< 0 > concat()
Definition: descr.h:83
cpp_function
Wraps an arbitrary C++ function/method/lambda function/.. into a callable Python object.
Definition: pybind11.h:62
gil_scoped_acquire
Definition: pybind11.h:2253
pybind11.h
move
detail::enable_if_t<!detail::move_never< T >::value, T > move(object &&obj)
Definition: cast.h:1798
type_caster< std::function< Return(Args...)> >::cast
static handle cast(Func &&f_, return_value_policy policy, handle)
Definition: functional.h:88
void_type
Helper type to replace 'void' in some expressions.
Definition: common.h:580
args
Definition: pytypes.h:1366
return_value_policy
return_value_policy
Approach used to cast a previously unknown C++ instance into a Python object.
Definition: common.h:357
function
Definition: pytypes.h:1386
none
Definition: pytypes.h:1075
type_caster< std::function< Return(Args...)> >::retval_type
conditional_t< std::is_same< Return, void >::value, void_type, Return > retval_type
Definition: functional.h:21