src/corelib/tools/qvarlengtharray.h
changeset 30 5dc02b23752f
parent 29 b72c6db6890b
child 33 3e2da88830cd
--- a/src/corelib/tools/qvarlengtharray.h	Wed Jun 23 19:07:03 2010 +0300
+++ b/src/corelib/tools/qvarlengtharray.h	Tue Jul 06 15:10:48 2010 +0300
@@ -107,6 +107,10 @@
         Q_ASSERT(idx >= 0 && idx < s);
         return ptr[idx];
     }
+    inline const T &at(int idx) const { return operator[](idx); }
+
+    T value(int i) const;
+    T value(int i, const T &defaultValue) const;
 
     inline void append(const T &t) {
         if (s == a)   // i.e. s != 0
@@ -128,9 +132,9 @@
     friend class QPodList<T, Prealloc>;
     void realloc(int size, int alloc);
 
-    int a;      // capacity
-    int s;      // size
-    T *ptr;     // data
+    int a;
+    int s;
+    T *ptr;
     union {
         // ### Qt 5: Use 'Prealloc * sizeof(T)' as array size
         char array[sizeof(qint64) * (((Prealloc * sizeof(T)) / sizeof(qint64)) + 1)];
@@ -193,8 +197,8 @@
     Q_ASSERT(aalloc >= asize);
     T *oldPtr = ptr;
     int osize = s;
+    // s = asize;
 
-    const int copySize = qMin(asize, osize);
     if (aalloc != a) {
         ptr = reinterpret_cast<T *>(qMalloc(aalloc * sizeof(T)));
         Q_CHECK_PTR(ptr);
@@ -205,6 +209,7 @@
             if (QTypeInfo<T>::isStatic) {
                 QT_TRY {
                     // copy all the old elements
+                    const int copySize = qMin(asize, osize);
                     while (s < copySize) {
                         new (ptr+s) T(*(oldPtr+s));
                         (oldPtr+s)->~T();
@@ -220,19 +225,19 @@
                     QT_RETHROW;
                 }
             } else {
-                qMemCopy(ptr, oldPtr, copySize * sizeof(T));
+                qMemCopy(ptr, oldPtr, qMin(asize, osize) * sizeof(T));
             }
         } else {
             ptr = oldPtr;
             return;
         }
     }
-    s = copySize;
 
     if (QTypeInfo<T>::isComplex) {
-        // destroy remaining old objects
         while (osize > asize)
             (oldPtr+(--osize))->~T();
+        if (!QTypeInfo<T>::isStatic)
+            s = osize;
     }
 
     if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
@@ -247,6 +252,21 @@
     }
 }
 
+template <class T, int Prealloc>
+Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i) const
+{
+    if (i < 0 || i >= size()) {
+        return T();
+    }
+    return at(i);
+}
+template <class T, int Prealloc>
+Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i, const T &defaultValue) const
+{
+    return (i < 0 || i >= size()) ? defaultValue : at(i);
+}
+
+
 QT_END_NAMESPACE
 
 QT_END_HEADER