1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the core module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#ifndef QVARLENGTHARRAY_H
30
#define QVARLENGTHARRAY_H
32
#include <QtCore/qglobal.h>
34
template<class T, int Prealloc = 256>
38
inline explicit QVarLengthArray(int size = 0);
40
inline QVarLengthArray(const QVarLengthArray &other)
41
: a(Prealloc), s(0), ptr(reinterpret_cast<T *>(array))
43
append(other.constData(), other.size());
46
inline ~QVarLengthArray() {
47
if (QTypeInfo<T>::isComplex) {
52
if (ptr != reinterpret_cast<T *>(array))
55
inline QVarLengthArray &operator=(const QVarLengthArray &other)
59
append(other.constData(), other.size());
64
inline int size() const { return s; }
65
inline int count() const { return s; }
66
inline bool isEmpty() const { return (s == 0); }
67
inline void resize(int size);
68
inline void clear() { resize(0); }
70
inline int capacity() const { return a; }
71
inline void reserve(int size);
73
inline T &operator[](int idx) {
74
Q_ASSERT(idx >= 0 && idx < s);
77
inline const T &operator[](int idx) const {
78
Q_ASSERT(idx >= 0 && idx < s);
82
inline void append(const T &t) {
87
void append(const T *buf, int size);
89
inline T *data() { return ptr; }
90
inline const T *data() const { return ptr; }
91
inline const T * constData() const { return ptr; }
94
void realloc(int size, int alloc);
98
unsigned char array[Prealloc * sizeof(T)];
102
template <class T, int Prealloc>
103
Q_INLINE_TEMPLATE QVarLengthArray<T, Prealloc>::QVarLengthArray(int asize)
106
ptr = reinterpret_cast<T *>(qMalloc(s * sizeof(T)));
109
ptr = reinterpret_cast<T *>(array);
112
if (QTypeInfo<T>::isComplex) {
119
template <class T, int Prealloc>
120
Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::resize(int asize)
121
{ realloc(asize, qMax(asize, a)); }
123
template <class T, int Prealloc>
124
Q_INLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::reserve(int asize)
125
{ if (asize > a) realloc(s, asize); }
127
template <class T, int Prealloc>
128
Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::append(const T *abuf, int asize)
137
if (QTypeInfo<T>::isComplex) {
141
new (i++) T(*abuf++);
143
qMemCopy(&ptr[idx], abuf, asize * sizeof(T));
147
template <class T, int Prealloc>
148
Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::realloc(int asize, int aalloc)
150
Q_ASSERT(aalloc >= asize);
156
ptr = reinterpret_cast<T *>(qMalloc(aalloc * sizeof(T)));
159
if (QTypeInfo<T>::isStatic) {
161
T *j = oldPtr + osize;
167
qMemCopy(ptr, oldPtr, osize * sizeof(T));
171
if (QTypeInfo<T>::isComplex) {
173
T *i = oldPtr + osize;
174
T *j = oldPtr + asize;
185
if (oldPtr != reinterpret_cast<T *>(array) && oldPtr != ptr)
189
#endif // QVARLENGTHARRAY_H