Move emplaceFront/Back() implementation into QArrayDataOps

This simplifies and clean up the code.

Change-Id: I4cbfa69bda95187f97daf814eb3d44d90c502d92
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
This commit is contained in:
Lars Knoll 2020-11-09 10:29:58 +01:00
parent fc172c4313
commit 4ee3c4a546
2 changed files with 22 additions and 22 deletions

View File

@ -1373,18 +1373,32 @@ public:
template <typename ...Args>
void emplaceBack(Args&&... args)
{
Q_ASSERT(!this->isShared());
Q_ASSERT(this->freeSpaceAtEnd() >= 1);
new (this->end()) T(std::forward<Args>(args)...);
if (this->needsDetach() || !this->freeSpaceAtEnd()) {
// protect against args being an element of the container
T tmp(std::forward<Args>(args)...);
this->reallocateAndGrow(QArrayData::GrowsAtEnd, 1);
Q_ASSERT(!this->isShared());
Q_ASSERT(this->freeSpaceAtEnd() >= 1);
new (this->end()) T(std::move(tmp));
} else {
new (this->end()) T(std::forward<Args>(args)...);
}
++this->size;
}
template <typename ...Args>
void emplaceFront(Args&&... args)
{
Q_ASSERT(!this->isShared());
Q_ASSERT(this->freeSpaceAtBegin() >= 1);
new (this->ptr - 1) T(std::forward<Args>(args)...);
if (this->needsDetach() || !this->freeSpaceAtBegin()) {
// protect against args being an element of the container
T tmp(std::forward<Args>(args)...);
this->reallocateAndGrow(QArrayData::GrowsAtBeginning, 1);
Q_ASSERT(!this->isShared());
Q_ASSERT(this->freeSpaceAtBegin() >= 1);
new (this->ptr - 1) T(std::move(tmp));
} else {
new (this->ptr - 1) T(std::forward<Args>(args)...);
}
--this->ptr;
++this->size;
}

View File

@ -715,14 +715,7 @@ template<typename T>
template<typename... Args>
inline typename QList<T>::reference QList<T>::emplaceFront(Args &&... args)
{
if (d->needsDetach() || !d.freeSpaceAtBegin()) {
// protect against args being an element of the container
T tmp(std::forward<Args>(args)...);
d.reallocateAndGrow(QArrayData::GrowsAtBeginning, 1);
d->emplaceFront(std::move(tmp));
} else {
d->emplaceFront(std::forward<Args>(args)...);
}
d->emplaceFront(std::forward<Args>(args)...);
return *d.begin();
}
@ -751,14 +744,7 @@ template<typename T>
template<typename... Args>
inline typename QList<T>::reference QList<T>::emplaceBack(Args &&... args)
{
if (d->needsDetach() || !d.freeSpaceAtEnd()) {
// protect against args being an element of the container
T tmp(std::forward<Args>(args)...);
d.reallocateAndGrow(QArrayData::GrowsAtEnd, 1);
d->emplaceBack(std::move(tmp));
} else {
d->emplaceBack(std::forward<Args>(args)...);
}
d->emplaceBack(std::forward<Args>(args)...);
return *(d.end() - 1);
}