/****************************************************************************
**
** Copyright (C) 1992-2004 Trolltech AS. All rights reserved.
**
** This file is part of the compatibility classes of the Qt Toolkit.
**
** Licensees holding valid Qt Preview licenses may use this file in
** accordance with the Qt Preview License Agreement provided with the
** Software.
**
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
** information about Qt Commercial License Agreements.
**
** Contact info@trolltech.com if any conditions of this licensing are
** not clear to you.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/

#ifndef QMEMARRAY_H
#define QMEMARRAY_H

#include "qgarray.h"


template<class type>
class QMemArray : public QGArray
{
public:
    typedef type* Iterator;
    typedef const type* ConstIterator;
    typedef type ValueType;

protected:
    QMemArray(int, int) : QGArray(0, 0) {}

public:
    QMemArray() {}
    QMemArray(int size) : QGArray(size*sizeof(type)) {}
    QMemArray(const QMemArray<type> &a) : QGArray(a) {}
    ~QMemArray() {}
    QMemArray<type> &operator=(const QMemArray<type> &a)
                                { return static_cast<QMemArray<type> &>(QGArray::assign(a)); }
    type *data()    const        { return static_cast<type *>(QGArray::data()); }
    uint  nrefs()   const        { return QGArray::nrefs(); }
    uint  size()    const        { return QGArray::size()/sizeof(type); }
    uint  count()   const        { return size(); }
    bool  isEmpty() const        { return QGArray::size() == 0; }
    bool  isNull()  const        { return QGArray::data() == 0; }
    bool  resize(uint size)        { return QGArray::resize(size*sizeof(type)); }
    bool  resize(uint size, Optimization optim) { return QGArray::resize(size*sizeof(type), optim); }
    bool  truncate(uint pos)        { return QGArray::resize(pos*sizeof(type)); }
    bool  fill(const type &d, int size = -1)
        { return QGArray::fill(reinterpret_cast<char*>(&d),size,sizeof(type)); }
    void  detach()                { QGArray::detach(); }
    QMemArray<type>   copy() const
        { QMemArray<type> tmp; return tmp.duplicate(*this); }
    QMemArray<type>& assign(const QMemArray<type>& a)
        { return static_cast<QMemArray<type> &>(QGArray::assign(a)); }
    QMemArray<type>& assign(const type *a, uint n)
        { return static_cast<QMemArray<type>&>(QGArray::assign(reinterpret_cast<char*>(a),n*sizeof(type))); }
    QMemArray<type>& duplicate(const QMemArray<type>& a)
        { return static_cast<QMemArray<type> &>(QGArray::duplicate(a)); }
    QMemArray<type>& duplicate(const type *a, uint n)
        { return static_cast<QMemArray<type> &>(QGArray::duplicate(reinterpret_cast<char*>(a),n*sizeof(type))); }
    QMemArray<type>& setRawData(const type *a, uint n)
        { return static_cast<QMemArray<type> &>(QGArray::setRawData(reinterpret_cast<char*>(a), n*sizeof(type))); }
    void resetRawData(const type *a, uint n)
        { QGArray::resetRawData(reinterpret_cast<char*>(a),n*sizeof(type)); }
    int         find(const type &d, uint i=0) const
        { return QGArray::find(reinterpret_cast<char*>(&d),i,sizeof(type)); }
    int         contains(const type &d) const
        { return QGArray::contains(reinterpret_cast<char*>(&d),sizeof(type)); }
    void sort() { QGArray::sort(sizeof(type)); }
    int  bsearch(const type &d) const
        { return QGArray::bsearch(reinterpret_cast<const char*>(&d),sizeof(type)); }
    type& operator[](int i) const
        { return *static_cast<type *>(QGArray::at(i*sizeof(type))); }
    type& at(uint i) const
        { return *static_cast<type *>(QGArray::at(i*sizeof(type))); }
    operator const type*() const { return static_cast<const type *>(QGArray::data()); }
    bool operator==(const QMemArray<type> &a) const { return isEqual(a); }
    bool operator!=(const QMemArray<type> &a) const { return !isEqual(a); }
    Iterator begin() { return data(); }
    Iterator end() { return data() + size(); }
    ConstIterator begin() const { return data(); }
    ConstIterator end() const { return data() + size(); }
};

#endif // QARRAY_H
