1 #ifndef AI_TOOLBOX_PYTHON_UTILS_HEADER_FILE
2 #define AI_TOOLBOX_PYTHON_UTILS_HEADER_FILE
8 #include <boost/python.hpp>
15 boost::python::to_python_converter<T, TupleToPython<T>>();
21 template<
int N,
int... S>
31 return boost::python::make_tuple(std::get<I>(t)...);
46 boost::python::to_python_converter<T, PairToPython<T>>();
50 return boost::python::incref(boost::python::make_tuple(t.first, t.second).ptr());
63 if (!PyTuple_CheckExact(obj_ptr))
return 0;
67 template <
size_t Id,
bool = true>
70 std::get<Id>(t) = boost::python::extract<std::tuple_element_t<Id, T>>(PyTuple_GetItem(tuple, Id));
75 template <
bool dummyForSpecialization>
78 std::get<0>(t) = boost::python::extract<std::tuple_element_t<0, T>>(PyTuple_GetItem(tuple, 0));
82 static void construct(PyObject* tuple, boost::python::converter::rvalue_from_python_stage1_data* data) {
84 void* storage = ((boost::python::converter::rvalue_from_python_storage<T>*)data)->storage.bytes;
86 T& t = *(
new (storage) T());
92 data->convertible = storage;
103 if (!PyTuple_CheckExact(obj_ptr))
return 0;
107 static void construct(PyObject* tuple, boost::python::converter::rvalue_from_python_stage1_data* data) {
109 void* storage = ((boost::python::converter::rvalue_from_python_storage<T>*)data)->storage.bytes;
111 T& t = *(
new (storage) T());
114 t.first = boost::python::extract<std::tuple_element_t<0, T>>(PyTuple_GetItem(tuple, 0));
115 t.second = boost::python::extract<std::tuple_element_t<1, T>>(PyTuple_GetItem(tuple, 1));
118 data->convertible = storage;
126 static void construct(PyObject* list, boost::python::converter::rvalue_from_python_stage1_data* data);
136 if (!PyList_Check(obj_ptr))
return 0;
140 static void construct(PyObject* list, boost::python::converter::rvalue_from_python_stage1_data* data) {
142 void* storage = ((boost::python::converter::rvalue_from_python_storage<std::vector<T>>*)data)->storage.bytes;
144 std::vector<T>& v = *(
new (storage) std::vector<T>());
147 auto size = PyList_Size(list);
149 for(decltype(size) i = 0; i < size; ++i)
150 v[i] = boost::python::extract<T>(PyList_GetItem(list, i));
153 data->convertible = storage;
157 template <
typename T>
159 using V2D = std::vector<std::vector<T>>;
166 if (!PyList_Check(obj_ptr) ||
167 !PyList_Check(PyList_GetItem(obj_ptr,0)))
return 0;
171 static void construct(PyObject* list, boost::python::converter::rvalue_from_python_stage1_data* data) {
173 void* storage = ((boost::python::converter::rvalue_from_python_storage<V2D>*)data)->storage.bytes;
175 V2D& v = *(
new (storage)
V2D());
178 auto size2 = PyList_Size(list);
180 for(decltype(size2) i = 0; i < size2; ++i) {
181 auto size1 = PyList_Size(PyList_GetItem(list,0));
183 for(decltype(size1) j = 0; j < size1; ++j) {
184 v[i][j] = boost::python::extract<T>(PyList_GetItem(PyList_GetItem(list, i), j));
189 data->convertible = storage;
193 template <
typename T>
195 using V3D = std::vector<std::vector<std::vector<T>>>;
202 if (!PyList_Check(obj_ptr) ||
203 !PyList_Check(PyList_GetItem(obj_ptr,0)) ||
204 !PyList_Check(PyList_GetItem(PyList_GetItem(obj_ptr,0),0)))
return 0;
208 static void construct(PyObject* list, boost::python::converter::rvalue_from_python_stage1_data* data) {
210 void* storage = ((boost::python::converter::rvalue_from_python_storage<V3D>*)data)->storage.bytes;
212 V3D& v = *(
new (storage)
V3D());
215 auto size3 = PyList_Size(list);
217 for(decltype(size3) i = 0; i < size3; ++i) {
218 auto size2 = PyList_Size(PyList_GetItem(list,0));
220 for(decltype(size2) j = 0; j < size2; ++j) {
221 auto size1 = PyList_Size(PyList_GetItem(PyList_GetItem(list,0),0));
222 v[i][j].resize(size1);
223 for(decltype(size1) k = 0; k < size1; ++k)
224 v[i][j][k] = boost::python::extract<T>(PyList_GetItem(PyList_GetItem(PyList_GetItem(list, i), j), k));
229 data->convertible = storage;