2015-12-10 05:06:13 +02:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
|
|
|
** Copyright (C) 2015 The Qt Company Ltd.
|
2019-12-29 23:21:34 +00:00
|
|
|
** Copyright (C) 2016-2020 Ivailo Monev
|
2015-12-10 05:06:13 +02:00
|
|
|
**
|
2019-07-02 18:13:44 +00:00
|
|
|
** This file is part of the QtCore module of the Katie Toolkit.
|
2015-12-10 05:06:13 +02:00
|
|
|
**
|
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2019-12-29 23:21:34 +00:00
|
|
|
**
|
2015-12-10 05:06:13 +02:00
|
|
|
** GNU Lesser General Public License Usage
|
2019-12-29 23:21:34 +00:00
|
|
|
** This file may be used under the terms of the GNU Lesser
|
|
|
|
** General Public License version 2.1 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
|
|
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
2015-12-10 05:06:13 +02:00
|
|
|
**
|
|
|
|
** As a special exception, The Qt Company gives you certain additional
|
|
|
|
** rights. These rights are described in The Qt Company LGPL Exception
|
|
|
|
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
|
|
**
|
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3.0 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU General Public License version 3.0 requirements will be
|
|
|
|
** met: http://www.gnu.org/copyleft/gpl.html.
|
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef QCACHE_H
|
|
|
|
#define QCACHE_H
|
|
|
|
|
|
|
|
#include <QtCore/qhash.h>
|
|
|
|
|
|
|
|
QT_BEGIN_HEADER
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
template <class Key, class T>
|
2019-12-19 19:16:29 +00:00
|
|
|
class Q_CORE_EXPORT QCache
|
2015-12-10 05:06:13 +02:00
|
|
|
{
|
2019-07-29 17:42:30 +00:00
|
|
|
QHash<Key, T*> hash;
|
|
|
|
int mx;
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
Q_DISABLE_COPY(QCache)
|
|
|
|
|
|
|
|
public:
|
|
|
|
inline explicit QCache(int maxCost = 100);
|
|
|
|
inline ~QCache() { clear(); }
|
|
|
|
|
|
|
|
inline int maxCost() const { return mx; }
|
2019-07-29 17:42:30 +00:00
|
|
|
inline void setMaxCost(int m);
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
inline int size() const { return hash.size(); }
|
2019-07-30 12:06:08 +00:00
|
|
|
inline int count() const { return hash.count(); }
|
2015-12-10 05:06:13 +02:00
|
|
|
inline bool isEmpty() const { return hash.isEmpty(); }
|
|
|
|
inline QList<Key> keys() const { return hash.keys(); }
|
2019-07-29 17:42:30 +00:00
|
|
|
inline void clear() { hash.clear(); }
|
2015-12-10 05:06:13 +02:00
|
|
|
|
2019-07-29 17:42:30 +00:00
|
|
|
inline bool insert(const Key &key, T *object, int cost = 1);
|
|
|
|
inline T *object(const Key &key) const { return hash.value(key, Q_NULLPTR); }
|
2015-12-10 05:06:13 +02:00
|
|
|
inline bool contains(const Key &key) const { return hash.contains(key); }
|
2019-11-30 03:02:38 +00:00
|
|
|
inline T *operator[](const Key &key) const { return object(key); }
|
2015-12-10 05:06:13 +02:00
|
|
|
|
2019-11-30 03:02:38 +00:00
|
|
|
inline bool remove(const Key &key) { return hash.remove(key) != 0; }
|
|
|
|
inline T *take(const Key &key) { return hash.take(key); }
|
2015-12-10 05:06:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
inline QCache<Key, T>::QCache(int amaxCost)
|
2019-07-29 17:42:30 +00:00
|
|
|
: mx(amaxCost)
|
2019-11-30 03:02:38 +00:00
|
|
|
{
|
|
|
|
hash.reserve(amaxCost);
|
|
|
|
}
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
template <class Key, class T>
|
|
|
|
inline void QCache<Key,T>::setMaxCost(int m)
|
2019-07-29 17:42:30 +00:00
|
|
|
{
|
|
|
|
mx = m;
|
|
|
|
hash.clear();
|
|
|
|
hash.reserve(m);
|
|
|
|
}
|
2015-12-10 05:06:13 +02:00
|
|
|
|
|
|
|
template <class Key, class T>
|
2019-07-29 17:42:30 +00:00
|
|
|
inline bool QCache<Key,T>::insert(const Key &akey, T *aobject, int acost)
|
2015-12-10 05:06:13 +02:00
|
|
|
{
|
|
|
|
if (acost > mx) {
|
|
|
|
delete aobject;
|
|
|
|
return false;
|
2019-07-29 17:42:30 +00:00
|
|
|
} else if (hash.size() >= mx) {
|
|
|
|
hash.erase(hash.begin());
|
2015-12-10 05:06:13 +02:00
|
|
|
}
|
2019-07-29 17:42:30 +00:00
|
|
|
hash.insert(akey, aobject);
|
2015-12-10 05:06:13 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
QT_END_HEADER
|
|
|
|
|
|
|
|
#endif // QCACHE_H
|