source: trunk/doc/src/frameworks-technologies/implicit-sharing.qdoc@ 846

Last change on this file since 846 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

  • Property svn:eol-style set to native
File size: 5.2 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 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:FDL$
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 a
14** written agreement between you and Nokia.
15**
16** GNU Free Documentation License
17** Alternatively, this file may be used under the terms of the GNU Free
18** Documentation License version 1.3 as published by the Free Software
19** Foundation and appearing in the file included in the packaging of this
20** file.
21**
22** If you have questions regarding the use of this file, please contact
23** Nokia at [email protected].
24** $QT_END_LICENSE$
25**
26****************************************************************************/
27
28/* TODO: Move some of the documentation from QSharedDataPointer into this
29 document. */
30
31/*!
32 \group shared
33 \title Implicitly Shared Classes
34*/
35
36/*!
37 \page implicit-sharing.html
38 \title Implicit Sharing
39 \ingroup qt-basic-concepts
40
41 \brief Reference counting for fast copying.
42
43 \keyword implicit data sharing
44 \keyword implicit sharing
45 \keyword implicitly shared
46 \keyword reference counting
47 \keyword shared implicitly
48 \keyword shared classes
49
50 Many C++ classes in Qt use implicit data sharing to maximize
51 resource usage and minimize copying. Implicitly shared classes are
52 both safe and efficient when passed as arguments, because only a
53 pointer to the data is passed around, and the data is copied only
54 if and when a function writes to it, i.e., \e {copy-on-write}.
55
56 \tableofcontents
57
58 \section1 Overview
59
60 A shared class consists of a pointer to a shared data block that
61 contains a reference count and the data.
62
63 When a shared object is created, it sets the reference count to 1. The
64 reference count is incremented whenever a new object references the
65 shared data, and decremented when the object dereferences the shared
66 data. The shared data is deleted when the reference count becomes
67 zero.
68
69 \keyword deep copy
70 \keyword shallow copy
71
72 When dealing with shared objects, there are two ways of copying an
73 object. We usually speak about \e deep and \e shallow copies. A deep
74 copy implies duplicating an object. A shallow copy is a reference
75 copy, i.e. just a pointer to a shared data block. Making a deep copy
76 can be expensive in terms of memory and CPU. Making a shallow copy is
77 very fast, because it only involves setting a pointer and incrementing
78 the reference count.
79
80 Object assignment (with operator=()) for implicitly shared objects is
81 implemented using shallow copies.
82
83 The benefit of sharing is that a program does not need to duplicate
84 data unnecessarily, which results in lower memory use and less copying
85 of data. Objects can easily be assigned, sent as function arguments,
86 and returned from functions.
87
88 Implicit sharing takes place behind the scenes; the programmer
89 does not need to worry about it. Even in multithreaded
90 applications, implicit sharing takes place, as explained in
91 \l{Thread-Support in Qt Modules#Threads and Implicitly Shared Classes}
92 {Threads and Implicitly Shared Classes}.
93
94 When implementing your own implicitly shared classes, use the
95 QSharedData and QSharedDataPointer classes.
96
97 \section1 Implicit Sharing in Detail
98
99 Implicit sharing automatically detaches the object from a shared
100 block if the object is about to change and the reference count is
101 greater than one. (This is often called \e {copy-on-write} or
102 \e {value semantics}.)
103
104 An implicitly shared class has total control of its internal data. In
105 any member functions that modify its data, it automatically detaches
106 before modifying the data.
107
108 The QPen class, which uses implicit sharing, detaches from the shared
109 data in all member functions that change the internal data.
110
111 Code fragment:
112 \snippet doc/src/snippets/code/doc_src_groups.qdoc 0
113
114
115 \section1 List of Classes
116
117 The classes listed below automatically detach from common data if
118 an object is about to be changed. The programmer will not even
119 notice that the objects are shared. Thus you should treat
120 separate instances of them as separate objects. They will always
121 behave as separate objects but with the added benefit of sharing
122 data whenever possible. For this reason, you can pass instances
123 of these classes as arguments to functions by value without
124 concern for the copying overhead.
125
126 Example:
127 \snippet doc/src/snippets/code/doc_src_groups.qdoc 1
128
129 In this example, \c p1 and \c p2 share data until QPainter::begin()
130 is called for \c p2, because painting a pixmap will modify it.
131
132 \warning Do not copy an implicitly shared container (QMap,
133 QVector, etc.) while you are iterating over it using an non-const
134 \l{STL-style iterator}.
135
136 \keyword implicitly shared classes
137 \annotatedlist shared
138*/
Note: See TracBrowser for help on using the repository browser.