summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qarraydataops.h
diff options
context:
space:
mode:
authorJoão Abecasis <joao.abecasis@nokia.com>2011-11-22 12:16:24 +0100
committerQt by Nokia <qt-info@nokia.com>2011-12-14 15:55:47 +0100
commit9c04f721a6b0521f596950771e9d88a5d00a29ee (patch)
treef1a8df435cf1cce1b7bb982137ded0dd45860142 /src/corelib/tools/qarraydataops.h
parent4da0b5fc02fe3a647d32892905c42928841d6487 (diff)
QArrayDataOps::insert
Inserting elements anywhere in the array requires moving the elements that follow out of the way and writing in the new ones. Trivial for PODs and almost as much for movable types. For "complex" types, we start by extending the array with placement new and copy constructing elements. Then, copy assignment resets the elements that were previously part of the array. QPodArrayOps uses non-throwing operations. QMovableArrayOps provides full rollback in the face of exceptions (strong guarantee). QGenericArrayOps enforces that no data is leaked (all destructors called) and invariants are maintained on exceptions -- the basic guarantee. With 3 different implementations, 2 of which are non-trivial, this operation is a good showcase for QArrayOpsSelector and the different implementations. As such, it warrants its own commit. Change-Id: I21d9b4cb8e810db82623bcd1d78f583ebf3b6cb7 Reviewed-by: Bradley T. Hughes <bradley.hughes@nokia.com>
Diffstat (limited to 'src/corelib/tools/qarraydataops.h')
-rw-r--r--src/corelib/tools/qarraydataops.h142
1 files changed, 142 insertions, 0 deletions
diff --git a/src/corelib/tools/qarraydataops.h b/src/corelib/tools/qarraydataops.h
index d62c411511..e7c66a456b 100644
--- a/src/corelib/tools/qarraydataops.h
+++ b/src/corelib/tools/qarraydataops.h
@@ -88,6 +88,19 @@ struct QPodArrayOps
// As this is to be called only from destructor, it doesn't need to be
// exception safe; size not updated.
}
+
+ void insert(T *where, const T *b, const T *e)
+ {
+ Q_ASSERT(this->ref == 1);
+ Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
+ Q_ASSERT(b < e);
+ Q_ASSERT(e <= where || b > this->end()); // No overlap
+ Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+
+ ::memmove(where + (e - b), where, (this->end() - where) * sizeof(T));
+ ::memcpy(where, b, (e - b) * sizeof(T));
+ this->size += (e - b);
+ }
};
template <class T>
@@ -133,6 +146,71 @@ struct QGenericArrayOps
while (i != b)
(--i)->~T();
}
+
+ void insert(T *where, const T *b, const T *e)
+ {
+ Q_ASSERT(this->ref == 1);
+ Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
+ Q_ASSERT(b < e);
+ Q_ASSERT(e <= where || b > this->end()); // No overlap
+ Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+
+ // Array may be truncated at where in case of exceptions
+
+ T *const end = this->end();
+ const T *readIter = end;
+ T *writeIter = end + (e - b);
+
+ const T *const step1End = where + qMax(e - b, end - where);
+
+ struct Destructor
+ {
+ Destructor(T *&iter)
+ : iter(&iter)
+ , end(iter)
+ {
+ }
+
+ void commit()
+ {
+ iter = &end;
+ }
+
+ ~Destructor()
+ {
+ for (; *iter != end; --*iter)
+ (*iter)->~T();
+ }
+
+ T **iter;
+ T *end;
+ } destroyer(writeIter);
+
+ // Construct new elements in array
+ do {
+ --readIter, --writeIter;
+ new (writeIter) T(*readIter);
+ } while (writeIter != step1End);
+
+ while (writeIter != end) {
+ --e, --writeIter;
+ new (writeIter) T(*e);
+ }
+
+ destroyer.commit();
+ this->size += destroyer.end - end;
+
+ // Copy assign over existing elements
+ while (readIter != where) {
+ --readIter, --writeIter;
+ *writeIter = *readIter;
+ }
+
+ while (writeIter != where) {
+ --e, --writeIter;
+ *writeIter = *e;
+ }
+ }
};
template <class T>
@@ -141,6 +219,70 @@ struct QMovableArrayOps
{
// using QGenericArrayOps<T>::copyAppend;
// using QGenericArrayOps<T>::destroyAll;
+
+ void insert(T *where, const T *b, const T *e)
+ {
+ Q_ASSERT(this->ref == 1);
+ Q_ASSERT(where >= this->begin() && where < this->end()); // Use copyAppend at end
+ Q_ASSERT(b < e);
+ Q_ASSERT(e <= where || b > this->end()); // No overlap
+ Q_ASSERT(size_t(e - b) <= this->alloc - uint(this->size));
+
+ // Provides strong exception safety guarantee,
+ // provided T::~T() nothrow
+
+ struct ReversibleDisplace
+ {
+ ReversibleDisplace(T *begin, T *end, size_t displace)
+ : begin(begin)
+ , end(end)
+ , displace(displace)
+ {
+ ::memmove(begin + displace, begin, (end - begin) * sizeof(T));
+ }
+
+ void commit() { displace = 0; }
+
+ ~ReversibleDisplace()
+ {
+ if (displace)
+ ::memmove(begin, begin + displace, (end - begin) * sizeof(T));
+ }
+
+ T *const begin;
+ T *const end;
+ size_t displace;
+
+ } displace(where, this->end(), size_t(e - b));
+
+ struct CopyConstructor
+ {
+ CopyConstructor(T *where) : where(where) {}
+
+ void copy(const T *src, const T *const srcEnd)
+ {
+ n = 0;
+ for (; src != srcEnd; ++src) {
+ new (where + n) T(*src);
+ ++n;
+ }
+ n = 0;
+ }
+
+ ~CopyConstructor()
+ {
+ while (n)
+ where[--n].~T();
+ }
+
+ T *const where;
+ size_t n;
+ } copier(where);
+
+ copier.copy(b, e);
+ displace.commit();
+ this->size += (e - b);
+ }
};
template <class T, class = void>