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