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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
'\" t
.TH QDeepCopy 3qt "2 February 2007" "Trolltech AS" \" -*- nroff -*-
.\" Copyright 1992-2007 Trolltech ASA. All rights reserved. See the
.\" license file included in the distribution for a complete license
.\" statement.
.\"
.ad l
.nh
.SH NAME
QDeepCopy \- Template class which ensures that
.SH SYNOPSIS
All the functions in this class are reentrant when TQt is built with thread support.</p>
.PP
\fC#include <ntqdeepcopy.h>\fR
.PP
.SS "Public Members"
.in +1c
.ti -1c
.BI "\fBQDeepCopy\fR ()"
.br
.ti -1c
.BI "\fBQDeepCopy\fR ( const T & t )"
.br
.ti -1c
.BI "QDeepCopy<T> & \fBoperator=\fR ( const T & t )"
.br
.ti -1c
.BI "\fBoperator T\fR ()"
.br
.in -1c
.SH DESCRIPTION
The QDeepCopy class is a template class which ensures that implicitly shared and explicitly shared classes reference unique data.
.PP
Normally, shared copies reference the same data to optimize memory use and for maximum speed. In the example below, \fCs1\fR, \fCs2\fR, \fCs3\fR, \fCs4\fR and \fCs5\fR share data.
.PP
.nf
.br
// all 5 strings share the same data
.br
QString s1 = "abcd";
.br
QString s2 = s1;
.br
QString s3 = s2;
.br
QString s4 = s3;
.br
QString s5 = s2;
.br
.fi
.PP
QDeepCopy can be used several ways to ensure that an object references unique, unshared data. In the example below, \fCs1\fR, \fCs2\fR and \fCs5\fR share data, while neither \fCs3\fR nor \fCs4\fR share data.
.PP
.nf
.br
// s1, s2 and s5 share the same data, neither s3 nor s4 are shared
.br
QString s1 = "abcd";
.br
QString s2 = s1;
.br
QDeepCopy<QString> s3 = s2; // s3 is a deep copy of s2
.br
QString s4 = s3; // s4 is a deep copy of s3
.br
QString s5 = s2;
.br
.fi
.PP
In the example below, \fCs1\fR, \fCs2\fR and \fCs5\fR share data, and \fCs3\fR and \fCs4\fR share data.
.PP
.nf
.br
// s1, s2 and s5 share the same data, s3 and s4 share the same data
.br
QString s1 = "abcd";
.br
QString s2 = s1;
.br
QString s3 = QDeepCopy<QString>( s2 ); // s3 is a deep copy of s2
.br
QString s4 = s3; // s4 is a shallow copy of s3
.br
QString s5 = s2;
.br
.fi
.PP
QDeepCopy can also provide safety in multithreaded applications that use shared classes. In the example below, the variable \fCglobal_string\fR is used safely since the data contained in \fCglobal_string\fR is always a deep copy. This ensures that all threads get a unique copy of the data, and that any assignments to \fCglobal_string\fR will result in a deep copy.
.PP
.nf
.br
QDeepCopy<QString> global_string; // global string data
.br
QMutex global_mutex; // mutex to protext global_string
.br
.br
...
.br
.br
void setGlobalString( const QString &str )
.br
{
.br
global_mutex.lock();
.br
global_string = str; // global_string is a deep copy of str
.br
global_mutex.unlock();
.br
}
.br
.br
...
.br
.br
void MyThread::run()
.br
{
.br
global_mutex.lock();
.br
QString str = global_string; // str is a deep copy of global_string
.br
global_mutex.unlock();
.br
.br
// process the string data
.br
...
.br
.br
// update global_string
.br
setGlobalString( str );
.br
}
.br
.fi
.PP
\fBWarning:\fR It is the application developer's responsibility to protect the object shared across multiple threads.
.PP
The examples above use QString, which is an implicitly shared class. The behavior of QDeepCopy is the same when using explicitly shared classes like QByteArray.
.PP
Currently, QDeepCopy works with the following classes:
.TP
QMemArray (including subclasses like QByteArray and QCString)
.TP
QMap
.TP
QString
.TP
QValueList (including subclasses like QStringList and QValueStack)
.TP
QValueVector
.PP
See also Thread Support in Qt, Implicitly and Explicitly Shared Classes, and Non-GUI Classes.
.SH MEMBER FUNCTION DOCUMENTATION
.SH "QDeepCopy::QDeepCopy ()"
Constructs an empty instance of type \fIT\fR.
.SH "QDeepCopy::QDeepCopy ( const T & t )"
Constructs a deep copy of \fIt\fR.
.SH "QDeepCopy::operator T ()"
Returns a deep copy of the encapsulated data.
.SH "QDeepCopy<T> & QDeepCopy::operator= ( const T & t )"
Assigns a deep copy of \fIt\fR.
.SH "SEE ALSO"
.BR http://doc.trolltech.com/ntqdeepcopy.html
.BR http://www.trolltech.com/faq/tech.html
.SH COPYRIGHT
Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the
license file included in the distribution for a complete license
statement.
.SH AUTHOR
Generated automatically from the source code.
.SH BUGS
If you find a bug in Qt, please report it as described in
.BR http://doc.trolltech.com/bughowto.html .
Good bug reports help us to help you. Thank you.
.P
The definitive TQt documentation is provided in HTML format; it is
located at $TQTDIR/doc/html and can be read using TQt Assistant or with
a web browser. This man page is provided as a convenience for those
users who prefer man pages, although this format is not officially
supported by Trolltech.
.P
If you find errors in this manual page, please report them to
.BR qt-bugs@trolltech.com .
Please include the name of the manual page (tqdeepcopy.3qt) and the Qt
version (3.3.8).
|