/****************************************************************************
**
** 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 Q3CACHE_H
#define Q3CACHE_H

#include "qgcache.h"

template<class type>
class Q3Cache
#ifdef qdoc
        : public QPtrCollection
#else
        : public QGCache
#endif
{
public:
    Q3Cache(const Q3Cache<type> &c) : QGCache(c) {}
    Q3Cache(int maxCost=100, int size=17, bool caseSensitive=true)
        : QGCache(maxCost, size, StringKey, caseSensitive, false) {}
   ~Q3Cache()                                { clear(); }
    Q3Cache<type> &operator=(const Q3Cache<type> &c)
                        { return static_cast<Q3Cache<type> &>(QGCache::operator=(c)); }
    int          maxCost()   const                { return QGCache::maxCost(); }
    int          totalCost() const                { return QGCache::totalCost(); }
    void  setMaxCost(int m)                { QGCache::setMaxCost(m); }
    uint  count()     const                { return QGCache::count(); }
    uint  size()      const                { return QGCache::size(); }
    bool  isEmpty()   const                { return QGCache::count() == 0; }
    void  clear()                        { QGCache::clear(); }
    bool  insert(const QString &k, const type *d, int c=1, int p=0)
                        { return QGCache::insert_string(k,Item(d),c,p);}
    bool  remove(const QString &k)
                        { return QGCache::remove_string(k); }
    type *take(const QString &k)
                        { return static_cast<type *>(QGCache::take_string(k)); }
    type *find(const QString &k, bool ref=true) const
                        { return static_cast<type *>(QGCache::find_string(k,ref));}
    type *operator[](const QString &k) const
                        { return static_cast<type *>(QGCache::find_string(k));}
    void  statistics() const              { QGCache::statistics(); }
private:
    void  deleteItem(Item d);
};

#if !defined(Q_BROKEN_TEMPLATE_SPECIALIZATION)
template<> inline void Q3Cache<void>::deleteItem(QPtrCollection::Item)
{
}
#endif

template<class type> inline void Q3Cache<type>::deleteItem(QPtrCollection::Item d)
{
    if (del_item) delete static_cast<type *>(d);
}

template<class type>
class Q3CacheIterator : public QGCacheIterator
{
public:
    Q3CacheIterator(const Q3Cache<type> &c):QGCacheIterator(c) {}
    Q3CacheIterator(const Q3CacheIterator<type> &ci)
                                : QGCacheIterator(ci) {}
    Q3CacheIterator<type> &operator=(const Q3CacheIterator<type>&ci)
        { return static_cast<Q3CacheIterator<type> &>(QGCacheIterator::operator=(ci)); }
    uint  count()   const     { return QGCacheIterator::count(); }
    bool  isEmpty() const     { return QGCacheIterator::count() == 0; }
    bool  atFirst() const     { return QGCacheIterator::atFirst(); }
    bool  atLast()  const     { return QGCacheIterator::atLast(); }
    type *toFirst()              { return static_cast<type *>(QGCacheIterator::toFirst()); }
    type *toLast()              { return static_cast<type *>(QGCacheIterator::toLast()); }
    operator type *() const   { return static_cast<type *>(QGCacheIterator::get()); }
    type *current()   const   { return static_cast<type *>(QGCacheIterator::get()); }
    QString currentKey() const{ return QGCacheIterator::getKeyString(); }
    type *operator()()              { return static_cast<type *>(QGCacheIterator::operator()());}
    type *operator++()              { return static_cast<type *>(QGCacheIterator::operator++()); }
    type *operator+=(uint j)  { return static_cast<type *>(QGCacheIterator::operator+=(j));}
    type *operator--()              { return static_cast<type *>(QGCacheIterator::operator--()); }
    type *operator-=(uint j)  { return static_cast<type *>(QGCacheIterator::operator-=(j));}
};

#endif // Q3CACHE_H
