diff options
author | Olivier Goffart <ogoffart@trolltech.com> | 2010-03-29 08:34:23 (GMT) |
---|---|---|
committer | Olivier Goffart <ogoffart@trolltech.com> | 2010-03-29 12:05:16 (GMT) |
commit | 809583a34cb69367ecb4218f798a5928de9aacec (patch) | |
tree | 83c8e127b069957f17d9b1684956b5d684895bae | |
parent | 62b082833d904688e44e1bac4849bfa54bf5fe82 (diff) | |
download | Qt-809583a34cb69367ecb4218f798a5928de9aacec.zip Qt-809583a34cb69367ecb4218f798a5928de9aacec.tar.gz Qt-809583a34cb69367ecb4218f798a5928de9aacec.tar.bz2 |
QVarLenghtArray: add some API to be consistant to QVector
That way it is easier to have QVarLenghtArray as a drop-in replacement for QVector
Reviewed-by: Joao
-rw-r--r-- | src/corelib/tools/qvarlengtharray.h | 19 | ||||
-rw-r--r-- | src/corelib/tools/qvarlengtharray.qdoc | 32 | ||||
-rw-r--r-- | tests/auto/qvarlengtharray/tst_qvarlengtharray.cpp | 13 |
3 files changed, 63 insertions, 1 deletions
diff --git a/src/corelib/tools/qvarlengtharray.h b/src/corelib/tools/qvarlengtharray.h index aecb66e..9773d4b 100644 --- a/src/corelib/tools/qvarlengtharray.h +++ b/src/corelib/tools/qvarlengtharray.h @@ -107,6 +107,10 @@ public: Q_ASSERT(idx >= 0 && idx < s); return ptr[idx]; } + inline const T &at(int idx) const { return operator[](idx); } + + T value(int i) const; + T value(int i, const T &defaultValue) const; inline void append(const T &t) { if (s == a) // i.e. s != 0 @@ -248,6 +252,21 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int a } } +template <class T, int Prealloc> +Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i) const +{ + if (i < 0 || i >= size()) { + return T(); + } + return at(i); +} +template <class T, int Prealloc> +Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i, const T &defaultValue) const +{ + return (i < 0 || i >= size()) ? defaultValue : at(i); +} + + QT_END_NAMESPACE QT_END_HEADER diff --git a/src/corelib/tools/qvarlengtharray.qdoc b/src/corelib/tools/qvarlengtharray.qdoc index bb7a3de..38901e5 100644 --- a/src/corelib/tools/qvarlengtharray.qdoc +++ b/src/corelib/tools/qvarlengtharray.qdoc @@ -197,7 +197,7 @@ \a i must be a valid index position in the array (i.e., 0 <= \a i < size()). - \sa data() + \sa data(), at() */ /*! \fn const T &QVarLengthArray::operator[](int i) const @@ -272,3 +272,33 @@ Constructs a copy of \a other. */ +/*! \fn const T &QVarLengthArray::at(int i) const + + Returns a reference to the item at index position \a i. + + \a i must be a valid index position in the array (i.e., 0 <= \a i + < size()). + + \sa value(), operator[]() +*/ + +/*! \fn T QVarLengthArray::value(int i) const + + Returns the value at index position \a i. + + If the index \a i is out of bounds, the function returns + a \l{default-constructed value}. If you are certain that + \a i is within bounds, you can use at() instead, which is slightly + faster. + + \sa at(), operator[]() +*/ + +/*! \fn T QVarLengthArray::value(int i, const T &defaultValue) const + + \overload + + If the index \a i is out of bounds, the function returns + \a defaultValue. +*/ + diff --git a/tests/auto/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/qvarlengtharray/tst_qvarlengtharray.cpp index 1c43069..5708726 100644 --- a/tests/auto/qvarlengtharray/tst_qvarlengtharray.cpp +++ b/tests/auto/qvarlengtharray/tst_qvarlengtharray.cpp @@ -133,6 +133,12 @@ void tst_QVarLengthArray::oldTests() QVERIFY(sa.data() == &sa[0]); QVERIFY(sa[0] == 0xfee); QVERIFY(sa[10] == 0xff); + QVERIFY(sa.at(0) == 0xfee); + QVERIFY(sa.at(10) == 0xff); + QVERIFY(sa.value(0) == 0xfee); + QVERIFY(sa.value(10) == 0xff); + QVERIFY(sa.value(1000) == 0); + QVERIFY(sa.value(1000, 12) == 12); QVERIFY(sa.size() == 512); sa.reserve(1024); QVERIFY(sa.capacity() == 1024); @@ -168,6 +174,13 @@ void tst_QVarLengthArray::oldTests() QCOMPARE(sa.size(), 12); QCOMPARE(sa[10], QString("hello")); QCOMPARE(sa[11], QString("world")); + QCOMPARE(sa.at(10), QString("hello")); + QCOMPARE(sa.at(11), QString("world")); + QCOMPARE(sa.value(10), QString("hello")); + QCOMPARE(sa.value(11), QString("world")); + QCOMPARE(sa.value(10000), QString()); + QCOMPARE(sa.value(1212112, QString("none")), QString("none")); + QCOMPARE(sa.value(-12, QString("neg")), QString("neg")); sa.append(arr, 1); QCOMPARE(sa.size(), 13); |