summaryrefslogtreecommitdiffstats
path: root/doc/html/ntqdeepcopy.html
blob: ac516d08963ba9a699cf27413865217139fdc30e (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>TQDeepCopy 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>TQDeepCopy Class Reference</h1>

<p>The TQDeepCopy 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 TQt is built with thread support.</p>
<p><tt>#include &lt;<a href="qdeepcopy-h.html">ntqdeepcopy.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="#TQDeepCopy"><b>TQDeepCopy</b></a> ()</li>
<li class=fn><a href="#TQDeepCopy-2"><b>TQDeepCopy</b></a> ( const&nbsp;T&nbsp;&amp;&nbsp;t )</li>
<li class=fn>TQDeepCopy&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 TQDeepCopy 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="ntqstring.html">TQString</a> s1 = "abcd";
    <a href="ntqstring.html">TQString</a> s2 = s1;
    <a href="ntqstring.html">TQString</a> s3 = s2;
    <a href="ntqstring.html">TQString</a> s4 = s3;
    <a href="ntqstring.html">TQString</a> s5 = s2;
    </pre>
 
<p> TQDeepCopy 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="ntqstring.html">TQString</a> s1 = "abcd";
    <a href="ntqstring.html">TQString</a> s2 = s1;
    TQDeepCopy&lt;TQString&gt; s3 = s2;  // s3 is a <a href="shclass.html#deep-copy">deep copy</a> of s2
    <a href="ntqstring.html">TQString</a> s4 = s3;             // s4 is a deep copy of s3
    <a href="ntqstring.html">TQString</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="ntqstring.html">TQString</a> s1 = "abcd";
    <a href="ntqstring.html">TQString</a> s2 = s1;
    <a href="ntqstring.html">TQString</a> s3 = TQDeepCopy&lt;TQString&gt;( s2 );  // s3 is a deep copy of s2
    <a href="ntqstring.html">TQString</a> s4 = s3;                        // s4 is a <a href="shclass.html#shallow-copy">shallow copy</a> of s3
    <a href="ntqstring.html">TQString</a> s5 = s2;
    </pre>
 
<p> TQDeepCopy 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>
    TQDeepCopy&lt;TQString&gt; global_string;  // global string data
    <a href="ntqmutex.html">TQMutex</a> global_mutex;               // mutex to protext global_string

    ...

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

    ...

    void MyThread::run()
    {
        global_mutex.<a href="ntqmutex.html#lock">lock</a>();
        <a href="ntqstring.html">TQString</a> str = global_string;          // str is a deep copy of global_string
        global_mutex.<a href="ntqmutex.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="ntqstring.html">TQString</a>, which is an implicitly shared
class. The behavior of TQDeepCopy is the same when using explicitly
shared classes like <a href="qbytearray.html">TQByteArray</a>.
<p> Currently, TQDeepCopy works with the following classes:
<ul>
<li> <a href="ntqmemarray.html">TQMemArray</a> (including subclasses like TQByteArray and <a href="ntqcstring.html">TQCString</a>)
<li> <a href="tqmap.html">TQMap</a>
<li> TQString
<li> <a href="tqvaluelist.html">TQValueList</a> (including subclasses like <a href="ntqstringlist.html">TQStringList</a> and <a href="tqvaluestack.html">TQValueStack</a>)
<li> <a href="tqvaluevector.html">TQValueVector</a>
</ul>
<p> <p>See also <a href="threads.html">Thread Support in TQt</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="TQDeepCopy"></a>TQDeepCopy::TQDeepCopy ()
</h3>

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

<h3 class=fn><a name="TQDeepCopy-2"></a>TQDeepCopy::TQDeepCopy ( 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>TQDeepCopy::operator T ()
</h3>

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

<h3 class=fn><a href="ntqdeepcopy.html">TQDeepCopy</a>&lt;T&gt;&nbsp;&amp; <a name="operator-eq"></a>TQDeepCopy::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">TQt 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>TQt 3.3.8</div>
</table></div></address></body>
</html>