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 <thiago.macieira@intel.com>
bb10
Edward Welbourne 2018-10-04 18:55:29 +02:00
parent ff7f09d187
commit bc099f33dd
3 changed files with 16 additions and 5 deletions

View File

@ -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)

View File

@ -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<size_t N> 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<size_t N> 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);

View File

@ -413,6 +413,9 @@ void tst_QCborValue::mapDefaultInitialization()
QVERIFY(m.value(QLatin1String("Hello")).isUndefined());
QVERIFY(m.value(QStringLiteral("Hello")).isUndefined());
QVERIFY(m.value(QCborValue()).isUndefined());
#if !defined(QT_NO_CAST_FROM_ASCII) && !defined(QT_RESTRICTED_CAST_FROM_ASCII)
QVERIFY(m.value("Hello").isUndefined());
#endif
QVERIFY(m == m);
QVERIFY(m == QCborMap{});