diff --git a/src/corelib/tools/qlist.h b/src/corelib/tools/qlist.h index 96efe28a310..15c2e53e1dc 100644 --- a/src/corelib/tools/qlist.h +++ b/src/corelib/tools/qlist.h @@ -316,12 +316,15 @@ public: void append(rvalue_ref t) { emplaceBack(std::move(t)); } void append(const QList &l) { append(l.constBegin(), l.constEnd()); } void append(QList &&l); - void prepend(rvalue_ref t); - void prepend(parameter_type t); + void prepend(rvalue_ref t) { emplaceFront(std::move(t)); } + void prepend(parameter_type t) { emplaceFront(t); } template reference emplaceBack(Args&&... args) { return *emplace(count(), std::forward(args)...); } + template + inline reference emplaceFront(Args&&... args); + iterator insert(qsizetype i, parameter_type t) { return insert(i, 1, t); } iterator insert(qsizetype i, qsizetype n, parameter_type t); @@ -651,13 +654,6 @@ inline void QList::remove(qsizetype i, qsizetype n) } } -template -inline void QList::prepend(parameter_type t) -{ insert(0, 1, t); } -template -void QList::prepend(rvalue_ref t) -{ insert(0, std::move(t)); } - template inline T QList::value(qsizetype i, parameter_type defaultValue) const { @@ -711,6 +707,29 @@ inline void QList::append(QList &&other) } } +template +template +inline typename QList::reference QList::emplaceFront(Args &&... args) +{ + const bool shouldGrow = d->shouldGrowBeforeInsert(d.begin(), 1); + const auto newSize = size() + 1; + if (d->needsDetach() || newSize > d->constAllocatedCapacity() || shouldGrow) { + const auto flags = d->detachFlags() | Data::GrowsBackwards; + DataPointer detached(DataPointer::allocateGrow(d, newSize, flags)); + + T tmp(std::forward(args)...); + detached->copyAppend(constBegin(), constEnd()); + // insert here makes sure we have extra free space at beginning. we + // actually need a proper copyPrepend here instead. + detached->insert(detached.begin(), 1, std::move(tmp)); + d.swap(detached); + } else { + // ### replace with emplaceFront + d->emplace(d.begin(), std::forward(args)...); + } + return *d.begin(); +} + template inline typename QList::iterator