summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoman Pasechnik <roman@pasechnik.net>2013-01-10 14:45:18 +0200
committerThe Qt Project <gerrit-noreply@qt-project.org>2013-01-24 11:38:54 +0100
commit45be71bf7d7c855e74b84d2fabb4e626afc57a22 (patch)
treeadfa3c7881d90835b089af251a7e5240e568d4ea
parentfc663b5f9aae16fe6a03160e3eb148a5f742ac58 (diff)
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>
-rw-r--r--src/corelib/tools/qhash.cpp20
-rw-r--r--src/corelib/tools/qhash.h21
-rw-r--r--src/corelib/tools/qmap.cpp20
-rw-r--r--src/corelib/tools/qmap.h19
-rw-r--r--src/corelib/tools/qset.h11
-rw-r--r--src/corelib/tools/qset.qdoc10
-rw-r--r--tests/auto/corelib/tools/qhash/tst_qhash.cpp30
-rw-r--r--tests/auto/corelib/tools/qmap/tst_qmap.cpp30
-rw-r--r--tests/auto/corelib/tools/qset/tst_qset.cpp22
9 files changed, 183 insertions, 0 deletions
diff --git a/src/corelib/tools/qhash.cpp b/src/corelib/tools/qhash.cpp
index 0103a208f3..b8cd076cb6 100644
--- a/src/corelib/tools/qhash.cpp
+++ b/src/corelib/tools/qhash.cpp
@@ -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
diff --git a/src/corelib/tools/qhash.h b/src/corelib/tools/qhash.h
index 98965b9121..69a8afe195 100644
--- a/src/corelib/tools/qhash.h
+++ b/src/corelib/tools/qhash.h
@@ -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
diff --git a/src/corelib/tools/qmap.cpp b/src/corelib/tools/qmap.cpp
index 75104e4825..a4c28b5bd4 100644
--- a/src/corelib/tools/qmap.cpp
+++ b/src/corelib/tools/qmap.cpp
@@ -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
diff --git a/src/corelib/tools/qmap.h b/src/corelib/tools/qmap.h
index b0ec6fb3c6..93134a43dd 100644
--- a/src/corelib/tools/qmap.h
+++ b/src/corelib/tools/qmap.h
@@ -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); }
diff --git a/src/corelib/tools/qset.h b/src/corelib/tools/qset.h
index 6fdc8e6281..ee91336c51 100644
--- a/src/corelib/tools/qset.h
+++ b/src/corelib/tools/qset.h
@@ -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)
diff --git a/src/corelib/tools/qset.qdoc b/src/corelib/tools/qset.qdoc
index ef7a4c4f52..cd90b4949b 100644
--- a/src/corelib/tools/qset.qdoc
+++ b/src/corelib/tools/qset.qdoc
@@ -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)
diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
index 6df84e6363..903a4e1012 100644
--- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp
+++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
@@ -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"
diff --git a/tests/auto/corelib/tools/qmap/tst_qmap.cpp b/tests/auto/corelib/tools/qmap/tst_qmap.cpp
index 5069b805bf..801656e1c3 100644
--- a/tests/auto/corelib/tools/qmap/tst_qmap.cpp
+++ b/tests/auto/corelib/tools/qmap/tst_qmap.cpp
@@ -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"
diff --git a/tests/auto/corelib/tools/qset/tst_qset.cpp b/tests/auto/corelib/tools/qset/tst_qset.cpp
index a3232d05c3..eaa1c018ba 100644
--- a/tests/auto/corelib/tools/qset/tst_qset.cpp
+++ b/tests/auto/corelib/tools/qset/tst_qset.cpp
@@ -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"