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 | #ifndef QQMLGUARD_P_H |
41 | #define QQMLGUARD_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 for the convenience |
48 | // of qapplication_*.cpp, qwidget*.cpp and qfiledialog.cpp. This header |
49 | // file may change from version to version without notice, or even be removed. |
50 | // |
51 | // We mean it. |
52 | // |
53 | |
54 | #include <QtCore/qglobal.h> |
55 | #include <QtCore/qvariant.h> |
56 | #include <private/qqmldata_p.h> |
57 | |
58 | QT_BEGIN_NAMESPACE |
59 | |
60 | class QQmlGuardImpl |
61 | { |
62 | public: |
63 | inline QQmlGuardImpl(); |
64 | inline QQmlGuardImpl(QObject *); |
65 | inline QQmlGuardImpl(const QQmlGuardImpl &); |
66 | inline ~QQmlGuardImpl(); |
67 | |
68 | QObject *o = nullptr; |
69 | QQmlGuardImpl *next = nullptr; |
70 | QQmlGuardImpl **prev = nullptr; |
71 | |
72 | inline void addGuard(); |
73 | inline void remGuard(); |
74 | }; |
75 | |
76 | class QObject; |
77 | template<class T> |
78 | class QQmlGuard : private QQmlGuardImpl |
79 | { |
80 | friend class QQmlData; |
81 | public: |
82 | inline QQmlGuard(); |
83 | inline QQmlGuard(T *); |
84 | inline QQmlGuard(const QQmlGuard<T> &); |
85 | inline virtual ~QQmlGuard(); |
86 | |
87 | inline QQmlGuard<T> &operator=(const QQmlGuard<T> &o); |
88 | inline QQmlGuard<T> &operator=(T *); |
89 | |
90 | inline T *object() const; |
91 | inline void setObject(T *g); |
92 | |
93 | inline bool isNull() const |
94 | { return !o; } |
95 | |
96 | inline T* operator->() const |
97 | { return static_cast<T*>(const_cast<QObject*>(o)); } |
98 | inline T& operator*() const |
99 | { return *static_cast<T*>(const_cast<QObject*>(o)); } |
100 | inline operator T*() const |
101 | { return static_cast<T*>(const_cast<QObject*>(o)); } |
102 | inline T* data() const |
103 | { return static_cast<T*>(const_cast<QObject*>(o)); } |
104 | |
105 | protected: |
106 | virtual void objectDestroyed(T *) {} |
107 | }; |
108 | |
109 | template <typename T> |
110 | class QQmlStrongJSQObjectReference : public QQmlGuard<T> |
111 | { |
112 | public: |
113 | void setObject(T *o, QObject *parent) { |
114 | T *old = this->object(); |
115 | if (o == old) |
116 | return; |
117 | |
118 | if (m_jsOwnership && old && old->parent() == parent) |
119 | QQml_setParent_noEvent(old, nullptr); |
120 | |
121 | this->QQmlGuard<T>::operator=(o); |
122 | |
123 | if (o && !o->parent() && !QQmlData::keepAliveDuringGarbageCollection(object: o)) { |
124 | m_jsOwnership = true; |
125 | QQml_setParent_noEvent(o, parent); |
126 | } else { |
127 | m_jsOwnership = false; |
128 | } |
129 | } |
130 | |
131 | private: |
132 | using QQmlGuard<T>::setObject; |
133 | using QQmlGuard<T>::operator=; |
134 | bool m_jsOwnership = false; |
135 | }; |
136 | |
137 | QT_END_NAMESPACE |
138 | |
139 | Q_DECLARE_METATYPE(QQmlGuard<QObject>) |
140 | |
141 | QT_BEGIN_NAMESPACE |
142 | |
143 | QQmlGuardImpl::QQmlGuardImpl() |
144 | { |
145 | } |
146 | |
147 | QQmlGuardImpl::QQmlGuardImpl(QObject *g) |
148 | : o(g) |
149 | { |
150 | if (o) addGuard(); |
151 | } |
152 | |
153 | QQmlGuardImpl::QQmlGuardImpl(const QQmlGuardImpl &g) |
154 | : o(g.o) |
155 | { |
156 | if (o) addGuard(); |
157 | } |
158 | |
159 | QQmlGuardImpl::~QQmlGuardImpl() |
160 | { |
161 | if (prev) remGuard(); |
162 | o = nullptr; |
163 | } |
164 | |
165 | void QQmlGuardImpl::addGuard() |
166 | { |
167 | Q_ASSERT(!prev); |
168 | |
169 | if (QObjectPrivate::get(o)->wasDeleted) |
170 | return; |
171 | |
172 | QQmlData *data = QQmlData::get(object: o, create: true); |
173 | next = data->guards; |
174 | if (next) next->prev = &next; |
175 | data->guards = this; |
176 | prev = &data->guards; |
177 | } |
178 | |
179 | void QQmlGuardImpl::remGuard() |
180 | { |
181 | Q_ASSERT(prev); |
182 | |
183 | if (next) next->prev = prev; |
184 | *prev = next; |
185 | next = nullptr; |
186 | prev = nullptr; |
187 | } |
188 | |
189 | template<class T> |
190 | QQmlGuard<T>::QQmlGuard() |
191 | { |
192 | } |
193 | |
194 | template<class T> |
195 | QQmlGuard<T>::QQmlGuard(T *g) |
196 | : QQmlGuardImpl(g) |
197 | { |
198 | } |
199 | |
200 | template<class T> |
201 | QQmlGuard<T>::QQmlGuard(const QQmlGuard<T> &g) |
202 | : QQmlGuardImpl(g) |
203 | { |
204 | } |
205 | |
206 | template<class T> |
207 | QQmlGuard<T>::~QQmlGuard() |
208 | { |
209 | } |
210 | |
211 | template<class T> |
212 | QQmlGuard<T> &QQmlGuard<T>::operator=(const QQmlGuard<T> &g) |
213 | { |
214 | setObject(g.object()); |
215 | return *this; |
216 | } |
217 | |
218 | template<class T> |
219 | QQmlGuard<T> &QQmlGuard<T>::operator=(T *g) |
220 | { |
221 | setObject(g); |
222 | return *this; |
223 | } |
224 | |
225 | template<class T> |
226 | T *QQmlGuard<T>::object() const |
227 | { |
228 | return static_cast<T *>(o); |
229 | } |
230 | |
231 | template<class T> |
232 | void QQmlGuard<T>::setObject(T *g) |
233 | { |
234 | if (g != o) { |
235 | if (prev) remGuard(); |
236 | o = g; |
237 | if (o) addGuard(); |
238 | } |
239 | } |
240 | |
241 | QT_END_NAMESPACE |
242 | |
243 | #endif // QQMLGUARD_P_H |
244 | |