~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to doc/html/qcache.html

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="iso-8859-1"?>
 
2
<!DOCTYPE html
 
3
    PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
 
4
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
 
5
<!-- /tmp/qt-4.0.0-espenr-1119621036935/qt-x11-opensource-desktop-4.0.0/doc/src/qcache.qdoc -->
 
6
<head>
 
7
    <title>Qt 4.0: QCache Class Reference</title>
 
8
    <style>h3.fn,span.fn { margin-left: 1cm; text-indent: -1cm; }
 
9
a:link { color: #004faf; text-decoration: none }
 
10
a:visited { color: #672967; text-decoration: none }
 
11
td.postheader { font-family: sans-serif }
 
12
tr.address { font-family: sans-serif }
 
13
body { background: #ffffff; color: black; }</style>
 
14
</head>
 
15
<body>
 
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
17
<tr>
 
18
<td align="left" valign="top" width="32"><img src="images/qt-logo.png" align="left" width="32" height="32" border="0" /></td>
 
19
<td width="1">&nbsp;&nbsp;</td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a>&nbsp;&middot; <a href="classes.html"><font color="#004faf">All&nbsp;Classes</font></a>&nbsp;&middot; <a href="mainclasses.html"><font color="#004faf">Main&nbsp;Classes</font></a>&nbsp;&middot; <a href="annotated.html"><font color="#004faf">Annotated</font></a>&nbsp;&middot; <a href="groups.html"><font color="#004faf">Grouped&nbsp;Classes</font></a>&nbsp;&middot; <a href="functions.html"><font color="#004faf">Functions</font></a></td>
 
20
<td align="right" valign="top" width="230"><img src="images/trolltech-logo.png" align="right" width="203" height="32" border="0" /></td></tr></table><h1 align="center">QCache Class Reference</h1>
 
21
<p>The QCache class is a template class that provides a cache. <a href="#details">More...</a></p>
 
22
<pre>#include &lt;QCache&gt;</pre><p>Part of the <a href="qtcore.html">QtCore</a> module.</p>
 
23
<p><b>Note:</b> All the functions in this class are <a href="threads.html#reentrant">reentrant</a>.</p>
 
24
<ul>
 
25
<li><a href="qcache-members.html">List of all members, including inherited members</a></li>
 
26
<li><a href="qcache-qt3.html">Qt 3 support members</a></li>
 
27
</ul>
 
28
<a name="public-functions"></a>
 
29
<h3>Public Functions</h3>
 
30
<ul>
 
31
<li><div class="fn"/><b><a href="qcache.html#QCache">QCache</a></b> ( int <i>maxCost</i> = 100 )</li>
 
32
<li><div class="fn"/><b><a href="qcache.html#dtor.QCache">~QCache</a></b> ()</li>
 
33
<li><div class="fn"/>void <b><a href="qcache.html#clear">clear</a></b> ()</li>
 
34
<li><div class="fn"/>bool <b><a href="qcache.html#contains">contains</a></b> ( const Key &amp; <i>key</i> ) const</li>
 
35
<li><div class="fn"/>int <b><a href="qcache.html#count">count</a></b> () const</li>
 
36
<li><div class="fn"/>bool <b><a href="qcache.html#insert">insert</a></b> ( const Key &amp; <i>key</i>, T * <i>object</i>, int <i>cost</i> = 1 )</li>
 
37
<li><div class="fn"/>bool <b><a href="qcache.html#isEmpty">isEmpty</a></b> () const</li>
 
38
<li><div class="fn"/>QList&lt;Key&gt; <b><a href="qcache.html#keys">keys</a></b> () const</li>
 
39
<li><div class="fn"/>int <b><a href="qcache.html#maxCost">maxCost</a></b> () const</li>
 
40
<li><div class="fn"/>T * <b><a href="qcache.html#object">object</a></b> ( const Key &amp; <i>key</i> ) const</li>
 
41
<li><div class="fn"/>bool <b><a href="qcache.html#remove">remove</a></b> ( const Key &amp; <i>key</i> )</li>
 
42
<li><div class="fn"/>void <b><a href="qcache.html#setMaxCost">setMaxCost</a></b> ( int <i>cost</i> )</li>
 
43
<li><div class="fn"/>int <b><a href="qcache.html#size">size</a></b> () const</li>
 
44
<li><div class="fn"/>T * <b><a href="qcache.html#take">take</a></b> ( const Key &amp; <i>key</i> )</li>
 
45
<li><div class="fn"/>int <b><a href="qcache.html#totalCost">totalCost</a></b> () const</li>
 
46
<li><div class="fn"/>T * <b><a href="qcache.html#operator-5b-5d">operator[]</a></b> ( const Key &amp; <i>key</i> ) const</li>
 
47
</ul>
 
48
<a name="details"></a>
 
49
<hr />
 
50
<h2>Detailed Description</h2>
 
51
<p>The QCache class is a template class that provides a cache.</p>
 
52
<p>QCache&lt;Key, T&gt; 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>
 
53
<pre>&nbsp;   QCache&lt;int, Employee&gt; cache;</pre>
 
54
<p>Here's how to insert an object in the cache:</p>
 
55
<pre>&nbsp;   Employee *employee = new Employee;
 
56
    employee-&gt;setId(37);
 
57
    employee-&gt;setName(&quot;Richard Schmit&quot;);
 
58
    ...
 
59
    cache.insert(employee-&gt;id(), employee);</pre>
 
60
<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>
 
61
<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>
 
62
<pre>&nbsp;   QCache&lt;int, MyDataStructure&gt; cache(5000);</pre>
 
63
<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>
 
64
<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>
 
65
<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>
 
66
<p>See also <a href="qpixmapcache.html">QPixmapCache</a>, <a href="qhash.html">QHash</a>, and <a href="qmap.html">QMap</a>.</p>
 
67
<hr />
 
68
<h2>Member Function Documentation</h2>
 
69
<h3 class="fn"><a name="QCache"></a>QCache::QCache ( int <i>maxCost</i> = 100 )</h3>
 
70
<p>Constructs a cache whose contents will never have a total cost greater than <i>maxCost</i>.</p>
 
71
<h3 class="fn"><a name="dtor.QCache"></a>QCache::~QCache ()</h3>
 
72
<p>Destroys the cache. Deletes all the objects in the cache.</p>
 
73
<h3 class="fn"><a name="clear"></a>void QCache::clear ()</h3>
 
74
<p>Deletes all the objects in the cache.</p>
 
75
<p>See also <a href="qcache.html#remove">remove</a>() and <a href="qcache.html#take">take</a>().</p>
 
76
<h3 class="fn"><a name="contains"></a>bool QCache::contains ( const Key &amp; <i>key</i> ) const</h3>
 
77
<p>Returns true if the cache contains an object associated with key <i>key</i>; otherwise returns false.</p>
 
78
<p>See also <a href="qcache.html#take">take</a>() and <a href="qcache.html#remove">remove</a>().</p>
 
79
<h3 class="fn"><a name="count"></a>int QCache::count () const</h3>
 
80
<p>Same as <a href="qcache.html#size">size</a>().</p>
 
81
<h3 class="fn"><a name="insert"></a>bool QCache::insert ( const Key &amp; <i>key</i>, T * <i>object</i>, int <i>cost</i> = 1 )</h3>
 
82
<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>
 
83
<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>
 
84
<p>The function returns true if the object was inserted into the cache; otherwise it returns false.</p>
 
85
<p>See also <a href="qcache.html#take">take</a>() and <a href="qcache.html#remove">remove</a>().</p>
 
86
<h3 class="fn"><a name="isEmpty"></a>bool QCache::isEmpty () const</h3>
 
87
<p>Returns true if the cache contains no objects; otherwise returns false.</p>
 
88
<p>See also <a href="qcache.html#size">size</a>().</p>
 
89
<h3 class="fn"><a name="keys"></a><a href="qlist.html">QList</a>&lt;Key&gt; QCache::keys () const</h3>
 
90
<p>Returns a list of the keys in the cache.</p>
 
91
<h3 class="fn"><a name="maxCost"></a>int QCache::maxCost () const</h3>
 
92
<p>Returns the maximum allowed total cost of the cache.</p>
 
93
<p>See also <a href="qcache.html#setMaxCost">setMaxCost</a>() and <a href="qcache.html#totalCost">totalCost</a>().</p>
 
94
<h3 class="fn"><a name="object"></a>T * QCache::object ( const Key &amp; <i>key</i> ) const</h3>
 
95
<p>Returns the object associated with key <i>key</i>, or 0 if the key does not exist in the cache.</p>
 
96
<p><b>Warning:</b> The returned object is owned by <a href="qcache.html">QCache</a> and may be deleted at any time.</p>
 
97
<p>See also <a href="qcache.html#take">take</a>() and <a href="qcache.html#remove">remove</a>().</p>
 
98
<h3 class="fn"><a name="remove"></a>bool QCache::remove ( const Key &amp; <i>key</i> )</h3>
 
99
<p>Deletes the object associated with key <i>key</i>. Returns true if the object was found in the cache; otherwise returns false.</p>
 
100
<p>See also <a href="qcache.html#take">take</a>() and <a href="qcache.html#clear">clear</a>().</p>
 
101
<h3 class="fn"><a name="setMaxCost"></a>void QCache::setMaxCost ( int <i>cost</i> )</h3>
 
102
<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>
 
103
<p>See also <a href="qcache.html#maxCost">maxCost</a>() and <a href="qcache.html#totalCost">totalCost</a>().</p>
 
104
<h3 class="fn"><a name="size"></a>int QCache::size () const</h3>
 
105
<p>Returns the number of objects in the cache.</p>
 
106
<p>See also <a href="qcache.html#isEmpty">isEmpty</a>().</p>
 
107
<h3 class="fn"><a name="take"></a>T * QCache::take ( const Key &amp; <i>key</i> )</h3>
 
108
<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>
 
109
<p>The ownership of the returned object is passed to the caller.</p>
 
110
<p>See also <a href="qcache.html#remove">remove</a>().</p>
 
111
<h3 class="fn"><a name="totalCost"></a>int QCache::totalCost () const</h3>
 
112
<p>Returns the total cost of the objects in the cache.</p>
 
113
<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="shclass.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>
 
114
<p>See also <a href="qcache.html#setMaxCost">setMaxCost</a>().</p>
 
115
<h3 class="fn"><a name="operator-5b-5d"></a>T * QCache::operator[] ( const Key &amp; <i>key</i> ) const</h3>
 
116
<p>Returns the object associated with key <i>key</i>, or 0 if the key does not exist in the cache.</p>
 
117
<p>This is the same as <a href="qcache.html#object">object</a>().</p>
 
118
<p><b>Warning:</b> The returned object is owned by <a href="qcache.html">QCache</a> and may be deleted at any time.</p>
 
119
<p /><address><hr /><div align="center">
 
120
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
121
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
122
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
123
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
124
</tr></table></div></address></body>
 
125
</html>