From 7dce96220003e3fa3f932341aaecd8e7f55f4d95 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Fri, 15 Aug 2014 15:41:44 +0200 Subject: Add operator-> to QJson iterators The iterators for QJsonArray and QJsonObject are currently lacking an operator-> definition. Unfortunately it is not possible to do in clean way without redefining either the iterators or QJsonValueRef class. This patch instead adds two fake pointer classes that are only used to handle the operator-> return value. Task-number: QTBUG-29573 Change-Id: Ief785a6afbbedc9e89cf3b6f3958c2c755997a66 Reviewed-by: Lars Knoll --- src/corelib/json/qjsonarray.cpp | 10 ++++++++++ src/corelib/json/qjsonarray.h | 14 ++++++++++---- src/corelib/json/qjsonobject.cpp | 10 ++++++++++ src/corelib/json/qjsonobject.h | 13 ++++++++++--- src/corelib/json/qjsonvalue.h | 27 +++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/corelib/json/qjsonarray.cpp b/src/corelib/json/qjsonarray.cpp index c1e831192a..6f3a960f05 100644 --- a/src/corelib/json/qjsonarray.cpp +++ b/src/corelib/json/qjsonarray.cpp @@ -746,6 +746,11 @@ bool QJsonArray::operator!=(const QJsonArray &other) const from which you got the reference. */ +/*! \fn QJsonValueRef *QJsonArray::iterator::operator->() const + + Returns a pointer to a modifiable reference to the current item. +*/ + /*! \fn QJsonValueRef QJsonArray::iterator::operator[](int j) const Returns a modifiable reference to the item at offset \a j from the @@ -971,6 +976,11 @@ bool QJsonArray::operator!=(const QJsonArray &other) const Returns the current item. */ +/*! \fn QJsonValue *QJsonArray::const_iterator::operator->() const + + Returns a pointer to the current item. +*/ + /*! \fn QJsonValue QJsonArray::const_iterator::operator[](int j) const Returns the item at offset \a j from the item pointed to by this iterator (the item at diff --git a/src/corelib/json/qjsonarray.h b/src/corelib/json/qjsonarray.h index 4cada7cec1..f3efa3d201 100644 --- a/src/corelib/json/qjsonarray.h +++ b/src/corelib/json/qjsonarray.h @@ -112,14 +112,17 @@ public: 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; } +#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); } inline bool operator==(const iterator &o) const { return i == o.i; } @@ -153,7 +156,6 @@ public: 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) { } @@ -162,7 +164,11 @@ public: inline const_iterator(const iterator &o) : a(o.a), i(o.i) {} inline QJsonValue operator*() const { return a->at(i); } - //inline T *operator->() const { return &concrete(i)->value; } +#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; } diff --git a/src/corelib/json/qjsonobject.cpp b/src/corelib/json/qjsonobject.cpp index cfd797990f..b393701411 100644 --- a/src/corelib/json/qjsonobject.cpp +++ b/src/corelib/json/qjsonobject.cpp @@ -710,6 +710,11 @@ QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const \sa key() */ +/*! \fn QJsonValueRef *QJsonObject::iterator::operator->() const + + Returns a pointer to a modifiable reference to the current item. +*/ + /*! \fn bool QJsonObject::iterator::operator==(const iterator &other) const \fn bool QJsonObject::iterator::operator==(const const_iterator &other) const @@ -893,6 +898,11 @@ QJsonObject::const_iterator QJsonObject::constFind(const QString &key) const \sa key() */ +/*! \fn QJsonValue *QJsonObject::const_iterator::operator->() const + + Returns a pointer to the current item. +*/ + /*! \fn bool QJsonObject::const_iterator::operator==(const const_iterator &other) const \fn bool QJsonObject::const_iterator::operator==(const iterator &other) const diff --git a/src/corelib/json/qjsonobject.h b/src/corelib/json/qjsonobject.h index 92dd19af5e..7973b8ab92 100644 --- a/src/corelib/json/qjsonobject.h +++ b/src/corelib/json/qjsonobject.h @@ -107,7 +107,6 @@ public: typedef std::bidirectional_iterator_tag iterator_category; typedef int difference_type; typedef QJsonValue value_type; -// typedef T *pointer; typedef QJsonValueRef reference; Q_DECL_CONSTEXPR inline iterator() : o(0), i(0) {} @@ -116,7 +115,11 @@ public: inline QString key() const { return o->keyAt(i); } inline QJsonValueRef value() const { return QJsonValueRef(o, i); } inline QJsonValueRef operator*() const { return QJsonValueRef(o, i); } - //inline T *operator->() const { return &concrete(i)->value; } +#ifdef Q_QDOC + inline QJsonValueRef* operator->() const; +#else + inline QJsonValueRefPtr operator->() const { return QJsonValueRefPtr(o, i); } +#endif inline bool operator==(const iterator &other) const { return i == other.i; } inline bool operator!=(const iterator &other) const { return i != other.i; } @@ -157,7 +160,11 @@ public: inline QString key() const { return o->keyAt(i); } inline QJsonValue value() const { return o->valueAt(i); } inline QJsonValue operator*() const { return o->valueAt(i); } - //inline const T *operator->() const { return &concrete(i)->value; } +#ifdef Q_QDOC + inline QJsonValue* operator->() const; +#else + inline QJsonValuePtr operator->() const { return QJsonValuePtr(o->valueAt(i)); } +#endif inline bool operator==(const const_iterator &other) const { return i == other.i; } inline bool operator!=(const const_iterator &other) const { return i != other.i; } diff --git a/src/corelib/json/qjsonvalue.h b/src/corelib/json/qjsonvalue.h index a00bc0b72f..2d0453f130 100644 --- a/src/corelib/json/qjsonvalue.h +++ b/src/corelib/json/qjsonvalue.h @@ -192,6 +192,33 @@ private: struct UnionHelper; }; +#ifndef Q_QDOC +// ### 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; } +}; +#endif + #if !defined(QT_NO_DEBUG_STREAM) && !defined(QT_JSON_READONLY) Q_CORE_EXPORT QDebug operator<<(QDebug, const QJsonValue &); #endif -- cgit v1.2.3