6.0.0-rc4Source

QCache Class

template <typename Key, typename T> class QCache

The QCache class is a template class that provides a cache. More…

Header#include <QCache>
CMaketarget_link_libraries(app PRIVATE Nt::Core)
ModuleNt Core C++ Classes

List of all members, including inherited members

Public Functions

QCache(qsizetype maxCost = 100)
~QCache()
qsizetypemaxCost() const
voidsetMaxCost(qsizetype cost)
qsizetypetotalCost() const
qsizetypesize() const
qsizetypecount() const
boolisEmpty() const
boolcontains(const Key &key) const
QList<Key>keys() const
boolinsert(const Key &key, T *object, qsizetype cost = 1)
T *object(const Key &key) const
T *operator[](const Key &key) const
boolremove(const Key &key)
T *take(const Key &key)
voidclear()

Detailed Description

QCache<Key, T> stores objects of type T* under keys of type Key and owns them. Each object has a cost, and the cache keeps the total cost within maxCost() by deleting the least recently used objects. Looking up an object with object() marks it as the most recently used.

QCache<QString, QImage> cache(10000);

QImage *loadImage(const QString &name)
{
    if (QImage *image = cache.object(name))
        return image;
    auto *image = new QImage(name);
    cache.insert(name, image, image->sizeInBytes());
    return image;
}

The key type needs the requirements of QHash keys. A QCache cannot be copied.

See also QContiguousCache and QHash.

Differences from Qt

There is no emplace(). The cache is not copyable, and objects are reached only through object(), operator[]() and take().

Member Function Documentation

[explicit] QCache::QCache(qsizetype maxCost = 100)

Constructs a cache whose maximum total cost is maxCost.

See also setMaxCost().

QCache::~QCache()

Destroys the cache and deletes all the objects it contains.

void QCache::clear()

Deletes all objects in the cache.

See also remove().

bool QCache::contains(const Key &key) const

Returns true if the cache contains an object associated with key; otherwise returns false. The object is not marked as recently used.

See also object().

qsizetype QCache::count() const

Returns the number of objects in the cache; the same as size().

bool QCache::insert(const Key &key, T *object, qsizetype cost = 1)

Inserts object into the cache under key with the given cost, replacing any object already stored under that key. The cache takes ownership of object. If the total cost would exceed maxCost(), the least recently used objects are deleted to make room.

Returns true if the object was inserted. If cost is greater than maxCost(), object is deleted immediately and the function returns false.

See also object(), remove() and take().

bool QCache::isEmpty() const

Returns true if the cache contains no objects; otherwise returns false.

See also size().

QList<Key> QCache::keys() const

Returns the keys of all objects in the cache, from the most recently used to the least recently used.

See also object().

qsizetype QCache::maxCost() const

Returns the maximum allowed total cost of the cache.

See also setMaxCost() and totalCost().

T * QCache::object(const Key &key) const

Returns the object associated with key and marks it as the most recently used, or nullptr if there is no such object. The cache keeps ownership of the returned object.

See also insert(), contains() and take().

T * QCache::operator[](const Key &key) const

Returns the object associated with key, or nullptr if there is none; the same as object().

bool QCache::remove(const Key &key)

Deletes the object associated with key. Returns true if an object was removed; otherwise returns false.

See also take() and clear().

void QCache::setMaxCost(qsizetype cost)

Sets the maximum allowed total cost of the cache to cost. If the current total cost is greater, the least recently used objects are deleted until the total cost fits.

See also maxCost() and totalCost().

qsizetype QCache::size() const

Returns the number of objects in the cache.

See also isEmpty() and count().

T * QCache::take(const Key &key)

Removes the object associated with key from the cache and returns it, or returns nullptr if there is none. The caller becomes the owner of the returned object.

See also remove() and insert().

qsizetype QCache::totalCost() const

Returns the total cost of the objects currently in the cache. This is never more than maxCost().

See also maxCost() and insert().