Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
implicit-sharing.qdoc
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
3
4/* TODO: Move some of the documentation from QSharedDataPointer into this
5 document. */
6
7/*!
8 \group shared
9 \brief How to maximize resource usage by implicit data sharing.
10 \title Implicitly Shared Classes
11
12 These \l{Qt Core} classes provides a safe and efficient way of sharing and
13 manipulating data by \l{Implicit Sharing}{implicitly sharing} data.
14
15*/
16
17/*!
18 \page implicit-sharing.html
19 \title Implicit Sharing
20 \ingroup qt-basic-concepts
21
22 \brief Reference counting for fast copying.
23
24 \keyword implicit data sharing
25 \keyword implicit sharing
26 \keyword implicitly shared
27 \keyword reference counting
28 \keyword shared implicitly
29 \keyword shared classes
30
31 Many C++ classes in Qt use implicit data sharing to maximize
32 resource usage and minimize copying. Implicitly shared classes are
33 both safe and efficient when passed as arguments, because only a
34 pointer to the data is passed around, and the data is copied only
35 if and when a function writes to it, i.e., \e {copy-on-write}.
36
37 \tableofcontents
38
39 \section1 Overview
40
41 A shared class consists of a pointer to a shared data block that
42 contains a reference count and the data.
43
44 When a shared object is created, it sets the reference count to 1. The
45 reference count is incremented whenever a new object references the
46 shared data, and decremented when the object dereferences the shared
47 data. The shared data is deleted when the reference count becomes
48 zero.
49
50 \target deep copy
51 \target shallow copy
52
53 When dealing with shared objects, there are two ways of copying an
54 object. We usually speak about \e deep and \e shallow copies. A deep
55 copy implies duplicating an object. A shallow copy is a reference
56 copy, i.e. just a pointer to a shared data block. Making a deep copy
57 can be expensive in terms of memory and CPU. Making a shallow copy is
58 very fast, because it only involves setting a pointer and incrementing
59 the reference count.
60
61 Object assignment (with operator=()) for implicitly shared objects is
62 implemented using shallow copies.
63
64 The benefit of sharing is that a program does not need to duplicate
65 data unnecessarily, which results in lower memory use and less copying
66 of data. Objects can easily be assigned, sent as function arguments,
67 and returned from functions.
68
69 Implicit sharing mostly takes place behind the scenes;
70 the programmer rarely needs to worry about it. However, Qt's
71 container iterators have different behavior than those from
72 the STL. Read \l{Implicit sharing iterator problem}.
73
74 In multithreaded applications, implicit sharing takes place, as explained in
75 \l{Thread-Support in Qt Modules#Threads and Implicitly Shared Classes}
76 {Threads and Implicitly Shared Classes}.
77
78 When implementing your own implicitly shared classes, use the
79 QSharedData and QSharedDataPointer classes.
80
81 \section1 Implicit Sharing in Detail
82
83 Implicit sharing automatically detaches the object from a shared
84 block if the object is about to change and the reference count is
85 greater than one. (This is often called \e {copy-on-write} or
86 \e {value semantics}.)
87
88 An implicitly shared class has control of its internal data. In
89 any member functions that modify its data, it automatically detaches
90 before modifying the data. Notice, however, the special case with
91 container iterators; see \l{Implicit sharing iterator problem}.
92
93 The QPen class, which uses implicit sharing, detaches from the shared
94 data in all member functions that change the internal data.
95
96 Code fragment:
97 \snippet code/doc_src_groups.cpp 0
98
99
100 \section1 List of Classes
101
102 The classes listed below automatically detach from common data if
103 an object is about to be changed. The programmer will not even
104 notice that the objects are shared. Thus you should treat
105 separate instances of them as separate objects. They will always
106 behave as separate objects but with the added benefit of sharing
107 data whenever possible. For this reason, you can pass instances
108 of these classes as arguments to functions by value without
109 concern for the copying overhead.
110
111 Example:
112 \snippet code/doc_src_groups.cpp 1
113
114 In this example, \c p1 and \c p2 share data until QPainter::begin()
115 is called for \c p2, because painting a pixmap will modify it.
116
117 \warning Be careful with copying an implicitly shared container
118 (QMap, QList, etc.) while you use
119 \l{STL-style iterators}{STL-style iterator}. See \l{Implicit sharing iterator problem}.
120
121 \target implicitly shared classes
122 \annotatedlist shared
123*/