From c22d43237363463e3409286470392a3227f49961 Mon Sep 17 00:00:00 2001 From: Olivier Goffart Date: Sun, 19 Apr 2009 22:45:06 +0200 Subject: C++0x: being able to create a list with the {,,,} notation Reviewed-by: Joao --- src/corelib/tools/qlist.h | 8 ++++++++ src/corelib/tools/qvector.h | 23 ++++++++++++++++++++++- tests/auto/qlist/tst_qlist.cpp | 16 ++++++++++++++++ tests/auto/qvector/tst_qvector.cpp | 15 +++++++++++++++ 4 files changed, 61 insertions(+), 1 deletion(-) 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 #include #endif +#ifdef Q_COMPILER_INITIALIZER_LISTS +#include +#include +#endif #include #include @@ -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 args) : d(&QListData::shared_null) + { d->ref.ref(); qCopy(args.begin(), args.end(), std::back_inserter(*this)); } +#endif bool operator==(const QList &l) const; inline bool operator!=(const QList &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 #include +#ifdef Q_COMPILER_INITIALIZER_LISTS +#include +#endif QT_BEGIN_HEADER @@ -122,7 +125,9 @@ public: inline QVector operator=(QVector &&other) { qSwap(p, other.p); return *this; } #endif - +#ifdef Q_COMPILER_INITIALIZER_LISTS + inline QVector(std::initializer_list args); +#endif bool operator==(const QVector &v) const; inline bool operator!=(const QVector &v) const { return !(*this == v); } @@ -430,6 +435,22 @@ QVector::QVector(int asize, const T &t) new (--i) T(t); } +#ifdef Q_COMPILER_INITIALIZER_LISTS +template +QVector::QVector(std::initializer_list 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 void QVector::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 v1{2,3,4}; + QCOMPARE(v1, QList() << 2 << 3 << 4); + QCOMPARE(v1, (QList{2,3,4})); + + QList> v2{ v1, {1}, QList(), {2,3,4} }; + QList> v3; + v3 << v1 << (QList() << 1) << QList() << 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 v1{2,3,4}; + QCOMPARE(v1, QVector() << 2 << 3 << 4); + QCOMPARE(v1, (QVector{2,3,4})); + + QVector> v2{ v1, {1}, QVector(), {2,3,4} }; + QVector> v3; + v3 << v1 << (QVector() << 1) << QVector() << v1; + QCOMPARE(v3, v2); +#endif +} + QTEST_APPLESS_MAIN(tst_QVector) #include "tst_qvector.moc" -- cgit v0.12