pybind11.hpp
Go to the documentation of this file.
1 #ifndef __pinocchio_python_pybind11_hpp__
2 #define __pinocchio_python_pybind11_hpp__
3 
35 
36 #include <iostream>
37 #include <pinocchio/fwd.hpp>
38 
39 // This lines forces clang-format to keep the include split here
40 #include <pybind11/pybind11.h>
41 
42 #include <boost/python.hpp>
43 
44 namespace pinocchio {
45 namespace python {
46 namespace bp = boost::python;
47 namespace py = pybind11;
48 
49 template <typename T>
50 inline py::object to(T& t) {
51  // Create PyObject using boost Python
52  bp::object obj = bp::api::object(t);
53  PyObject* pyobj = obj.ptr();
54  return pybind11::reinterpret_borrow<py::object>(pyobj);
55 }
56 template <typename T>
57 inline py::object to(T* t) {
58  // Create PyObject using boost Python
59  typename bp::manage_new_object::apply<T*>::type converter;
60  PyObject* pyobj = converter(t);
61  // Create the Pybind11 object
62  return py::reinterpret_borrow<py::object>(pyobj);
63 }
64 
65 template <typename ReturnType>
66 inline ReturnType& from(py::handle model) {
67  return bp::extract<ReturnType&>(model.ptr());
68 }
69 
70 template <typename T>
71 struct convert_type {
72  typedef T type;
73  static inline T _to(T t) { return t; }
74  static inline type _from(type t) { return t; }
75 };
76 template <>
77 struct convert_type<void> {
78  // typedef void type;
79  // static inline void _to() {}
80 };
81 
82 template <typename T>
84  typedef py::object type;
85  static inline type _to(T t) {
86  return to<typename std::remove_pointer<typename std::remove_reference<
88  }
89  static inline T _from(type t) {
90  return from<
92  t);
93  }
94 };
95 
97 #define PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(CLASS) \
98  namespace pinocchio { \
99  namespace python { \
100  template <> \
101  struct convert_type<CLASS> : convert_boost_python_object<CLASS> {}; \
102  } \
103  }
104 
106 #define _SINGLE_ARG(...) __VA_ARGS__
107 #define PINOCCHIO_PYBIND11_ADD_ALL_CONVERT_TYPE(CLASS) \
108  PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS)) \
109  PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS const)) \
110  PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS&)) \
111  PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS const&)) \
112  PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS*)) \
113  PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS const*))
114 
115 namespace internal {
116 
117 template <typename R, typename... Args>
118 auto call(R (*f)(Args...), typename convert_type<Args>::type... args) {
120 }
121 template <typename... Args>
122 void call(void (*f)(Args...), typename convert_type<Args>::type... args) {
124 }
125 
126 template <typename T>
128 
129 template <typename R, typename... Args>
130 struct function_wrapper<R (*)(Args...)> {
131  static const size_t nargs = sizeof...(Args);
132 
133  typedef R result_type;
134 
135  template <size_t i>
136  struct arg {
137  typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
138  };
139 
140  typedef R (*func_type)(Args...);
141 
142  func_type f;
143 
144  // typename convert_type<result_type>::type
145  auto operator()(typename convert_type<Args>::type... args) {
146  return call(f, args...);
147  }
148 };
149 } // namespace internal
150 
160 template <typename R, typename... Args>
162  R (*func)(Args...)) {
163  internal::function_wrapper<R (*)(Args...)> wrapper;
164  wrapper.f = func;
165  return wrapper;
166 }
167 
168 template <typename T>
169 py::object default_arg(T t) {
170  py::object obj = to<T>(t);
171  //obj.inc_ref();
172  return obj;
173 }
174 
182 #define PINOCCHIO_PYBIND11_TYPE_CASTER(native_type, boost_python_name) \
183  namespace pybind11 { \
184  namespace detail { \
185  template <> \
186  struct type_caster<native_type> { \
187  PYBIND11_TYPE_CASTER(_SINGLE_ARG(native_type), boost_python_name); \
188  \
189  /* Python -> C++ */ \
190  bool load(pybind11::handle src, bool) { \
191  PyObject* source = src.ptr(); \
192  value = boost::python::extract<native_type>(source); \
193  return !PyErr_Occurred(); \
194  } \
195  /* C++ -> Python */ \
196  static pybind11::handle cast(native_type src, \
197  pybind11::return_value_policy /*policy*/, \
198  pybind11::handle /*parent*/) { \
199  return boost::python::api::object(src).ptr(); \
200  } \
201  }; \
202  } /* namespace detail */ \
203  } /* namespace pybind11 */
204 
205 } // namespace python
206 } // namespace pinocchio
207 
208 #undef _SINGLE_ARG
209 
210 #endif // #ifndef __pinocchio_python_pybind11_hpp__
pinocchio::python::make_pybind11_function
internal::function_wrapper< R(*)(Args...)> make_pybind11_function(R(*func)(Args...))
Creates a function wrapper.
Definition: pybind11.hpp:161
boost::python
pinocchio::python::convert_boost_python_object::type
py::object type
Definition: pybind11.hpp:84
pinocchio::python::internal::function_wrapper
Definition: pybind11.hpp:127
simulation-pendulum.T
int T
Definition: simulation-pendulum.py:113
pinocchio::python::convert_boost_python_object::_from
static T _from(type t)
Definition: pybind11.hpp:89
fwd.hpp
inverse-kinematics.i
int i
Definition: inverse-kinematics.py:20
pinocchio::python::convert_type::type
T type
Definition: pybind11.hpp:72
pinocchio::python::from
ReturnType & from(py::handle model)
Definition: pybind11.hpp:66
R
R
pinocchio::python::convert_type
Definition: pybind11.hpp:71
simulation-pendulum.type
type
Definition: simulation-pendulum.py:14
args
args
python
pinocchio::python::default_arg
py::object default_arg(T t)
Definition: pybind11.hpp:169
example_robot_data.__main__.nargs
nargs
Definition: __main__.py:8
pinocchio::python::convert_boost_python_object::_to
static type _to(T t)
Definition: pybind11.hpp:85
pinocchio::python::convert_type::_to
static T _to(T t)
Definition: pybind11.hpp:73
pinocchio::python::internal::call
auto call(R(*f)(Args...), typename convert_type< Args >::type... args)
Definition: pybind11.hpp:118
pinocchio::python::to
py::object to(T &t)
Definition: pybind11.hpp:50
t
Transform3f t
pinocchio::python::internal::function_wrapper< R(*)(Args...)>::operator()
auto operator()(typename convert_type< Args >::type... args)
Definition: pybind11.hpp:145
pinocchio::python::convert_type::_from
static type _from(type t)
Definition: pybind11.hpp:74
pinocchio::python::internal::function_wrapper< R(*)(Args...)>::f
func_type f
Definition: pybind11.hpp:142
pinocchio::python::convert_boost_python_object
Definition: pybind11.hpp:83
pinocchio::python::internal::function_wrapper< R(*)(Args...)>::arg::type
std::tuple_element< i, std::tuple< Args... > >::type type
Definition: pybind11.hpp:137
pinocchio::model
JointCollectionTpl & model
Definition: joint-configuration.hpp:746
pinocchio::python::internal::function_wrapper< R(*)(Args...)>::result_type
R result_type
Definition: pybind11.hpp:133
pinocchio
Main pinocchio namespace.
Definition: timings.cpp:28


pinocchio
Author(s):
autogenerated on Tue Apr 16 2024 02:40:55