|
| 1 | +// |
| 2 | +// Copyright (c) 2023 INRIA |
| 3 | +// |
| 4 | + |
| 5 | +#ifndef __eigenpy_utils_std_pair_hpp__ |
| 6 | +#define __eigenpy_utils_std_pair_hpp__ |
| 7 | + |
| 8 | +#include <boost/python.hpp> |
| 9 | +#include <utility> |
| 10 | + |
| 11 | +namespace eigenpy { |
| 12 | + |
| 13 | +template <typename pair_type> |
| 14 | +struct StdPairConverter { |
| 15 | + typedef typename pair_type::first_type T1; |
| 16 | + typedef typename pair_type::second_type T2; |
| 17 | + |
| 18 | + static PyObject* convert(const pair_type& pair) { |
| 19 | + return boost::python::incref( |
| 20 | + boost::python::make_tuple(pair.first, pair.second).ptr()); |
| 21 | + } |
| 22 | + |
| 23 | + static void* convertible(PyObject* obj) { |
| 24 | + if (!PyTuple_CheckExact(obj)) return 0; |
| 25 | + if (PyTuple_Size(obj) != 2) return 0; |
| 26 | + { |
| 27 | + boost::python::tuple tuple(boost::python::borrowed(obj)); |
| 28 | + boost::python::extract<T1> elt1(tuple[0]); |
| 29 | + if (!elt1.check()) return 0; |
| 30 | + boost::python::extract<T2> elt2(tuple[1]); |
| 31 | + if (!elt2.check()) return 0; |
| 32 | + } |
| 33 | + return obj; |
| 34 | + } |
| 35 | + |
| 36 | + static void construct( |
| 37 | + PyObject* obj, |
| 38 | + boost::python::converter::rvalue_from_python_stage1_data* memory) { |
| 39 | + boost::python::tuple tuple(boost::python::borrowed(obj)); |
| 40 | + void* storage = |
| 41 | + reinterpret_cast< |
| 42 | + boost::python::converter::rvalue_from_python_storage<pair_type>*>( |
| 43 | + reinterpret_cast<void*>(memory)) |
| 44 | + ->storage.bytes; |
| 45 | + new (storage) pair_type(boost::python::extract<T1>(tuple[0]), |
| 46 | + boost::python::extract<T2>(tuple[1])); |
| 47 | + memory->convertible = storage; |
| 48 | + } |
| 49 | + |
| 50 | + static PyTypeObject const* get_pytype() { |
| 51 | + PyTypeObject const* py_type = &PyTuple_Type; |
| 52 | + return py_type; |
| 53 | + } |
| 54 | + |
| 55 | + static void registration() { |
| 56 | + boost::python::converter::registry::push_back( |
| 57 | + &convertible, &construct, boost::python::type_id<pair_type>() |
| 58 | +#ifndef BOOST_PYTHON_NO_PY_SIGNATURES |
| 59 | + , |
| 60 | + get_pytype |
| 61 | +#endif |
| 62 | + ); |
| 63 | + boost::python::to_python_converter<pair_type, StdPairConverter, true>(); |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +} // namespace eigenpy |
| 68 | + |
| 69 | +#endif // ifndef __eigenpy_utils_std_pair_hpp__ |
0 commit comments