diff options
author | Olivier Goffart <ogoffart@trolltech.com> | 2009-04-19 20:45:06 (GMT) |
---|---|---|
committer | Olivier Goffart <olivier.goffart@nokia.com> | 2010-09-03 10:57:24 (GMT) |
commit | c22d43237363463e3409286470392a3227f49961 (patch) | |
tree | 9c1929a796ae8175242a7d6baf359592b52b3339 | |
parent | da0e1a682362144b9f13b4c564f86e09efb681bb (diff) | |
download | Qt-c22d43237363463e3409286470392a3227f49961.zip Qt-c22d43237363463e3409286470392a3227f49961.tar.gz Qt-c22d43237363463e3409286470392a3227f49961.tar.bz2 |
C++0x: being able to create a list with the {,,,} notation
Reviewed-by: Joao
-rw-r--r-- | src/corelib/tools/qlist.h | 8 | ||||
-rw-r--r-- | src/corelib/tools/qvector.h | 23 | ||||
-rw-r--r-- | tests/auto/qlist/tst_qlist.cpp | 16 | ||||
-rw-r--r-- | tests/auto/qvector/tst_qvector.cpp | 15 |
4 files changed, 61 insertions, 1 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index b1d07bd..8f988d6 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -50,6 +50,10 @@ #include <iterator> #include <list> #endif +#ifdef Q_COMPILER_INITIALIZER_LISTS +#include <iterator> +#include <initializer_list> +#endif #include <new> #include <limits.h> @@ -122,6 +126,10 @@ public: inline QList &operator=(QList &&other) { qSwap(d, other.d); return *this; } #endif +#ifdef Q_COMPILER_INITIALIZER_LISTS + inline QList(std::initializer_list<T> args) : d(&QListData::shared_null) + { d->ref.ref(); qCopy(args.begin(), args.end(), std::back_inserter(*this)); } +#endif bool operator==(const QList<T> &l) const; inline bool operator!=(const QList<T> &l) const { return !(*this == l); } diff --git a/src/corelib/tools/qvector.h b/src/corelib/tools/qvector.h index c001699..535f43d 100644 --- a/src/corelib/tools/qvector.h +++ b/src/corelib/tools/qvector.h @@ -53,6 +53,9 @@ #endif #include <stdlib.h> #include <string.h> +#ifdef Q_COMPILER_INITIALIZER_LISTS +#include <initializer_list> +#endif QT_BEGIN_HEADER @@ -122,7 +125,9 @@ public: inline QVector<T> operator=(QVector<T> &&other) { qSwap(p, other.p); return *this; } #endif - +#ifdef Q_COMPILER_INITIALIZER_LISTS + inline QVector(std::initializer_list<T> args); +#endif bool operator==(const QVector<T> &v) const; inline bool operator!=(const QVector<T> &v) const { return !(*this == v); } @@ -430,6 +435,22 @@ QVector<T>::QVector(int asize, const T &t) new (--i) T(t); } +#ifdef Q_COMPILER_INITIALIZER_LISTS +template <typename T> +QVector<T>::QVector(std::initializer_list<T> args) +{ + p = malloc(args.size()); + d->ref = 1; + d->alloc = d->size = args.size(); + d->sharable = true; + d->capacity = false; + T* i = p->array + d->size; + auto it = args.end(); + while (i != p->array) + new (--i) T(*(--it)); +} +#endif + template <typename T> void QVector<T>::free(Data *x) { diff --git a/tests/auto/qlist/tst_qlist.cpp b/tests/auto/qlist/tst_qlist.cpp index ba8aefa..9f6b4a5 100644 --- a/tests/auto/qlist/tst_qlist.cpp +++ b/tests/auto/qlist/tst_qlist.cpp @@ -89,6 +89,8 @@ private slots: void testSTLIterators() const; void testOperators() const; + + void initializeList() const; }; void tst_QList::length() const @@ -662,5 +664,19 @@ void tst_QList::testSTLIterators() const QCOMPARE(*it, QLatin1String("foo")); } +void tst_QList::initializeList() const +{ +#ifdef QT_CXX0X_INITIALIZERLIST + QList<int> v1{2,3,4}; + QCOMPARE(v1, QList<int>() << 2 << 3 << 4); + QCOMPARE(v1, (QList<int>{2,3,4})); + + QList<QList<int>> v2{ v1, {1}, QList<int>(), {2,3,4} }; + QList<QList<int>> v3; + v3 << v1 << (QList<int>() << 1) << QList<int>() << v1; + QCOMPARE(v3, v2); +#endif +} + QTEST_APPLESS_MAIN(tst_QList) #include "tst_qlist.moc" diff --git a/tests/auto/qvector/tst_qvector.cpp b/tests/auto/qvector/tst_qvector.cpp index 2bc8d15..d8dfacf 100644 --- a/tests/auto/qvector/tst_qvector.cpp +++ b/tests/auto/qvector/tst_qvector.cpp @@ -88,6 +88,7 @@ private slots: void outOfMemory(); void QTBUG6416_reserve(); + void initializeList(); }; void tst_QVector::constructors() const @@ -834,5 +835,19 @@ void tst_QVector::QTBUG6416_reserve() QCOMPARE(fooCtor, fooDtor); } +void tst_QVector::initializeList() +{ +#ifdef QT_CXX0X_INITIALIZERLIST + QVector<int> v1{2,3,4}; + QCOMPARE(v1, QVector<int>() << 2 << 3 << 4); + QCOMPARE(v1, (QVector<int>{2,3,4})); + + QVector<QVector<int>> v2{ v1, {1}, QVector<int>(), {2,3,4} }; + QVector<QVector<int>> v3; + v3 << v1 << (QVector<int>() << 1) << QVector<int>() << v1; + QCOMPARE(v3, v2); +#endif +} + QTEST_APPLESS_MAIN(tst_QVector) #include "tst_qvector.moc" |