1#ifndef __pinocchio_python_pybind11_hpp__ 2#define __pinocchio_python_pybind11_hpp__ 37#include <pinocchio/fwd.hpp> 40#include <pybind11/pybind11.h> 42#include <eigenpy/eigenpy.hpp> 48 namespace bp = boost::python;
49 namespace py = pybind11;
52 inline py::object to(T & t)
55 bp::object obj = bp::api::object(t);
56 PyObject * pyobj = obj.ptr();
57 return pybind11::reinterpret_borrow<py::object>(pyobj);
60 inline py::object to(T * t)
63 typename bp::manage_new_object::apply<T *>::type converter;
64 PyObject * pyobj = converter(t);
66 return py::reinterpret_borrow<py::object>(pyobj);
69 template<
typename ReturnType>
70 inline ReturnType & from(py::handle model)
72 return bp::extract<ReturnType &>(model.ptr());
79 static inline T _to(T t)
83 static inline type _from(type t)
98 typedef py::object type;
99 static inline type _to(T t)
101 return to<
typename std::remove_pointer<
102 typename std::remove_reference<typename std::remove_cv<T>::type>::type>::type>(t);
104 static inline T _from(type t)
106 return from<typename std::remove_cv<typename std::remove_reference<T>::type>::type>(t);
111#define PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(CLASS) \ 112 namespace pinocchio \ 117 struct convert_type<CLASS> : convert_boost_python_object<CLASS> \ 124#define _SINGLE_ARG(...) __VA_ARGS__ 125#define PINOCCHIO_PYBIND11_ADD_ALL_CONVERT_TYPE(CLASS) \ 126 PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS)) \ 127 PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS const)) \ 128 PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS &)) \ 129 PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS const &)) \ 130 PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS *)) \ 131 PINOCCHIO_PYBIND11_ADD_CONVERT_TYPE(_SINGLE_ARG(CLASS const *)) 136 template<
typename R,
typename... Args>
137 auto call(R (*f)(Args...),
typename convert_type<Args>::type... args)
139 return convert_type<R>::_to(f(convert_type<Args>::_from(args)...));
141 template<
typename... Args>
142 void call(
void (*f)(Args...),
typename convert_type<Args>::type... args)
144 f(convert_type<Args>::_from(args)...);
148 struct function_wrapper;
150 template<
typename R,
typename... Args>
151 struct function_wrapper<R (*)(Args...)>
153 static const size_t nargs =
sizeof...(Args);
155 typedef R result_type;
160 typedef typename std::tuple_element<i, std::tuple<Args...>>::type type;
163 typedef R (*func_type)(Args...);
168 auto operator()(
typename convert_type<Args>::type... args)
170 return call(f, args...);
184 template<
typename R,
typename... Args>
185 internal::function_wrapper<R (*)(Args...)> make_pybind11_function(R (*func)(Args...))
187 internal::function_wrapper<R (*)(Args...)> wrapper;
193 py::object default_arg(T t)
195 py::object obj = to<T>(t);
207#define PINOCCHIO_PYBIND11_TYPE_CASTER(native_type, boost_python_name) \ 213 struct type_caster<native_type> \ 215 PYBIND11_TYPE_CASTER(_SINGLE_ARG(native_type), boost_python_name); \ 218 bool load(pybind11::handle src, bool) \ 220 PyObject * source = src.ptr(); \ 221 value = boost::python::extract<native_type>(source); \ 222 return !PyErr_Occurred(); \ 225 static pybind11::handle cast( \ 226 native_type src, pybind11::return_value_policy , pybind11::handle ) \ 228 return boost::python::api::object(src).ptr(); \ Main pinocchio namespace.