1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en_US" lang="en_US">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qcache.qdoc -->
<title>Qt 4.8: QCache Class Reference</title>
<link rel="stylesheet" type="text/css" href="style/offline.css" />
</head>
<body>
<div class="header" id="qtdocheader">
<div class="content">
<a href="index.html" class="qtref"><span>Qt Reference Documentation</span></a>
</div>
<div class="breadcrumb toolblock">
<ul>
<li class="first"><a href="index.html">Home</a></li>
<!-- Breadcrumbs go here -->
<li><a href="modules.html">Modules</a></li>
<li><a href="qtcore.html">QtCore</a></li>
<li>QCache</li>
</ul>
</div>
</div>
<div class="content mainContent">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#public-functions">Public Functions</a></li>
<li class="level1"><a href="#details">Detailed Description</a></li>
</ul>
</div>
<h1 class="title">QCache Class Reference</h1>
<!-- $$$QCache-brief -->
<p>The QCache class is a template class that provides a cache. <a href="#details">More...</a></p>
<!-- @@@QCache -->
<pre class="cpp"> <span class="preprocessor">#include <QCache></span></pre><p><b>Note:</b> All functions in this class are <a href="threads-reentrancy.html#reentrant">reentrant</a>.</p>
<ul>
<li><a href="qcache-members.html">List of all members, including inherited members</a></li>
<li><a href="qcache-qt3.html">Qt 3 support members</a></li>
</ul>
<a name="public-functions"></a>
<h2>Public Functions</h2>
<table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#QCache">QCache</a></b> ( int <i>maxCost</i> = 100 )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#dtor.QCache">~QCache</a></b> ()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#clear">clear</a></b> ()</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#contains">contains</a></b> ( const Key & <i>key</i> ) const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#count">count</a></b> () const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#insert">insert</a></b> ( const Key & <i>key</i>, T * <i>object</i>, int <i>cost</i> = 1 )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#isEmpty">isEmpty</a></b> () const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QList<Key> </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#keys">keys</a></b> () const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#maxCost">maxCost</a></b> () const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> T * </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#object">object</a></b> ( const Key & <i>key</i> ) const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#remove">remove</a></b> ( const Key & <i>key</i> )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#setMaxCost">setMaxCost</a></b> ( int <i>cost</i> )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#size">size</a></b> () const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> T * </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#take">take</a></b> ( const Key & <i>key</i> )</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> int </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#totalCost">totalCost</a></b> () const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> T * </td><td class="memItemRight bottomAlign"><b><a href="qcache.html#operator-5b-5d">operator[]</a></b> ( const Key & <i>key</i> ) const</td></tr>
</table>
<a name="details"></a>
<!-- $$$QCache-description -->
<div class="descr">
<h2>Detailed Description</h2>
<p>The QCache class is a template class that provides a cache.</p>
<p>QCache<Key, T> defines a cache that stores objects of type T associated with keys of type Key. For example, here's the definition of a cache that stores objects of type Employee associated with an integer key:</p>
<pre class="cpp"> <span class="type">QCache</span><span class="operator"><</span><span class="type">int</span><span class="operator">,</span> Employee<span class="operator">></span> cache;</pre>
<p>Here's how to insert an object in the cache:</p>
<pre class="cpp"> Employee <span class="operator">*</span>employee <span class="operator">=</span> <span class="keyword">new</span> Employee;
employee<span class="operator">-</span><span class="operator">></span>setId(<span class="number">37</span>);
employee<span class="operator">-</span><span class="operator">></span>setName(<span class="string">"Richard Schmit"</span>);
<span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
cache<span class="operator">.</span><a href="qcache.html#insert">insert</a>(employee<span class="operator">-</span><span class="operator">></span>id()<span class="operator">,</span> employee);</pre>
<p>The advantage of using QCache over some other key-based data structure (such as <a href="qmap.html">QMap</a> or <a href="qhash.html">QHash</a>) is that QCache automatically takes ownership of the objects that are inserted into the cache and deletes them to make room for new objects, if necessary. When inserting an object into the cache, you can specify a <i>cost</i>, which should bear some approximate relationship to the amount of memory taken by the object. When the sum of all objects' costs (<a href="qcache.html#totalCost">totalCost</a>()) exceeds the cache's limit (<a href="qcache.html#maxCost">maxCost</a>()), QCache starts deleting objects in the cache to keep under the limit, starting with less recently accessed objects.</p>
<p>By default, QCache's <a href="qcache.html#maxCost">maxCost</a>() is 100. You can specify a different value in the QCache constructor:</p>
<pre class="cpp"> <span class="type">QCache</span><span class="operator"><</span><span class="type">int</span><span class="operator">,</span> MyDataStructure<span class="operator">></span> cache(<span class="number">5000</span>);</pre>
<p>Each time you call <a href="qcache.html#insert">insert</a>(), you can specify a cost as third argument (after the key and a pointer to the object to insert). After the call, the inserted object is owned by the QCache, which may delete it at any time to make room for other objects.</p>
<p>To look up objects in the cache, use <a href="qcache.html#object">object</a>() or operator[](). This function looks up an object by its key, and returns either a pointer to the cached object (which is owned by the cache) or 0.</p>
<p>If you want to remove an object from the cache for a particular key, call <a href="qcache.html#remove">remove</a>(). This will also delete the object. If you want to remove an object from the cache without the QCache deleting it, use <a href="qcache.html#take">take</a>().</p>
</div>
<p><b>See also </b><a href="qpixmapcache.html">QPixmapCache</a>, <a href="qhash.html">QHash</a>, and <a href="qmap.html">QMap</a>.</p>
<!-- @@@QCache -->
<div class="func">
<h2>Member Function Documentation</h2>
<!-- $$$QCache[overload1]$$$QCacheint -->
<h3 class="fn"><a name="QCache"></a>QCache::<span class="name">QCache</span> ( <span class="type">int</span> <i>maxCost</i> = 100 )</h3>
<p>Constructs a cache whose contents will never have a total cost greater than <i>maxCost</i>.</p>
<!-- @@@QCache -->
<!-- $$$~QCache[overload1]$$$~QCache -->
<h3 class="fn"><a name="dtor.QCache"></a>QCache::<span class="name">~QCache</span> ()</h3>
<p>Destroys the cache. Deletes all the objects in the cache.</p>
<!-- @@@~QCache -->
<!-- $$$clear[overload1]$$$clear -->
<h3 class="fn"><a name="clear"></a><span class="type">void</span> QCache::<span class="name">clear</span> ()</h3>
<p>Deletes all the objects in the cache.</p>
<p><b>See also </b><a href="qcache.html#remove">remove</a>() and <a href="qcache.html#take">take</a>().</p>
<!-- @@@clear -->
<!-- $$$contains[overload1]$$$containsconstKey& -->
<h3 class="fn"><a name="contains"></a><span class="type">bool</span> QCache::<span class="name">contains</span> ( const <span class="type">Key</span> & <i>key</i> ) const</h3>
<p>Returns true if the cache contains an object associated with key <i>key</i>; otherwise returns false.</p>
<p><b>See also </b><a href="qcache.html#take">take</a>() and <a href="qcache.html#remove">remove</a>().</p>
<!-- @@@contains -->
<!-- $$$count[overload1]$$$count -->
<h3 class="fn"><a name="count"></a><span class="type">int</span> QCache::<span class="name">count</span> () const</h3>
<p>Same as <a href="qcache.html#size">size</a>().</p>
<!-- @@@count -->
<!-- $$$insert[overload1]$$$insertconstKey&T*int -->
<h3 class="fn"><a name="insert"></a><span class="type">bool</span> QCache::<span class="name">insert</span> ( const <span class="type">Key</span> & <i>key</i>, <span class="type">T</span> * <i>object</i>, <span class="type">int</span> <i>cost</i> = 1 )</h3>
<p>Inserts <i>object</i> into the cache with key <i>key</i> and associated cost <i>cost</i>. Any object with the same key already in the cache will be removed.</p>
<p>After this call, <i>object</i> is owned by the <a href="qcache.html">QCache</a> and may be deleted at any time. In particular, if <i>cost</i> is greater than <a href="qcache.html#maxCost">maxCost</a>(), the object will be deleted immediately.</p>
<p>The function returns true if the object was inserted into the cache; otherwise it returns false.</p>
<p><b>See also </b><a href="qcache.html#take">take</a>() and <a href="qcache.html#remove">remove</a>().</p>
<!-- @@@insert -->
<!-- $$$isEmpty[overload1]$$$isEmpty -->
<h3 class="fn"><a name="isEmpty"></a><span class="type">bool</span> QCache::<span class="name">isEmpty</span> () const</h3>
<p>Returns true if the cache contains no objects; otherwise returns false.</p>
<p><b>See also </b><a href="qcache.html#size">size</a>().</p>
<!-- @@@isEmpty -->
<!-- $$$keys[overload1]$$$keys -->
<h3 class="fn"><a name="keys"></a><span class="type"><a href="qlist.html">QList</a></span><<span class="type">Key</span>> QCache::<span class="name">keys</span> () const</h3>
<p>Returns a list of the keys in the cache.</p>
<!-- @@@keys -->
<!-- $$$maxCost[overload1]$$$maxCost -->
<h3 class="fn"><a name="maxCost"></a><span class="type">int</span> QCache::<span class="name">maxCost</span> () const</h3>
<p>Returns the maximum allowed total cost of the cache.</p>
<p><b>See also </b><a href="qcache.html#setMaxCost">setMaxCost</a>() and <a href="qcache.html#totalCost">totalCost</a>().</p>
<!-- @@@maxCost -->
<!-- $$$object[overload1]$$$objectconstKey& -->
<h3 class="fn"><a name="object"></a><span class="type">T</span> * QCache::<span class="name">object</span> ( const <span class="type">Key</span> & <i>key</i> ) const</h3>
<p>Returns the object associated with key <i>key</i>, or 0 if the key does not exist in the cache.</p>
<p><b>Warning:</b> The returned object is owned by <a href="qcache.html">QCache</a> and may be deleted at any time.</p>
<p><b>See also </b><a href="qcache.html#take">take</a>() and <a href="qcache.html#remove">remove</a>().</p>
<!-- @@@object -->
<!-- $$$remove[overload1]$$$removeconstKey& -->
<h3 class="fn"><a name="remove"></a><span class="type">bool</span> QCache::<span class="name">remove</span> ( const <span class="type">Key</span> & <i>key</i> )</h3>
<p>Deletes the object associated with key <i>key</i>. Returns true if the object was found in the cache; otherwise returns false.</p>
<p><b>See also </b><a href="qcache.html#take">take</a>() and <a href="qcache.html#clear">clear</a>().</p>
<!-- @@@remove -->
<!-- $$$setMaxCost[overload1]$$$setMaxCostint -->
<h3 class="fn"><a name="setMaxCost"></a><span class="type">void</span> QCache::<span class="name">setMaxCost</span> ( <span class="type">int</span> <i>cost</i> )</h3>
<p>Sets the maximum allowed total cost of the cache to <i>cost</i>. If the current total cost is greater than <i>cost</i>, some objects are deleted immediately.</p>
<p><b>See also </b><a href="qcache.html#maxCost">maxCost</a>() and <a href="qcache.html#totalCost">totalCost</a>().</p>
<!-- @@@setMaxCost -->
<!-- $$$size[overload1]$$$size -->
<h3 class="fn"><a name="size"></a><span class="type">int</span> QCache::<span class="name">size</span> () const</h3>
<p>Returns the number of objects in the cache.</p>
<p><b>See also </b><a href="qcache.html#isEmpty">isEmpty</a>().</p>
<!-- @@@size -->
<!-- $$$take[overload1]$$$takeconstKey& -->
<h3 class="fn"><a name="take"></a><span class="type">T</span> * QCache::<span class="name">take</span> ( const <span class="type">Key</span> & <i>key</i> )</h3>
<p>Takes the object associated with key <i>key</i> out of the cache without deleting it. Returns a pointer to the object taken out, or 0 if the key does not exist in the cache.</p>
<p>The ownership of the returned object is passed to the caller.</p>
<p><b>See also </b><a href="qcache.html#remove">remove</a>().</p>
<!-- @@@take -->
<!-- $$$totalCost[overload1]$$$totalCost -->
<h3 class="fn"><a name="totalCost"></a><span class="type">int</span> QCache::<span class="name">totalCost</span> () const</h3>
<p>Returns the total cost of the objects in the cache.</p>
<p>This value is normally below <a href="qcache.html#maxCost">maxCost</a>(), but <a href="qcache.html">QCache</a> makes an exception for Qt's <a href="implicit-sharing.html#implicitly-shared">implicitly shared</a> classes. If a cached object shares its internal data with another instance, <a href="qcache.html">QCache</a> may keep the object lying around, possibly contributing to making totalCost() larger than <a href="qcache.html#maxCost">maxCost</a>().</p>
<p><b>See also </b><a href="qcache.html#setMaxCost">setMaxCost</a>().</p>
<!-- @@@totalCost -->
<!-- $$$operator[][overload1]$$$operator[]constKey& -->
<h3 class="fn"><a name="operator-5b-5d"></a><span class="type">T</span> * QCache::<span class="name">operator[]</span> ( const <span class="type">Key</span> & <i>key</i> ) const</h3>
<p>Returns the object associated with key <i>key</i>, or 0 if the key does not exist in the cache.</p>
<p>This is the same as <a href="qcache.html#object">object</a>().</p>
<p><b>Warning:</b> The returned object is owned by <a href="qcache.html">QCache</a> and may be deleted at any time.</p>
<!-- @@@operator[] -->
</div>
<div class="ft">
<span></span>
</div>
</div>
<div class="footer">
<p>
<acronym title="Copyright">©</acronym> 2012 Nokia Corporation and/or its
subsidiaries. Documentation contributions included herein are the copyrights of
their respective owners.</p>
<br />
<p>
The documentation provided herein is licensed under the terms of the
<a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation
License version 1.3</a> as published by the Free Software Foundation.</p>
<p>
Documentation sources may be obtained from <a href="http://www.qt-project.org">
www.qt-project.org</a>.</p>
<br />
<p>
Nokia, Qt and their respective logos are trademarks of Nokia Corporation
in Finland and/or other countries worldwide. All other trademarks are property
of their respective owners. <a title="Privacy Policy"
href="http://en.gitorious.org/privacy_policy/">Privacy Policy</a></p>
</div>
</body>
</html>
|