src/gui/painting/qdatabuffer_p.h
changeset 30 5dc02b23752f
parent 18 2f34d5167611
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
    58 QT_BEGIN_NAMESPACE
    58 QT_BEGIN_NAMESPACE
    59 
    59 
    60 template <typename Type> class QDataBuffer
    60 template <typename Type> class QDataBuffer
    61 {
    61 {
    62 public:
    62 public:
    63     QDataBuffer(int res = 64)
    63     QDataBuffer(int res)
    64     {
    64     {
    65         capacity = res;
    65         capacity = res;
    66         buffer = (Type*) qMalloc(capacity * sizeof(Type));
    66         if (res)
       
    67             buffer = (Type*) qMalloc(capacity * sizeof(Type));
       
    68         else
       
    69             buffer = 0;
    67         siz = 0;
    70         siz = 0;
    68     }
    71     }
    69 
    72 
    70     ~QDataBuffer()
    73     ~QDataBuffer()
    71     {
    74     {
    72         qFree(buffer);
    75         if (buffer)
       
    76             qFree(buffer);
    73     }
    77     }
    74 
    78 
    75     inline void reset() { siz = 0; }
    79     inline void reset() { siz = 0; }
    76 
    80 
    77     inline bool isEmpty() const { return siz==0; }
    81     inline bool isEmpty() const { return siz==0; }
    79     inline int size() const { return siz; }
    83     inline int size() const { return siz; }
    80     inline Type *data() const { return buffer; }
    84     inline Type *data() const { return buffer; }
    81 
    85 
    82     inline Type &at(int i) { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; }
    86     inline Type &at(int i) { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; }
    83     inline const Type &at(int i) const { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; }
    87     inline const Type &at(int i) const { Q_ASSERT(i >= 0 && i < siz); return buffer[i]; }
       
    88     inline Type &last() { Q_ASSERT(!isEmpty()); return buffer[siz-1]; }
    84     inline const Type &last() const { Q_ASSERT(!isEmpty()); return buffer[siz-1]; }
    89     inline const Type &last() const { Q_ASSERT(!isEmpty()); return buffer[siz-1]; }
       
    90     inline Type &first() { Q_ASSERT(!isEmpty()); return buffer[0]; }
    85     inline const Type &first() const { Q_ASSERT(!isEmpty()); return buffer[0]; }
    91     inline const Type &first() const { Q_ASSERT(!isEmpty()); return buffer[0]; }
    86 
    92 
    87     inline void add(const Type &t) {
    93     inline void add(const Type &t) {
    88         reserve(siz + 1);
    94         reserve(siz + 1);
    89         buffer[siz] = t;
    95         buffer[siz] = t;
    90         ++siz;
    96         ++siz;
       
    97     }
       
    98 
       
    99     inline void pop_back() {
       
   100         Q_ASSERT(siz > 0);
       
   101         --siz;
    91     }
   102     }
    92 
   103 
    93     inline void resize(int size) {
   104     inline void resize(int size) {
    94         reserve(size);
   105         reserve(size);
    95         siz = size;
   106         siz = size;
    96     }
   107     }
    97 
   108 
    98     inline void reserve(int size) {
   109     inline void reserve(int size) {
    99         if (size > capacity) {
   110         if (size > capacity) {
       
   111             if (capacity == 0)
       
   112                 capacity = 1;
   100             while (capacity < size)
   113             while (capacity < size)
   101                 capacity *= 2;
   114                 capacity *= 2;
   102             buffer = (Type*) qRealloc(buffer, capacity * sizeof(Type));
   115             buffer = (Type*) qRealloc(buffer, capacity * sizeof(Type));
   103         }
   116         }
   104     }
   117     }
   105 
   118 
   106     inline void shrink(int size) {
   119     inline void shrink(int size) {
   107         capacity = size;
   120         capacity = size;
   108         buffer = (Type*) qRealloc(buffer, capacity * sizeof(Type));
   121         if (size)
       
   122             buffer = (Type*) qRealloc(buffer, capacity * sizeof(Type));
       
   123         else {
       
   124             qFree(buffer);
       
   125             buffer = 0;
       
   126         }
   109     }
   127     }
   110 
   128 
   111     inline void swap(QDataBuffer<Type> &other) {
   129     inline void swap(QDataBuffer<Type> &other) {
   112         qSwap(capacity, other.capacity);
   130         qSwap(capacity, other.capacity);
   113         qSwap(siz, other.siz);
   131         qSwap(siz, other.siz);