1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this 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 have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | /* TODO: Move some of the documentation from QSharedDataPointer into this
|
---|
43 | document. */
|
---|
44 |
|
---|
45 | /*!
|
---|
46 | \group shared
|
---|
47 | \title Implicitly Shared Classes
|
---|
48 | */
|
---|
49 |
|
---|
50 | /*!
|
---|
51 | \page implicit-sharing.html
|
---|
52 | \title Implicit Sharing
|
---|
53 | \ingroup frameworks-technologies
|
---|
54 |
|
---|
55 | \brief Reference counting for fast copying.
|
---|
56 |
|
---|
57 | \keyword implicit data sharing
|
---|
58 | \keyword implicit sharing
|
---|
59 | \keyword implicitly shared
|
---|
60 | \keyword reference counting
|
---|
61 | \keyword shared implicitly
|
---|
62 | \keyword shared classes
|
---|
63 |
|
---|
64 | Many C++ classes in Qt use implicit data sharing to maximize
|
---|
65 | resource usage and minimize copying. Implicitly shared classes are
|
---|
66 | both safe and efficient when passed as arguments, because only a
|
---|
67 | pointer to the data is passed around, and the data is copied only
|
---|
68 | if and when a function writes to it, i.e., \e {copy-on-write}.
|
---|
69 |
|
---|
70 | \tableofcontents
|
---|
71 |
|
---|
72 | \section1 Overview
|
---|
73 |
|
---|
74 | A shared class consists of a pointer to a shared data block that
|
---|
75 | contains a reference count and the data.
|
---|
76 |
|
---|
77 | When a shared object is created, it sets the reference count to 1. The
|
---|
78 | reference count is incremented whenever a new object references the
|
---|
79 | shared data, and decremented when the object dereferences the shared
|
---|
80 | data. The shared data is deleted when the reference count becomes
|
---|
81 | zero.
|
---|
82 |
|
---|
83 | \keyword deep copy
|
---|
84 | \keyword shallow copy
|
---|
85 |
|
---|
86 | When dealing with shared objects, there are two ways of copying an
|
---|
87 | object. We usually speak about \e deep and \e shallow copies. A deep
|
---|
88 | copy implies duplicating an object. A shallow copy is a reference
|
---|
89 | copy, i.e. just a pointer to a shared data block. Making a deep copy
|
---|
90 | can be expensive in terms of memory and CPU. Making a shallow copy is
|
---|
91 | very fast, because it only involves setting a pointer and incrementing
|
---|
92 | the reference count.
|
---|
93 |
|
---|
94 | Object assignment (with operator=()) for implicitly shared objects is
|
---|
95 | implemented using shallow copies.
|
---|
96 |
|
---|
97 | The benefit of sharing is that a program does not need to duplicate
|
---|
98 | data unnecessarily, which results in lower memory use and less copying
|
---|
99 | of data. Objects can easily be assigned, sent as function arguments,
|
---|
100 | and returned from functions.
|
---|
101 |
|
---|
102 | Implicit sharing takes place behind the scenes; the programmer
|
---|
103 | does not need to worry about it. Even in multithreaded
|
---|
104 | applications, implicit sharing takes place, as explained in
|
---|
105 | \l{Thread-Support in Qt Modules#Threads and Implicitly Shared Classes}
|
---|
106 | {Threads and Implicitly Shared Classes}.
|
---|
107 |
|
---|
108 | When implementing your own implicitly shared classes, use the
|
---|
109 | QSharedData and QSharedDataPointer classes.
|
---|
110 |
|
---|
111 | \section1 Implicit Sharing in Detail
|
---|
112 |
|
---|
113 | Implicit sharing automatically detaches the object from a shared
|
---|
114 | block if the object is about to change and the reference count is
|
---|
115 | greater than one. (This is often called \e {copy-on-write} or
|
---|
116 | \e {value semantics}.)
|
---|
117 |
|
---|
118 | An implicitly shared class has total control of its internal data. In
|
---|
119 | any member functions that modify its data, it automatically detaches
|
---|
120 | before modifying the data.
|
---|
121 |
|
---|
122 | The QPen class, which uses implicit sharing, detaches from the shared
|
---|
123 | data in all member functions that change the internal data.
|
---|
124 |
|
---|
125 | Code fragment:
|
---|
126 | \snippet doc/src/snippets/code/doc_src_groups.qdoc 0
|
---|
127 |
|
---|
128 |
|
---|
129 | \section1 List of Classes
|
---|
130 |
|
---|
131 | The classes listed below automatically detach from common data if
|
---|
132 | an object is about to be changed. The programmer will not even
|
---|
133 | notice that the objects are shared. Thus you should treat
|
---|
134 | separate instances of them as separate objects. They will always
|
---|
135 | behave as separate objects but with the added benefit of sharing
|
---|
136 | data whenever possible. For this reason, you can pass instances
|
---|
137 | of these classes as arguments to functions by value without
|
---|
138 | concern for the copying overhead.
|
---|
139 |
|
---|
140 | Example:
|
---|
141 | \snippet doc/src/snippets/code/doc_src_groups.qdoc 1
|
---|
142 |
|
---|
143 | In this example, \c p1 and \c p2 share data until QPainter::begin()
|
---|
144 | is called for \c p2, because painting a pixmap will modify it.
|
---|
145 |
|
---|
146 | \warning Do not copy an implicitly shared container (QMap,
|
---|
147 | QVector, etc.) while you are iterating over it using an non-const
|
---|
148 | \l{STL-style iterator}.
|
---|
149 |
|
---|
150 | \keyword implicitly shared classes
|
---|
151 | \annotatedlist shared
|
---|
152 | */
|
---|