Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qshareddata.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4
#
include
<
qshareddata
.
h
>
5
6
QT_BEGIN_NAMESPACE
7
8
/*!
9
\class QSharedData
10
\inmodule QtCore
11
\brief The QSharedData class is a base class for shared data objects.
12
\reentrant
13
14
QSharedData is designed to be used with QSharedDataPointer or
15
QExplicitlySharedDataPointer to implement custom \l{implicitly
16
shared} or explicitly shared classes. QSharedData provides
17
\l{thread-safe} reference counting.
18
19
See QSharedDataPointer and QExplicitlySharedDataPointer for details.
20
*/
21
22
/*! \fn QSharedData::QSharedData()
23
Constructs a QSharedData object with a reference count of 0.
24
*/
25
26
/*! \fn QSharedData::QSharedData(const QSharedData& )
27
Constructs a QSharedData object with reference count 0.
28
The parameter is ignored.
29
*/
30
31
/*!
32
\class QAdoptSharedDataTag
33
\inmodule QtCore
34
\threadsafe
35
\brief The QAdoptSharedDataTag is a helper tag class.
36
\since 6.0
37
38
QAdoptSharedDataTag objects are used in QSharedDataPointer
39
and QExplicitlySharedDataPointer to adopt a pointer to
40
shared data.
41
42
See QSharedDataPointer and QExplicitlySharedDataPointer for details.
43
*/
44
45
/*!
46
\class QSharedDataPointer
47
\inmodule QtCore
48
\brief The QSharedDataPointer class represents a pointer to an implicitly shared object.
49
\since 4.0
50
\reentrant
51
52
\compares strong
53
\compareswith strong T* std::nullptr_t
54
\endcompareswith
55
56
QSharedDataPointer is a template class where the template parameter
57
\a T specifies the type of the shared data class that derives from
58
QSharedData.
59
60
QSharedDataPointer<T> makes writing your own \l {implicitly
61
shared} classes easy. QSharedDataPointer implements \l {thread-safe}
62
reference counting, ensuring that adding QSharedDataPointers to your
63
\l {reentrant} classes won't make them non-reentrant.
64
65
\l {Implicit sharing} is used by many Qt classes to combine the
66
speed and memory efficiency of pointers with the ease of use of
67
classes. See the \l{Shared Classes} page for more information.
68
69
\target Employee example
70
Suppose you want to make an \c Employee class implicitly shared. The
71
procedure is:
72
73
\list
74
75
\li Define the class \c Employee to have a single data member of
76
type \c {QSharedDataPointer<EmployeeData>}.
77
78
\li Define the \c EmployeeData class derived from \l QSharedData to
79
contain all the data members you would normally have put in the
80
\c Employee class.
81
82
\endlist
83
84
To show this in practice, we review the source code for the
85
implicitly shared \c Employee class. In the header file we define the
86
two classes \c Employee and \c EmployeeData.
87
88
\snippet sharedemployee/employee.h 0
89
90
In class \c Employee, note the single data member, a \e {d pointer}
91
of type \c {QSharedDataPointer<EmployeeData>}. All accesses of
92
employee data must go through the \e {d pointer's} \c
93
{operator->()}. For write accesses, \c {operator->()} will
94
automatically call detach(), which creates a copy of the shared data
95
object if the shared data object's reference count is greater than
96
1. This ensures that writes to one \c Employee object don't affect
97
any other \c Employee objects that share the same \c EmployeeData
98
object.
99
100
Class \c EmployeeData inherits QSharedData, which provides the
101
\e{behind the scenes} reference counter. \c EmployeeData has a default
102
constructor, a copy constructor, and a destructor. Normally, trivial
103
implementations of these are all that is needed in the \e {data}
104
class for an implicitly shared class.
105
106
Implementing the two constructors for class \c Employee is also
107
straightforward. Both create a new instance of \c EmployeeData
108
and assign it to the \e{d pointer} .
109
110
\snippet sharedemployee/employee.h 1
111
\codeline
112
\snippet sharedemployee/employee.h 2
113
114
Note that class \c Employee also has a trivial copy constructor
115
defined, which is not strictly required in this case.
116
117
\snippet sharedemployee/employee.h 7
118
119
The copy constructor is not strictly required here, because class \c
120
EmployeeData is included in the same file as class \c Employee
121
(\c{employee.h}). However, including the private subclass of
122
QSharedData in the same file as the public class containing the
123
QSharedDataPointer is not typical. Normally, the idea is to hide the
124
private subclass of QSharedData from the user by putting it in a
125
separate file which would not be included in the public file. In
126
this case, we would normally put class \c EmployeeData in a separate
127
file, which would \e{not} be included in \c{employee.h}. Instead, we
128
would just predeclare the private subclass \c EmployeeData in \c
129
{employee.h} this way:
130
131
\snippet code/src_corelib_tools_qshareddata.cpp 0
132
133
If we had done it that way here, the copy constructor shown would be
134
required. Since the copy constructor is trivial, you might as well
135
just always include it.
136
137
Behind the scenes, QSharedDataPointer automatically increments the
138
reference count whenever an \c Employee object is copied, assigned,
139
or passed as a parameter. It decrements the reference count whenever
140
an \c Employee object is deleted or goes out of scope. The shared
141
\c EmployeeData object is deleted automatically if and when the
142
reference count reaches 0.
143
144
In a non-const member function of \c Employee, whenever the \e {d
145
pointer} is dereferenced, QSharedDataPointer automatically calls
146
detach() to ensure that the function operates on its own copy of the
147
data.
148
149
\snippet sharedemployee/employee.h 3
150
\codeline
151
\snippet sharedemployee/employee.h 4
152
153
Note that if detach() is called more than once in a member function
154
due to multiple dereferences of the \e {d pointer}, detach() will
155
only create a copy of the shared data the first time it is called,
156
if at all, because on the second and subsequent calls of detach(),
157
the reference count will be 1 again.
158
159
But note that in the second \c Employee constructor, which takes an
160
employee ID and a name, both setId() and setName() are called, but
161
they don't cause \e{copy on write}, because the reference count for
162
the newly constructed \c EmployeeData object has just been set to 1.
163
164
In \c Employee's \e const member functions, dereferencing the \e {d
165
pointer} does \e not cause detach() to be called.
166
167
\snippet sharedemployee/employee.h 5
168
\codeline
169
\snippet sharedemployee/employee.h 6
170
171
Notice that there is no need to implement a copy constructor or an
172
assignment operator for the \c Employee class, because the copy
173
constructor and assignment operator provided by the C++ compiler
174
will do the \e{member by member} shallow copy required. The only
175
member to copy is the \e {d pointer}, which is a QSharedDataPointer,
176
whose \c {operator=()} just increments the reference count of the
177
shared \c EmployeeData object.
178
179
\target Implicit vs Explicit Sharing
180
\section1 Implicit vs Explicit Sharing
181
182
Implicit sharing might not be right for the \c Employee class.
183
Consider a simple example that creates two instances of the
184
implicitly shared \c Employee class.
185
186
\snippet sharedemployee/main.cpp 0
187
188
After the second employee e2 is created and e1 is assigned to it,
189
both \c e1 and \c e2 refer to Albrecht Durer, employee 1001. Both \c
190
Employee objects point to the same instance of \c EmployeeData,
191
which has reference count 2. Then \c {e1.setName("Hans Holbein")} is
192
called to change the employee name, but because the reference count
193
is greater than 1, a \e{copy on write} is performed before the name
194
is changed. Now \c e1 and \c e2 point to different \c EmployeeData
195
objects. They have different names, but both have ID 1001, which is
196
probably not what you want. You can, of course, just continue with
197
\c {e1.setId(1002)}, if you really mean to create a second, unique
198
employee, but if you only want to change the employee's name
199
everywhere, consider using \l {QExplicitlySharedDataPointer}
200
{explicit sharing} in the \c Employee class instead of implicit
201
sharing.
202
203
If you declare the \e {d pointer} in the \c Employee class to be
204
\c {QExplicitlySharedDataPointer<EmployeeData>}, then explicit
205
sharing is used and \e{copy on write} operations are not performed
206
automatically (i.e. detach() is not called in non-const
207
functions). In that case, after \c {e1.setName("Hans Holbein")}, the
208
employee's name has been changed, but both e1 and e2 still refer to
209
the same instance of \c EmployeeData, so there is only one employee
210
with ID 1001.
211
212
In the member function documentation, \e{d pointer} always refers
213
to the internal pointer to the shared data object.
214
215
\section1 Optimize Performance for Usage in Qt Containers
216
217
You should consider marking your implicitly shared class as a movable type
218
using the Q_DECLARE_TYPEINFO() macro if it resembles the \c Employee class
219
above and uses a QSharedDataPointer or QExplicitlySharedDataPointer as the
220
only member. This can improve performance and memory efficiency when using
221
Qt's \l{container classes}.
222
223
\sa QSharedData, QExplicitlySharedDataPointer, QScopedPointer, QSharedPointer
224
*/
225
226
/*! \typedef QSharedDataPointer::Type
227
This is the type of the shared data object. The \e{d pointer}
228
points to an object of this type.
229
*/
230
231
/*! \typedef QSharedDataPointer::pointer
232
\internal
233
*/
234
235
/*! \fn template <class T> T& QSharedDataPointer<T>::operator*()
236
Provides access to the shared data object's members.
237
This function calls detach().
238
*/
239
240
/*! \fn template <class T> const T& QSharedDataPointer<T>::operator*() const
241
Provides const access to the shared data object's members.
242
This function does \e not call detach().
243
*/
244
245
/*! \fn template <class T> T* QSharedDataPointer<T>::operator->()
246
Provides access to the shared data object's members.
247
This function calls detach().
248
*/
249
250
/*! \fn template <class T> const T* QSharedDataPointer<T>::operator->() const
251
Provides const access to the shared data object's members.
252
This function does \e not call detach().
253
*/
254
255
/*! \fn template <class T> QSharedDataPointer<T>::operator T*()
256
Returns a pointer to the shared data object.
257
This function calls detach().
258
259
\sa data(), constData()
260
*/
261
262
/*! \fn template <class T> QSharedDataPointer<T>::operator const T*() const
263
Returns a pointer to the shared data object.
264
This function does \e not call detach().
265
*/
266
267
/*! \fn template <class T> T* QSharedDataPointer<T>::data()
268
Returns a pointer to the shared data object.
269
This function calls detach().
270
271
\sa constData()
272
*/
273
274
/*! \fn template <class T> T* QSharedDataPointer<T>::get()
275
\since 6.0
276
277
Same as data(). This function is provided for STL compatibility.
278
*/
279
280
/*! \fn template <class T> const T* QSharedDataPointer<T>::data() const
281
Returns a pointer to the shared data object.
282
This function does \e not call detach().
283
*/
284
285
/*! \fn template <class T> const T* QSharedDataPointer<T>::get() const
286
\since 6.0
287
288
Same as data(). This function is provided for STL compatibility.
289
*/
290
291
/*! \fn template <class T> const T* QSharedDataPointer<T>::take()
292
\since 6.0
293
294
Returns a pointer to the shared object, and resets \e this to be \nullptr.
295
(That is, this function sets the \e{d pointer} of \e this to \nullptr.)
296
297
\note The reference count of the returned object will \b{not} be
298
decremented. This function can be used together with the
299
constructor that takes a QAdoptSharedDataTag tag object to transfer
300
the shared data object without intervening atomic operations.
301
*/
302
303
/*! \fn template <class T> const T* QSharedDataPointer<T>::constData() const
304
Returns a const pointer to the shared data object.
305
This function does \e not call detach().
306
307
\sa data()
308
*/
309
310
/*! \fn template <class T> void QSharedDataPointer<T>::reset(T *ptr = nullptr)
311
\since 6.0
312
313
Sets the \e{d pointer} of \e this to \a ptr and increments \a{ptr}'s reference
314
count if \a ptr is not \nullptr.
315
The reference count of the old shared data object is decremented,
316
and the object deleted if the reference count reaches 0.
317
*/
318
319
/*! \fn template <class T> void QSharedDataPointer<T>::swap(QSharedDataPointer &other)
320
\memberswap{shared data pointer}
321
*/
322
323
/*!
324
\fn template <class T> QSharedDataPointer<T> &QSharedDataPointer<T>::operator=(QSharedDataPointer<T> &&other)
325
326
Move-assigns \a other to this QSharedDataPointer instance.
327
328
\since 5.2
329
*/
330
331
/*! \fn template <class T> bool QSharedDataPointer<T>::operator==(const QSharedDataPointer<T>& lhs, const QSharedDataPointer<T>& rhs)
332
Returns \c true if \a lhs and \a rhs have the same \e{d pointer}.
333
This function does \e not call detach().
334
*/
335
336
/*! \fn template <class T> bool QSharedDataPointer<T>::operator!=(const QSharedDataPointer<T>& lhs, const QSharedDataPointer<T>& rhs)
337
Returns \c true if \a lhs and \a rhs do \e not have the same
338
\e{d pointer}. This function does \e not call detach().
339
*/
340
341
/*! \fn template <class T> bool QSharedDataPointer<T>::operator==(T* const &lhs, const QSharedDataPointer<T>& rhs)
342
Returns \c true if the \e{d pointer} of \a rhs is \a lhs.
343
This function does \e not call detach().
344
*/
345
346
/*! \fn template <class T> bool QSharedDataPointer<T>::operator!=(T* const &lhs, const QSharedDataPointer<T>& rhs)
347
Returns \c true if the \e{d pointer} of \a rhs is \e not \a lhs.
348
\e{d pointer}. This function does \e not call detach().
349
*/
350
351
/*! \fn template <class T> QSharedDataPointer<T>::QSharedDataPointer()
352
Constructs a QSharedDataPointer initialized with \nullptr as \e{d pointer}.
353
*/
354
355
/*!
356
\fn template <class T> QSharedDataPointer<T>::QSharedDataPointer(QSharedDataPointer &&o)
357
358
Move-constructs a QSharedDataPointer instance, making it point at the same
359
object that \a o was pointing to.
360
361
\since 5.2
362
*/
363
364
/*! \fn template <class T> QSharedDataPointer<T>::~QSharedDataPointer()
365
Decrements the reference count of the shared data object.
366
If the reference count becomes 0, the shared data object
367
is deleted. \e This is then destroyed.
368
*/
369
370
/*! \fn template <class T> QSharedDataPointer<T>::QSharedDataPointer(T* data)
371
Constructs a QSharedDataPointer with \e{d pointer} set to
372
\a data and increments \a{data}'s reference count.
373
*/
374
375
/*! \fn template <class T> QSharedDataPointer<T>::QSharedDataPointer(T* data, QAdoptSharedDataTag)
376
\since 6.0
377
Constructs a QSharedDataPointer with \e{d pointer} set to
378
\a data. \a data's reference counter is \b{not} incremented;
379
this can be used to adopt pointers obtained from take().
380
381
\sa take()
382
*/
383
384
/*! \fn template <class T> QSharedDataPointer<T>::QSharedDataPointer(const QSharedDataPointer<T>& o)
385
Sets the \e{d pointer} of \e this to the \e{d pointer} in
386
\a o and increments the reference count of the shared
387
data object.
388
*/
389
390
/*! \fn template <class T> QSharedDataPointer<T>& QSharedDataPointer<T>::operator=(const QSharedDataPointer<T>& o)
391
Sets the \e{d pointer} of \e this to the \e{d pointer} of
392
\a o and increments the reference count of the shared
393
data object. The reference count of the old shared data
394
object of \e this is decremented. If the reference count
395
of the old shared data object becomes 0, the old shared
396
data object is deleted.
397
*/
398
399
/*! \fn template <class T> QSharedDataPointer& QSharedDataPointer<T>::operator=(T* o)
400
Sets the \e{d pointer} og \e this to \a o and increments
401
\a{o}'s reference count. The reference count of the old
402
shared data object of \e this is decremented. If the reference
403
count of the old shared data object becomes 0, the old shared data
404
object is deleted.
405
*/
406
407
/*! \fn template <class T> bool QSharedDataPointer<T>::operator!() const
408
Returns \c true if the \e{d pointer} of \e this is \nullptr.
409
*/
410
411
/*! \fn template <class T> void QSharedDataPointer<T>::detach()
412
If the shared data object's reference count is greater than 1, this
413
function creates a deep copy of the shared data object and sets the
414
\e{d pointer} of \e this to the copy.
415
416
This function is called automatically by non-const member
417
functions of QSharedDataPointer if \e{copy on write} is
418
required. You don't need to call it yourself.
419
*/
420
421
/*! \fn template <class T> T *QSharedDataPointer<T>::clone()
422
\since 4.5
423
424
Creates and returns a deep copy of the current data. This function
425
is called by detach() when the reference count is greater than 1 in
426
order to create the new copy. This function uses the \e {operator
427
new} and calls the copy constructor of the type \a T.
428
429
This function is provided so that you may support "virtual copy
430
constructors" for your own types. In order to so, you should declare
431
a template-specialization of this function for your own type, like
432
the example below:
433
434
\snippet code/src_corelib_tools_qshareddata.cpp 1
435
436
In the example above, the template specialization for the clone()
437
function calls the \e {EmployeeData::clone()} virtual function. A
438
class derived from EmployeeData could override that function and
439
return the proper polymorphic type.
440
*/
441
442
/*!
443
\class QExplicitlySharedDataPointer
444
\inmodule QtCore
445
\brief The QExplicitlySharedDataPointer class represents a pointer to an explicitly shared object.
446
\since 4.4
447
\reentrant
448
449
\compares strong
450
\compareswith strong T* std::nullptr_t
451
\endcompareswith
452
453
QExplicitlySharedDataPointer is a template class where the template
454
parameter \a T specifies the type of the shared data class that
455
derives from QSharedData.
456
457
QExplicitlySharedDataPointer<T> makes writing your own explicitly
458
shared classes easy. QExplicitlySharedDataPointer implements
459
\l {thread-safe} reference counting, ensuring that adding
460
QExplicitlySharedDataPointers to your \l {reentrant} classes won't
461
make them non-reentrant.
462
463
Except for one big difference, QExplicitlySharedDataPointer is just
464
like QSharedDataPointer. The big difference is that member functions
465
of QExplicitlySharedDataPointer \e{do not} do the automatic
466
\e{copy on write} operation (detach()) that non-const members of
467
QSharedDataPointer do before allowing the shared data object to be
468
modified. There is a detach() function available, but if you really
469
want to detach(), you have to call it yourself. This means that
470
QExplicitlySharedDataPointers behave like regular C++ pointers,
471
except that by doing reference counting and not deleting the shared
472
data object until the reference count is 0, they avoid the dangling
473
pointer problem.
474
475
It is instructive to compare QExplicitlySharedDataPointer with
476
QSharedDataPointer by way of an example. Consider the \l {Employee
477
example} in QSharedDataPointer, modified to use explicit sharing as
478
explained in the discussion \l {Implicit vs Explicit Sharing}.
479
480
Note that if you use this class but find you are calling detach() a
481
lot, you probably should be using QSharedDataPointer instead.
482
483
In the member function documentation, \e{d pointer} always refers
484
to the internal pointer to the shared data object.
485
486
\sa QSharedData, QSharedDataPointer
487
*/
488
489
/*! \fn template <class T> T& QExplicitlySharedDataPointer<T>::operator*() const
490
Provides access to the shared data object's members.
491
*/
492
493
/*! \fn template <class T> T* QExplicitlySharedDataPointer<T>::operator->()
494
Provides access to the shared data object's members.
495
*/
496
497
/*! \fn template <class T> const T* QExplicitlySharedDataPointer<T>::operator->() const
498
Provides const access to the shared data object's members.
499
*/
500
501
/*! \fn template <class T> T* QExplicitlySharedDataPointer<T>::data() const
502
Returns a pointer to the shared data object.
503
*/
504
505
/*! \fn template <class T> T* QExplicitlySharedDataPointer<T>::get() const
506
\since 6.0
507
508
Same as data(). This function is provided for STL compatibility.
509
*/
510
511
/*! \fn template <class T> const T* QExplicitlySharedDataPointer<T>::constData() const
512
Returns a const pointer to the shared data object.
513
514
\sa data()
515
*/
516
517
/*! \fn template <class T> void QExplicitlySharedDataPointer<T>::swap(QExplicitlySharedDataPointer &other)
518
\memberswap{explicitly-shared data pointer}
519
*/
520
521
/*! \fn template <class T> bool QExplicitlySharedDataPointer<T>::operator==(const QExplicitlySharedDataPointer<T>& lhs, const QExplicitlySharedDataPointer<T>& rhs)
522
Returns \c true if \a lhs and \a rhs have the same \e{d pointer}.
523
*/
524
525
/*!
526
\fn template <class T> QExplicitlySharedDataPointer<T> &QExplicitlySharedDataPointer<T>::operator=(QExplicitlySharedDataPointer<T> &&other)
527
528
Move-assigns \a other to this QExplicitlySharedDataPointer instance.
529
530
\since 5.2
531
*/
532
533
/*! \fn template <class T> bool QExplicitlySharedDataPointer<T>::operator==(const T* const &lhs, const QExplicitlySharedDataPointer<T>& rhs)
534
Returns \c true if the \e{d pointer} of \a rhs is \a lhs.
535
*/
536
537
/*! \fn template <class T> bool QExplicitlySharedDataPointer<T>::operator!=(const QExplicitlySharedDataPointer<T>& lhs, const QExplicitlySharedDataPointer<T>& rhs)
538
Returns \c true if \a lhs and \a rhs do \e not have the same
539
\e{d pointer}.
540
*/
541
542
/*! \fn template <class T> bool QExplicitlySharedDataPointer<T>::operator!=(const T* const &lhs, const QExplicitlySharedDataPointer<T>& rhs)
543
Returns \c true if the \e{d pointer} of \a rhs is \e not \a lhs.
544
*/
545
546
/*! \fn template <class T> QExplicitlySharedDataPointer<T>::QExplicitlySharedDataPointer()
547
Constructs a QExplicitlySharedDataPointer initialized with \nullptr
548
as \e{d pointer}.
549
*/
550
551
/*! \fn template <class T> QExplicitlySharedDataPointer<T>::~QExplicitlySharedDataPointer()
552
Decrements the reference count of the shared data object.
553
If the reference count becomes 0, the shared data object
554
is deleted. \e This is then destroyed.
555
*/
556
557
/*!
558
\fn template <class T> QExplicitlySharedDataPointer<T>::QExplicitlySharedDataPointer(QExplicitlySharedDataPointer &&o)
559
560
Move-constructs a QExplicitlySharedDataPointer instance, making it point at the same
561
object that \a o was pointing to.
562
563
\since 5.2
564
*/
565
566
/*! \fn template <class T> QExplicitlySharedDataPointer<T>::QExplicitlySharedDataPointer(T* data)
567
Constructs a QExplicitlySharedDataPointer with \e{d pointer}
568
set to \a data and increments \a{data}'s reference
569
count.
570
*/
571
572
/*! \fn template <class T> QExplicitlySharedDataPointer<T>::QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<T>& o)
573
This standard copy constructor sets the \e {d pointer} of \e this to
574
the \e {d pointer} in \a o and increments the reference count of
575
the shared data object.
576
*/
577
578
/*! \fn template <class T> template <class X> QExplicitlySharedDataPointer<T>::QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<X>& o)
579
This copy constructor is different in that it allows \a o to be
580
a different type of explicitly shared data pointer but one that has
581
a compatible shared data object.
582
583
By default, the \e{d pointer} of \a o (of type \c{\a X *}) gets
584
implicitly converted to the type \c{\a T *}; the result of this
585
conversion is set as the \e{d pointer} of \e{this}, and the
586
reference count of the shared data object is incremented.
587
*/
588
589
/*! \fn template <class T> QExplicitlySharedDataPointer<T>& QExplicitlySharedDataPointer<T>::operator=(const QExplicitlySharedDataPointer<T>& o)
590
Sets the \e{d pointer} of \e this to the \e{d pointer} of
591
\a o and increments the reference count of the shared
592
data object. The reference count of the old shared data
593
object of \e this is decremented. If the reference count
594
of the old shared data object becomes 0, the old shared
595
data object is deleted.
596
*/
597
598
/*! \fn template <class T> QExplicitlySharedDataPointer& QExplicitlySharedDataPointer<T>::operator=(T* o)
599
Sets the \e{d pointer} of \e this to \a o and
600
increments \a{o}'s reference count. The reference
601
count of the old shared data object of \e this is decremented.
602
If the reference count of the old shared data object becomes
603
0, the old shared data object is deleted.
604
*/
605
606
/*! \fn template <class T> void QExplicitlySharedDataPointer<T>::reset(T *ptr = nullptr)
607
\since 6.0
608
609
Sets the \e{d pointer} of \e this to \a ptr and increments \a{ptr}'s reference
610
count if \a ptr is not \nullptr.
611
The reference count of the old shared data object is decremented,
612
and the object deleted if the reference count reaches 0.
613
*/
614
615
/*! \fn template <class T> T *QExplicitlySharedDataPointer<T>::take()
616
\since 5.12
617
618
Returns a pointer to the shared object, and resets \e this to be \nullptr.
619
(That is, this function sets the \e{d pointer} of \e this to \nullptr.)
620
621
\note The reference count of the returned object will \b{not} be
622
decremented. This function can be used together with the
623
constructor that takes a QAdoptSharedDataTag tag object to transfer
624
the shared data object without intervening atomic operations.
625
*/
626
627
/*! \fn template <class T> QExplicitlySharedDataPointer<T>::operator bool () const
628
Returns \c true if the \e{d pointer} of \e this is \e not null.
629
*/
630
631
/*! \fn template <class T> bool QExplicitlySharedDataPointer<T>::operator!() const
632
Returns \c true if the \e{d pointer} of \e this is \nullptr.
633
*/
634
635
/*! \fn template <class T> void QExplicitlySharedDataPointer<T>::detach()
636
If the shared data object's reference count is greater than 1, this
637
function creates a deep copy of the shared data object and sets the
638
\e{d pointer} of \e this to the copy.
639
640
Because QExplicitlySharedDataPointer does not do the automatic
641
\e{copy on write} operations that members of QSharedDataPointer do,
642
detach() is \e not called automatically anywhere in the member
643
functions of this class. If you find that you are calling detach()
644
everywhere in your code, consider using QSharedDataPointer instead.
645
*/
646
647
/*! \fn template <class T> T *QExplicitlySharedDataPointer<T>::clone()
648
\since 4.5
649
650
Creates and returns a deep copy of the current data. This function
651
is called by detach() when the reference count is greater than 1 in
652
order to create the new copy. This function uses the \e {operator
653
new} and calls the copy constructor of the type \a T.
654
655
See QSharedDataPointer<T>::clone() for an explanation of how to use it.
656
*/
657
658
/*!
659
\typedef QExplicitlySharedDataPointer::Type
660
661
This is the type of the shared data object. The \e{d pointer}
662
points to an object of this type.
663
*/
664
665
/*! \typedef QExplicitlySharedDataPointer::pointer
666
\internal
667
*/
668
669
QT_END_NAMESPACE
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qrandomaccessasyncfile_darwin.mm:17
qtbase
src
corelib
tools
qshareddata.cpp
Generated on
for Qt by
1.16.1