src/qt3support/tools/q3memarray.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt3Support 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 Q3MEMARRAY_H
       
    43 #define Q3MEMARRAY_H
       
    44 
       
    45 #include <Qt3Support/q3garray.h>
       
    46 #include <QtCore/qvector.h>
       
    47 
       
    48 QT_BEGIN_HEADER
       
    49 
       
    50 QT_BEGIN_NAMESPACE
       
    51 
       
    52 QT_MODULE(Qt3SupportLight)
       
    53 
       
    54 template<class type>
       
    55 class Q3MemArray : public Q3GArray
       
    56 {
       
    57 public:
       
    58     typedef type* Iterator;
       
    59     typedef const type* ConstIterator;
       
    60     typedef type ValueType;
       
    61 
       
    62 protected:
       
    63     Q3MemArray(int, int) : Q3GArray(0, 0) {}
       
    64 
       
    65 public:
       
    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 
       
   123 template<class type>
       
   124 Q_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 
       
   131 template<class type>
       
   132 Q_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 
       
   140 QT_END_NAMESPACE
       
   141 
       
   142 QT_END_HEADER
       
   143 
       
   144 #endif // Q3MEMARRAY_H