Warning: That file was not part of the compilation database. It may have many parsing errors.

1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the documentation of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:FDL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Free Documentation License Usage
18** Alternatively, this file may be used under the terms of the GNU Free
19** Documentation License version 1.3 as published by the Free Software
20** Foundation and appearing in the file included in the packaging of
21** this file. Please review the following information to ensure
22** the GNU Free Documentation License version 1.3 requirements
23** will be met: https://www.gnu.org/licenses/fdl-1.3.html.
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/*!
29 \class QCache
30 \inmodule QtCore
31 \brief The QCache class is a template class that provides a cache.
32
33 \ingroup tools
34 \ingroup shared
35
36 \reentrant
37
38 QCache\<Key, T\> defines a cache that stores objects of type T
39 associated with keys of type Key. For example, here's the
40 definition of a cache that stores objects of type Employee
41 associated with an integer key:
42
43 \snippet code/doc_src_qcache.cpp 0
44
45 Here's how to insert an object in the cache:
46
47 \snippet code/doc_src_qcache.cpp 1
48
49 The advantage of using QCache over some other key-based data
50 structure (such as QMap or QHash) is that QCache automatically
51 takes ownership of the objects that are inserted into the cache and
52 deletes them to make room for new objects, if necessary. When
53 inserting an object into the cache, you can specify a \e{cost},
54 which should bear some approximate relationship to the amount of
55 memory taken by the object. When the sum of all objects' costs
56 (totalCost()) exceeds the cache's limit (maxCost()), QCache starts
57 deleting objects in the cache to keep under the limit, starting with
58 less recently accessed objects.
59
60 By default, QCache's maxCost() is 100. You can specify a
61 different value in the QCache constructor:
62
63 \snippet code/doc_src_qcache.cpp 2
64
65 Each time you call insert(), you can specify a cost as third
66 argument (after the key and a pointer to the object to insert).
67 After the call, the inserted object is owned by the QCache, which
68 may delete it at any time to make room for other objects.
69
70 To look up objects in the cache, use object() or
71 operator[](). This function looks up an object by its key, and
72 returns either a pointer to the cached object (which is owned by
73 the cache) or \nullptr.
74
75 If you want to remove an object from the cache for a particular key,
76 call remove(). This will also delete the object. If you want to
77 remove an object from the cache without the QCache deleting it, use
78 take().
79
80 \sa QPixmapCache, QHash, QMap
81*/
82
83/*! \fn template <class Key, class T> QCache<Key, T>::QCache(int maxCost = 100)
84
85 Constructs a cache whose contents will never have a total cost
86 greater than \a maxCost.
87*/
88
89/*! \fn template <class Key, class T> QCache<Key, T>::~QCache()
90
91 Destroys the cache. Deletes all the objects in the cache.
92*/
93
94/*! \fn template <class Key, class T> int QCache<Key, T>::maxCost() const
95
96 Returns the maximum allowed total cost of the cache.
97
98 \sa setMaxCost(), totalCost()
99*/
100
101/*! \fn template <class Key, class T> void QCache<Key, T>::setMaxCost(int cost)
102
103 Sets the maximum allowed total cost of the cache to \a cost. If
104 the current total cost is greater than \a cost, some objects are
105 deleted immediately.
106
107 \sa maxCost(), totalCost()
108*/
109
110/*! \fn template <class Key, class T> int QCache<Key, T>::totalCost() const
111
112 Returns the total cost of the objects in the cache.
113
114 This value is normally below maxCost(), but QCache makes an
115 exception for Qt's \l{implicitly shared} classes. If a cached
116 object shares its internal data with another instance, QCache may
117 keep the object lying around, possibly contributing to making
118 totalCost() larger than maxCost().
119
120 \sa setMaxCost()
121*/
122
123/*! \fn template <class Key, class T> int QCache<Key, T>::size() const
124
125 Returns the number of objects in the cache.
126
127 \sa isEmpty()
128*/
129
130/*! \fn template <class Key, class T> int QCache<Key, T>::count() const
131
132 Same as size().
133*/
134
135/*! \fn template <class Key, class T> bool QCache<Key, T>::isEmpty() const
136
137 Returns \c true if the cache contains no objects; otherwise
138 returns \c false.
139
140 \sa size()
141*/
142
143/*! \fn template <class Key, class T> QList<Key> QCache<Key, T>::keys() const
144
145 Returns a list of the keys in the cache.
146*/
147
148/*! \fn template <class Key, class T> void QCache<Key, T>::clear();
149
150 Deletes all the objects in the cache.
151
152 \sa remove(), take()
153*/
154
155
156/*! \fn template <class Key, class T> bool QCache<Key, T>::insert(const Key &key, T *object, int cost = 1)
157
158 Inserts \a object into the cache with key \a key and
159 associated cost \a cost. Any object with the same key already in
160 the cache will be removed.
161
162 After this call, \a object is owned by the QCache and may be
163 deleted at any time. In particular, if \a cost is greater than
164 maxCost(), the object will be deleted immediately.
165
166 The function returns \c true if the object was inserted into the
167 cache; otherwise it returns \c false.
168
169 \sa take(), remove()
170*/
171
172/*! \fn template <class Key, class T> T *QCache<Key, T>::object(const Key &key) const
173
174 Returns the object associated with key \a key, or \nullptr if the key does
175 not exist in the cache.
176
177 \warning The returned object is owned by QCache and may be
178 deleted at any time.
179
180 \sa take(), remove()
181*/
182
183/*! \fn template <class Key, class T> bool QCache<Key, T>::contains(const Key &key) const
184
185 Returns \c true if the cache contains an object associated with key \a
186 key; otherwise returns \c false.
187
188 \sa take(), remove()
189*/
190
191/*! \fn template <class Key, class T> T *QCache<Key, T>::operator[](const Key &key) const
192
193 Returns the object associated with key \a key, or \nullptr if the key does
194 not exist in the cache.
195
196 This is the same as object().
197
198 \warning The returned object is owned by QCache and may be
199 deleted at any time.
200*/
201
202/*! \fn template <class Key, class T> bool QCache<Key, T>::remove(const Key &key)
203
204 Deletes the object associated with key \a key. Returns \c true if the
205 object was found in the cache; otherwise returns \c false.
206
207 \sa take(), clear()
208*/
209
210/*! \fn template <class Key, class T> T *QCache<Key, T>::take(const Key &key)
211
212 Takes the object associated with key \a key out of the cache
213 without deleting it. Returns a pointer to the object taken out, or
214 0 if the key does not exist in the cache.
215
216 The ownership of the returned object is passed to the caller.
217
218 \sa remove()
219*/
220

Warning: That file was not part of the compilation database. It may have many parsing errors.