source: trunk/src/qt3support/tools/q3memarray.h@ 352

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

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

File size: 5.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 Qt3Support 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 Q3MEMARRAY_H
43#define Q3MEMARRAY_H
44
45#include <Qt3Support/q3garray.h>
46#include <QtCore/qvector.h>
47
48QT_BEGIN_HEADER
49
50QT_BEGIN_NAMESPACE
51
52QT_MODULE(Qt3SupportLight)
53
54template<class type>
55class Q3MemArray : public Q3GArray
56{
57public:
58 typedef type* Iterator;
59 typedef const type* ConstIterator;
60 typedef type ValueType;
61
62protected:
63 Q3MemArray(int, int) : Q3GArray(0, 0) {}
64
65public:
66 Q3MemArray() {}
67 Q3MemArray(int size) : Q3GArray(size*sizeof(type)) {}
68 Q3MemArray(const Q3MemArray<type> &a) : Q3GArray(a) {}
69 Q3MemArray(const QVector<type> &vector);
70 ~Q3MemArray() {}
71 Q3MemArray<type> &operator=(const Q3MemArray<type> &a)
72 { return (Q3MemArray<type>&)Q3GArray::assign(a); }
73 type *data() const { return (type *)Q3GArray::data(); }
74 uint nrefs() const { return Q3GArray::nrefs(); }
75 uint size() const { return Q3GArray::size()/sizeof(type); }
76 uint count() const { return size(); }
77 bool isEmpty() const { return Q3GArray::size() == 0; }
78 bool isNull() const { return Q3GArray::data() == 0; }
79 bool resize(uint size) { return Q3GArray::resize(size*sizeof(type)); }
80 bool resize(uint size, Optimization optim) { return Q3GArray::resize(size*sizeof(type), optim); }
81 bool truncate(uint pos) { return Q3GArray::resize(pos*sizeof(type)); }
82 bool fill(const type &d, int size = -1)
83 { return Q3GArray::fill((char*)&d,size,sizeof(type)); }
84 void detach() { Q3GArray::detach(); }
85 Q3MemArray<type> copy() const
86 { Q3MemArray<type> tmp; return tmp.duplicate(*this); }
87 Q3MemArray<type>& assign(const Q3MemArray<type>& a)
88 { return (Q3MemArray<type>&)Q3GArray::assign(a); }
89 Q3MemArray<type>& assign(const type *a, uint n)
90 { return (Q3MemArray<type>&)Q3GArray::assign((char*)a,n*sizeof(type)); }
91 Q3MemArray<type>& duplicate(const Q3MemArray<type>& a)
92 { return (Q3MemArray<type>&)Q3GArray::duplicate(a); }
93 Q3MemArray<type>& duplicate(const type *a, uint n)
94 { return (Q3MemArray<type>&)Q3GArray::duplicate((char*)a,n*sizeof(type)); }
95 Q3MemArray<type>& setRawData(const type *a, uint n)
96 { return (Q3MemArray<type>&)Q3GArray::setRawData((char*)a,
97 n*sizeof(type)); }
98 void resetRawData(const type *a, uint n)
99 { Q3GArray::resetRawData((char*)a,n*sizeof(type)); }
100 int find(const type &d, uint i=0) const
101 { return Q3GArray::find((char*)&d,i,sizeof(type)); }
102 int contains(const type &d) const
103 { return Q3GArray::contains((char*)&d,sizeof(type)); }
104 void sort() { Q3GArray::sort(sizeof(type)); }
105 int bsearch(const type &d) const
106 { return Q3GArray::bsearch((const char*)&d,sizeof(type)); }
107 // ### Qt 4.0: maybe provide uint overload as work-around for MSVC bug
108 type& operator[](int i) const
109 { return (type &)(*(type *)Q3GArray::at(i*sizeof(type))); }
110 type& at(uint i) const
111 { return (type &)(*(type *)Q3GArray::at(i*sizeof(type))); }
112 operator const type*() const { return (const type *)Q3GArray::data(); }
113 bool operator==(const Q3MemArray<type> &a) const { return isEqual(a); }
114 bool operator!=(const Q3MemArray<type> &a) const { return !isEqual(a); }
115 Iterator begin() { return data(); }
116 Iterator end() { return data() + size(); }
117 ConstIterator begin() const { return data(); }
118 ConstIterator end() const { return data() + size(); }
119
120 operator QVector<type>() const;
121};
122
123template<class type>
124Q_OUTOFLINE_TEMPLATE Q3MemArray<type>::Q3MemArray(const QVector<type> &vector)
125 : Q3GArray(vector.size()*sizeof(type))
126{
127 for (int i = 0; i < vector.size(); ++i)
128 at(i) = vector.at(i);
129}
130
131template<class type>
132Q_OUTOFLINE_TEMPLATE Q3MemArray<type>::operator QVector<type>() const
133{
134 QVector<type> vector;
135 for (int i = 0; i < size(); ++i)
136 vector.append(at(i));
137 return vector;
138}
139
140QT_END_NAMESPACE
141
142QT_END_HEADER
143
144#endif // Q3MEMARRAY_H
Note: See TracBrowser for help on using the repository browser.