author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Mon, 04 Oct 2010 01:19:32 +0300 | |
changeset 37 | 758a864f9613 |
parent 33 | 3e2da88830cd |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
18
2f34d5167611
Revision: 201011
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 4 |
** All rights reserved. |
5 |
** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 |
** |
|
7 |
** This file is part of the QtCore module of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
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 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
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 |
||
49 |
QT_BEGIN_HEADER |
|
50 |
||
51 |
QT_BEGIN_NAMESPACE |
|
52 |
||
53 |
QT_MODULE(Core) |
|
54 |
||
55 |
template<class T, int Prealloc> |
|
56 |
class QPodList; |
|
57 |
||
58 |
// Prealloc = 256 by default, specified in qcontainerfwd.h |
|
59 |
template<class T, int Prealloc> |
|
60 |
class QVarLengthArray |
|
61 |
{ |
|
62 |
public: |
|
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 |
} |
|
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
110 |
inline const T &at(int idx) const { return operator[](idx); } |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
111 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
112 |
T value(int i) const; |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
113 |
T value(int i, const T &defaultValue) const; |
0 | 114 |
|
115 |
inline void append(const T &t) { |
|
116 |
if (s == a) // i.e. s != 0 |
|
117 |
realloc(s, s<<1); |
|
118 |
const int idx = s++; |
|
119 |
if (QTypeInfo<T>::isComplex) { |
|
120 |
new (ptr + idx) T(t); |
|
121 |
} else { |
|
122 |
ptr[idx] = t; |
|
123 |
} |
|
124 |
} |
|
125 |
void append(const T *buf, int size); |
|
126 |
||
127 |
inline T *data() { return ptr; } |
|
128 |
inline const T *data() const { return ptr; } |
|
129 |
inline const T * constData() const { return ptr; } |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
130 |
typedef int size_type; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
131 |
typedef T value_type; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
132 |
typedef value_type *pointer; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
133 |
typedef const value_type *const_pointer; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
134 |
typedef value_type &reference; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
135 |
typedef const value_type &const_reference; |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
136 |
typedef qptrdiff difference_type; |
0 | 137 |
|
138 |
private: |
|
139 |
friend class QPodList<T, Prealloc>; |
|
140 |
void realloc(int size, int alloc); |
|
141 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
142 |
int a; // capacity |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
143 |
int s; // size |
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
144 |
T *ptr; // data |
0 | 145 |
union { |
146 |
// ### Qt 5: Use 'Prealloc * sizeof(T)' as array size |
|
147 |
char array[sizeof(qint64) * (((Prealloc * sizeof(T)) / sizeof(qint64)) + 1)]; |
|
148 |
qint64 q_for_alignment_1; |
|
149 |
double q_for_alignment_2; |
|
150 |
}; |
|
151 |
}; |
|
152 |
||
153 |
template <class T, int Prealloc> |
|
154 |
Q_INLINE_TEMPLATE QVarLengthArray<T, Prealloc>::QVarLengthArray(int asize) |
|
155 |
: s(asize) { |
|
156 |
if (s > Prealloc) { |
|
157 |
ptr = reinterpret_cast<T *>(qMalloc(s * sizeof(T))); |
|
158 |
Q_CHECK_PTR(ptr); |
|
159 |
a = s; |
|
160 |
} else { |
|
161 |
ptr = reinterpret_cast<T *>(array); |
|
162 |
a = Prealloc; |
|
163 |
} |
|
164 |
if (QTypeInfo<T>::isComplex) { |
|
165 |
T *i = ptr + s; |
|
166 |
while (i != ptr) |
|
167 |
new (--i) T; |
|
168 |
} |
|
169 |
} |
|
170 |
||
171 |
template <class T, int Prealloc> |
|
172 |
Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::resize(int asize) |
|
173 |
{ realloc(asize, qMax(asize, a)); } |
|
174 |
||
175 |
template <class T, int Prealloc> |
|
176 |
Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::reserve(int asize) |
|
177 |
{ if (asize > a) realloc(s, asize); } |
|
178 |
||
179 |
template <class T, int Prealloc> |
|
180 |
Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, int increment) |
|
181 |
{ |
|
182 |
Q_ASSERT(abuf); |
|
183 |
if (increment <= 0) |
|
184 |
return; |
|
185 |
||
186 |
const int asize = s + increment; |
|
187 |
||
188 |
if (asize >= a) |
|
189 |
realloc(s, qMax(s*2, asize)); |
|
190 |
||
191 |
if (QTypeInfo<T>::isComplex) { |
|
192 |
// call constructor for new objects (which can throw) |
|
193 |
while (s < asize) |
|
194 |
new (ptr+(s++)) T(*abuf++); |
|
195 |
} else { |
|
196 |
qMemCopy(&ptr[s], abuf, increment * sizeof(T)); |
|
197 |
s = asize; |
|
198 |
} |
|
199 |
} |
|
200 |
||
201 |
template <class T, int Prealloc> |
|
202 |
Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int aalloc) |
|
203 |
{ |
|
204 |
Q_ASSERT(aalloc >= asize); |
|
205 |
T *oldPtr = ptr; |
|
206 |
int osize = s; |
|
207 |
||
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
208 |
const int copySize = qMin(asize, osize); |
0 | 209 |
if (aalloc != a) { |
210 |
ptr = reinterpret_cast<T *>(qMalloc(aalloc * sizeof(T))); |
|
211 |
Q_CHECK_PTR(ptr); |
|
212 |
if (ptr) { |
|
213 |
s = 0; |
|
214 |
a = aalloc; |
|
215 |
||
216 |
if (QTypeInfo<T>::isStatic) { |
|
217 |
QT_TRY { |
|
218 |
// copy all the old elements |
|
219 |
while (s < copySize) { |
|
220 |
new (ptr+s) T(*(oldPtr+s)); |
|
221 |
(oldPtr+s)->~T(); |
|
222 |
s++; |
|
223 |
} |
|
224 |
} QT_CATCH(...) { |
|
225 |
// clean up all the old objects and then free the old ptr |
|
226 |
int sClean = s; |
|
227 |
while (sClean < osize) |
|
228 |
(oldPtr+(sClean++))->~T(); |
|
229 |
if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr) |
|
230 |
qFree(oldPtr); |
|
231 |
QT_RETHROW; |
|
232 |
} |
|
233 |
} else { |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
234 |
qMemCopy(ptr, oldPtr, copySize * sizeof(T)); |
0 | 235 |
} |
236 |
} else { |
|
237 |
ptr = oldPtr; |
|
238 |
return; |
|
239 |
} |
|
240 |
} |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
241 |
s = copySize; |
0 | 242 |
|
243 |
if (QTypeInfo<T>::isComplex) { |
|
33
3e2da88830cd
Revision: 201031
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
30
diff
changeset
|
244 |
// destroy remaining old objects |
0 | 245 |
while (osize > asize) |
246 |
(oldPtr+(--osize))->~T(); |
|
247 |
} |
|
248 |
||
249 |
if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr) |
|
250 |
qFree(oldPtr); |
|
251 |
||
252 |
if (QTypeInfo<T>::isComplex) { |
|
253 |
// call default constructor for new objects (which can throw) |
|
254 |
while (s < asize) |
|
255 |
new (ptr+(s++)) T; |
|
256 |
} else { |
|
257 |
s = asize; |
|
258 |
} |
|
259 |
} |
|
260 |
||
30
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
261 |
template <class T, int Prealloc> |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
262 |
Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i) const |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
263 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
264 |
if (i < 0 || i >= size()) { |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
265 |
return T(); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
266 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
267 |
return at(i); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
268 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
269 |
template <class T, int Prealloc> |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
270 |
Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i, const T &defaultValue) const |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
271 |
{ |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
272 |
return (i < 0 || i >= size()) ? defaultValue : at(i); |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
273 |
} |
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
274 |
|
5dc02b23752f
Revision: 201025
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
29
diff
changeset
|
275 |
|
0 | 276 |
QT_END_NAMESPACE |
277 |
||
278 |
QT_END_HEADER |
|
279 |
||
280 |
#endif // QVARLENGTHARRAY_H |