summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qarraydataops.h
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-11-09 10:29:58 +0100
committerLars Knoll <lars.knoll@qt.io>2020-11-17 11:45:58 +0100
commit4ee3c4a546e4bf9d9f716f9d2e567e380e0453c7 (patch)
treea169297a88cd94534acac09b888c5e82accd6268 /src/corelib/tools/qarraydataops.h
parentfc172c43132a15e17bd435db90e85722c7100361 (diff)
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>
Diffstat (limited to 'src/corelib/tools/qarraydataops.h')
-rw-r--r--src/corelib/tools/qarraydataops.h26
1 files changed, 20 insertions, 6 deletions
diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h
index c2414f22f2..a0c3da7cce 100644
--- a/src/corelib/tools/qarraydataops.h
+++ b/src/corelib/tools/qarraydataops.h
@@ -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;
}