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 QtQml 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 | #ifndef QQMLREFCOUNT_P_H |
41 | #define QQMLREFCOUNT_P_H |
42 | |
43 | // |
44 | // W A R N I N G |
45 | // ------------- |
46 | // |
47 | // This file is not part of the Qt API. It exists purely as an |
48 | // implementation detail. This header file may change from version to |
49 | // version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtCore/qglobal.h> |
55 | #include <QtCore/qatomic.h> |
56 | #include <private/qv4global_p.h> |
57 | |
58 | QT_BEGIN_NAMESPACE |
59 | |
60 | |
61 | class Q_QML_PRIVATE_EXPORT QQmlRefCount |
62 | { |
63 | Q_DISABLE_COPY_MOVE(QQmlRefCount) |
64 | public: |
65 | inline QQmlRefCount(); |
66 | inline void addref() const; |
67 | inline void release() const; |
68 | inline int count() const; |
69 | |
70 | protected: |
71 | inline virtual ~QQmlRefCount(); |
72 | |
73 | private: |
74 | mutable QAtomicInt refCount; |
75 | }; |
76 | |
77 | template<class T> |
78 | class QQmlRefPointer |
79 | { |
80 | public: |
81 | enum Mode { |
82 | AddRef, |
83 | Adopt |
84 | }; |
85 | inline QQmlRefPointer(); |
86 | inline QQmlRefPointer(T *, Mode m = AddRef); |
87 | inline QQmlRefPointer(const QQmlRefPointer<T> &); |
88 | inline QQmlRefPointer(QQmlRefPointer<T> &&); |
89 | inline ~QQmlRefPointer(); |
90 | |
91 | inline QQmlRefPointer<T> &operator=(const QQmlRefPointer<T> &o); |
92 | inline QQmlRefPointer<T> &operator=(QQmlRefPointer<T> &&o); |
93 | |
94 | inline bool isNull() const { return !o; } |
95 | |
96 | inline T* operator->() const { return o; } |
97 | inline T& operator*() const { return *o; } |
98 | explicit inline operator bool() const { return o != nullptr; } |
99 | inline T* data() const { return o; } |
100 | |
101 | inline QQmlRefPointer<T> &adopt(T *); |
102 | |
103 | inline T* take() { T *res = o; o = nullptr; return res; } |
104 | |
105 | private: |
106 | T *o; |
107 | }; |
108 | |
109 | QQmlRefCount::QQmlRefCount() |
110 | : refCount(1) |
111 | { |
112 | } |
113 | |
114 | QQmlRefCount::~QQmlRefCount() |
115 | { |
116 | Q_ASSERT(refCount.loadRelaxed() == 0); |
117 | } |
118 | |
119 | void QQmlRefCount::addref() const |
120 | { |
121 | Q_ASSERT(refCount.loadRelaxed() > 0); |
122 | refCount.ref(); |
123 | } |
124 | |
125 | void QQmlRefCount::release() const |
126 | { |
127 | Q_ASSERT(refCount.loadRelaxed() > 0); |
128 | if (!refCount.deref()) |
129 | delete this; |
130 | } |
131 | |
132 | int QQmlRefCount::count() const |
133 | { |
134 | return refCount.loadRelaxed(); |
135 | } |
136 | |
137 | template<class T> |
138 | QQmlRefPointer<T>::QQmlRefPointer() |
139 | : o(nullptr) |
140 | { |
141 | } |
142 | |
143 | template<class T> |
144 | QQmlRefPointer<T>::QQmlRefPointer(T *o, Mode m) |
145 | : o(o) |
146 | { |
147 | if (m == AddRef && o) |
148 | o->addref(); |
149 | } |
150 | |
151 | template<class T> |
152 | QQmlRefPointer<T>::QQmlRefPointer(const QQmlRefPointer<T> &other) |
153 | : o(other.o) |
154 | { |
155 | if (o) o->addref(); |
156 | } |
157 | |
158 | template <class T> |
159 | QQmlRefPointer<T>::QQmlRefPointer(QQmlRefPointer<T> &&other) |
160 | : o(other.take()) |
161 | { |
162 | } |
163 | |
164 | template<class T> |
165 | QQmlRefPointer<T>::~QQmlRefPointer() |
166 | { |
167 | if (o) o->release(); |
168 | } |
169 | |
170 | template<class T> |
171 | QQmlRefPointer<T> &QQmlRefPointer<T>::operator=(const QQmlRefPointer<T> &other) |
172 | { |
173 | if (other.o) other.o->addref(); |
174 | if (o) o->release(); |
175 | o = other.o; |
176 | return *this; |
177 | } |
178 | |
179 | template <class T> |
180 | QQmlRefPointer<T> &QQmlRefPointer<T>::operator=(QQmlRefPointer<T> &&other) |
181 | { |
182 | QQmlRefPointer<T> m(std::move(other)); |
183 | qSwap(o, m.o); |
184 | return *this; |
185 | } |
186 | |
187 | /*! |
188 | Takes ownership of \a other. take() does *not* add a reference, as it assumes ownership |
189 | of the callers reference of other. |
190 | */ |
191 | template<class T> |
192 | QQmlRefPointer<T> &QQmlRefPointer<T>::adopt(T *other) |
193 | { |
194 | if (o) o->release(); |
195 | o = other; |
196 | return *this; |
197 | } |
198 | |
199 | QT_END_NAMESPACE |
200 | |
201 | #endif // QQMLREFCOUNT_P_H |
202 | |