1
<?xml version="1.0" encoding="iso-8859-1"?>
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 -->
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>
16
<table border="0" cellpadding="0" cellspacing="0" width="100%">
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"> </td><td class="postheader" valign="center"><a href="index.html"><font color="#004faf">Home</font></a> · <a href="classes.html"><font color="#004faf">All Classes</font></a> · <a href="mainclasses.html"><font color="#004faf">Main Classes</font></a> · <a href="annotated.html"><font color="#004faf">Annotated</font></a> · <a href="groups.html"><font color="#004faf">Grouped Classes</font></a> · <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 <Q3DeepCopy></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>
26
<li><a href="q3deepcopy-members.html">List of all members, including inherited members</a></li>
28
<a name="public-functions"></a>
29
<h3>Public Functions</h3>
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 & <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<T> & <b><a href="q3deepcopy.html#operator-eq">operator=</a></b> ( const T & <i>t</i> )</li>
36
<a name="details"></a>
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> // all 5 strings share the same data
42
QString s1 = "abcd";
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> // s1, s2 and s5 share the same data, neither s3 nor s4 are shared
49
QString s1 = "abcd";
51
Q3DeepCopy<QString> 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> // s1, s2 and s5 share the same data, s3 and s4 share the same data
56
QString s1 = "abcd";
58
QString s3 = Q3DeepCopy<QString>( 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> Q3DeepCopy<QString> global_string; // global string data
63
QMutex global_mutex; // mutex to protext global_string
67
void setGlobalString( const QString &str )
70
global_string = str; // global_string is a deep copy of str
71
global_mutex.unlock();
79
QString str = global_string; // str is a deep copy of global_string
80
global_mutex.unlock();
82
// process the string data
85
// update global_string
86
setGlobalString( str );
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>
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>
98
<p>See also <a href="threads.html">Thread Support in Qt</a>.</p>
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 & <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<T> & Q3DeepCopy::operator= ( const T & <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 © 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>