diff options
Diffstat (limited to 'src/corelib/tools/qlist.h')
-rw-r--r-- | src/corelib/tools/qlist.h | 38 |
1 files changed, 35 insertions, 3 deletions
diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 72e1403e76..c81968dce1 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -346,6 +346,11 @@ private: void node_destruct(Node *n); void node_copy(Node *from, Node *to, Node *src); void node_destruct(Node *from, Node *to); + + bool isValidIterator(const iterator &i) const + { + return (constBegin().i <= i.i) && (i.i <= constEnd().i); + } }; #if defined(Q_CC_BOR) @@ -433,8 +438,14 @@ Q_INLINE_TEMPLATE QList<T> &QList<T>::operator=(const QList<T> &l) template <typename T> inline typename QList<T>::iterator QList<T>::insert(iterator before, const T &t) { + Q_ASSERT_X(isValidIterator(before), "QList::insert", "The specified iterator argument 'before' is invalid"); + int iBefore = int(before.i - reinterpret_cast<Node *>(p.begin())); - Node *n = reinterpret_cast<Node *>(p.insert(iBefore)); + Node *n = 0; + if (d->ref.isShared()) + n = detach_helper_grow(iBefore, 1); + else + n = reinterpret_cast<Node *>(p.insert(iBefore)); QT_TRY { node_construct(n, t); } QT_CATCH(...) { @@ -445,8 +456,16 @@ inline typename QList<T>::iterator QList<T>::insert(iterator before, const T &t) } template <typename T> inline typename QList<T>::iterator QList<T>::erase(iterator it) -{ node_destruct(it.i); - return reinterpret_cast<Node *>(p.erase(reinterpret_cast<void**>(it.i))); } +{ + Q_ASSERT_X(isValidIterator(it), "QList::erase", "The specified iterator argument 'it' is invalid"); + if (d->ref.isShared()) { + int offset = int(it.i - reinterpret_cast<Node *>(p.begin())); + it = begin(); // implies detach() + it += offset; + } + node_destruct(it.i); + return reinterpret_cast<Node *>(p.erase(reinterpret_cast<void**>(it.i))); +} template <typename T> inline const T &QList<T>::at(int i) const { Q_ASSERT_X(i >= 0 && i < p.size(), "QList<T>::at", "index out of range"); @@ -807,6 +826,19 @@ template <typename T> Q_OUTOFLINE_TEMPLATE typename QList<T>::iterator QList<T>::erase(typename QList<T>::iterator afirst, typename QList<T>::iterator alast) { + Q_ASSERT_X(isValidIterator(afirst), "QList::erase", "The specified iterator argument 'afirst' is invalid"); + Q_ASSERT_X(isValidIterator(alast), "QList::erase", "The specified iterator argument 'alast' is invalid"); + + if (d->ref.isShared()) { + // ### A block is erased and a detach is needed. We should shrink and only copy relevant items. + int offsetfirst = int(afirst.i - reinterpret_cast<Node *>(p.begin())); + int offsetlast = int(alast.i - reinterpret_cast<Node *>(p.begin())); + afirst = begin(); // implies detach() + alast = afirst; + afirst += offsetfirst; + alast += offsetlast; + } + for (Node *n = afirst.i; n < alast.i; ++n) node_destruct(n); int idx = afirst - begin(); |