Skip to content

Commit 707613b

Browse files
committed
core: add std::pair converter support
1 parent 0d63d8c commit 707613b

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ set(${PROJECT_NAME}_HEADERS
164164
include/eigenpy/register.hpp
165165
include/eigenpy/std-array.hpp
166166
include/eigenpy/std-map.hpp
167+
include/eigenpy/std-pair.hpp
167168
include/eigenpy/std-vector.hpp
168169
include/eigenpy/optional.hpp
169170
include/eigenpy/pickle-vector.hpp

include/eigenpy/std-pair.hpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
reinterpret_cast<void* (*)(_object*)>(&convertible), &construct,
58+
boost::python::type_id<pair_type>()
59+
#ifndef BOOST_PYTHON_NO_PY_SIGNATURES
60+
,
61+
get_pytype
62+
#endif
63+
);
64+
}
65+
};
66+
67+
} // namespace eigenpy
68+
69+
#endif // ifndef __eigenpy_utils_std_pair_hpp__

0 commit comments

Comments
 (0)