diff --git a/src/corelib/kernel/qjniarray.h b/src/corelib/kernel/qjniarray.h index 998465273e1..7f958dc28d4 100644 --- a/src/corelib/kernel/qjniarray.h +++ b/src/corelib/kernel/qjniarray.h @@ -48,23 +48,54 @@ struct QJniArrayIterator ++that.m_index; return that; } - friend QJniArrayIterator operator++(QJniArrayIterator &that, int) noexcept + friend QJniArrayIterator operator++(QJniArrayIterator &that, difference_type) noexcept { auto copy = that; ++that; return copy; } + friend QJniArrayIterator operator+(const QJniArrayIterator &that, difference_type n) noexcept + { + return {that.m_index + n, that.m_array}; + } + friend QJniArrayIterator operator+(difference_type n, const QJniArrayIterator &that) noexcept + { + return that + n; + } + friend QJniArrayIterator &operator+=(QJniArrayIterator &that, difference_type n) noexcept + { + that.m_index += n; + return that; + } friend QJniArrayIterator &operator--(QJniArrayIterator &that) noexcept { --that.m_index; return that; } - friend QJniArrayIterator operator--(QJniArrayIterator &that, int) noexcept + friend QJniArrayIterator operator--(QJniArrayIterator &that, difference_type) noexcept { auto copy = that; --that; return copy; } + friend QJniArrayIterator operator-(const QJniArrayIterator &that, difference_type n) noexcept + { + return {that.m_index - n, that.m_array}; + } + friend QJniArrayIterator operator-(difference_type n, const QJniArrayIterator &that) noexcept + { + return {n - that.m_index, that.m_array}; + } + friend QJniArrayIterator &operator-=(QJniArrayIterator &that, difference_type n) noexcept + { + that.m_index -= n; + return that; + } + friend difference_type operator-(const QJniArrayIterator &lhs, const QJniArrayIterator &rhs) + { + Q_ASSERT(lhs.m_array == rhs.m_array); + return lhs.m_index - rhs.m_index; + } void swap(QJniArrayIterator &other) noexcept { std::swap(m_index, other.m_index); diff --git a/tests/auto/corelib/kernel/qjniarray/tst_qjniarray.cpp b/tests/auto/corelib/kernel/qjniarray/tst_qjniarray.cpp index 4b1a7ba6e68..37b632b6349 100644 --- a/tests/auto/corelib/kernel/qjniarray/tst_qjniarray.cpp +++ b/tests/auto/corelib/kernel/qjniarray/tst_qjniarray.cpp @@ -234,6 +234,23 @@ void tst_QJniArray::operators() it++; QCOMPARE(*it, 'e'); QCOMPARE(++it, array.end()); + + it -= array.size(); + QCOMPARE(it, array.begin()); + it += 2; + QCOMPARE(*it, 'c'); + + const auto it2 = it + 2; + QCOMPARE(*it2, 'e'); + QCOMPARE(it2 - it, 2); + + it = it2 - 2; + QCOMPARE(*it, 'c'); + + it = 1 + it; + QCOMPARE(*it, 'd'); + it = array.size() - it; + QCOMPARE(*it, 'c'); } { auto it = array.rbegin();