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

« back to all changes in this revision

Viewing changes to doc/html/q3deepcopy.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/src/qt3support/tools/q3deepcopy.cpp -->
 
6
<head>
 
7
    <title>Qt 4.0: Q3DeepCopy 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">Q3DeepCopy Class Reference</h1>
 
21
<p>The Q3DeepCopy class is a template class which ensures that implicitly shared and explicitly shared classes reference unique data. <a href="#details">More...</a></p>
 
22
<pre>#include &lt;Q3DeepCopy&gt;</pre><p><b>This class is part of the Qt 3 support library.</b> It is provided to keep old source code working. We strongly advise against using it in new code. See the <a href="porting4.html">Porting Guide</a> for more information.</p>
 
23
<p>Part of the <a href="qt3support.html">Qt3Support</a> module.</p>
 
24
<p><b>Note:</b> All the functions in this class are <a href="threads.html#reentrant">reentrant</a>.</p>
 
25
<ul>
 
26
<li><a href="q3deepcopy-members.html">List of all members, including inherited 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="q3deepcopy.html#Q3DeepCopy">Q3DeepCopy</a></b> ()</li>
 
32
<li><div class="fn"/><b><a href="q3deepcopy.html#Q3DeepCopy-2">Q3DeepCopy</a></b> ( const T &amp; <i>t</i> )</li>
 
33
<li><div class="fn"/><b><a href="q3deepcopy.html#operator-T">operator T</a></b> ()</li>
 
34
<li><div class="fn"/>Q3DeepCopy&lt;T&gt; &amp; <b><a href="q3deepcopy.html#operator-eq">operator=</a></b> ( const T &amp; <i>t</i> )</li>
 
35
</ul>
 
36
<a name="details"></a>
 
37
<hr />
 
38
<h2>Detailed Description</h2>
 
39
<p>The Q3DeepCopy class is a template class which ensures that implicitly shared and explicitly shared classes reference unique data.</p>
 
40
<p>Normally, shared copies reference the same data to optimize memory use and for maximum speed. In the example below, <tt>s1</tt>, <tt>s2</tt>, <tt>s3</tt>, <tt>s4</tt> and <tt>s5</tt> share data.</p>
 
41
<pre>&nbsp;   // all 5 strings share the same data
 
42
    QString s1 = &quot;abcd&quot;;
 
43
    QString s2 = s1;
 
44
    QString s3 = s2;
 
45
    QString s4 = s3;
 
46
    QString s5 = s2;</pre>
 
47
<p>Q3DeepCopy can be used several ways to ensure that an object references unique, unshared data. In the example below, <tt>s1</tt>, <tt>s2</tt> and <tt>s5</tt> share data, while neither <tt>s3</tt> nor <tt>s4</tt> share data.</p>
 
48
<pre>&nbsp;   // s1, s2 and s5 share the same data, neither s3 nor s4 are shared
 
49
    QString s1 = &quot;abcd&quot;;
 
50
    QString s2 = s1;
 
51
    Q3DeepCopy&lt;QString&gt; s3 = s2;  // s3 is a deep copy of s2
 
52
    QString s4 = s3;             // s4 is a deep copy of s3
 
53
    QString s5 = s2;</pre>
 
54
<p>In the example below, <tt>s1</tt>, <tt>s2</tt> and <tt>s5</tt> share data, and <tt>s3</tt> and <tt>s4</tt> share data.</p>
 
55
<pre>&nbsp;   // s1, s2 and s5 share the same data, s3 and s4 share the same data
 
56
    QString s1 = &quot;abcd&quot;;
 
57
    QString s2 = s1;
 
58
    QString s3 = Q3DeepCopy&lt;QString&gt;( s2 );  // s3 is a deep copy of s2
 
59
    QString s4 = s3;                        // s4 is a shallow copy of s3
 
60
    QString s5 = s2;</pre>
 
61
<p>Q3DeepCopy can also provide safety in multithreaded applications that use shared classes. In the example below, the variable <tt>global_string</tt> is used safely since the data contained in <tt>global_string</tt> is always a deep copy. This ensures that all threads get a unique copy of the data, and that any assignments to <tt>global_string</tt> will result in a deep copy.</p>
 
62
<pre>&nbsp;   Q3DeepCopy&lt;QString&gt; global_string;  // global string data
 
63
    QMutex global_mutex;               // mutex to protext global_string
 
64
 
 
65
    ...
 
66
 
 
67
    void setGlobalString( const QString &amp;str )
 
68
    {
 
69
        global_mutex.lock();
 
70
        global_string = str;           // global_string is a deep copy of str
 
71
        global_mutex.unlock();
 
72
    }
 
73
 
 
74
    ...
 
75
 
 
76
    void MyThread::run()
 
77
    {
 
78
        global_mutex.lock();
 
79
        QString str = global_string;          // str is a deep copy of global_string
 
80
        global_mutex.unlock();
 
81
 
 
82
        // process the string data
 
83
        ...
 
84
 
 
85
        // update global_string
 
86
        setGlobalString( str );
 
87
    }</pre>
 
88
<p><b>Warning:</b> It is the application developer's responsibility to protect the object shared across multiple threads.</p>
 
89
<p>The examples above use <a href="qstring.html">QString</a>, which is an implicitly shared class. The behavior of Q3DeepCopy is the same when using explicitly shared classes like <a href="qbytearray.html">QByteArray</a>.</p>
 
90
<p>Currently, Q3DeepCopy works with the following classes:</p>
 
91
<ul>
 
92
<li>QMemArray (including subclasses like <a href="qbytearray.html">QByteArray</a> and <a href="porting4.html#qcstring">QCString</a>)</li>
 
93
<li><a href="qmap.html">QMap</a></li>
 
94
<li><a href="qstring.html">QString</a></li>
 
95
<li>QValueList (including subclasses like <a href="qstringlist.html">QStringList</a> and QValueStack)</li>
 
96
<li>QValueVector</li>
 
97
</ul>
 
98
<p>See also <a href="threads.html">Thread Support in Qt</a>.</p>
 
99
<hr />
 
100
<h2>Member Function Documentation</h2>
 
101
<h3 class="fn"><a name="Q3DeepCopy"></a>Q3DeepCopy::Q3DeepCopy ()</h3>
 
102
<p>Constructs an empty instance of type <i>T</i>.</p>
 
103
<h3 class="fn"><a name="Q3DeepCopy-2"></a>Q3DeepCopy::Q3DeepCopy ( const T &amp; <i>t</i> )</h3>
 
104
<p>Constructs a deep copy of <i>t</i>.</p>
 
105
<h3 class="fn"><a name="operator-T"></a>Q3DeepCopy::operator T ()</h3>
 
106
<p>Returns a deep copy of the encapsulated data.</p>
 
107
<h3 class="fn"><a name="operator-eq"></a>Q3DeepCopy&lt;T&gt; &amp; Q3DeepCopy::operator= ( const T &amp; <i>t</i> )</h3>
 
108
<p>Assigns a deep copy of <i>t</i>.</p>
 
109
<p /><address><hr /><div align="center">
 
110
<table width="100%" cellspacing="0" border="0"><tr class="address">
 
111
<td width="30%">Copyright &copy; 2005 <a href="trolltech.html">Trolltech</a></td>
 
112
<td width="40%" align="center"><a href="trademarks.html">Trademarks</a></td>
 
113
<td width="30%" align="right"><div align="right">Qt 4.0.0</div></td>
 
114
</tr></table></div></address></body>
 
115
</html>