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 | // |
41 | // W A R N I N G |
42 | // ------------- |
43 | // |
44 | // This file is not part of the Qt API. It exists for the convenience |
45 | // of internal files. This header file may change from version to version |
46 | // without notice, or even be removed. |
47 | // |
48 | // We mean it. |
49 | // |
50 | |
51 | #include <QtCore/private/qglobal_p.h> |
52 | |
53 | #ifndef QSCOPEDPOINTER_P_H |
54 | #define QSCOPEDPOINTER_P_H |
55 | |
56 | #include "QtCore/qscopedpointer.h" |
57 | |
58 | QT_BEGIN_NAMESPACE |
59 | |
60 | |
61 | /* Internal helper class - exposes the data through data_ptr (legacy from QShared). |
62 | Required for some internal Qt classes, do not use otherwise. */ |
63 | template <typename T, typename Cleanup = QScopedPointerDeleter<T> > |
64 | class QCustomScopedPointer : public QScopedPointer<T, Cleanup> |
65 | { |
66 | public: |
67 | explicit inline QCustomScopedPointer(T *p = 0) |
68 | : QScopedPointer<T, Cleanup>(p) |
69 | { |
70 | } |
71 | |
72 | inline T *&data_ptr() |
73 | { |
74 | return this->d; |
75 | } |
76 | |
77 | inline bool operator==(const QCustomScopedPointer<T, Cleanup> &other) const |
78 | { |
79 | return this->d == other.d; |
80 | } |
81 | |
82 | inline bool operator!=(const QCustomScopedPointer<T, Cleanup> &other) const |
83 | { |
84 | return this->d != other.d; |
85 | } |
86 | |
87 | private: |
88 | Q_DISABLE_COPY(QCustomScopedPointer) |
89 | }; |
90 | |
91 | /* Internal helper class - a handler for QShared* classes, to be used in QCustomScopedPointer */ |
92 | template <typename T> |
93 | class QScopedPointerSharedDeleter |
94 | { |
95 | public: |
96 | static inline void cleanup(T *d) |
97 | { |
98 | if (d && !d->ref.deref()) |
99 | delete d; |
100 | } |
101 | }; |
102 | |
103 | /* Internal. |
104 | This class is basically a scoped pointer pointing to a ref-counted object |
105 | */ |
106 | template <typename T> |
107 | class QScopedSharedPointer : public QCustomScopedPointer<T, QScopedPointerSharedDeleter<T> > |
108 | { |
109 | public: |
110 | explicit inline QScopedSharedPointer(T *p = 0) |
111 | : QCustomScopedPointer<T, QScopedPointerSharedDeleter<T> >(p) |
112 | { |
113 | } |
114 | |
115 | inline void detach() |
116 | { |
117 | qAtomicDetach(this->d); |
118 | } |
119 | |
120 | inline void assign(T *other) |
121 | { |
122 | if (this->d == other) |
123 | return; |
124 | if (other) |
125 | other->ref.ref(); |
126 | T *oldD = this->d; |
127 | this->d = other; |
128 | QScopedPointerSharedDeleter<T>::cleanup(oldD); |
129 | } |
130 | |
131 | inline bool operator==(const QScopedSharedPointer<T> &other) const |
132 | { |
133 | return this->d == other.d; |
134 | } |
135 | |
136 | inline bool operator!=(const QScopedSharedPointer<T> &other) const |
137 | { |
138 | return this->d != other.d; |
139 | } |
140 | |
141 | private: |
142 | Q_DISABLE_COPY(QScopedSharedPointer) |
143 | }; |
144 | |
145 | |
146 | QT_END_NAMESPACE |
147 | |
148 | #endif |
149 | |