Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qcache.qdoc
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4
/*!
5
\class QCache
6
\inmodule QtCore
7
\brief The QCache class is a template class that provides a cache.
8
9
\ingroup tools
10
\ingroup shared
11
12
\reentrant
13
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:
18
19
\snippet code/doc_src_qcache.cpp 0
20
21
Here's how to insert an object in the cache:
22
23
\snippet code/doc_src_qcache.cpp 1
24
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.
35
36
By default, QCache's maxCost() is 100. You can specify a
37
different value in the QCache constructor:
38
39
\snippet code/doc_src_qcache.cpp 2
40
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.
45
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.
50
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
54
take().
55
56
\sa QPixmapCache, QHash, QMap
57
*/
58
59
/*! \fn template <class Key, class T> QCache<Key, T>::QCache(qsizetype maxCost = 100)
60
61
Constructs a cache whose contents will never have a total cost
62
greater than \a maxCost.
63
*/
64
65
/*! \fn template <class Key, class T> QCache<Key, T>::~QCache()
66
67
Destroys the cache. Deletes all the objects in the cache.
68
*/
69
70
/*! \fn template <class Key, class T> qsizetype QCache<Key, T>::maxCost() const
71
72
Returns the maximum allowed total cost of the cache.
73
74
\sa setMaxCost(), totalCost()
75
*/
76
77
/*! \fn template <class Key, class T> void QCache<Key, T>::setMaxCost(qsizetype cost)
78
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
81
deleted immediately.
82
83
\sa maxCost(), totalCost()
84
*/
85
86
/*! \fn template <class Key, class T> qsizetype QCache<Key, T>::totalCost() const
87
88
Returns the total cost of the objects in the cache.
89
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().
95
96
\sa setMaxCost()
97
*/
98
99
/*! \fn template <class Key, class T> qsizetype QCache<Key, T>::size() const
100
101
Returns the number of objects in the cache.
102
103
\sa isEmpty()
104
*/
105
106
/*! \fn template <class Key, class T> qsizetype QCache<Key, T>::count() const
107
108
Same as size().
109
*/
110
111
/*! \fn template <class Key, class T> bool QCache<Key, T>::isEmpty() const
112
113
Returns \c true if the cache contains no objects; otherwise
114
returns \c false.
115
116
\sa size()
117
*/
118
119
/*! \fn template <class Key, class T> QList<Key> QCache<Key, T>::keys() const
120
121
Returns a list of the keys in the cache.
122
*/
123
124
/*! \fn template <class Key, class T> void QCache<Key, T>::clear();
125
126
Deletes all the objects in the cache.
127
128
\sa remove(), take()
129
*/
130
131
132
/*! \fn template <class Key, class T> bool QCache<Key, T>::insert(const Key &key, T *object, qsizetype cost = 1)
133
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.
137
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.
141
142
The function returns \c true if the object was inserted into the
143
cache; otherwise it returns \c false.
144
145
\sa take(), remove()
146
*/
147
148
/*! \fn template <class Key, class T> T *QCache<Key, T>::object(const Key &key) const
149
150
Returns the object associated with key \a key, or \nullptr if the key does
151
not exist in the cache.
152
153
\warning The returned object is owned by QCache and may be
154
deleted at any time.
155
156
\sa take(), remove()
157
*/
158
159
/*! \fn template <class Key, class T> bool QCache<Key, T>::contains(const Key &key) const
160
161
Returns \c true if the cache contains an object associated with key \a
162
key; otherwise returns \c false.
163
164
\sa take(), remove()
165
*/
166
167
/*! \fn template <class Key, class T> T *QCache<Key, T>::operator[](const Key &key) const
168
169
Returns the object associated with key \a key, or \nullptr if the key does
170
not exist in the cache.
171
172
This is the same as object().
173
174
\warning The returned object is owned by QCache and may be
175
deleted at any time.
176
*/
177
178
/*! \fn template <class Key, class T> bool QCache<Key, T>::remove(const Key &key)
179
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.
182
183
\sa take(), clear()
184
*/
185
186
/*! \fn template <class Key, class T> T *QCache<Key, T>::take(const Key &key)
187
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.
191
192
The ownership of the returned object is passed to the caller.
193
194
\sa remove()
195
*/
qtbase
src
corelib
tools
qcache.qdoc
Generated on Sun Mar 9 2025 01:10:25 for Qt by
1.13.2