summaryrefslogtreecommitdiffstats
path: root/doc/html/qdeepcopy.html
blob: 3d5cf613bd1d15e81e0e29b756e8dfc305077855 (plain)
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
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- /home/espenr/tmp/qt-3.3.8-espenr-2499/qt-x11-free-3.3.8/src/tools/qdeepcopy.cpp:40 -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>QDeepCopy Class</title>
<style type="text/css"><!--
fn { margin-left: 1cm; text-indent: -1cm; }
a:link { color: #004faf; text-decoration: none }
a:visited { color: #672967; text-decoration: none }
body { background: #ffffff; color: black; }
--></style>
</head>
<body>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr bgcolor="#E5E5E5">
<td valign=center>
 <a href="index.html">
<font color="#004faf">Home</font></a>
 | <a href="classes.html">
<font color="#004faf">All&nbsp;Classes</font></a>
 | <a href="mainclasses.html">
<font color="#004faf">Main&nbsp;Classes</font></a>
 | <a href="annotated.html">
<font color="#004faf">Annotated</font></a>
 | <a href="groups.html">
<font color="#004faf">Grouped&nbsp;Classes</font></a>
 | <a href="functions.html">
<font color="#004faf">Functions</font></a>
</td>
<td align="right" valign="center"><img src="logo32.png" align="right" width="64" height="32" border="0"></td></tr></table><h1 align=center>QDeepCopy Class Reference</h1>

<p>The QDeepCopy class is a template class which ensures that
implicitly shared and explicitly shared classes reference unique
data.
<a href="#details">More...</a>
<p>All the functions in this class are <a href="threads.html#reentrant">reentrant</a> when Qt is built with thread support.</p>
<p><tt>#include &lt;<a href="qdeepcopy-h.html">qdeepcopy.h</a>&gt;</tt>
<p><a href="qdeepcopy-members.html">List of all member functions.</a>
<h2>Public Members</h2>
<ul>
<li class=fn><a href="#QDeepCopy"><b>QDeepCopy</b></a> ()</li>
<li class=fn><a href="#QDeepCopy-2"><b>QDeepCopy</b></a> ( const&nbsp;T&nbsp;&amp;&nbsp;t )</li>
<li class=fn>QDeepCopy&lt;T&gt; &amp; <a href="#operator-eq"><b>operator=</b></a> ( const&nbsp;T&nbsp;&amp;&nbsp;t )</li>
<li class=fn><a href="#operator-T"><b>operator T</b></a> ()</li>
</ul>
<hr><a name="details"></a><h2>Detailed Description</h2>


The QDeepCopy class is a template class which ensures that
<a href="shclass.html#implicitly-shared">implicitly shared</a> and <a href="shclass.html#explicitly-shared">explicitly shared</a> classes reference unique
data.
<p> 
<p> 

<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> <pre>
    // all 5 strings share the same data
    <a href="qstring.html">QString</a> s1 = "abcd";
    <a href="qstring.html">QString</a> s2 = s1;
    <a href="qstring.html">QString</a> s3 = s2;
    <a href="qstring.html">QString</a> s4 = s3;
    <a href="qstring.html">QString</a> s5 = s2;
    </pre>
 
<p> QDeepCopy 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.
<pre>
    // s1, s2 and s5 share the same data, neither s3 nor s4 are shared
    <a href="qstring.html">QString</a> s1 = "abcd";
    <a href="qstring.html">QString</a> s2 = s1;
    QDeepCopy&lt;QString&gt; s3 = s2;  // s3 is a <a href="shclass.html#deep-copy">deep copy</a> of s2
    <a href="qstring.html">QString</a> s4 = s3;             // s4 is a deep copy of s3
    <a href="qstring.html">QString</a> s5 = s2;
    </pre>
 
<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.
<pre>
    // s1, s2 and s5 share the same data, s3 and s4 share the same data
    <a href="qstring.html">QString</a> s1 = "abcd";
    <a href="qstring.html">QString</a> s2 = s1;
    <a href="qstring.html">QString</a> s3 = QDeepCopy&lt;QString&gt;( s2 );  // s3 is a deep copy of s2
    <a href="qstring.html">QString</a> s4 = s3;                        // s4 is a <a href="shclass.html#shallow-copy">shallow copy</a> of s3
    <a href="qstring.html">QString</a> s5 = s2;
    </pre>
 
<p> QDeepCopy 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> <pre>
    QDeepCopy&lt;QString&gt; global_string;  // global string data
    <a href="qmutex.html">QMutex</a> global_mutex;               // mutex to protext global_string

    ...

    void setGlobalString( const <a href="qstring.html">QString</a> &amp;str )
    {
        global_mutex.<a href="qmutex.html#lock">lock</a>();
        global_string = str;           // global_string is a deep copy of str
        global_mutex.<a href="qmutex.html#unlock">unlock</a>();
    }

    ...

    void MyThread::run()
    {
        global_mutex.<a href="qmutex.html#lock">lock</a>();
        <a href="qstring.html">QString</a> str = global_string;          // str is a deep copy of global_string
        global_mutex.<a href="qmutex.html#unlock">unlock</a>();

        // process the string data
        ...

        // update global_string
        setGlobalString( str );
    }
    </pre>
 
<p> <b>Warning:</b> It is the application developer's responsibility to
protect the object shared across multiple threads.
<p> The examples above use <a href="qstring.html">QString</a>, which is an implicitly shared
class. The behavior of QDeepCopy is the same when using explicitly
shared classes like <a href="qbytearray.html">QByteArray</a>.
<p> Currently, QDeepCopy works with the following classes:
<ul>
<li> <a href="qmemarray.html">QMemArray</a> (including subclasses like QByteArray and <a href="qcstring.html">QCString</a>)
<li> <a href="qmap.html">QMap</a>
<li> QString
<li> <a href="qvaluelist.html">QValueList</a> (including subclasses like <a href="qstringlist.html">QStringList</a> and <a href="qvaluestack.html">QValueStack</a>)
<li> <a href="qvaluevector.html">QValueVector</a>
</ul>
<p> <p>See also <a href="threads.html">Thread Support in Qt</a>, <a href="shared.html">Implicitly and Explicitly Shared Classes</a>, and <a href="tools.html">Non-GUI Classes</a>.

<hr><h2>Member Function Documentation</h2>
<h3 class=fn><a name="QDeepCopy"></a>QDeepCopy::QDeepCopy ()
</h3>

<p> Constructs an empty instance of type <em>T</em>.

<h3 class=fn><a name="QDeepCopy-2"></a>QDeepCopy::QDeepCopy ( const&nbsp;T&nbsp;&amp;&nbsp;t )
</h3>

<p> Constructs a <a href="shclass.html#deep-copy">deep copy</a> of <em>t</em>.

<h3 class=fn><a name="operator-T"></a>QDeepCopy::operator T ()
</h3>

<p> Returns a <a href="shclass.html#deep-copy">deep copy</a> of the encapsulated data.

<h3 class=fn><a href="qdeepcopy.html">QDeepCopy</a>&lt;T&gt;&nbsp;&amp; <a name="operator-eq"></a>QDeepCopy::operator= ( const&nbsp;T&nbsp;&amp;&nbsp;t )
</h3>

<p> Assigns a <a href="shclass.html#deep-copy">deep copy</a> of <em>t</em>.

<!-- eof -->
<hr><p>
This file is part of the <a href="index.html">Qt toolkit</a>.
Copyright &copy; 1995-2007
<a href="http://www.trolltech.com/">Trolltech</a>. All Rights Reserved.<p><address><hr><div align=center>
<table width=100% cellspacing=0 border=0><tr>
<td>Copyright &copy; 2007
<a href="troll.html">Trolltech</a><td align=center><a href="trademarks.html">Trademarks</a>
<td align=right><div align=right>Qt 3.3.8</div>
</table></div></address></body>
</html>