QJsonObject: use QJsonValueConstRef

Change-Id: I5e52dc5b093c43a3b678fffd16b608f32f8eb4b4
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
bb10
Thiago Macieira 2021-11-09 16:49:15 -08:00
parent f5762cd4b3
commit 800f60657d
3 changed files with 26 additions and 77 deletions

View File

@ -1003,11 +1003,16 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const
\sa key()
*/
/*! \fn QJsonValueRef *QJsonObject::iterator::operator->() const
/*! \fn QJsonValueRef *QJsonObject::iterator::operator->()
Returns a pointer to a modifiable reference to the current item.
*/
/*! \fn const QJsonValueConstRef *QJsonObject::iterator::operator->() const
Returns a pointer to a constant reference to the current item.
*/
/*! \fn const QJsonValueRef QJsonObject::iterator::operator[](qsizetype j)
Returns a modifiable reference to the item at offset \a j from the
@ -1238,14 +1243,14 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const
\sa value()
*/
/*! \fn QJsonValueRef QJsonObject::const_iterator::value() const
/*! \fn QJsonValueConstRef QJsonObject::const_iterator::value() const
Returns the current item's value.
\sa key(), operator*()
*/
/*! \fn const QJsonValueRef QJsonObject::const_iterator::operator*() const
/*! \fn const QJsonValueConstRef QJsonObject::const_iterator::operator*() const
Returns the current item's value.
@ -1254,12 +1259,12 @@ QJsonObject::const_iterator QJsonObject::constFindImpl(T key) const
\sa key()
*/
/*! \fn const QJsonValueRef *QJsonObject::const_iterator::operator->() const
/*! \fn const QJsonValueConstRef *QJsonObject::const_iterator::operator->() const
Returns a pointer to the current item.
*/
/*! \fn const QJsonValue QJsonObject::const_iterator::operator[](qsizetype j)
/*! \fn const QJsonValueConstRef QJsonObject::const_iterator::operator[](qsizetype j)
Returns the item at offset \a j from the item pointed to by this iterator (the item at
position \c{*this + j}).

View File

@ -121,7 +121,7 @@ public:
{
friend class const_iterator;
friend class QJsonObject;
mutable QJsonValueRef item;
QJsonValueRef item;
public:
typedef std::random_access_iterator_tag iterator_category;
@ -144,7 +144,8 @@ public:
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; }
inline const QJsonValueConstRef *operator->() const { return &item; }
inline QJsonValueRef *operator->() { return &item; }
const QJsonValueRef operator[](qsizetype j) { return { item.o, qsizetype(item.index) + j }; }
inline bool operator==(const iterator &other) const
@ -184,14 +185,14 @@ public:
class const_iterator
{
friend class iterator;
QJsonValueRef item;
QJsonValueConstRef item;
public:
typedef std::random_access_iterator_tag iterator_category;
typedef qsizetype difference_type;
typedef QJsonValue value_type;
typedef const QJsonValueRef reference;
typedef const QJsonValueRef *pointer;
typedef const QJsonValueConstRef reference;
typedef const QJsonValueConstRef *pointer;
inline const_iterator() : item(static_cast<QJsonObject*>(nullptr), 0) { }
inline const_iterator(const QJsonObject *obj, qsizetype index)
@ -208,10 +209,10 @@ public:
}
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[](qsizetype j) { return { item.o, qsizetype(item.index) + j }; }
inline QJsonValueConstRef value() const { return item; }
inline const QJsonValueConstRef operator*() const { return item; }
inline const QJsonValueConstRef *operator->() const { return &item; }
const QJsonValueConstRef operator[](qsizetype j) { return { item.o, qsizetype(item.index) + j }; }
inline bool operator==(const const_iterator &other) const
{ return item.o == other.item.o && item.index == other.item.index; }

View File

@ -205,65 +205,6 @@ QJsonObject tst_QHighDpi::offscreenConfiguration()
return getConfiguration(platformNativeInterface);
}
// JsonValueRef implements support for mutating nested JSON structures, e.g.
//
// JsonValueRef::get(&config)["screens"][0]["logicalDpi"] = 192
//
class JsonValueRef {
public:
static JsonValueRef get(QJsonValue *value) {
return JsonValueRef(value);
}
JsonValueRef(QJsonValue *value)
: m_value(value) { }
JsonValueRef(QJsonValue *value, JsonValueRef *parent, QString key)
: m_value(value), m_parent(parent), m_key(key) { }
JsonValueRef(QJsonValue *value, JsonValueRef *parent, int index)
: m_value(value), m_parent(parent), m_index(index) { }
~JsonValueRef() {
if (m_parent) {
if (!m_key.isNull()) {
QJsonObject parentObject = m_parent->m_value->toObject();
parentObject[m_key] = *m_value;
*m_parent->m_value = parentObject;
} else if (m_index > -1) {
QJsonArray parentArray = m_parent->m_value->toArray();
parentArray[m_index] = *m_value;
*m_parent->m_value = parentArray;
}
delete m_value; // owned if we have a parent, see operator[]
}
}
JsonValueRef operator[](const char *str) {
QString key = QString::fromUtf8(str);
return JsonValueRef(new QJsonValue((*m_value)[key]), this, key);
}
JsonValueRef operator[](int index) {
return JsonValueRef(new QJsonValue((*m_value)[index]), this, index);
}
void operator=(int value) {
*m_value = QJsonValue(value);
}
void operator=(const char *str) {
*m_value = QJsonValue(QString(str));
}
private:
Q_DISABLE_COPY(JsonValueRef);
QJsonValue *m_value = nullptr;
JsonValueRef *m_parent = nullptr;
QString m_key;
int m_index = -1;
};
void tst_QHighDpi::cleanup()
{
// Some test functions set environment variables. Unset them here,
@ -329,10 +270,12 @@ void tst_QHighDpi::screenDpiChange()
// Set new DPI
int newDpi = 192;
QJsonValue config = offscreenConfiguration();
JsonValueRef::get(&config)["screens"][0]["logicalDpi"] = newDpi;
JsonValueRef::get(&config)["screens"][1]["logicalDpi"] = newDpi;
JsonValueRef::get(&config)["screens"][2]["logicalDpi"] = newDpi;
setOffscreenConfiguration(config.toObject());
// API defect until Qt 7, so go indirectly via CBOR
QCborMap map = QCborMap::fromJsonObject(config.toObject());
map[QLatin1String("screens")][0][QLatin1String("logicalDpi")] = newDpi;
map[QLatin1String("screens")][1][QLatin1String("logicalDpi")] = newDpi;
map[QLatin1String("screens")][2][QLatin1String("logicalDpi")] = newDpi;
setOffscreenConfiguration(map.toJsonObject());
// TODO check events