1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QQMLCONTEXT_P_H
43 #define QQMLCONTEXT_P_H
49 // This file is not part of the Qt API. It exists purely as an
50 // implementation detail. This header file may change from version to
51 // version without notice, or even be removed.
56 #include "qqmlcontext.h"
58 #include "qqmldata_p.h"
59 #include "qqmlintegercache_p.h"
60 #include "qqmltypenamecache_p.h"
61 #include "qqmlnotifier_p.h"
63 #include "qqmlscript_p.h"
65 #include <QtCore/qhash.h>
66 #include <QtQml/qjsvalue.h>
67 #include <QtCore/qset.h>
69 #include <private/qobject_p.h>
70 #include <private/qflagpointer_p.h>
71 #include <private/qqmlguard_p.h>
73 #include <private/qv8_p.h>
83 class QQmlExpressionPrivate;
84 class QQmlAbstractExpression;
86 class QQmlContextData;
88 class QQmlContextPrivate : public QObjectPrivate
90 Q_DECLARE_PUBLIC(QQmlContext)
94 QQmlContextData *data;
96 QList<QVariant> propertyValues;
99 static QQmlContextPrivate *get(QQmlContext *context) {
100 return static_cast<QQmlContextPrivate *>(QObjectPrivate::get(context));
102 static QQmlContext *get(QQmlContextPrivate *context) {
103 return static_cast<QQmlContext *>(context->q_func());
106 // Only used for debugging
107 QList<QPointer<QObject> > instances;
109 static int context_count(QQmlListProperty<QObject> *);
110 static QObject *context_at(QQmlListProperty<QObject> *, int);
114 class QQmlComponentAttached;
115 class QQmlGuardedContextData;
116 class Q_QML_EXPORT QQmlContextData
120 QQmlContextData(QQmlContext *);
125 inline bool isValid() const {
126 return engine && (!isInternal || !contextObject || !QObjectPrivate::get(contextObject)->wasDeleted);
129 // My parent context and engine
130 QQmlContextData *parent;
133 void setParent(QQmlContextData *, bool parentTakesOwnership = false);
134 void refreshExpressions();
136 void addObject(QObject *);
138 QUrl resolvedUrl(const QUrl &);
140 // My containing QQmlContext. If isInternal is true this owns publicContext.
141 // If internal is false publicContext owns this.
142 QQmlContext *asQQmlContext();
143 QQmlContextPrivate *asQQmlContextPrivate();
144 quint32 isInternal:1;
145 quint32 ownedByParent:1; // unrelated to isInternal; parent context deletes children if true.
146 quint32 isJSContext:1;
147 quint32 isPragmaLibraryContext:1;
148 quint32 unresolvedNames:1; // True if expressions in this context failed to resolve a toplevel name
150 QQmlContext *publicContext;
152 // VME data that is constructing this context if any
155 // Property name cache
156 QQmlIntegerCache *propertyNames;
159 QObject *contextObject;
161 // Any script blocks that exist on this context
162 QList<v8::Persistent<v8::Object> > importedScripts;
168 // List of imports that apply to this context
169 QQmlTypeNameCache *imports;
172 QQmlContextData *childContexts;
174 // My peers in parent's childContexts list
175 QQmlContextData *nextChild;
176 QQmlContextData **prevChild;
178 // Expressions that use this context
179 QQmlAbstractExpression *expressions;
181 // Doubly-linked list of objects that are owned by this context
182 QQmlData *contextObjects;
184 // Doubly-linked list of context guards (XXX merge with contextObjects)
185 QQmlGuardedContextData *contextGuards;
188 struct ContextGuard : public QQmlGuard<QObject>
190 inline ContextGuard();
191 inline ContextGuard &operator=(QObject *obj);
192 inline void objectDestroyed(QObject *);
194 inline bool wasSet() const;
196 QFlagPointer<QQmlContextData> context;
197 QQmlNotifier bindings;
199 ContextGuard *idValues;
201 void setIdProperty(int, QObject *);
202 void setIdPropertyData(QQmlIntegerCache *);
204 // Linked contexts. this owns linkedContext.
205 QQmlContextData *linkedContext;
207 // Linked list of uses of the Component attached property in this
209 QQmlComponentAttached *componentAttached;
211 // Optimized binding objects. Needed for deferred properties.
212 QV4Bindings *v4bindings;
213 QV8Bindings *v8bindings;
215 // Return the outermost id for obj, if any.
216 QString findObjectId(const QObject *obj) const;
218 static QQmlContextData *get(QQmlContext *context) {
219 return QQmlContextPrivate::get(context)->data;
223 void refreshExpressionsRecursive(bool isGlobal);
224 void refreshExpressionsRecursive(QQmlAbstractExpression *);
225 ~QQmlContextData() {}
228 class QQmlGuardedContextData
231 inline QQmlGuardedContextData();
232 inline QQmlGuardedContextData(QQmlContextData *);
233 inline ~QQmlGuardedContextData();
235 inline QQmlContextData *contextData();
236 inline void setContextData(QQmlContextData *);
238 inline bool isNull() const { return !m_contextData; }
240 inline operator QQmlContextData*() const { return m_contextData; }
241 inline QQmlContextData* operator->() const { return m_contextData; }
242 inline QQmlGuardedContextData &operator=(QQmlContextData *d);
245 QQmlGuardedContextData &operator=(const QQmlGuardedContextData &);
246 QQmlGuardedContextData(const QQmlGuardedContextData &);
247 friend class QQmlContextData;
251 QQmlContextData *m_contextData;
252 QQmlGuardedContextData *m_next;
253 QQmlGuardedContextData **m_prev;
256 QQmlGuardedContextData::QQmlGuardedContextData()
257 : m_contextData(0), m_next(0), m_prev(0)
261 QQmlGuardedContextData::QQmlGuardedContextData(QQmlContextData *data)
262 : m_contextData(0), m_next(0), m_prev(0)
264 setContextData(data);
267 QQmlGuardedContextData::~QQmlGuardedContextData()
272 void QQmlGuardedContextData::setContextData(QQmlContextData *contextData)
277 m_contextData = contextData;
278 m_next = contextData->contextGuards;
279 if (m_next) m_next->m_prev = &m_next;
280 m_prev = &contextData->contextGuards;
281 contextData->contextGuards = this;
285 QQmlContextData *QQmlGuardedContextData::contextData()
287 return m_contextData;
290 void QQmlGuardedContextData::clear()
294 if (m_next) m_next->m_prev = m_prev;
301 QQmlGuardedContextData &
302 QQmlGuardedContextData::operator=(QQmlContextData *d)
308 QQmlContextData::ContextGuard::ContextGuard()
313 QQmlContextData::ContextGuard &QQmlContextData::ContextGuard::operator=(QObject *obj)
315 QQmlGuard<QObject>::operator=(obj);
317 bindings.notify(); // For alias connections
321 void QQmlContextData::ContextGuard::objectDestroyed(QObject *)
323 if (context->contextObject && !QObjectPrivate::get(context->contextObject)->wasDeleted)
327 bool QQmlContextData::ContextGuard::wasSet() const
329 return context.flag();
334 #endif // QQMLCONTEXT_P_H