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

Last change on this file since 175 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 6.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** 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 are unsure which license is appropriate for your use, please
37** contact the sales department 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
55// Prealloc = 256 by default, specified in qcontainerfwd.h
56template<class T, int Prealloc>
57class QVarLengthArray
58{
59public:
60 inline explicit QVarLengthArray(int size = 0);
61
62 inline QVarLengthArray(const QVarLengthArray<T, Prealloc> &other)
63 : a(Prealloc), s(0), ptr(reinterpret_cast<T *>(array))
64 {
65 append(other.constData(), other.size());
66 }
67
68 inline ~QVarLengthArray() {
69 if (QTypeInfo<T>::isComplex) {
70 T *i = ptr + s;
71 while (i-- != ptr)
72 i->~T();
73 }
74 if (ptr != reinterpret_cast<T *>(array))
75 qFree(ptr);
76 }
77 inline QVarLengthArray<T, Prealloc> &operator=(const QVarLengthArray<T, Prealloc> &other)
78 {
79 if (this != &other) {
80 clear();
81 append(other.constData(), other.size());
82 }
83 return *this;
84 }
85
86 inline void removeLast() {
87 Q_ASSERT(s > 0);
88 realloc(s - 1, a);
89 }
90 inline int size() const { return s; }
91 inline int count() const { return s; }
92 inline bool isEmpty() const { return (s == 0); }
93 inline void resize(int size);
94 inline void clear() { resize(0); }
95
96 inline int capacity() const { return a; }
97 inline void reserve(int size);
98
99 inline T &operator[](int idx) {
100 Q_ASSERT(idx >= 0 && idx < s);
101 return ptr[idx];
102 }
103 inline const T &operator[](int idx) const {
104 Q_ASSERT(idx >= 0 && idx < s);
105 return ptr[idx];
106 }
107
108 inline void append(const T &t) {
109 if (s == a) // i.e. s != 0
110 realloc(s, s<<1);
111 const int idx = s++;
112 if (QTypeInfo<T>::isComplex) {
113 new (ptr + idx) T(t);
114 } else {
115 ptr[idx] = t;
116 }
117 }
118 void append(const T *buf, int size);
119
120 inline T *data() { return ptr; }
121 inline const T *data() const { return ptr; }
122 inline const T * constData() const { return ptr; }
123
124private:
125 void realloc(int size, int alloc);
126
127 int a;
128 int s;
129 T *ptr;
130 union {
131 // ### Qt 5: Use 'Prealloc * sizeof(T)' as array size
132 char array[sizeof(qint64) * (((Prealloc * sizeof(T)) / sizeof(qint64)) + 1)];
133 qint64 q_for_alignment_1;
134 double q_for_alignment_2;
135 };
136};
137
138template <class T, int Prealloc>
139Q_INLINE_TEMPLATE QVarLengthArray<T, Prealloc>::QVarLengthArray(int asize)
140 : s(asize) {
141 if (s > Prealloc) {
142 ptr = reinterpret_cast<T *>(qMalloc(s * sizeof(T)));
143 a = s;
144 } else {
145 ptr = reinterpret_cast<T *>(array);
146 a = Prealloc;
147 }
148 if (QTypeInfo<T>::isComplex) {
149 T *i = ptr + s;
150 while (i != ptr)
151 new (--i) T;
152 }
153}
154
155template <class T, int Prealloc>
156Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::resize(int asize)
157{ realloc(asize, qMax(asize, a)); }
158
159template <class T, int Prealloc>
160Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::reserve(int asize)
161{ if (asize > a) realloc(s, asize); }
162
163template <class T, int Prealloc>
164Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, int asize)
165{
166 Q_ASSERT(abuf);
167 if (asize <= 0)
168 return;
169
170 const int idx = s;
171 const int news = s + asize;
172 if (news >= a)
173 realloc(s, qMax(s<<1, news));
174 s = news;
175
176 if (QTypeInfo<T>::isComplex) {
177 T *i = ptr + idx;
178 T *j = i + asize;
179 while (i < j)
180 new (i++) T(*abuf++);
181 } else {
182 qMemCopy(&ptr[idx], abuf, asize * sizeof(T));
183 }
184}
185
186template <class T, int Prealloc>
187Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int aalloc)
188{
189 Q_ASSERT(aalloc >= asize);
190 T *oldPtr = ptr;
191 int osize = s;
192 s = asize;
193
194 if (aalloc != a) {
195 ptr = reinterpret_cast<T *>(qMalloc(aalloc * sizeof(T)));
196 if (ptr) {
197 a = aalloc;
198
199 if (QTypeInfo<T>::isStatic) {
200 T *i = ptr + osize;
201 T *j = oldPtr + osize;
202 while (i != ptr) {
203 new (--i) T(*--j);
204 j->~T();
205 }
206 } else {
207 qMemCopy(ptr, oldPtr, osize * sizeof(T));
208 }
209 } else {
210 ptr = oldPtr;
211 s = 0;
212 asize = 0;
213 }
214 }
215
216 if (QTypeInfo<T>::isComplex) {
217 if (asize < osize) {
218 T *i = oldPtr + osize;
219 T *j = oldPtr + asize;
220 while (i-- != j)
221 i->~T();
222 } else {
223 T *i = ptr + asize;
224 T *j = ptr + osize;
225 while (i != j)
226 new (--i) T;
227 }
228 }
229
230 if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
231 qFree(oldPtr);
232}
233
234QT_END_NAMESPACE
235
236QT_END_HEADER
237
238#endif // QVARLENGTHARRAY_H
Note: See TracBrowser for help on using the repository browser.