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

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

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

File size: 8.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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 inline const T &at(int idx) const { return operator[](idx); }
111
112 T value(int i) const;
113 T value(int i, const T &defaultValue) const;
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; }
130 typedef int size_type;
131 typedef T value_type;
132 typedef value_type *pointer;
133 typedef const value_type *const_pointer;
134 typedef value_type &reference;
135 typedef const value_type &const_reference;
136 typedef qptrdiff difference_type;
137
138private:
139 friend class QPodList<T, Prealloc>;
140 void realloc(int size, int alloc);
141
142 int a; // capacity
143 int s; // size
144 T *ptr; // data
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
153template <class T, int Prealloc>
154Q_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
171template <class T, int Prealloc>
172Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::resize(int asize)
173{ realloc(asize, qMax(asize, a)); }
174
175template <class T, int Prealloc>
176Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::reserve(int asize)
177{ if (asize > a) realloc(s, asize); }
178
179template <class T, int Prealloc>
180Q_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
201template <class T, int Prealloc>
202Q_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
208 const int copySize = qMin(asize, osize);
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 {
234 qMemCopy(ptr, oldPtr, copySize * sizeof(T));
235 }
236 } else {
237 ptr = oldPtr;
238 return;
239 }
240 }
241 s = copySize;
242
243 if (QTypeInfo<T>::isComplex) {
244 // destroy remaining old objects
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
261template <class T, int Prealloc>
262Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i) const
263{
264 if (i < 0 || i >= size()) {
265 return T();
266 }
267 return at(i);
268}
269template <class T, int Prealloc>
270Q_OUTOFLINE_TEMPLATE T QVarLengthArray<T, Prealloc>::value(int i, const T &defaultValue) const
271{
272 return (i < 0 || i >= size()) ? defaultValue : at(i);
273}
274
275
276QT_END_NAMESPACE
277
278QT_END_HEADER
279
280#endif // QVARLENGTHARRAY_H
Note: See TracBrowser for help on using the repository browser.