source: trunk/src/corelib/tools/qvarlengtharray.h@ 813

Last change on this file since 813 was 769, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.3 sources from branches/vendor/nokia/qt.

File size: 7.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtCore module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#ifndef QVARLENGTHARRAY_H
43#define QVARLENGTHARRAY_H
44
45#include <QtCore/qcontainerfwd.h>
46#include <QtCore/qglobal.h>
47#include <new>
48
49QT_BEGIN_HEADER
50
51QT_BEGIN_NAMESPACE
52
53QT_MODULE(Core)
54
55template<class T, int Prealloc>
56class QPodList;
57
58// Prealloc = 256 by default, specified in qcontainerfwd.h
59template<class T, int Prealloc>
60class QVarLengthArray
61{
62public:
63 inline explicit QVarLengthArray(int size = 0);
64
65 inline QVarLengthArray(const QVarLengthArray<T, Prealloc> &other)
66 : a(Prealloc), s(0), ptr(reinterpret_cast<T *>(array))
67 {
68 append(other.constData(), other.size());
69 }
70
71 inline ~QVarLengthArray() {
72 if (QTypeInfo<T>::isComplex) {
73 T *i = ptr + s;
74 while (i-- != ptr)
75 i->~T();
76 }
77 if (ptr != reinterpret_cast<T *>(array))
78 qFree(ptr);
79 }
80 inline QVarLengthArray<T, Prealloc> &operator=(const QVarLengthArray<T, Prealloc> &other)
81 {
82 if (this != &other) {
83 clear();
84 append(other.constData(), other.size());
85 }
86 return *this;
87 }
88
89 inline void removeLast() {
90 Q_ASSERT(s > 0);
91 realloc(s - 1, a);
92 }
93 inline int size() const { return s; }
94 inline int count() const { return s; }
95 inline bool isEmpty() const { return (s == 0); }
96 inline void resize(int size);
97 inline void clear() { resize(0); }
98
99 inline int capacity() const { return a; }
100 inline void reserve(int size);
101
102 inline T &operator[](int idx) {
103 Q_ASSERT(idx >= 0 && idx < s);
104 return ptr[idx];
105 }
106 inline const T &operator[](int idx) const {
107 Q_ASSERT(idx >= 0 && idx < s);
108 return ptr[idx];
109 }
110
111 inline void append(const T &t) {
112 if (s == a) // i.e. s != 0
113 realloc(s, s<<1);
114 const int idx = s++;
115 if (QTypeInfo<T>::isComplex) {
116 new (ptr + idx) T(t);
117 } else {
118 ptr[idx] = t;
119 }
120 }
121 void append(const T *buf, int size);
122
123 inline T *data() { return ptr; }
124 inline const T *data() const { return ptr; }
125 inline const T * constData() const { return ptr; }
126
127private:
128 friend class QPodList<T, Prealloc>;
129 void realloc(int size, int alloc);
130
131 int a; // capacity
132 int s; // size
133 T *ptr; // data
134 union {
135 // ### Qt 5: Use 'Prealloc * sizeof(T)' as array size
136 char array[sizeof(qint64) * (((Prealloc * sizeof(T)) / sizeof(qint64)) + 1)];
137 qint64 q_for_alignment_1;
138 double q_for_alignment_2;
139 };
140};
141
142template <class T, int Prealloc>
143Q_INLINE_TEMPLATE QVarLengthArray<T, Prealloc>::QVarLengthArray(int asize)
144 : s(asize) {
145 if (s > Prealloc) {
146 ptr = reinterpret_cast<T *>(qMalloc(s * sizeof(T)));
147 Q_CHECK_PTR(ptr);
148 a = s;
149 } else {
150 ptr = reinterpret_cast<T *>(array);
151 a = Prealloc;
152 }
153 if (QTypeInfo<T>::isComplex) {
154 T *i = ptr + s;
155 while (i != ptr)
156 new (--i) T;
157 }
158}
159
160template <class T, int Prealloc>
161Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::resize(int asize)
162{ realloc(asize, qMax(asize, a)); }
163
164template <class T, int Prealloc>
165Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::reserve(int asize)
166{ if (asize > a) realloc(s, asize); }
167
168template <class T, int Prealloc>
169Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, int increment)
170{
171 Q_ASSERT(abuf);
172 if (increment <= 0)
173 return;
174
175 const int asize = s + increment;
176
177 if (asize >= a)
178 realloc(s, qMax(s*2, asize));
179
180 if (QTypeInfo<T>::isComplex) {
181 // call constructor for new objects (which can throw)
182 while (s < asize)
183 new (ptr+(s++)) T(*abuf++);
184 } else {
185 qMemCopy(&ptr[s], abuf, increment * sizeof(T));
186 s = asize;
187 }
188}
189
190template <class T, int Prealloc>
191Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int aalloc)
192{
193 Q_ASSERT(aalloc >= asize);
194 T *oldPtr = ptr;
195 int osize = s;
196
197 const int copySize = qMin(asize, osize);
198 if (aalloc != a) {
199 ptr = reinterpret_cast<T *>(qMalloc(aalloc * sizeof(T)));
200 Q_CHECK_PTR(ptr);
201 if (ptr) {
202 s = 0;
203 a = aalloc;
204
205 if (QTypeInfo<T>::isStatic) {
206 QT_TRY {
207 // copy all the old elements
208 while (s < copySize) {
209 new (ptr+s) T(*(oldPtr+s));
210 (oldPtr+s)->~T();
211 s++;
212 }
213 } QT_CATCH(...) {
214 // clean up all the old objects and then free the old ptr
215 int sClean = s;
216 while (sClean < osize)
217 (oldPtr+(sClean++))->~T();
218 if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
219 qFree(oldPtr);
220 QT_RETHROW;
221 }
222 } else {
223 qMemCopy(ptr, oldPtr, copySize * sizeof(T));
224 }
225 } else {
226 ptr = oldPtr;
227 return;
228 }
229 }
230 s = copySize;
231
232 if (QTypeInfo<T>::isComplex) {
233 // destroy remaining old objects
234 while (osize > asize)
235 (oldPtr+(--osize))->~T();
236 }
237
238 if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
239 qFree(oldPtr);
240
241 if (QTypeInfo<T>::isComplex) {
242 // call default constructor for new objects (which can throw)
243 while (s < asize)
244 new (ptr+(s++)) T;
245 } else {
246 s = asize;
247 }
248}
249
250QT_END_NAMESPACE
251
252QT_END_HEADER
253
254#endif // QVARLENGTHARRAY_H
Note: See TracBrowser for help on using the repository browser.