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