source: trunk/src/corelib/tools/qshareddata.cpp@ 421

Last change on this file since 421 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 21.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtCore module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <qshareddata.h>
43
44QT_BEGIN_NAMESPACE
45
46/*!
47 \class QSharedData
48 \brief The QSharedData class is a base class for shared data objects.
49 \reentrant
50 \ingroup misc
51
52 QSharedData is designed to be used with QSharedDataPointer or
53 QExplicitlySharedDataPointer to implement custom \l{implicitly
54 shared} or \l {explicitly shared} classes. QSharedData provides
55 \l{thread-safe} reference counting.
56
57 See QSharedDataPointer and QExplicitlySharedDataPointer for details.
58*/
59
60/*! \fn QSharedData::QSharedData()
61 Constructs a QSharedData object with a reference count of 0.
62*/
63
64/*! \fn QSharedData::QSharedData(const QSharedData& other)
65 Constructs a QSharedData object with reference count 0.
66 \a other is ignored.
67*/
68
69/*!
70 \class QSharedDataPointer
71 \brief The QSharedDataPointer class represents a pointer to an implicitly shared object.
72 \since 4.0
73 \reentrant
74 \ingroup misc
75 \mainclass
76
77 QSharedDataPointer\<T\> makes writing your own \l {implicitly
78 shared} classes easy. QSharedDataPointer implements \l {thread-safe}
79 reference counting, ensuring that adding QSharedDataPointers to your
80 \l {reentrant} classes won't make them non-reentrant.
81
82 \l {Implicit sharing} is used by many Qt classes to combine the
83 speed and memory efficiency of pointers with the ease of use of
84 classes. See the \l{Shared Classes} page for more information.
85
86 \target Employee example
87 Suppose you want to make an \c Employee class implicitly shared. The
88 procedure is:
89
90 \list
91
92 \o Define the class \c Employee to have a single data member of
93 type \c {QSharedDataPointer<EmployeeData>}.
94
95 \o Define the \c EmployeeData class derived from \l QSharedData to
96 contain all the data members you would normally have put in the
97 \c Employee class.
98
99 \endlist
100
101 To show this in practice, we review the source code for the
102 implicitly shared \c Employee class. In the header file we define the
103 two classes \c Employee and \c EmployeeData.
104
105 \snippet doc/src/snippets/sharedemployee/employee.h 0
106
107 In class \c Employee, note the single data member, a \e {d pointer}
108 of type \c {QSharedDataPointer<EmployeeData>}. All accesses of
109 employee data must go through the \e {d pointer's} \c
110 {operator->()}. For write accesses, \c {operator->()} will
111 automatically call detach(), which creates a copy of the shared data
112 object if the shared data object's reference count is greater than
113 1. This ensures that writes to one \c Employee object don't affect
114 any other \c Employee objects that share the same \c EmployeeData
115 object.
116
117 Class \c EmployeeData inherits QSharedData, which provides the
118 \e{behind the scenes} reference counter. \c EmployeeData has a default
119 constructor, a copy constructor, and a destructor. Normally, trivial
120 implementations of these are all that is needed in the \e {data}
121 class for an implicitly shared class.
122
123 Implementing the two constructors for class \c Employee is also
124 straightforward. Both create a new instance of \c EmployeeData
125 and assign it to the \e{d pointer} .
126
127 \snippet doc/src/snippets/sharedemployee/employee.h 1
128 \codeline
129 \snippet doc/src/snippets/sharedemployee/employee.h 2
130
131 Note that class \c Employee also has a trivial copy constructor
132 defined, which is not strictly required in this case.
133
134 \snippet doc/src/snippets/sharedemployee/employee.h 7
135
136 The copy constructor is not strictly required here, because class \c
137 EmployeeData is included in the same file as class \c Employee
138 (\c{employee.h}). However, including the private subclass of
139 QSharedData in the same file as the public class containing the
140 QSharedDataPointer is not typical. Normally, the idea is to hide the
141 private subclass of QSharedData from the user by putting it in a
142 separate file which would not be included in the public file. In
143 this case, we would normally put class \c EmployeeData in a separate
144 file, which would \e{not} be included in \c{employee.h}. Instead, we
145 would just predeclare the private subclass \c EmployeeData in \c
146 {employee.h} this way:
147
148 \code
149 class EmployeeData;
150 \endcode
151
152 If we had done it that way here, the copy constructor shown would be
153 required. Since the copy constructor is trivial, you might as well
154 just always include it.
155
156 Behind the scenes, QSharedDataPointer automatically increments the
157 reference count whenever an \c Employee object is copied, assigned,
158 or passed as a parameter. It decrements the reference count whenever
159 an \c Employee object is deleted or goes out of scope. The shared
160 \c EmployeeData object is deleted automatically if and when the
161 reference count reaches 0.
162
163 In a non-const member function of \c Employee, whenever the \e {d
164 pointer} is dereferenced, QSharedDataPointer automatically calls
165 detach() to ensure that the function operates on its own copy of the
166 data.
167
168 \snippet doc/src/snippets/sharedemployee/employee.h 3
169 \codeline
170 \snippet doc/src/snippets/sharedemployee/employee.h 4
171
172 Note that if detach() is called more than once in a member function
173 due to multiple dereferences of the \e {d pointer}, detach() will
174 only create a copy of the shared data the first time it is called,
175 if at all, because on the second and subsequent calls of detach(),
176 the reference count will be 1 again.
177
178 But note that in the second \c Employee constructor, which takes an
179 employee ID and a name, both setId() and setName() are called, but
180 they don't cause \e{copy on write}, because the reference count for
181 the newly constructed \c EmployeeData object has just been set to 1.
182
183 In \c Employee's \e const member functions, dereferencing the \e {d
184 pointer} does \e not cause detach() to be called.
185
186 \snippet doc/src/snippets/sharedemployee/employee.h 5
187 \codeline
188 \snippet doc/src/snippets/sharedemployee/employee.h 6
189
190 Notice that there is no need to implement a copy constructor or an
191 assignment operator for the \c Employee class, because the copy
192 constructor and assignment operator provided by the C++ compiler
193 will do the \e{member by member} shallow copy required. The only
194 member to copy is the \e {d pointer}, which is a QSharedDataPointer,
195 whose \c {operator=()} just increments the reference count of the
196 shared \c EmployeeData object.
197
198 \target Implicit vs Explicit Sharing
199 \section1 Implicit vs Explicit Sharing
200
201 Implicit sharing might not be right for the \c Employee class.
202 Consider a simple example that creates two instances of the
203 implicitly shared \c Employee class.
204
205 \snippet doc/src/snippets/sharedemployee/main.cpp 0
206
207 After the second employee e2 is created and e1 is assigned to it,
208 both \c e1 and \c e2 refer to Albrecht Durer, employee 1001. Both \c
209 Employee objects point to the same instance of \c EmployeeData,
210 which has reference count 2. Then \c {e1.setName("Hans Holbein")} is
211 called to change the employee name, but because the reference count
212 is greater than 1, a \e{copy on write} is performed before the name
213 is changed. Now \c e1 and \c e2 point to different \c EmployeeData
214 objects. They have different names, but both have ID 1001, which is
215 probably not what you want. You can, of course, just continue with
216 \c {e1.setId(1002)}, if you really mean to create a second, unique
217 employee, but if you only want to change the employee's name
218 everywhere, consider using \l {QExplicitlySharedDataPointer}
219 {explicit sharing} in the \c Employee class instead of implicit
220 sharing.
221
222 If you declare the \e {d pointer} in the \c Employee class to be
223 \c {QExplicitlySharedDataPointer<EmployeeData>}, then explicit
224 sharing is used and \e{copy on write} operations are not performed
225 automatically (i.e. detach() is not called in non-const
226 functions). In that case, after \c {e1.setName("Hans Holbein")}, the
227 employee's name has been changed, but both e1 and e2 still refer to
228 the same instance of \c EmployeeData, so there is only one employee
229 with ID 1001.
230
231 In the member function documentation, \e{d pointer} always refers
232 to the internal pointer to the shared data object.
233
234 \sa QSharedData, QExplicitlySharedDataPointer
235*/
236
237/*! \fn T& QSharedDataPointer::operator*()
238 Provides access to the shared data object's members.
239 This function calls detach().
240*/
241
242/*! \fn const T& QSharedDataPointer::operator*() const
243 Provides const access to the shared data object's members.
244 This function does \e not call detach().
245*/
246
247/*! \fn T* QSharedDataPointer::operator->()
248 Provides access to the shared data object's members.
249 This function calls detach().
250*/
251
252/*! \fn const T* QSharedDataPointer::operator->() const
253 Provides const access to the shared data object's members.
254 This function does \e not call detach().
255*/
256
257/*! \fn QSharedDataPointer::operator T*()
258 Returns a pointer to the shared data object.
259 This function calls detach().
260
261 \sa data(), constData()
262*/
263
264/*! \fn QSharedDataPointer::operator const T*() const
265 Returns a pointer to the shared data object.
266 This function does \e not call detach().
267*/
268
269/*! \fn T* QSharedDataPointer::data()
270 Returns a pointer to the shared data object.
271 This function calls detach().
272
273 \sa constData()
274*/
275
276/*! \fn const T* QSharedDataPointer::data() const
277 Returns a pointer to the shared data object.
278 This function does \e not call detach().
279*/
280
281/*! \fn const T* QSharedDataPointer::constData() const
282 Returns a const pointer to the shared data object.
283 This function does \e not call detach().
284
285 \sa data()
286*/
287
288/*! \fn bool QSharedDataPointer::operator==(const QSharedDataPointer<T>& other) const
289 Returns true if \a other and \e this have the same \e{d pointer}.
290 This function does \e not call detach().
291*/
292
293/*! \fn bool QSharedDataPointer::operator!=(const QSharedDataPointer<T>& other) const
294 Returns true if \a other and \e this do \e not have the same
295 \e{d pointer}. This function does \e not call detach().
296*/
297
298/*! \fn QSharedDataPointer::QSharedDataPointer()
299 Constructs a QSharedDataPointer initialized with a null \e{d pointer}.
300*/
301
302/*! \fn QSharedDataPointer::~QSharedDataPointer()
303 Decrements the reference count of the shared data object.
304 If the reference count becomes 0, the shared data object
305 is deleted. \e This is then destroyed.
306*/
307
308/*! \fn QSharedDataPointer::QSharedDataPointer(T* sharedData)
309 Constructs a QSharedDataPointer with \e{d pointer} set to
310 \a sharedData and increments \a{sharedData}'s reference count.
311*/
312
313/*! \fn QSharedDataPointer::QSharedDataPointer(const QSharedDataPointer<T>& other)
314 Sets the \e{d pointer} of \e this to the \e{d pointer} in
315 \a other and increments the reference count of the shared
316 data object.
317*/
318
319/*! \fn QSharedDataPointer<T>& QSharedDataPointer::operator=(const QSharedDataPointer<T>& other)
320 Sets the \e{d pointer} of \e this to the \e{d pointer} of
321 \a other and increments the reference count of the shared
322 data object. The reference count of the old shared data
323 object of \e this is decremented. If the reference count
324 of the old shared data object becomes 0, the old shared
325 data object is deleted.
326*/
327
328/*! \fn QSharedDataPointer& QSharedDataPointer::operator=(T* sharedData)
329 Sets the \e{d pointer} og \e this to \a sharedData and increments
330 \a{sharedData}'s reference count. The reference count of the old
331 shared data object of \e this is decremented. If the reference
332 count of the old shared data object becomes 0, the old shared data
333 object is deleted.
334*/
335
336/*! \fn bool QSharedDataPointer::operator!() const
337 Returns true if the \e{d pointer} of \e this is null.
338*/
339
340/*! \fn void QSharedDataPointer::detach()
341 If the shared data object's reference count is greater than 1, this
342 function creates a deep copy of the shared data object and sets the
343 \e{d pointer} of \e this to the copy.
344
345 This function is called automatically by non-const member
346 functions of QSharedDataPointer if \e{copy on write} is
347 required. You don't need to call it yourself.
348*/
349
350/*! \fn T *QSharedDataPointer::clone()
351 \since 4.5
352
353 Creates and returns a deep copy of the current data. This function
354 is called by detach() when the reference count is greater than 1 in
355 order to create the new copy. This function uses the \e {operator
356 new} and calls the copy constructor of the type T.
357
358 This function is provided so that you may support "virtual copy
359 constructors" for your own types. In order to so, you should declare
360 a template-specialization of this function for your own type, like
361 the example below:
362
363 \code
364 template<>
365 EmployeeData *QSharedDataPointer<EmployeeData>::clone()
366 {
367 return d->clone();
368 }
369 \endcode
370
371 In the example above, the template specialization for the clone()
372 function calls the \e {EmployeeData::clone()} virtual function. A
373 class derived from EmployeeData could override that function and
374 return the proper polymorphic type.
375*/
376
377/*!
378 \class QExplicitlySharedDataPointer
379 \brief The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object.
380 \since 4.4
381 \reentrant
382 \ingroup misc
383 \mainclass
384
385 QExplicitlySharedDataPointer\<T\> makes writing your own explicitly
386 shared classes easy. QExplicitlySharedDataPointer implements
387 \l {thread-safe} reference counting, ensuring that adding
388 QExplicitlySharedDataPointers to your \l {reentrant} classes won't
389 make them non-reentrant.
390
391 Except for one big difference, QExplicitlySharedDataPointer is just
392 like QSharedDataPointer. The big difference is that member functions
393 of QExplicitlySharedDataPointer \e{do not} do the automatic
394 \e{copy on write} operation (detach()) that non-const members of
395 QSharedDataPointer do before allowing the shared data object to be
396 modified. There is a detach() function available, but if you really
397 want to detach(), you have to call it yourself. This means that
398 QExplicitlySharedDataPointers behave like regular C++ pointers,
399 except that by doing reference counting and not deleting the shared
400 data object until the reference count is 0, they avoid the dangling
401 pointer problem.
402
403 It is instructive to compare QExplicitlySharedDataPointer with
404 QSharedDataPointer by way of an example. Consider the \l {Employee
405 example} in QSharedDataPointer, modified to use explicit sharing as
406 explained in the discussion \l {Implicit vs Explicit Sharing}.
407
408 Note that if you use this class but find you are calling detach() a
409 lot, you probably should be using QSharedDataPointer instead.
410
411 In the member function documentation, \e{d pointer} always refers
412 to the internal pointer to the shared data object.
413
414 \sa QSharedData, QSharedDataPointer
415*/
416
417/*! \fn T& QExplicitlySharedDataPointer::operator*() const
418 Provides access to the shared data object's members.
419*/
420
421/*! \fn T* QExplicitlySharedDataPointer::operator->()
422 Provides access to the shared data object's members.
423*/
424
425/*! \fn const T* QExplicitlySharedDataPointer::operator->() const
426 Provides const access to the shared data object's members.
427*/
428
429/*! \fn T* QExplicitlySharedDataPointer::data() const
430 Returns a pointer to the shared data object.
431*/
432
433/*! \fn const T* QExplicitlySharedDataPointer::constData() const
434 Returns a const pointer to the shared data object.
435
436 \sa data()
437*/
438
439/*! \fn bool QExplicitlySharedDataPointer::operator==(const QExplicitlySharedDataPointer<T>& other) const
440 Returns true if \a other and \e this have the same \e{d pointer}.
441*/
442
443/*! \fn bool QExplicitlySharedDataPointer::operator==(const T* ptr) const
444 Returns true if the \e{d pointer} of \e this is \a ptr.
445 */
446
447/*! \fn bool QExplicitlySharedDataPointer::operator!=(const QExplicitlySharedDataPointer<T>& other) const
448 Returns true if \a other and \e this do \e not have the same
449 \e{d pointer}.
450*/
451
452/*! \fn bool QExplicitlySharedDataPointer::operator!=(const T* ptr) const
453 Returns true if the \e{d pointer} of \e this is \e not \a ptr.
454 */
455
456/*! \fn QExplicitlySharedDataPointer::QExplicitlySharedDataPointer()
457 Constructs a QExplicitlySharedDataPointer initialized with a null
458 \e{d pointer}.
459*/
460
461/*! \fn QExplicitlySharedDataPointer::~QExplicitlySharedDataPointer()
462 Decrements the reference count of the shared data object.
463 If the reference count becomes 0, the shared data object
464 is deleted. \e This is then destroyed.
465*/
466
467/*! \fn QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(T* sharedData)
468 Constructs a QExplicitlySharedDataPointer with \e{d pointer}
469 set to \a sharedData and increments \a{sharedData}'s reference
470 count.
471*/
472
473/*! \fn QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<T>& other)
474 This standard copy constructor sets the \e {d pointer} of \e this to
475 the \e {d pointer} in \a other and increments the reference count of
476 the shared data object.
477*/
478
479/*! \fn QExplicitlySharedDataPointer::QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<X>& other)
480 This copy constructor is different in that it allows \a other to be
481 a different type of explicitly shared data pointer but one that has
482 a compatible shared data object. It performs a static cast of the
483 \e{d pointer} in \a other and sets the \e {d pointer} of \e this to
484 the converted \e{d pointer}. It increments the reference count of
485 the shared data object.
486 */
487
488/*! \fn QExplicitlySharedDataPointer<T>& QExplicitlySharedDataPointer::operator=(const QExplicitlySharedDataPointer<T>& other)
489 Sets the \e{d pointer} of \e this to the \e{d pointer} of
490 \a other and increments the reference count of the shared
491 data object. The reference count of the old shared data
492 object of \e this is decremented. If the reference count
493 of the old shared data object becomes 0, the old shared
494 data object is deleted.
495*/
496
497/*! \fn QExplicitlySharedDataPointer& QExplicitlySharedDataPointer::operator=(T* sharedData)
498 Sets the \e{d pointer} of \e this to \a sharedData and
499 increments \a{sharedData}'s reference count. The reference
500 count of the old shared data object of \e this is decremented.
501 If the reference count of the old shared data object becomes
502 0, the old shared data object is deleted.
503*/
504
505/*! \fn void QExplicitlySharedDataPointer::reset()
506 Resets \e this to be null. i.e., this function sets the
507 \e{d pointer} of \e this to 0, but first it decrements
508 the reference count of the shared data object and deletes
509 the shared data object if the reference count became 0.
510 */
511
512/*! \fn QExplicitlySharedDataPointer::operator bool () const
513 Returns true if the \e{d pointer} of \e this is \e not null.
514 */
515
516/*! \fn bool QExplicitlySharedDataPointer::operator!() const
517 Returns true if the \e{d pointer} of \e this is null.
518*/
519
520/*! \fn void QExplicitlySharedDataPointer::detach()
521 If the shared data object's reference count is greater than 1, this
522 function creates a deep copy of the shared data object and sets the
523 \e{d pointer} of \e this to the copy.
524
525 Because QExplicitlySharedDataPointer does not do the automatic
526 \e{copy on write} operations that members of QSharedDataPointer do,
527 detach() is \e not called automatically anywhere in the member
528 functions of this class. If you find that you are calling detach()
529 everywhere in your code, consider using QSharedDataPointer instead.
530*/
531
532/*! \fn T *QExplicitlySharedDataPointer::clone()
533 \since 4.5
534
535 Creates and returns a deep copy of the current data. This function
536 is called by detach() when the reference count is greater than 1 in
537 order to create the new copy. This function uses the \e {operator
538 new} and calls the copy constructor of the type T.
539
540 See QSharedDataPointer::clone() for an explanation of how to use it.
541*/
542
543/*!
544 \typedef QExplicitlySharedDataPointer::Type
545
546 This is the type of the shared data object. The \e{d pointer}
547 points to an object of this type.
548 */
549
550QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.