1QDeepCopy(3qt) QDeepCopy(3qt)
2
3
4
6 QDeepCopy - Template class which ensures that
7
9 All the functions in this class are reentrant when Qt is built with
10 thread support.</p>
11
12 #include <qdeepcopy.h>
13
14 Public Members
15 QDeepCopy ()
16 QDeepCopy ( const T & t )
17 QDeepCopy<T> & operator= ( const T & t )
18 operator T ()
19
21 The QDeepCopy class is a template class which ensures that implicitly
22 shared and explicitly shared classes reference unique data.
23
24 Normally, shared copies reference the same data to optimize memory use
25 and for maximum speed. In the example below, s1, s2, s3, s4 and s5
26 share data.
27
28 // all 5 strings share the same data
29 QString s1 = "abcd";
30 QString s2 = s1;
31 QString s3 = s2;
32 QString s4 = s3;
33 QString s5 = s2;
34
35 QDeepCopy can be used several ways to ensure that an object references
36 unique, unshared data. In the example below, s1, s2 and s5 share data,
37 while neither s3 nor s4 share data.
38
39 // s1, s2 and s5 share the same data, neither s3 nor s4 are shared
40 QString s1 = "abcd";
41 QString s2 = s1;
42 QDeepCopy<QString> s3 = s2; // s3 is a deep copy of s2
43 QString s4 = s3; // s4 is a deep copy of s3
44 QString s5 = s2;
45
46 In the example below, s1, s2 and s5 share data, and s3 and s4 share
47 data.
48
49 // s1, s2 and s5 share the same data, s3 and s4 share the same data
50 QString s1 = "abcd";
51 QString s2 = s1;
52 QString s3 = QDeepCopy<QString>( s2 ); // s3 is a deep copy of s2
53 QString s4 = s3; // s4 is a shallow copy of s3
54 QString s5 = s2;
55
56 QDeepCopy can also provide safety in multithreaded applications that
57 use shared classes. In the example below, the variable global_string is
58 used safely since the data contained in global_string is always a deep
59 copy. This ensures that all threads get a unique copy of the data, and
60 that any assignments to global_string will result in a deep copy.
61
62 QDeepCopy<QString> global_string; // global string data
63 QMutex global_mutex; // mutex to protext global_string
64 ...
65 void setGlobalString( const QString &str )
66 {
67 global_mutex.lock();
68 global_string = str; // global_string is a deep copy of str
69 global_mutex.unlock();
70 }
71 ...
72 void MyThread::run()
73 {
74 global_mutex.lock();
75 QString str = global_string; // str is a deep copy of global_string
76 global_mutex.unlock();
77 // process the string data
78 ...
79 // update global_string
80 setGlobalString( str );
81 }
82
83 Warning: It is the application developer's responsibility to protect
84 the object shared across multiple threads.
85
86 The examples above use QString, which is an implicitly shared class.
87 The behavior of QDeepCopy is the same when using explicitly shared
88 classes like QByteArray.
89
90 Currently, QDeepCopy works with the following classes:
91
92 QMemArray (including subclasses like QByteArray and QCString)
93
94 QMap
95
96 QString
97
98 QValueList (including subclasses like QStringList and QValueStack)
99
100 QValueVector
101
102 See also Thread Support in Qt, Implicitly and Explicitly Shared
103 Classes, and Non-GUI Classes.
104
107 Constructs an empty instance of type T.
108
110 Constructs a deep copy of t.
111
113 Returns a deep copy of the encapsulated data.
114
116 Assigns a deep copy of t.
117
118
120 http://doc.trolltech.com/qdeepcopy.html
121 http://www.trolltech.com/faq/tech.html
122
124 Copyright 1992-2007 Trolltech ASA, http://www.trolltech.com. See the
125 license file included in the distribution for a complete license
126 statement.
127
129 Generated automatically from the source code.
130
132 If you find a bug in Qt, please report it as described in
133 http://doc.trolltech.com/bughowto.html. Good bug reports help us to
134 help you. Thank you.
135
136 The definitive Qt documentation is provided in HTML format; it is
137 located at $QTDIR/doc/html and can be read using Qt Assistant or with a
138 web browser. This man page is provided as a convenience for those users
139 who prefer man pages, although this format is not officially supported
140 by Trolltech.
141
142 If you find errors in this manual page, please report them to qt-
143 bugs@trolltech.com. Please include the name of the manual page
144 (qdeepcopy.3qt) and the Qt version (3.3.8).
145
146
147
148Trolltech AS 2 February 2007 QDeepCopy(3qt)