QJniArray: fix const_iterator declaration, make it default-constructible

Iterators are required to be default-constructible and
value-initialized iterators must compare equal.

In a const_iterator, the pointee should be const, not
the iterator itself.

Found during header review.

Task-number: QTBUG-119952
Change-Id: I036c0a62ade8c59dc5d62c0823b375223719af3f
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
(cherry picked from commit 100071af82963e712cb055e5d98795376d89e65c)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Volker Hilsheimer 2024-01-22 13:41:02 +01:00 committed by Qt Cherry-pick Bot
parent 6a5b47ecac
commit 6f959d4b05
2 changed files with 24 additions and 5 deletions

View File

@ -19,6 +19,8 @@ template <typename T> class QJniArray;
template <typename T>
struct QJniArrayIterator
{
QJniArrayIterator() = default;
constexpr QJniArrayIterator(const QJniArrayIterator &other) noexcept = default;
constexpr QJniArrayIterator(QJniArrayIterator &&other) noexcept = default;
constexpr QJniArrayIterator &operator=(const QJniArrayIterator &other) noexcept = default;
@ -49,13 +51,13 @@ struct QJniArrayIterator
}
private:
friend class QJniArray<T>;
using VT = std::remove_const_t<T>;
friend class QJniArray<VT>;
qsizetype m_index = 0;
const QJniArray<T> *m_array = nullptr;
const QJniArray<VT> *m_array = nullptr;
QJniArrayIterator() = delete;
QJniArrayIterator(qsizetype index, const QJniArray<T> *array)
QJniArrayIterator(qsizetype index, const QJniArray<VT> *array)
: m_index(index), m_array(array)
{}
};
@ -154,7 +156,7 @@ class QJniArray : public QJniArrayBase
friend struct QJniArrayIterator<T>;
public:
using Type = T;
using const_iterator = const QJniArrayIterator<T>;
using const_iterator = QJniArrayIterator<const T>;
QJniArray() = default;
explicit QJniArray(jarray array) : QJniArrayBase(array) {}

View File

@ -17,6 +17,7 @@ public:
private slots:
void size();
void operators();
};
using namespace QtJniTypes;
@ -85,6 +86,22 @@ void tst_QJniArray::size()
QCOMPARE(intArray.size(), 10);
}
void tst_QJniArray::operators()
{
QByteArray bytes("abcde");
QJniArray<jbyte> array(bytes);
QVERIFY(array.isValid());
{
QJniArray<jbyte>::const_iterator it = {};
QCOMPARE(it, QJniArray<jbyte>::const_iterator{});
QCOMPARE_NE(array.begin(), array.end());
it = array.begin();
QCOMPARE(it, array.begin());
}
}
QTEST_MAIN(tst_QJniArray)
#include "tst_qjniarray.moc"