QJniArray: add arithmetic operators

Required for making the iterator random access.

Based on review comments.

Task-number: QTBUG-126150
Change-Id: I80ee8ed584747759acb17ee956551caba4d5bdaa
Reviewed-by: Soheil Armin <soheil.armin@qt.io>
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit c4e406e3792405cfc0b8cc97a29f136c0fd44a2e)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2024-06-24 15:18:27 +02:00
parent 46ed4e47d2
commit 0db64ca53b
2 changed files with 50 additions and 2 deletions

View File

@ -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);

View File

@ -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();