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.
*/
/*! \fn QJsonValue QJsonArray::const_iterator::operator*() const
/*! \fn const QJsonValueRef QJsonArray::const_iterator::operator*() const
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.
*/

View File

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

View File

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

View File

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

View File

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