summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKai Koehne <kai.koehne@theqtcompany.com>2015-03-19 17:13:22 +0100
committerMarc Mutz <marc.mutz@kdab.com>2015-04-04 15:48:13 +0000
commit1db3de6a1e09216aaf92b60fc167a7326c6a62d8 (patch)
tree2ebf2397b9ea39ded8c4ddafaef355c87ddbc7b3
parentc51240ca759e863b96fe65662a22ae6a301a170f (diff)
QVLA: Add operator= for initializer lists
Add a dedicated operator=(std::initializer_list) that first resizes the QCLA, and then replaces the elements one by one. This should be usually faster than creating a temporary QCLA and then copying it, except for the case where the new array does not fit into the allocated stack - but this is IMO nothing to optimize for. Task-number: QTBUG-45041 Change-Id: I147d6d01186b1ca3c635b2c8365d8f6e638ce6fe GPush-Base: 08de3113051e1289f0de0651ec5647c9ee6feb27 Reviewed-by: Marc Mutz <marc.mutz@kdab.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
-rw-r--r--src/corelib/tools/qvarlengtharray.h9
-rw-r--r--src/corelib/tools/qvarlengtharray.qdoc9
-rw-r--r--tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp20
3 files changed, 38 insertions, 0 deletions
diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h
index 95cd0447d8..90b54b7297 100644
--- a/src/corelib/tools/qvarlengtharray.h
+++ b/src/corelib/tools/qvarlengtharray.h
@@ -91,6 +91,15 @@ public:
return *this;
}
+#ifdef Q_COMPILER_INITIALIZER_LISTS
+ QVarLengthArray<T, Prealloc> &operator=(std::initializer_list<T> list)
+ {
+ resize(list.size());
+ std::copy(list.begin(), list.end(), this->begin());
+ return *this;
+ }
+#endif
+
inline void removeLast() {
Q_ASSERT(s > 0);
realloc(s - 1, a);
diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc
index d1b87f381e..a2d4c55f7a 100644
--- a/src/corelib/tools/qvarlengtharray.qdoc
+++ b/src/corelib/tools/qvarlengtharray.qdoc
@@ -362,6 +362,15 @@
Assigns \a other to this array and returns a reference to this array.
*/
+/*! \fn QVarLengthArray<T, Prealloc> &QVarLengthArray::operator=(std::initializer_list<T> list)
+ \since 5.5
+
+ Assigns the values of \a list to this array, and returns a reference to this array.
+
+ This constructor is only enabled if the compiler supports C++11 initializer
+ lists.
+*/
+
/*! \fn QVarLengthArray::QVarLengthArray(const QVarLengthArray<T, Prealloc> &other)
Constructs a copy of \a other.
*/
diff --git a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
index 40917eebea..5d12cd9804 100644
--- a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
+++ b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp
@@ -780,6 +780,7 @@ void tst_QVarLengthArray::initializeList()
T val3(101);
T val4(114);
+ // QVarLengthArray(std::initializer_list<>)
QVarLengthArray<T> v1 {val1, val2, val3};
QCOMPARE(v1, QVarLengthArray<T>() << val1 << val2 << val3);
QCOMPARE(v1, (QVarLengthArray<T> {val1, val2, val3}));
@@ -791,6 +792,25 @@ void tst_QVarLengthArray::initializeList()
QVarLengthArray<T> v4({});
QCOMPARE(v4.size(), 0);
+
+ // operator=(std::initializer_list<>)
+
+ QVarLengthArray<T> v5({val2, val1});
+ v1 = { val1, val2 }; // make array smaller
+ v4 = { val1, val2 }; // make array bigger
+ v5 = { val1, val2 }; // same size
+ QCOMPARE(v1, QVarLengthArray<T>() << val1 << val2);
+ QCOMPARE(v4, v1);
+ QCOMPARE(v5, v1);
+
+ QVarLengthArray<T, 1> v6 = { val1 };
+ v6 = { val1, val2 }; // force allocation on heap
+ QCOMPARE(v6.size(), 2);
+ QCOMPARE(v6.first(), val1);
+ QCOMPARE(v6.last(), val2);
+
+ v6 = {}; // assign empty
+ QCOMPARE(v6.size(), 0);
#else
QSKIP("This tests requires a compiler that supports initializer lists.");
#endif