1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
7 \brief The QCache class is a template class that provides a cache.
14 QCache<Key, T> defines a cache that stores objects of type T
15 associated with keys of type Key. For example, here's the
16 definition of a cache that stores objects of type Employee
17 associated with an integer key:
19 \snippet code/doc_src_qcache.cpp 0
21 Here's how to insert an object in the cache:
23 \snippet code/doc_src_qcache.cpp 1
25 The advantage of using QCache over some other key-based data
26 structure (such as QMap or QHash) is that QCache automatically
27 takes ownership of the objects that are inserted into the cache and
28 deletes them to make room for new objects, if necessary. When
29 inserting an object into the cache, you can specify a \e{cost},
30 which should bear some approximate relationship to the amount of
31 memory taken by the object. When the sum of all objects' costs
32 (totalCost()) exceeds the cache's limit (maxCost()), QCache starts
33 deleting objects in the cache to keep under the limit, starting with
34 less recently accessed objects.
36 By default, QCache's maxCost() is 100. You can specify a
37 different value in the QCache constructor:
39 \snippet code/doc_src_qcache.cpp 2
41 Each time you call insert(), you can specify a cost as third
42 argument (after the key and a pointer to the object to insert).
43 After the call, the inserted object is owned by the QCache, which
44 may delete it at any time to make room for other objects.
46 To look up objects in the cache, use object() or
47 operator[](). This function looks up an object by its key, and
48 returns either a pointer to the cached object (which is owned by
49 the cache) or \nullptr.
51 If you want to remove an object from the cache for a particular key,
52 call remove(). This will also delete the object. If you want to
53 remove an object from the cache without the QCache deleting it, use
56 \sa QPixmapCache, QHash, QMap
59/*! \fn template <class Key, class T> QCache<Key, T>::QCache(qsizetype maxCost = 100)
61 Constructs a cache whose contents will never have a total cost
62 greater than \a maxCost.
65/*! \fn template <class Key, class T> QCache<Key, T>::~QCache()
67 Destroys the cache. Deletes all the objects in the cache.
70/*! \fn template <class Key, class T> qsizetype QCache<Key, T>::maxCost() const
72 Returns the maximum allowed total cost of the cache.
74 \sa setMaxCost(), totalCost()
77/*! \fn template <class Key, class T> void QCache<Key, T>::setMaxCost(qsizetype cost)
79 Sets the maximum allowed total cost of the cache to \a cost. If
80 the current total cost is greater than \a cost, some objects are
83 \sa maxCost(), totalCost()
86/*! \fn template <class Key, class T> qsizetype QCache<Key, T>::totalCost() const
88 Returns the total cost of the objects in the cache.
90 This value is normally below maxCost(), but QCache makes an
91 exception for Qt's \l{implicitly shared} classes. If a cached
92 object shares its internal data with another instance, QCache may
93 keep the object lying around, possibly contributing to making
94 totalCost() larger than maxCost().
99/*! \fn template <class Key, class T> qsizetype QCache<Key, T>::size() const
101 Returns the number of objects in the cache.
106/*! \fn template <class Key, class T> qsizetype QCache<Key, T>::count() const
111/*! \fn template <class Key, class T> bool QCache<Key, T>::isEmpty() const
113 Returns \c true if the cache contains no objects; otherwise
119/*! \fn template <class Key, class T> QList<Key> QCache<Key, T>::keys() const
121 Returns a list of the keys in the cache.
124/*! \fn template <class Key, class T> void QCache<Key, T>::clear();
126 Deletes all the objects in the cache.
132/*! \fn template <class Key, class T> bool QCache<Key, T>::insert(const Key &key, T *object, qsizetype cost = 1)
134 Inserts \a object into the cache with key \a key and
135 associated cost \a cost. Any object with the same key already in
136 the cache will be removed.
138 After this call, \a object is owned by the QCache and may be
139 deleted at any time. In particular, if \a cost is greater than
140 maxCost(), the object will be deleted immediately.
142 The function returns \c true if the object was inserted into the
143 cache; otherwise it returns \c false.
148/*! \fn template <class Key, class T> T *QCache<Key, T>::object(const Key &key) const
150 Returns the object associated with key \a key, or \nullptr if the key does
151 not exist in the cache.
153 \warning The returned object is owned by QCache and may be
159/*! \fn template <class Key, class T> bool QCache<Key, T>::contains(const Key &key) const
161 Returns \c true if the cache contains an object associated with key \a
162 key; otherwise returns \c false.
167/*! \fn template <class Key, class T> T *QCache<Key, T>::operator[](const Key &key) const
169 Returns the object associated with key \a key, or \nullptr if the key does
170 not exist in the cache.
172 This is the same as object().
174 \warning The returned object is owned by QCache and may be
178/*! \fn template <class Key, class T> bool QCache<Key, T>::remove(const Key &key)
180 Deletes the object associated with key \a key. Returns \c true if the
181 object was found in the cache; otherwise returns \c false.
186/*! \fn template <class Key, class T> T *QCache<Key, T>::take(const Key &key)
188 Takes the object associated with key \a key out of the cache
189 without deleting it. Returns a pointer to the object taken out, or
190 0 if the key does not exist in the cache.
192 The ownership of the returned object is passed to the caller.