1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the documentation of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:FDL$
9 ** GNU Free Documentation License
10 ** Alternatively, this file may be used under the terms of the GNU Free
11 ** Documentation License version 1.3 as published by the Free Software
12 ** Foundation and appearing in the file included in the packaging of
16 ** Alternatively, this file may be used in accordance with the terms
17 ** and conditions contained in a signed written agreement between you
26 ****************************************************************************/
28 /* TODO: Move some of the documentation from QSharedDataPointer into this
33 \title Implicitly Shared Classes
37 \page implicit-sharing.html
38 \title Implicit Sharing
39 \ingroup qt-basic-concepts
41 \brief Reference counting for fast copying.
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
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}.
60 A shared class consists of a pointer to a shared data block that
61 contains a reference count and the data.
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
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
80 Object assignment (with operator=()) for implicitly shared objects is
81 implemented using shallow copies.
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.
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}.
94 When implementing your own implicitly shared classes, use the
95 QSharedData and QSharedDataPointer classes.
97 \section1 Implicit Sharing in Detail
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}.)
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.
108 The QPen class, which uses implicit sharing, detaches from the shared
109 data in all member functions that change the internal data.
112 \snippet code/doc_src_groups.cpp 0
115 \section1 List of Classes
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.
127 \snippet code/doc_src_groups.cpp 1
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.
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}.
136 \keyword implicitly shared classes
137 \annotatedlist shared