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

« back to all changes in this revision

Viewing changes to doc/html/implicit-sharing.html

  • Committer: Bazaar Package Importer
  • Author(s): Alessandro Ghersi
  • Date: 2009-11-02 18:30:08 UTC
  • mfrom: (1.2.2 upstream)
  • mto: (15.2.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 88.
  • Revision ID: james.westby@ubuntu.com-20091102183008-b6a4gcs128mvfb3m
Tags: upstream-4.6.0~beta1
ImportĀ upstreamĀ versionĀ 4.6.0~beta1

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
<!-- implicit-sharing.qdoc -->
 
6
<head>
 
7
  <title>Qt 4.6: Implicit Sharing</title>
 
8
  <link href="classic.css" rel="stylesheet" type="text/css" />
 
9
</head>
 
10
<body>
 
11
<table border="0" cellpadding="0" cellspacing="0" width="100%">
 
12
<tr>
 
13
<td align="left" valign="top" width="32"><a href="http://qt.nokia.com/"><img src="images/qt-logo.png" align="left" border="0" /></a></td>
 
14
<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="functions.html"><font color="#004faf">All&nbsp;Functions</font></a>&nbsp;&middot; <a href="overviews.html"><font color="#004faf">Overviews</font></a></td><td class="searchBar" align="right" valign="center"><form action="http://www.google.com/cse" id="cse-search-box"><div><input type="hidden" name="cx" value="000136343326384750312:dhbxnqlakyu" /><input type="hidden" name="ie" value="UTF-8" /><input type="text" name="q" size="31" /><input type="submit" name="sa" value="Search" /></div></form></td>
 
15
</tr></table><h1 class="title">Implicit Sharing<br /><span class="subtitle"></span>
 
16
</h1>
 
17
<a name="implicit-data-sharing"></a><a name="implicit-sharing"></a><a name="implicitly-shared"></a><a name="reference-counting"></a><a name="shared-implicitly"></a><a name="shared-classes"></a><p>Many C++ classes in Qt use implicit data sharing to maximize resource usage and minimize copying. Implicitly shared classes are both safe and efficient when passed as arguments, because only a pointer to the data is passed around, and the data is copied only if and when a function writes to it, i.e&#x2e;, <i>copy-on-write</i>.</p>
 
18
<ul><li><a href="#overview">Overview</a></li>
 
19
<li><a href="#implicit-sharing-in-detail">Implicit Sharing in Detail</a></li>
 
20
<li><a href="#list-of-classes">List of Classes</a></li>
 
21
</ul>
 
22
<a name="overview"></a>
 
23
<h3>Overview</h3>
 
24
<p>A shared class consists of a pointer to a shared data block that contains a reference count and the data.</p>
 
25
<p>When a shared object is created, it sets the reference count to 1. The reference count is incremented whenever a new object references the shared data, and decremented when the object dereferences the shared data. The shared data is deleted when the reference count becomes zero.</p>
 
26
<a name="deep-copy"></a><a name="shallow-copy"></a><p>When dealing with shared objects, there are two ways of copying an object. We usually speak about <i>deep</i> and <i>shallow</i> copies. A deep copy implies duplicating an object. A shallow copy is a reference copy, i.e&#x2e; just a pointer to a shared data block. Making a deep copy can be expensive in terms of memory and CPU. Making a shallow copy is very fast, because it only involves setting a pointer and incrementing the reference count.</p>
 
27
<p>Object assignment (with operator=()) for implicitly shared objects is implemented using shallow copies.</p>
 
28
<p>The benefit of sharing is that a program does not need to duplicate data unnecessarily, which results in lower memory use and less copying of data. Objects can easily be assigned, sent as function arguments, and returned from functions.</p>
 
29
<p>Implicit sharing takes place behind the scenes; the programmer does not need to worry about it. Even in multithreaded applications, implicit sharing takes place, as explained in <a href="threads-modules.html#threads-and-implicitly-shared-classes">Threads and Implicitly Shared Classes</a>.</p>
 
30
<p>When implementing your own implicitly shared classes, use the <a href="qshareddata.html">QSharedData</a> and <a href="qshareddatapointer.html">QSharedDataPointer</a> classes.</p>
 
31
<a name="implicit-sharing-in-detail"></a>
 
32
<h3>Implicit Sharing in Detail</h3>
 
33
<p>Implicit sharing automatically detaches the object from a shared block if the object is about to change and the reference count is greater than one. (This is often called <i>copy-on-write</i> or <i>value semantics</i>.)</p>
 
34
<p>An implicitly shared class has total control of its internal data. In any member functions that modify its data, it automatically detaches before modifying the data.</p>
 
35
<p>The <a href="qpen.html">QPen</a> class, which uses implicit sharing, detaches from the shared data in all member functions that change the internal data.</p>
 
36
<p>Code fragment:</p>
 
37
<pre> void QPen::setStyle(Qt::PenStyle style)
 
38
 {
 
39
     detach();           // detach from common data
 
40
     d-&gt;style = style;   // set the style member
 
41
 }
 
42
 
 
43
 void QPen::detach()
 
44
 {
 
45
     if (d-&gt;ref != 1) {
 
46
         ...             // perform a deep copy
 
47
     }
 
48
 }</pre>
 
49
<a name="list-of-classes"></a>
 
50
<h3>List of Classes</h3>
 
51
<p>The classes listed below automatically detach from common data if an object is about to be changed. The programmer will not even notice that the objects are shared. Thus you should treat separate instances of them as separate objects. They will always behave as separate objects but with the added benefit of sharing data whenever possible. For this reason, you can pass instances of these classes as arguments to functions by value without concern for the copying overhead.</p>
 
52
<p>Example:</p>
 
53
<pre> QPixmap p1, p2;
 
54
 p1.load(&quot;image.bmp&quot;);
 
55
 p2 = p1;                        // p1 and p2 share data
 
56
 
 
57
 QPainter paint;
 
58
 paint.begin(&amp;p2);               // cuts p2 loose from p1
 
59
 paint.drawText(0,50, &quot;Hi&quot;);
 
60
 paint.end();</pre>
 
61
<p>In this example, <tt>p1</tt> and <tt>p2</tt> share data until <a href="qpainter.html#begin">QPainter::begin</a>() is called for <tt>p2</tt>, because painting a pixmap will modify it.</p>
 
62
<p><b>Warning:</b> Do not copy an implicitly shared container (<a href="qmap.html">QMap</a>, <a href="qvector.html">QVector</a>, etc.) while you are iterating over it using an non-const <a href="containers.html#stl-style-iterators">STL-style iterator</a>.</p>
 
63
<a name="implicitly-shared-classes"></a><p><table width="100%" class="annotated" cellpadding="2" cellspacing="1" border="0">
 
64
<tr valign="top" class="odd"><th><a href="qbitarray.html">QBitArray</a></th><td>Array of bits</td></tr>
 
65
<tr valign="top" class="even"><th><a href="qbitmap.html">QBitmap</a></th><td>Monochrome (1-bit depth) pixmaps</td></tr>
 
66
<tr valign="top" class="odd"><th><a href="qbrush.html">QBrush</a></th><td>Defines the fill pattern of shapes drawn by QPainter</td></tr>
 
67
<tr valign="top" class="even"><th><a href="qbytearray.html">QByteArray</a></th><td>Array of bytes</td></tr>
 
68
<tr valign="top" class="odd"><th><a href="qcache.html">QCache</a></th><td>Template class that provides a cache</td></tr>
 
69
<tr valign="top" class="even"><th><a href="qcontiguouscache.html">QContiguousCache</a></th><td>Template class that provides a contiguous cache</td></tr>
 
70
<tr valign="top" class="odd"><th><a href="qcursor.html">QCursor</a></th><td>Mouse cursor with an arbitrary shape</td></tr>
 
71
<tr valign="top" class="even"><th><a href="qdir.html">QDir</a></th><td>Access to directory structures and their contents</td></tr>
 
72
<tr valign="top" class="odd"><th><a href="qfileinfo.html">QFileInfo</a></th><td>System-independent file information</td></tr>
 
73
<tr valign="top" class="even"><th><a href="qfont.html">QFont</a></th><td>Specifies a font used for drawing text</td></tr>
 
74
<tr valign="top" class="odd"><th><a href="qfontinfo.html">QFontInfo</a></th><td>General information about fonts</td></tr>
 
75
<tr valign="top" class="even"><th><a href="qfontmetrics.html">QFontMetrics</a></th><td>Font metrics information</td></tr>
 
76
<tr valign="top" class="odd"><th><a href="qfontmetricsf.html">QFontMetricsF</a></th><td>Font metrics information</td></tr>
 
77
<tr valign="top" class="even"><th><a href="qglcolormap.html">QGLColormap</a></th><td>Used for installing custom colormaps into a QGLWidget</td></tr>
 
78
<tr valign="top" class="odd"><th><a href="qgradient.html">QGradient</a></th><td>Used in combination with QBrush to specify gradient fills</td></tr>
 
79
<tr valign="top" class="even"><th><a href="qhash.html">QHash</a></th><td>Template class that provides a hash-table-based dictionary</td></tr>
 
80
<tr valign="top" class="odd"><th><a href="qicon.html">QIcon</a></th><td>Scalable icons in different modes and states</td></tr>
 
81
<tr valign="top" class="even"><th><a href="qimage.html">QImage</a></th><td>Hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device</td></tr>
 
82
<tr valign="top" class="odd"><th><a href="qkeysequence.html">QKeySequence</a></th><td>Encapsulates a key sequence as used by shortcuts</td></tr>
 
83
<tr valign="top" class="even"><th><a href="qlinkedlist.html">QLinkedList</a></th><td>Template class that provides linked lists</td></tr>
 
84
<tr valign="top" class="odd"><th><a href="qlist.html">QList</a></th><td>Template class that provides lists</td></tr>
 
85
<tr valign="top" class="even"><th><a href="qlocale.html">QLocale</a></th><td>Converts between numbers and their string representations in various languages</td></tr>
 
86
<tr valign="top" class="odd"><th><a href="qmap.html">QMap</a></th><td>Template class that provides a skip-list-based dictionary</td></tr>
 
87
<tr valign="top" class="even"><th><a href="qmultihash.html">QMultiHash</a></th><td>Convenience QHash subclass that provides multi-valued hashes</td></tr>
 
88
<tr valign="top" class="odd"><th><a href="qmultimap.html">QMultiMap</a></th><td>Convenience QMap subclass that provides multi-valued maps</td></tr>
 
89
<tr valign="top" class="even"><th><a href="qpainterpath.html">QPainterPath</a></th><td>Container for painting operations, enabling graphical shapes to be constructed and reused</td></tr>
 
90
<tr valign="top" class="odd"><th><a href="qpalette.html">QPalette</a></th><td>Contains color groups for each widget state</td></tr>
 
91
<tr valign="top" class="even"><th><a href="qpen.html">QPen</a></th><td>Defines how a QPainter should draw lines and outlines of shapes</td></tr>
 
92
<tr valign="top" class="odd"><th><a href="qpicture.html">QPicture</a></th><td>Paint device that records and replays QPainter commands</td></tr>
 
93
<tr valign="top" class="even"><th><a href="qpixmap.html">QPixmap</a></th><td>Off-screen image representation that can be used as a paint device</td></tr>
 
94
<tr valign="top" class="odd"><th><a href="qpolygon.html">QPolygon</a></th><td>Vector of points using integer precision</td></tr>
 
95
<tr valign="top" class="even"><th><a href="qpolygonf.html">QPolygonF</a></th><td>Vector of points using floating point precision</td></tr>
 
96
<tr valign="top" class="odd"><th><a href="qqueue.html">QQueue</a></th><td>Generic container that provides a queue</td></tr>
 
97
<tr valign="top" class="even"><th><a href="qregexp.html">QRegExp</a></th><td>Pattern matching using regular expressions</td></tr>
 
98
<tr valign="top" class="odd"><th><a href="qregion.html">QRegion</a></th><td>Specifies a clip region for a painter</td></tr>
 
99
<tr valign="top" class="even"><th><a href="qset.html">QSet</a></th><td>Template class that provides a hash-table-based set</td></tr>
 
100
<tr valign="top" class="odd"><th><a href="qsqlfield.html">QSqlField</a></th><td>Manipulates the fields in SQL database tables and views</td></tr>
 
101
<tr valign="top" class="even"><th><a href="qsqlquery.html">QSqlQuery</a></th><td>Means of executing and manipulating SQL statements</td></tr>
 
102
<tr valign="top" class="odd"><th><a href="qsqlrecord.html">QSqlRecord</a></th><td>Encapsulates a database record</td></tr>
 
103
<tr valign="top" class="even"><th><a href="qstack.html">QStack</a></th><td>Template class that provides a stack</td></tr>
 
104
<tr valign="top" class="odd"><th><a href="qstring.html">QString</a></th><td>Unicode character string</td></tr>
 
105
<tr valign="top" class="even"><th><a href="qstringlist.html">QStringList</a></th><td>List of strings</td></tr>
 
106
<tr valign="top" class="odd"><th><a href="qtextboundaryfinder.html">QTextBoundaryFinder</a></th><td>Way of finding Unicode text boundaries in a string</td></tr>
 
107
<tr valign="top" class="even"><th><a href="qtextcursor.html">QTextCursor</a></th><td>Offers an API to access and modify QTextDocuments</td></tr>
 
108
<tr valign="top" class="odd"><th><a href="qtextdocumentfragment.html">QTextDocumentFragment</a></th><td>Represents a piece of formatted text from a QTextDocument</td></tr>
 
109
<tr valign="top" class="even"><th><a href="qtextformat.html">QTextFormat</a></th><td>Formatting information for a QTextDocument</td></tr>
 
110
<tr valign="top" class="odd"><th><a href="qurl.html">QUrl</a></th><td>Convenient interface for working with URLs</td></tr>
 
111
<tr valign="top" class="even"><th><a href="qvariant.html">QVariant</a></th><td>Acts like a union for the most common Qt data types</td></tr>
 
112
<tr valign="top" class="odd"><th><a href="qvector.html">QVector</a></th><td>Template class that provides a dynamic array</td></tr>
 
113
<tr valign="top" class="even"><th><a href="qx11info.html">QX11Info</a></th><td>Information about the X display configuration</td></tr>
 
114
</table></p>
 
115
<p /><address><hr /><div align="center">
 
116
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
117
<td width="40%" align="left">Copyright &copy; 2009 Nokia Corporation and/or its subsidiary(-ies)</td>
 
118
<td width="20%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
119
<td width="40%" align="right"><div align="right">Qt 4.6.0</div></td>
 
120
<script type="text/javascript" src="http://www.google.com/jsapi"></script><script type="text/javascript">google.load("elements", "1", {packages: "transliteration"});</script><script type="text/javascript" src="http://www.google.com/coop/cse/t13n?form=cse-search-box&t13n_langs=en"></script><script type="text/javascript" src="http://www.google.com/coop/cse/brand?form=cse-search-box&lang=en"></script></tr></table></div></address></body>
 
121
</html>