From bc099f33ddc03af48fcef226a6c95e7dd79a64d4 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Thu, 4 Oct 2018 18:55:29 +0200 Subject: Support QCborMap::operator[] taking a string literal Use a template on the size of the char[], as suggested by Ville Voutilainen. This resolves ambiguity about whether such look-ups should be done via QString or QCborValue (not that it would have made any difference). When we come to add mutating indexing of QCborValue, chained dereferences like map[i][j][k] need to stay in operator[] const throughout, to avoid detaching intermediates to create references into them due to using the mutating operator[] on the earlier dereference's return. So const-qualify the QCborValue operator[] const variants at the same time, to match those of QCborValue itself. Change-Id: Ib1652ae9440fe3767a653afa2856b74040210e07 Reviewed-by: Thiago Macieira --- src/corelib/serialization/qcborarray.h | 2 +- src/corelib/serialization/qcbormap.h | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'src/corelib/serialization') diff --git a/src/corelib/serialization/qcborarray.h b/src/corelib/serialization/qcborarray.h index ae6b8c09d7..ed0f4912ba 100644 --- a/src/corelib/serialization/qcborarray.h +++ b/src/corelib/serialization/qcborarray.h @@ -185,7 +185,7 @@ public: QCborValue at(qsizetype i) const; QCborValue first() const { return at(0); } QCborValue last() const { return at(size() - 1); } - QCborValue operator[](qsizetype i) const { return at(i); } + const QCborValue operator[](qsizetype i) const { return at(i); } QCborValueRef first() { Q_ASSERT(!isEmpty()); return begin()[0]; } QCborValueRef last() { Q_ASSERT(!isEmpty()); return begin()[size() - 1]; } QCborValueRef operator[](qsizetype i) diff --git a/src/corelib/serialization/qcbormap.h b/src/corelib/serialization/qcbormap.h index 45ef430e40..3eb2107691 100644 --- a/src/corelib/serialization/qcbormap.h +++ b/src/corelib/serialization/qcbormap.h @@ -195,14 +195,22 @@ public: { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } QCborValue value(const QCborValue &key) const { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } - QCborValue operator[](qint64 key) const +#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + template QT_ASCII_CAST_WARN const QCborValue value(const char (&key)[N]) const + { return value(QString::fromUtf8(key, N - 1)); } +#endif + const QCborValue operator[](qint64 key) const { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } - QCborValue operator[](QLatin1String key) const + const QCborValue operator[](QLatin1String key) const { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } - QCborValue operator[](const QString & key) const + const QCborValue operator[](const QString & key) const { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } - QCborValue operator[](const QCborValue &key) const + const QCborValue operator[](const QCborValue &key) const { const_iterator it = find(key); return it == end() ? QCborValue() : it.value(); } +#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII) + template QT_ASCII_CAST_WARN const QCborValue operator[](const char (&key)[N]) const + { return operator[](QString::fromUtf8(key, N - 1)); } +#endif QCborValueRef operator[](qint64 key); QCborValueRef operator[](QLatin1String key); QCborValueRef operator[](const QString & key); -- cgit v1.2.3