1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the Qt 3 compatibility classes of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "q3deepcopy.h"
32
\class Q3DeepCopy qdeepcopy.h
33
\brief The Q3DeepCopy class is a template class which ensures that
34
implicitly shared and explicitly shared classes reference unique
41
Normally, shared copies reference the same data to optimize memory
42
use and for maximum speed. In the example below, \c s1, \c s2, \c
43
s3, \c s4 and \c s5 share data.
46
// all 5 strings share the same data
54
Q3DeepCopy can be used several ways to ensure that an object
55
references unique, unshared data. In the example below, \c s1, \c
56
s2 and \c s5 share data, while neither \c s3 nor \c s4 share data.
58
// s1, s2 and s5 share the same data, neither s3 nor s4 are shared
61
Q3DeepCopy<QString> s3 = s2; // s3 is a deep copy of s2
62
QString s4 = s3; // s4 is a deep copy of s3
66
In the example below, \c s1, \c s2 and \c s5 share data, and \c s3
69
// s1, s2 and s5 share the same data, s3 and s4 share the same data
72
QString s3 = Q3DeepCopy<QString>( s2 ); // s3 is a deep copy of s2
73
QString s4 = s3; // s4 is a shallow copy of s3
77
Q3DeepCopy can also provide safety in multithreaded applications
78
that use shared classes. In the example below, the variable \c
79
global_string is used safely since the data contained in \c
80
global_string is always a deep copy. This ensures that all threads
81
get a unique copy of the data, and that any assignments to \c
82
global_string will result in a deep copy.
85
Q3DeepCopy<QString> global_string; // global string data
86
QMutex global_mutex; // mutex to protext global_string
90
void setGlobalString( const QString &str )
93
global_string = str; // global_string is a deep copy of str
94
global_mutex.unlock();
102
QString str = global_string; // str is a deep copy of global_string
103
global_mutex.unlock();
105
// process the string data
108
// update global_string
109
setGlobalString( str );
113
\warning It is the application developer's responsibility to
114
protect the object shared across multiple threads.
116
The examples above use QString, which is an implicitly shared
117
class. The behavior of Q3DeepCopy is the same when using explicitly
118
shared classes like QByteArray.
120
Currently, Q3DeepCopy works with the following classes:
122
\i QMemArray (including subclasses like QByteArray and QCString)
125
\i QValueList (including subclasses like QStringList and QValueStack)
129
\sa \link threads.html Thread Support in Qt \endlink
133
\fn Q3DeepCopy::Q3DeepCopy()
135
Constructs an empty instance of type \e T.
139
\fn Q3DeepCopy::Q3DeepCopy( const T &t )
141
Constructs a deep copy of \a t.
145
\fn Q3DeepCopy<T>& Q3DeepCopy::operator=( const T &t )
147
Assigns a deep copy of \a t.
151
\fn Q3DeepCopy::operator T ()
153
Returns a deep copy of the encapsulated data.