QJniArray: make reverse-iterable

Add rbeing/rend overload, relevant typedefs, and decrement operators.
As a drive-by, add noexcept to begin/end functions.

Found during header review.

Task-number: QTBUG-119952
Change-Id: I32d9a7d50a1f03550944c2247516c455d4822fe7
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit cc5251ed59ea6ecf0cc666ae5321be6de5383e90)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2024-01-22 16:06:17 +01:00 committed by Qt Cherry-pick Bot
parent 7ff956c426
commit 0dbe271b9c
2 changed files with 35 additions and 8 deletions

View File

@ -31,7 +31,7 @@ struct QJniArrayIterator
using pointer = T *;
using reference = T; // difference to container requirements
using const_reference = reference;
using iterator_category = std::forward_iterator_tag;
using iterator_category = std::bidirectional_iterator_tag;
friend bool operator==(const QJniArrayIterator &lhs, const QJniArrayIterator &rhs) noexcept
{
@ -56,7 +56,17 @@ struct QJniArrayIterator
++that;
return copy;
}
friend QJniArrayIterator &operator--(QJniArrayIterator &that) noexcept
{
--that.m_index;
return that;
}
friend QJniArrayIterator operator--(QJniArrayIterator &that, int) noexcept
{
auto copy = that;
--that;
return copy;
}
void swap(QJniArrayIterator &other) noexcept
{
std::swap(m_index, other.m_index);
@ -190,6 +200,7 @@ public:
// read-only container, so no iterator typedef
using const_iterator = QJniArrayIterator<const T>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
QJniArray() = default;
explicit QJniArray(jarray array) : QJniArrayBase(array) {}
@ -238,12 +249,17 @@ public:
return object<jarray>();
}
const_iterator begin() const { return {0, this}; }
const_iterator constBegin() const { return begin(); }
const_iterator cbegin() const { return begin(); }
const_iterator end() const { return {size(), this}; }
const_iterator constEnd() const { return {end()}; }
const_iterator cend() const { return {end()}; }
const_iterator begin() const noexcept { return {0, this}; }
const_iterator constBegin() const noexcept { return begin(); }
const_iterator cbegin() const noexcept { return begin(); }
const_iterator end() const noexcept { return {size(), this}; }
const_iterator constEnd() const noexcept { return {end()}; }
const_iterator cend() const noexcept { return {end()}; }
const_reverse_iterator rbegin() const noexcept { return const_reverse_iterator(end()); }
const_reverse_iterator rend() const noexcept { return const_reverse_iterator(begin()); }
const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(end()); }
const_reverse_iterator crend() const noexcept { return const_reverse_iterator(begin()); }
const_reference operator[](size_type i) const { return at(i); }
const_reference at(size_type i) const

View File

@ -103,6 +103,17 @@ void tst_QJniArray::operators()
QCOMPARE(*it, 'e');
QCOMPARE(++it, array.end());
}
{
auto it = array.rbegin();
QCOMPARE(*it, 'e');
QCOMPARE(*++it, 'd');
QCOMPARE(*it++, 'd');
QCOMPARE(*it, 'c');
++it;
it++;
QCOMPARE(*it, 'a');
QCOMPARE(++it, array.rend());
}
{
QJniArray<jbyte>::const_iterator it = {};
QCOMPARE(it, QJniArray<jbyte>::const_iterator{});