1 #ifndef __pinocchio_python_pybind11_hpp__
2 #define __pinocchio_python_pybind11_hpp__
40 #include <pybind11/pybind11.h>
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)
64 PyObject * pyobj = converter(
t);
66 return py::reinterpret_borrow<py::object>(pyobj);
69 template<
typename ReturnType>
72 return bp::extract<ReturnType &>(
model.ptr());
101 return to<
typename std::remove_pointer<
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>
141 template<
typename... Args>
150 template<
typename R,
typename... Args>
153 static const size_t nargs =
sizeof...(Args);
160 typedef typename std::tuple_element<
i, std::tuple<Args...>>
::type type;
163 typedef R (*func_type)(Args...);
184 template<
typename R,
typename... Args>
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(); \
239 #endif // #ifndef __pinocchio_python_pybind11_hpp__