Added initializer list constructors for Qt associative containers.
Affected: QSet, QMap, QMultiMap, QHash, QMultiHash. Task-number: QTBUG-25679 Change-Id: I01f3ecfbca805f4c053a75232188bd2a77fdb1f2 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Jędrzej Nowacki <jedrzej.nowacki@digia.com>
This commit is contained in:
parent
fc663b5f9a
commit
45be71bf7d
@ -924,6 +924,16 @@ void QHashData::checkSanity()
|
||||
\sa clear()
|
||||
*/
|
||||
|
||||
/*! \fn QHash::QHash(std::initializer_list<std::pair<Key,T> > list)
|
||||
\since 5.1
|
||||
|
||||
Constructs a hash with a copy of each of the elements in the
|
||||
initializer list \a list.
|
||||
|
||||
This function is only available if the program is being
|
||||
compiled in C++11 mode.
|
||||
*/
|
||||
|
||||
/*! \fn QHash::QHash(const QHash<Key, T> &other)
|
||||
|
||||
Constructs a copy of \a other.
|
||||
@ -1981,6 +1991,16 @@ void QHashData::checkSanity()
|
||||
Constructs an empty hash.
|
||||
*/
|
||||
|
||||
/*! \fn QMultiHash::QMultiHash(std::initializer_list<std::pair<Key,T> > list)
|
||||
\since 5.1
|
||||
|
||||
Constructs a multi hash with a copy of each of the elements in the
|
||||
initializer list \a list.
|
||||
|
||||
This function is only available if the program is being
|
||||
compiled in C++11 mode.
|
||||
*/
|
||||
|
||||
/*! \fn QMultiHash::QMultiHash(const QHash<Key, T> &other)
|
||||
|
||||
Constructs a copy of \a other (which can be a QHash or a
|
||||
|
@ -48,6 +48,10 @@
|
||||
#include <QtCore/qpair.h>
|
||||
#include <QtCore/qrefcount.h>
|
||||
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
#include <initializer_list>
|
||||
#endif
|
||||
|
||||
QT_BEGIN_HEADER
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
@ -285,6 +289,15 @@ class QHash
|
||||
|
||||
public:
|
||||
inline QHash() : d(const_cast<QHashData *>(&QHashData::shared_null)) { }
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
inline QHash(std::initializer_list<std::pair<Key,T> > list)
|
||||
: d(const_cast<QHashData *>(&QHashData::shared_null))
|
||||
{
|
||||
reserve(list.size());
|
||||
for (typename std::initializer_list<std::pair<Key,T> >::const_iterator it = list.begin(); it != list.end(); ++it)
|
||||
insert(it->first, it->second);
|
||||
}
|
||||
#endif
|
||||
inline QHash(const QHash<Key, T> &other) : d(other.d) { d->ref.ref(); if (!d->sharable) detach(); }
|
||||
inline ~QHash() { if (!d->ref.deref()) freeData(d); }
|
||||
|
||||
@ -921,6 +934,14 @@ class QMultiHash : public QHash<Key, T>
|
||||
{
|
||||
public:
|
||||
QMultiHash() {}
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
inline QMultiHash(std::initializer_list<std::pair<Key,T> > list)
|
||||
{
|
||||
this->reserve(list.size());
|
||||
for (typename std::initializer_list<std::pair<Key,T> >::const_iterator it = list.begin(); it != list.end(); ++it)
|
||||
insert(it->first, it->second);
|
||||
}
|
||||
#endif
|
||||
QMultiHash(const QHash<Key, T> &other) : QHash<Key, T>(other) {}
|
||||
inline void swap(QMultiHash<Key, T> &other) { QHash<Key, T>::swap(other); } // prevent QMultiHash<->QHash swaps
|
||||
|
||||
|
@ -538,6 +538,16 @@ void QMapDataBase::freeData(QMapDataBase *d)
|
||||
\sa toStdMap()
|
||||
*/
|
||||
|
||||
/*! \fn QMap::QMap(std::initializer_list<std::pair<Key,T> > list)
|
||||
\since 5.1
|
||||
|
||||
Constructs a map with a copy of each of the elements in the
|
||||
initializer list \a list.
|
||||
|
||||
This function is only available if the program is being
|
||||
compiled in C++11 mode.
|
||||
*/
|
||||
|
||||
/*! \fn std::map<Key, T> QMap::toStdMap() const
|
||||
|
||||
Returns an STL map equivalent to this QMap.
|
||||
@ -1576,6 +1586,16 @@ void QMapDataBase::freeData(QMapDataBase *d)
|
||||
Constructs an empty map.
|
||||
*/
|
||||
|
||||
/*! \fn QMultiMap::QMultiMap(std::initializer_list<std::pair<Key,T> > list)
|
||||
\since 5.1
|
||||
|
||||
Constructs a multi map with a copy of each of the elements in the
|
||||
initializer list \a list.
|
||||
|
||||
This function is only available if the program is being
|
||||
compiled in C++11 mode.
|
||||
*/
|
||||
|
||||
/*! \fn QMultiMap::QMultiMap(const QMap<Key, T> &other)
|
||||
|
||||
Constructs a copy of \a other (which can be a QMap or a
|
||||
|
@ -54,6 +54,10 @@
|
||||
#include <map>
|
||||
#include <new>
|
||||
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
#include <initializer_list>
|
||||
#endif
|
||||
|
||||
QT_BEGIN_HEADER
|
||||
|
||||
QT_BEGIN_NAMESPACE
|
||||
@ -327,6 +331,14 @@ class QMap
|
||||
|
||||
public:
|
||||
inline QMap() : d(static_cast<QMapData<Key, T> *>(const_cast<QMapDataBase *>(&QMapDataBase::shared_null))) { }
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
inline QMap(std::initializer_list<std::pair<Key,T> > list)
|
||||
: d(static_cast<QMapData<Key, T> *>(const_cast<QMapDataBase *>(&QMapDataBase::shared_null)))
|
||||
{
|
||||
for (typename std::initializer_list<std::pair<Key,T> >::const_iterator it = list.begin(); it != list.end(); ++it)
|
||||
insert(it->first, it->second);
|
||||
}
|
||||
#endif
|
||||
QMap(const QMap<Key, T> &other);
|
||||
|
||||
inline ~QMap() { if (!d->ref.deref()) d->destroy(); }
|
||||
@ -960,6 +972,13 @@ class QMultiMap : public QMap<Key, T>
|
||||
{
|
||||
public:
|
||||
QMultiMap() {}
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
inline QMultiMap(std::initializer_list<std::pair<Key,T> > list)
|
||||
{
|
||||
for (typename std::initializer_list<std::pair<Key,T> >::const_iterator it = list.begin(); it != list.end(); ++it)
|
||||
insert(it->first, it->second);
|
||||
}
|
||||
#endif
|
||||
QMultiMap(const QMap<Key, T> &other) : QMap<Key, T>(other) {}
|
||||
inline void swap(QMultiMap<Key, T> &other) { QMap<Key, T>::swap(other); }
|
||||
|
||||
|
@ -43,6 +43,9 @@
|
||||
#define QSET_H
|
||||
|
||||
#include <QtCore/qhash.h>
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
#include <initializer_list>
|
||||
#endif
|
||||
|
||||
QT_BEGIN_HEADER
|
||||
|
||||
@ -56,6 +59,14 @@ class QSet
|
||||
|
||||
public:
|
||||
inline QSet() {}
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
inline QSet(std::initializer_list<T> list)
|
||||
{
|
||||
reserve(list.size());
|
||||
for (typename std::initializer_list<T>::const_iterator it = list.begin(); it != list.end(); ++it)
|
||||
insert(*it);
|
||||
}
|
||||
#endif
|
||||
inline QSet(const QSet<T> &other) : q_hash(other.q_hash) {}
|
||||
|
||||
inline QSet<T> &operator=(const QSet<T> &other)
|
||||
|
@ -103,6 +103,16 @@
|
||||
\sa clear()
|
||||
*/
|
||||
|
||||
/*! \fn QSet::QSet(std::initializer_list<T> list)
|
||||
\since 5.1
|
||||
|
||||
Constructs a set with a copy of each of the elements in the
|
||||
initializer list \a list.
|
||||
|
||||
This function is only available if the program is being
|
||||
compiled in C++11 mode.
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn QSet::QSet(const QSet<T> &other)
|
||||
|
||||
|
@ -74,6 +74,7 @@ private slots:
|
||||
|
||||
void const_shared_null();
|
||||
void twoArguments_qHash();
|
||||
void initializerList();
|
||||
};
|
||||
|
||||
struct Foo {
|
||||
@ -1300,5 +1301,34 @@ void tst_QHash::twoArguments_qHash()
|
||||
QCOMPARE(wrongqHashOverload, 0);
|
||||
}
|
||||
|
||||
void tst_QHash::initializerList()
|
||||
{
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
QHash<int, QString> hash{{1, "hello"}, {2, "initializer_list"}};
|
||||
QCOMPARE(hash.count(), 2);
|
||||
QVERIFY(hash[1] == "hello");
|
||||
QVERIFY(hash[2] == "initializer_list");
|
||||
|
||||
QMultiHash<QString, int> multiHash{{"il", 1}, {"il", 2}, {"il", 3}};
|
||||
QCOMPARE(multiHash.count(), 3);
|
||||
QList<int> values = multiHash.values("il");
|
||||
QCOMPARE(values.count(), 3);
|
||||
|
||||
QHash<int, int> emptyHash{};
|
||||
QVERIFY(emptyHash.isEmpty());
|
||||
|
||||
QHash<int, char> emptyPairs{{}, {}};
|
||||
QVERIFY(!emptyPairs.isEmpty());
|
||||
|
||||
QMultiHash<QString, double> emptyMultiHash{};
|
||||
QVERIFY(emptyMultiHash.isEmpty());
|
||||
|
||||
QMultiHash<int, float> emptyPairs2{{}, {}};
|
||||
QVERIFY(!emptyPairs2.isEmpty());
|
||||
#else
|
||||
QSKIP("Compiler doesn't support initializer lists");
|
||||
#endif
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(tst_QHash)
|
||||
#include "tst_qhash.moc"
|
||||
|
@ -84,6 +84,7 @@ private slots:
|
||||
|
||||
void insert();
|
||||
void checkMostLeftNode();
|
||||
void initializerList();
|
||||
};
|
||||
|
||||
typedef QMap<QString, QString> StringMap;
|
||||
@ -1129,5 +1130,34 @@ void tst_QMap::checkMostLeftNode()
|
||||
sanityCheckTree(map, __LINE__);
|
||||
}
|
||||
|
||||
void tst_QMap::initializerList()
|
||||
{
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
QMap<int, QString> map{{1, "hello"}, {2, "initializer_list"}};
|
||||
QCOMPARE(map.count(), 2);
|
||||
QVERIFY(map[1] == "hello");
|
||||
QVERIFY(map[2] == "initializer_list");
|
||||
|
||||
QMultiMap<QString, int> multiMap{{"il", 1}, {"il", 2}, {"il", 3}};
|
||||
QCOMPARE(multiMap.count(), 3);
|
||||
QList<int> values = multiMap.values("il");
|
||||
QCOMPARE(values.count(), 3);
|
||||
|
||||
QMap<int, int> emptyMap{};
|
||||
QVERIFY(emptyMap.isEmpty());
|
||||
|
||||
QMap<char, char> emptyPairs{{}, {}};
|
||||
QVERIFY(!emptyPairs.isEmpty());
|
||||
|
||||
QMultiMap<double, double> emptyMultiMap{};
|
||||
QVERIFY(emptyMultiMap.isEmpty());
|
||||
|
||||
QMultiMap<float, float> emptyPairs2{{}, {}};
|
||||
QVERIFY(!emptyPairs2.isEmpty());
|
||||
#else
|
||||
QSKIP("Compiler doesn't support initializer lists");
|
||||
#endif
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(tst_QMap)
|
||||
#include "tst_qmap.moc"
|
||||
|
@ -79,6 +79,7 @@ private slots:
|
||||
void javaIterator();
|
||||
void javaMutableIterator();
|
||||
void makeSureTheComfortFunctionsCompile();
|
||||
void initializerList();
|
||||
};
|
||||
|
||||
void tst_QSet::operator_eq()
|
||||
@ -918,6 +919,27 @@ void tst_QSet::makeSureTheComfortFunctionsCompile()
|
||||
set1 = set2 - set3;
|
||||
}
|
||||
|
||||
void tst_QSet::initializerList()
|
||||
{
|
||||
#ifdef Q_COMPILER_INITIALIZER_LISTS
|
||||
QSet<int> set{1, 2, 3, 4, 5};
|
||||
QCOMPARE(set.count(), 5);
|
||||
QVERIFY(set.contains(1));
|
||||
QVERIFY(set.contains(2));
|
||||
QVERIFY(set.contains(3));
|
||||
QVERIFY(set.contains(4));
|
||||
QVERIFY(set.contains(5));
|
||||
|
||||
QSet<int> emptySet{};
|
||||
QVERIFY(emptySet.isEmpty());
|
||||
|
||||
QSet<int> set3{{}, {}, {}};
|
||||
QVERIFY(!set3.isEmpty());
|
||||
#else
|
||||
QSKIP("Compiler doesn't support initializer lists");
|
||||
#endif
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(tst_QSet)
|
||||
|
||||
#include "tst_qset.moc"
|
||||
|
Loading…
Reference in New Issue
Block a user