diff options
author | Lars Knoll <lars.knoll@nokia.com> | 2012-01-10 13:30:23 +0100 |
---|---|---|
committer | Lars Knoll <lars.knoll@nokia.com> | 2012-01-11 14:58:14 +0100 |
commit | 482d34d2babcfd7827bc366df9a324a6d901817e (patch) | |
tree | 9cac67aef6f77bcb9d46743577ae7d9ae209a6b7 | |
parent | 2e20b0abf92b2ee81a4328ae5d1ae4aaff46e189 (diff) |
Added iterator interface to QJsonArray
Added both iterator and const_iterator and the
required typedefs and methods for STL compatibility.
Change-Id: Ib33a9c36c3f56a7da453bdc184f5166246a53bec
Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com>
Reviewed-by: Denis Dzyubenko <denis.dzyubenko@nokia.com>
-rw-r--r-- | src/qjsonarray.h | 121 | ||||
-rw-r--r-- | tests/auto/tst_qtjson.cpp | 62 |
2 files changed, 181 insertions, 2 deletions
diff --git a/src/qjsonarray.h b/src/qjsonarray.h index 29ed254..24b6769 100644 --- a/src/qjsonarray.h +++ b/src/qjsonarray.h @@ -42,7 +42,7 @@ #ifndef QJSONARRAY_H #define QJSONARRAY_H -#include <qjsonglobal.h> +#include <qjsonvalue.h> #include <qvariant.h> QT_BEGIN_NAMESPACE @@ -78,6 +78,8 @@ public: void append(const QJsonValue &value); void removeAt(int i); QJsonValue takeAt(int i); + inline void removeFirst() { removeAt(0); } + inline void removeLast() { removeAt(size() - 1); } void insert(int i, const QJsonValue &value); void replace(int i, const QJsonValue &value); @@ -89,6 +91,123 @@ public: bool operator==(const QJsonArray &other) const; bool operator!=(const QJsonArray &other) const; + class const_iterator; + + class iterator { + public: + QJsonArray *a; + int i; + typedef std::random_access_iterator_tag iterator_category; + typedef int difference_type; + typedef QJsonValue value_type; + //typedef T *pointer; + typedef QJsonValueRef reference; + + inline iterator() : a(0), i(0) { } + explicit inline iterator(QJsonArray *array, int index) : a(array), i(index) { } + + inline QJsonValueRef operator*() const { return QJsonValueRef(a, i); } + //inline T *operator->() const { return &concrete(i)->value; } + inline QJsonValueRef operator[](int j) const { return QJsonValueRef(a, i + j); } + + inline bool operator==(const iterator &o) const { return i == o.i; } + inline bool operator!=(const iterator &o) const { return i != o.i; } + inline bool operator<(const iterator& other) const { return i < other.i; } + inline bool operator<=(const iterator& other) const { return i <= other.i; } + inline bool operator>(const iterator& other) const { return i > other.i; } + inline bool operator>=(const iterator& other) const { return i >= other.i; } +#ifndef QT_STRICT_ITERATORS + inline bool operator==(const const_iterator &o) const { return i == o.i; } + inline bool operator!=(const const_iterator &o) const { return i != o.i; } + inline bool operator<(const const_iterator& other) const { return i < other.i; } + inline bool operator<=(const const_iterator& other) const { return i <= other.i; } + inline bool operator>(const const_iterator& other) const { return i > other.i; } + inline bool operator>=(const const_iterator& other) const { return i >= other.i; } +#endif + inline iterator &operator++() { ++i; return *this; } + inline iterator operator++(int) { iterator n = *this; ++i; return n; } + inline iterator &operator--() { i--; return *this; } + inline iterator operator--(int) { iterator n = *this; i--; return n; } + inline iterator &operator+=(int j) { i+=j; return *this; } + inline iterator &operator-=(int j) { i-=j; return *this; } + inline iterator operator+(int j) const { return iterator(a, i+j); } + inline iterator operator-(int j) const { return iterator(a, i-j); } + inline int operator-(iterator j) const { return i - j.i; } + }; + friend class iterator; + + class const_iterator { + public: + const QJsonArray *a; + int i; + typedef std::random_access_iterator_tag iterator_category; + typedef qptrdiff difference_type; + typedef QJsonValue value_type; + //typedef const T *pointer; + typedef QJsonValue reference; + + inline const_iterator() : a(0), i(0) { } + explicit inline const_iterator(const QJsonArray *array, int index) : a(array), i(index) { } + inline const_iterator(const const_iterator &o) : a(o.a), i(o.i) {} +#ifdef QT_STRICT_ITERATORS + inline explicit const_iterator(const iterator &o): a(o.a), i(o.i) {} +#else + inline const_iterator(const iterator &o) : a(o.a), i(o.i) {} +#endif + inline QJsonValue operator*() const { return a->at(i); } + //inline T *operator->() const { return &concrete(i)->value; } + inline QJsonValue operator[](int j) const { return a->at(i+j); } + inline bool operator==(const const_iterator &o) const { return i == o.i; } + inline bool operator!=(const const_iterator &o) const { return i != o.i; } + inline bool operator<(const const_iterator& other) const { return i < other.i; } + inline bool operator<=(const const_iterator& other) const { return i <= other.i; } + inline bool operator>(const const_iterator& other) const { return i > other.i; } + inline bool operator>=(const const_iterator& other) const { return i >= other.i; } + inline const_iterator &operator++() { ++i; return *this; } + inline const_iterator operator++(int) { const_iterator n = *this; ++i; return n; } + inline const_iterator &operator--() { i--; return *this; } + inline const_iterator operator--(int) { const_iterator n = *this; i--; return n; } + inline const_iterator &operator+=(int j) { i+=j; return *this; } + inline const_iterator &operator-=(int j) { i-=j; return *this; } + inline const_iterator operator+(int j) const { return const_iterator(a, i+j); } + inline const_iterator operator-(int j) const { return const_iterator(a, i-j); } + inline int operator-(const_iterator j) const { return i - j.i; } + }; + friend class const_iterator; + + // stl style + inline iterator begin() { detach(); return iterator(this, 0); } + inline const_iterator begin() const { return const_iterator(this, 0); } + inline const_iterator constBegin() const { return const_iterator(this, 0); } + inline iterator end() { detach(); return iterator(this, size()); } + inline const_iterator end() const { return const_iterator(this, size()); } + inline const_iterator constEnd() const { return const_iterator(this, size()); } + iterator insert(iterator before, const QJsonValue &value) { insert(before.i, value); return before; } + iterator erase(iterator pos) { removeAt(pos.i); return pos; } +// iterator erase(iterator first, iterator last); + + // more Qt + typedef iterator Iterator; + typedef const_iterator ConstIterator; + + // stl compatibility + inline void push_back(const QJsonValue &t) { append(t); } + inline void push_front(const QJsonValue &t) { prepend(t); } +// inline T& front() { return first(); } +// inline const T& front() const { return first(); } +// inline T& back() { return last(); } +// inline const T& back() const { return last(); } + inline void pop_front() { removeFirst(); } + inline void pop_back() { removeLast(); } + inline bool empty() const { return isEmpty(); } + typedef int size_type; + typedef QJsonValue value_type; + typedef value_type *pointer; + typedef const value_type *const_pointer; + typedef QJsonValueRef reference; + typedef QJsonValue const_reference; + typedef int difference_type; + private: friend class Private::Data; friend class QJsonValue; diff --git a/tests/auto/tst_qtjson.cpp b/tests/auto/tst_qtjson.cpp index 6a818e7..70dfa74 100644 --- a/tests/auto/tst_qtjson.cpp +++ b/tests/auto/tst_qtjson.cpp @@ -71,6 +71,7 @@ private Q_SLOTS: void testValueRef(); void testObjectIteration(); + void testArrayIteration(); void testObjectFind(); @@ -560,6 +561,66 @@ void TestQtJson::testObjectIteration() QVERIFY(it == object.end()); } +void TestQtJson::testArrayIteration() +{ + QJsonArray array; + for (int i = 0; i < 10; ++i) + array.append(i); + + QCOMPARE(array.size(), 10); + + int i = 0; + for (QJsonArray::iterator it = array.begin(); it != array.end(); ++it, ++i) { + QJsonValue value = (*it); + QCOMPARE(i, value.toInt()); + } + + { + QJsonArray array2 = array; + QVERIFY(array == array2); + + QJsonValue val = *array2.begin(); + array2.erase(array2.begin()); + QCOMPARE(array.size(), 10); + QCOMPARE(array2.size(), 9); + + i = 1; + for (QJsonArray::const_iterator it = array2.constBegin(); it != array2.constEnd(); ++it, ++i) { + QJsonValue value = (*it); + QCOMPARE(i, value.toInt()); + } + } + + { + QJsonArray::Iterator it = array.begin(); + it += 5; + QCOMPARE(QJsonValue((*it)).toInt(), 5); + it -= 3; + QCOMPARE(QJsonValue((*it)).toInt(), 2); + QJsonArray::Iterator it2 = it + 5; + QCOMPARE(QJsonValue(*it2).toInt(), 7); + it2 = it - 1; + QCOMPARE(QJsonValue(*it2).toInt(), 1); + } + + { + QJsonArray::ConstIterator it = array.constBegin(); + it += 5; + QCOMPARE(QJsonValue((*it)).toInt(), 5); + it -= 3; + QCOMPARE(QJsonValue((*it)).toInt(), 2); + QJsonArray::ConstIterator it2 = it + 5; + QCOMPARE(QJsonValue(*it2).toInt(), 7); + it2 = it - 1; + QCOMPARE(QJsonValue(*it2).toInt(), 1); + } + + QJsonArray::Iterator it = array.begin(); + while (!array.isEmpty()) + it = array.erase(it); + QCOMPARE(array.size() , 0); + QVERIFY(it == array.end()); +} void TestQtJson::testObjectFind() { @@ -582,7 +643,6 @@ void TestQtJson::testObjectFind() QVERIFY(it == object.end()); } - void TestQtJson::testDocument() { QJsonDocument doc; |