diff options
author | João Abecasis <joao.abecasis@nokia.com> | 2012-02-16 23:27:44 +0100 |
---|---|---|
committer | Qt by Nokia <qt-info@nokia.com> | 2012-04-02 16:34:21 +0200 |
commit | 15e3ae6b9da9b32236d3e3348ede86c3acf06fb4 (patch) | |
tree | 20e674797a075c94b1319a0547233595b26fa34d /src | |
parent | 8c413f3eff2de761ae038294195831de753b9252 (diff) |
Introduce QArrayDataOps::truncate
This enables a truncating resize() to be implemented. It is similar to
destroyAll(), but updates the size() as it goes, so it is safe to use
outside a container's destructor (and doesn't necessarily destroy all
elements).
The appendInitialize test was repurposed and now doubles as an
additional test for QArrayDataOps as well as exercising SimpleVector's
resize().
Change-Id: Iee94a685c9ea436c6af5b1b77486734a38c49ca1
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/corelib/tools/qarraydataops.h | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h index 785a26c3a8..de149b701c 100644 --- a/src/corelib/tools/qarraydataops.h +++ b/src/corelib/tools/qarraydataops.h @@ -89,6 +89,14 @@ struct QPodArrayOps this->size += n; } + void truncate(size_t newSize) + { + Q_ASSERT(!this->ref.isShared()); + Q_ASSERT(newSize < size_t(this->size)); + + this->size = newSize; + } + void destroyAll() // Call from destructors, ONLY! { Q_ASSERT(this->ref.atomic.load() == 0); @@ -153,6 +161,17 @@ struct QGenericArrayOps } } + void truncate(size_t newSize) + { + Q_ASSERT(!this->ref.isShared()); + Q_ASSERT(newSize < size_t(this->size)); + + const T *const b = this->begin(); + do { + (b + --this->size)->~T(); + } while (uint(this->size) != newSize); + } + void destroyAll() // Call from destructors, ONLY { // As this is to be called only from destructor, it doesn't need to be @@ -239,6 +258,7 @@ struct QMovableArrayOps { // using QGenericArrayOps<T>::appendInitialize; // using QGenericArrayOps<T>::copyAppend; + // using QGenericArrayOps<T>::truncate; // using QGenericArrayOps<T>::destroyAll; void insert(T *where, const T *b, const T *e) |