diff options
Diffstat (limited to 'Lib/python/std_array.i')
-rw-r--r-- | Lib/python/std_array.i | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/Lib/python/std_array.i b/Lib/python/std_array.i index a3de3125b..9cca563c0 100644 --- a/Lib/python/std_array.i +++ b/Lib/python/std_array.i @@ -30,6 +30,54 @@ } template <class T, size_t N> + struct IteratorProtocol<std::array<T, N>, T> { + static int assign(PyObject *obj, std::array<T, N> **seq) { + int ret = SWIG_ERROR; + PyObject *iter = PyObject_GetIter(obj); + if (iter) { + PyObject *item = PyIter_Next(iter); + size_t count = 0; + typename std::array<T, N>::iterator array_iter = nullptr; + ret = SWIG_OK; + if (seq) { + *seq = new std::array<T, N>(); + array_iter = (*seq)->begin(); + } + while (item && (count < N)) { + try { + if (seq) { + *array_iter++ = swig::as<T>(item); + } else { + if (!swig::check<T>(item)) + ret = SWIG_ERROR; + } + } catch (std::exception& e) { + if (seq) { + if (!PyErr_Occurred()) { + PyErr_SetString(PyExc_TypeError, e.what()); + } + } + ret = SWIG_ERROR; + } + ++count; + Py_DECREF(item); + item = (ret == SWIG_OK) ? PyIter_Next(iter) : 0; + } + if ((ret == SWIG_OK) && (count != N || item)) { + PyErr_SetString(PyExc_TypeError, "std::array size does not match source container size"); + ret = SWIG_ERROR; + } + Py_XDECREF(item); + Py_DECREF(iter); + if (seq && (ret == SWIG_ERROR)) + delete *seq; + } + + return ret; + } + }; + + template <class T, size_t N> inline void erase(std::array<T, N>* SWIGUNUSEDPARM(seq), const typename std::array<T, N>::iterator& SWIGUNUSEDPARM(position)) { throw std::invalid_argument("std::array object does not support item deletion"); |