Improve the iterators of QJsonArray and QJsonObject

Remove the fake QJsonValuePtr and QJsonValueRefPtr required for
operator()-> of QJsonArray and QJsonObject iterators.

Task-number: QTBUG-85700
Change-Id: I622a5a426edb13b32f9d00a02c3c148320fbccba
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Sona Kurazyan 2020-07-23 12:16:42 +02:00
parent bd3088ceb3
commit 062bee3a2a
5 changed files with 189 additions and 168 deletions

View File

@ -941,12 +941,12 @@ bool QJsonArray::operator!=(const QJsonArray &other) const
Constructs a copy of \a other. Constructs a copy of \a other.
*/ */
/*! \fn QJsonValue QJsonArray::const_iterator::operator*() const /*! \fn const QJsonValueRef QJsonArray::const_iterator::operator*() const
Returns the current item. Returns the current item.
*/ */
/*! \fn QJsonValue *QJsonArray::const_iterator::operator->() const /*! \fn const QJsonValueRef *QJsonArray::const_iterator::operator->() const
Returns a pointer to the current item. Returns a pointer to the current item.
*/ */

View File

@ -109,85 +109,108 @@ public:
class iterator { class iterator {
public: public:
QJsonArray *a;
int i;
typedef std::random_access_iterator_tag iterator_category; typedef std::random_access_iterator_tag iterator_category;
typedef int difference_type; typedef int difference_type;
typedef QJsonValue value_type; typedef QJsonValue value_type;
typedef QJsonValueRef reference; typedef QJsonValueRef reference;
typedef QJsonValueRefPtr pointer; typedef QJsonValueRef *pointer;
inline iterator() : a(nullptr), i(0) { } inline iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { }
explicit inline iterator(QJsonArray *array, int index) : a(array), i(index) { } explicit inline iterator(QJsonArray *array, int index) : item(array, index) { }
inline QJsonValueRef operator*() const { return QJsonValueRef(a, i); } constexpr iterator(const iterator &other) = default;
#ifdef Q_QDOC iterator &operator=(const iterator &other)
inline QJsonValueRef* operator->() const; {
#else item.a = other.item.a;
inline QJsonValueRefPtr operator->() const { return QJsonValueRefPtr(a, i); } item.index = other.item.index;
#endif return *this;
inline QJsonValueRef operator[](int j) const { return QJsonValueRef(a, i + j); } }
inline bool operator==(const iterator &o) const { return i == o.i; } inline QJsonValueRef operator*() const { return item; }
inline bool operator!=(const iterator &o) const { return i != o.i; } inline QJsonValueRef *operator->() const { return &item; }
inline bool operator<(const iterator& other) const { return i < other.i; } inline QJsonValueRef operator[](int j) const { return { item.a, int(item.index) + j }; }
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 &o) const
inline bool operator>=(const iterator& other) const { return i >= other.i; } { return item.a == o.item.a && item.index == o.item.index; }
inline bool operator==(const const_iterator &o) const { return i == o.i; } inline bool operator!=(const iterator &o) const { return !(*this == o); }
inline bool operator!=(const const_iterator &o) const { return i != o.i; } inline bool operator<(const iterator &other) const
inline bool operator<(const const_iterator& other) const { return i < other.i; } { Q_ASSERT(item.a == other.item.a); return item.index < other.item.index; }
inline bool operator<=(const const_iterator& other) const { return i <= other.i; } inline bool operator<=(const iterator &other) const
inline bool operator>(const const_iterator& other) const { return i > other.i; } { Q_ASSERT(item.a == other.item.a); return item.index <= other.item.index; }
inline bool operator>=(const const_iterator& other) const { return i >= other.i; } inline bool operator>(const iterator &other) const { return !(*this <= other); }
inline iterator &operator++() { ++i; return *this; } inline bool operator>=(const iterator &other) const { return !(*this < other); }
inline iterator operator++(int) { iterator n = *this; ++i; return n; } inline bool operator==(const const_iterator &o) const
inline iterator &operator--() { i--; return *this; } { return item.a == o.item.a && item.index == o.item.index; }
inline iterator operator--(int) { iterator n = *this; i--; return n; } inline bool operator!=(const const_iterator &o) const { return !(*this == o); }
inline iterator &operator+=(int j) { i+=j; return *this; } inline bool operator<(const const_iterator &other) const
inline iterator &operator-=(int j) { i-=j; return *this; } { Q_ASSERT(item.a == other.item.a); return item.index < other.item.index; }
inline iterator operator+(int j) const { return iterator(a, i+j); } inline bool operator<=(const const_iterator &other) const
inline iterator operator-(int j) const { return iterator(a, i-j); } { Q_ASSERT(item.a == other.item.a); return item.index <= other.item.index; }
inline int operator-(iterator j) const { return i - j.i; } inline bool operator>(const const_iterator &other) const { return !(*this <= other); }
inline bool operator>=(const const_iterator &other) const { return !(*this < other); }
inline iterator &operator++() { ++item.index; return *this; }
inline iterator operator++(int) { iterator n = *this; ++item.index; return n; }
inline iterator &operator--() { item.index--; return *this; }
inline iterator operator--(int) { iterator n = *this; item.index--; return n; }
inline iterator &operator+=(int j) { item.index += j; return *this; }
inline iterator &operator-=(int j) { item.index -= j; return *this; }
inline iterator operator+(int j) const { return iterator(item.a, item.index + j); }
inline iterator operator-(int j) const { return iterator(item.a, item.index - j); }
inline int operator-(iterator j) const { return item.index - j.item.index; }
private:
mutable QJsonValueRef item;
friend class QJsonArray;
}; };
friend class iterator; friend class iterator;
class const_iterator { class const_iterator {
public: public:
const QJsonArray *a;
int i;
typedef std::random_access_iterator_tag iterator_category; typedef std::random_access_iterator_tag iterator_category;
typedef qptrdiff difference_type; typedef qptrdiff difference_type;
typedef QJsonValue value_type; typedef QJsonValue value_type;
typedef QJsonValue reference; typedef const QJsonValueRef reference;
typedef QJsonValuePtr pointer; typedef const QJsonValueRef *pointer;
inline const_iterator() : a(nullptr), i(0) { } inline const_iterator() : item(static_cast<QJsonArray *>(nullptr), 0) { }
explicit inline const_iterator(const QJsonArray *array, int index) : a(array), i(index) { } explicit inline const_iterator(const QJsonArray *array, int index)
inline const_iterator(const iterator &o) : a(o.a), i(o.i) {} : item(const_cast<QJsonArray *>(array), index) { }
inline const_iterator(const iterator &o) : item(o.item) { }
inline QJsonValue operator*() const { return a->at(i); } constexpr const_iterator(const const_iterator &other) = default;
#ifdef Q_QDOC const_iterator &operator=(const const_iterator &other)
inline QJsonValue* operator->() const; {
#else item.a = other.item.a;
inline QJsonValuePtr operator->() const { return QJsonValuePtr(a->at(i)); } item.index = other.item.index;
#endif return *this;
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 const QJsonValueRef operator*() const { return item; }
inline bool operator<(const const_iterator& other) const { return i < other.i; } inline const QJsonValueRef *operator->() const { return &item; }
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 QJsonValueRef operator[](int j) const { return { item.a, int(item.index) + j }; }
inline bool operator>=(const const_iterator& other) const { return i >= other.i; } inline bool operator==(const const_iterator &o) const
inline const_iterator &operator++() { ++i; return *this; } { return item.a == o.item.a && item.index == o.item.index; }
inline const_iterator operator++(int) { const_iterator n = *this; ++i; return n; } inline bool operator!=(const const_iterator &o) const { return !(*this == o); }
inline const_iterator &operator--() { i--; return *this; } inline bool operator<(const const_iterator &other) const
inline const_iterator operator--(int) { const_iterator n = *this; i--; return n; } { Q_ASSERT(item.a == other.item.a); return item.index < other.item.index; }
inline const_iterator &operator+=(int j) { i+=j; return *this; } inline bool operator<=(const const_iterator &other) const
inline const_iterator &operator-=(int j) { i-=j; return *this; } { Q_ASSERT(item.a == other.item.a); return item.index <= other.item.index; }
inline const_iterator operator+(int j) const { return const_iterator(a, i+j); } inline bool operator>(const const_iterator &other) const { return !(*this <= other); }
inline const_iterator operator-(int j) const { return const_iterator(a, i-j); } inline bool operator>=(const const_iterator &other) const { return !(*this < other); }
inline int operator-(const_iterator j) const { return i - j.i; } inline const_iterator &operator++() { ++item.index; return *this; }
inline const_iterator operator++(int) { const_iterator n = *this; ++item.index; return n; }
inline const_iterator &operator--() { item.index--; return *this; }
inline const_iterator operator--(int) { const_iterator n = *this; item.index--; return n; }
inline const_iterator &operator+=(int j) { item.index += j; return *this; }
inline const_iterator &operator-=(int j) { item.index -= j; return *this; }
inline const_iterator operator+(int j) const { return const_iterator(item.a, item.index + j); }
inline const_iterator operator-(int j) const { return const_iterator(item.a, item.index - j); }
inline int operator-(const_iterator j) const { return item.index - j.item.index; }
private:
QJsonValueRef item;
friend class QJsonArray;
}; };
friend class const_iterator; friend class const_iterator;
@ -200,8 +223,10 @@ public:
inline const_iterator end() const { return const_iterator(this, size()); } inline const_iterator end() const { return const_iterator(this, size()); }
inline const_iterator constEnd() const { return const_iterator(this, size()); } inline const_iterator constEnd() const { return const_iterator(this, size()); }
inline const_iterator cend() const { return const_iterator(this, size()); } inline const_iterator cend() const { return const_iterator(this, size()); }
iterator insert(iterator before, const QJsonValue &value) { insert(before.i, value); return before; } iterator insert(iterator before, const QJsonValue &value)
iterator erase(iterator it) { removeAt(it.i); return it; } { insert(before.item.index, value); return before; }
iterator erase(iterator it)
{ removeAt(it.item.index); return it; }
// more Qt // more Qt
typedef iterator Iterator; typedef iterator Iterator;

View File

@ -700,12 +700,10 @@ bool QJsonObject::operator!=(const QJsonObject &other) const
*/ */
QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it) QJsonObject::iterator QJsonObject::erase(QJsonObject::iterator it)
{ {
if (it.o != this || it.i < 0 || it.i >= o->elements.length()) if (it.item.o != this || it.item.index < 0 || it.item.index >= o->elements.length())
return {this, int(o->elements.length())}; return {this, int(o->elements.length())};
int index = it.i; removeAt(it.item.index);
removeAt(index);
// iterator hasn't changed // iterator hasn't changed
return it; return it;
@ -1233,14 +1231,14 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const
\sa value() \sa value()
*/ */
/*! \fn QJsonValue QJsonObject::const_iterator::value() const /*! \fn QJsonValueRef QJsonObject::const_iterator::value() const
Returns the current item's value. Returns the current item's value.
\sa key(), operator*() \sa key(), operator*()
*/ */
/*! \fn QJsonValue QJsonObject::const_iterator::operator*() const /*! \fn const QJsonValueRef QJsonObject::const_iterator::operator*() const
Returns the current item's value. Returns the current item's value.
@ -1249,7 +1247,7 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const
\sa key() \sa key()
*/ */
/*! \fn QJsonValue *QJsonObject::const_iterator::operator->() const /*! \fn const QJsonValueRef *QJsonObject::const_iterator::operator->() const
Returns a pointer to the current item. Returns a pointer to the current item.
*/ */

View File

@ -125,110 +125,128 @@ public:
{ {
friend class const_iterator; friend class const_iterator;
friend class QJsonObject; friend class QJsonObject;
QJsonObject *o; mutable QJsonValueRef item;
int i;
public: public:
typedef std::random_access_iterator_tag iterator_category; typedef std::random_access_iterator_tag iterator_category;
typedef int difference_type; typedef int difference_type;
typedef QJsonValue value_type; typedef QJsonValue value_type;
typedef QJsonValueRef reference; typedef QJsonValueRef reference;
typedef QJsonValuePtr pointer; typedef QJsonValueRef *pointer;
Q_DECL_CONSTEXPR inline iterator() : o(nullptr), i(0) {} inline iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
Q_DECL_CONSTEXPR inline iterator(QJsonObject *obj, int index) : o(obj), i(index) {} inline iterator(QJsonObject *obj, int index) : item(obj, index) { }
inline QString key() const { return o->keyAt(i); } constexpr iterator(const iterator &other) = default;
inline QJsonValueRef value() const { return QJsonValueRef(o, i); } iterator &operator=(const iterator &other)
inline QJsonValueRef operator*() const { return QJsonValueRef(o, i); } {
#ifdef Q_QDOC item.o = other.item.o;
inline QJsonValueRef* operator->() const; item.index = other.item.index;
#else return *this;
inline QJsonValueRefPtr operator->() const { return QJsonValueRefPtr(o, i); } }
#endif
const QJsonValueRef operator[](int j) { return QJsonValueRef(o, i + j); }
inline bool operator==(const iterator &other) const { return i == other.i; } inline QString key() const { return item.o->keyAt(item.index); }
inline bool operator!=(const iterator &other) const { return i != other.i; } inline QJsonValueRef value() const { return item; }
bool operator<(const iterator& other) const { return i < other.i; } inline QJsonValueRef operator*() const { return item; }
bool operator<=(const iterator& other) const { return i <= other.i; } inline QJsonValueRef *operator->() const { return &item; }
bool operator>(const iterator& other) const { return i > other.i; } const QJsonValueRef operator[](int j) { return { item.o, int(item.index) + j }; }
bool operator>=(const iterator& other) const { return i >= other.i; }
inline iterator &operator++() { ++i; return *this; } inline bool operator==(const iterator &other) const
inline iterator operator++(int) { iterator r = *this; ++i; return r; } { return item.o == other.item.o && item.index == other.item.index; }
inline iterator &operator--() { --i; return *this; } inline bool operator!=(const iterator &other) const { return !(*this == other); }
inline iterator operator--(int) { iterator r = *this; --i; return r; } bool operator<(const iterator& other) const
{ Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
bool operator<=(const iterator& other) const
{ Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
bool operator>(const iterator& other) const { return !(*this <= other); }
bool operator>=(const iterator& other) const { return !(*this < other); }
inline iterator &operator++() { ++item.index; return *this; }
inline iterator operator++(int) { iterator r = *this; ++item.index; return r; }
inline iterator &operator--() { --item.index; return *this; }
inline iterator operator--(int) { iterator r = *this; --item.index; return r; }
inline iterator operator+(int j) const inline iterator operator+(int j) const
{ iterator r = *this; r.i += j; return r; } { iterator r = *this; r.item.index += j; return r; }
inline iterator operator-(int j) const { return operator+(-j); } inline iterator operator-(int j) const { return operator+(-j); }
inline iterator &operator+=(int j) { i += j; return *this; } inline iterator &operator+=(int j) { item.index += j; return *this; }
inline iterator &operator-=(int j) { i -= j; return *this; } inline iterator &operator-=(int j) { item.index -= j; return *this; }
int operator-(iterator j) const { return i - j.i; } int operator-(iterator j) const { return item.index - j.item.index; }
public: public:
inline bool operator==(const const_iterator &other) const { return i == other.i; } inline bool operator==(const const_iterator &other) const
inline bool operator!=(const const_iterator &other) const { return i != other.i; } { return item.o == other.item.o && item.index == other.item.index; }
bool operator<(const const_iterator& other) const { return i < other.i; } inline bool operator!=(const const_iterator &other) const { return !(*this == other); }
bool operator<=(const const_iterator& other) const { return i <= other.i; } bool operator<(const const_iterator& other) const
bool operator>(const const_iterator& other) const { return i > other.i; } { Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
bool operator>=(const const_iterator& other) const { return i >= other.i; } bool operator<=(const const_iterator& other) const
{ Q_ASSERT(item.o == other.item.o); return item.index <= other.item.index; }
bool operator>(const const_iterator& other) const { return !(*this <= other); }
bool operator>=(const const_iterator& other) const { return !(*this < other); }
}; };
friend class iterator; friend class iterator;
class const_iterator class const_iterator
{ {
friend class iterator; friend class iterator;
const QJsonObject *o; QJsonValueRef item;
int i;
public: public:
typedef std::random_access_iterator_tag iterator_category; typedef std::random_access_iterator_tag iterator_category;
typedef int difference_type; typedef int difference_type;
typedef QJsonValue value_type; typedef QJsonValue value_type;
typedef QJsonValue reference; typedef const QJsonValueRef reference;
typedef QJsonValuePtr pointer; typedef const QJsonValueRef *pointer;
Q_DECL_CONSTEXPR inline const_iterator() : o(nullptr), i(0) {} inline const_iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
Q_DECL_CONSTEXPR inline const_iterator(const QJsonObject *obj, int index) inline const_iterator(const QJsonObject *obj, int index)
: o(obj), i(index) {} : item(const_cast<QJsonObject*>(obj), index) { }
inline const_iterator(const iterator &other) inline const_iterator(const iterator &other)
: o(other.o), i(other.i) {} : item(other.item) { }
inline QString key() const { return o->keyAt(i); } constexpr const_iterator(const const_iterator &other) = default;
inline QJsonValue value() const { return o->valueAt(i); } const_iterator &operator=(const const_iterator &other)
inline QJsonValue operator*() const { return o->valueAt(i); } {
#ifdef Q_QDOC item.o = other.item.o;
inline QJsonValue* operator->() const; item.index = other.item.index;
#else return *this;
inline QJsonValuePtr operator->() const { return QJsonValuePtr(o->valueAt(i)); } }
#endif
const QJsonValue operator[](int j) { return o->valueAt(i + j); }
inline bool operator==(const const_iterator &other) const { return i == other.i; } inline QString key() const { return item.o->keyAt(item.index); }
inline bool operator!=(const const_iterator &other) const { return i != other.i; } inline QJsonValueRef value() const { return item; }
bool operator<(const const_iterator& other) const { return i < other.i; } inline const QJsonValueRef operator*() const { return item; }
bool operator<=(const const_iterator& other) const { return i <= other.i; } inline const QJsonValueRef *operator->() const { return &item; }
bool operator>(const const_iterator& other) const { return i > other.i; } const QJsonValueRef operator[](int j) { return { item.o, int(item.index) + j }; }
bool operator>=(const const_iterator& other) const { return i >= other.i; }
inline const_iterator &operator++() { ++i; return *this; } inline bool operator==(const const_iterator &other) const
inline const_iterator operator++(int) { const_iterator r = *this; ++i; return r; } { return item.o == other.item.o && item.index == other.item.index; }
inline const_iterator &operator--() { --i; return *this; } inline bool operator!=(const const_iterator &other) const { return !(*this == other); }
inline const_iterator operator--(int) { const_iterator r = *this; --i; return r; } bool operator<(const const_iterator& other) const
{ Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
bool operator<=(const const_iterator& other) const
{ Q_ASSERT(item.o == other.item.o); return item.index <= other.item.index; }
bool operator>(const const_iterator& other) const { return !(*this <= other); }
bool operator>=(const const_iterator& other) const { return !(*this < other); }
inline const_iterator &operator++() { ++item.index; return *this; }
inline const_iterator operator++(int) { const_iterator r = *this; ++item.index; return r; }
inline const_iterator &operator--() { --item.index; return *this; }
inline const_iterator operator--(int) { const_iterator r = *this; --item.index; return r; }
inline const_iterator operator+(int j) const inline const_iterator operator+(int j) const
{ const_iterator r = *this; r.i += j; return r; } { const_iterator r = *this; r.item.index += j; return r; }
inline const_iterator operator-(int j) const { return operator+(-j); } inline const_iterator operator-(int j) const { return operator+(-j); }
inline const_iterator &operator+=(int j) { i += j; return *this; } inline const_iterator &operator+=(int j) { item.index += j; return *this; }
inline const_iterator &operator-=(int j) { i -= j; return *this; } inline const_iterator &operator-=(int j) { item.index -= j; return *this; }
int operator-(const_iterator j) const { return i - j.i; } int operator-(const_iterator j) const { return item.index - j.item.index; }
inline bool operator==(const iterator &other) const { return i == other.i; } inline bool operator==(const iterator &other) const
inline bool operator!=(const iterator &other) const { return i != other.i; } { return item.o == other.item.o && item.index == other.item.index; }
bool operator<(const iterator& other) const { return i < other.i; } inline bool operator!=(const iterator &other) const { return !(*this == other); }
bool operator<=(const iterator& other) const { return i <= other.i; } bool operator<(const iterator& other) const
bool operator>(const iterator& other) const { return i > other.i; } { Q_ASSERT(item.o == other.item.o); return item.index < other.item.index; }
bool operator>=(const iterator& other) const { return i >= other.i; } bool operator<=(const iterator& other) const
{ Q_ASSERT(item.o == other.item.o); return item.index <= other.item.index; }
bool operator>(const iterator& other) const { return !(*this <= other); }
bool operator>=(const iterator& other) const { return !(*this < other); }
}; };
friend class const_iterator; friend class const_iterator;

View File

@ -164,6 +164,8 @@ public:
QJsonValueRef(QJsonObject *object, int idx) QJsonValueRef(QJsonObject *object, int idx)
: o(object), is_object(true), index(static_cast<uint>(idx)) {} : o(object), is_object(true), index(static_cast<uint>(idx)) {}
QJsonValueRef(const QJsonValueRef &) = default;
inline operator QJsonValue() const { return toValue(); } inline operator QJsonValue() const { return toValue(); }
QJsonValueRef &operator = (const QJsonValue &val); QJsonValueRef &operator = (const QJsonValue &val);
QJsonValueRef &operator = (const QJsonValueRef &val); QJsonValueRef &operator = (const QJsonValueRef &val);
@ -203,31 +205,9 @@ private:
}; };
uint is_object : 1; uint is_object : 1;
uint index : 31; uint index : 31;
};
// ### Qt 6: Get rid of these fake pointer classes friend class QJsonArray;
class QJsonValuePtr friend class QJsonObject;
{
QJsonValue value;
public:
explicit QJsonValuePtr(const QJsonValue& val)
: value(val) {}
QJsonValue& operator*() { return value; }
QJsonValue* operator->() { return &value; }
};
class QJsonValueRefPtr
{
QJsonValueRef valueRef;
public:
QJsonValueRefPtr(QJsonArray *array, int idx)
: valueRef(array, idx) {}
QJsonValueRefPtr(QJsonObject *object, int idx)
: valueRef(object, idx) {}
QJsonValueRef& operator*() { return valueRef; }
QJsonValueRef* operator->() { return &valueRef; }
}; };
Q_DECLARE_SHARED(QJsonValue) Q_DECLARE_SHARED(QJsonValue)