1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #ifndef QDECLARATIVECONTEXT_P_H
43 #define QDECLARATIVECONTEXT_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 "qdeclarativecontext.h"
58 #include "qdeclarativedata_p.h"
59 #include "qdeclarativeintegercache_p.h"
60 #include "qdeclarativetypenamecache_p.h"
61 #include "qdeclarativenotifier_p.h"
62 #include "qdeclarativelist.h"
63 #include "qdeclarativescript_p.h"
65 #include <QtCore/qhash.h>
66 #include <QtDeclarative/qjsvalue.h>
67 #include <QtCore/qset.h>
69 #include <private/qobject_p.h>
70 #include <private/qflagpointer_p.h>
71 #include <private/qdeclarativeguard_p.h>
73 #include <private/qv8_p.h>
79 class QDeclarativeContext;
80 class QDeclarativeExpression;
81 class QDeclarativeEngine;
82 class QDeclarativeExpression;
83 class QDeclarativeExpressionPrivate;
84 class QDeclarativeAbstractExpression;
86 class QDeclarativeContextData;
88 class QDeclarativeContextPrivate : public QObjectPrivate
90 Q_DECLARE_PUBLIC(QDeclarativeContext)
92 QDeclarativeContextPrivate();
94 QDeclarativeContextData *data;
96 QList<QVariant> propertyValues;
99 static QDeclarativeContextPrivate *get(QDeclarativeContext *context) {
100 return static_cast<QDeclarativeContextPrivate *>(QObjectPrivate::get(context));
102 static QDeclarativeContext *get(QDeclarativeContextPrivate *context) {
103 return static_cast<QDeclarativeContext *>(context->q_func());
106 // Only used for debugging
107 QList<QPointer<QObject> > instances;
109 static int context_count(QDeclarativeListProperty<QObject> *);
110 static QObject *context_at(QDeclarativeListProperty<QObject> *, int);
113 class QDeclarativeVME;
114 class QDeclarativeComponentAttached;
115 class QDeclarativeGuardedContextData;
116 class Q_DECLARATIVE_EXPORT QDeclarativeContextData
119 QDeclarativeContextData();
120 QDeclarativeContextData(QDeclarativeContext *);
125 inline bool isValid() const {
126 return engine && (!isInternal || !contextObject || !QObjectPrivate::get(contextObject)->wasDeleted);
129 // My parent context and engine
130 QDeclarativeContextData *parent;
131 QDeclarativeEngine *engine;
133 void setParent(QDeclarativeContextData *, bool parentTakesOwnership = false);
134 void refreshExpressions();
136 void addObject(QObject *);
138 QUrl resolvedUrl(const QUrl &);
140 // My containing QDeclarativeContext. If isInternal is true this owns publicContext.
141 // If internal is false publicContext owns this.
142 QDeclarativeContext *asQDeclarativeContext();
143 QDeclarativeContextPrivate *asQDeclarativeContextPrivate();
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 QDeclarativeContext *publicContext;
152 // VME data that is constructing this context if any
155 // Property name cache
156 QDeclarativeIntegerCache *propertyNames;
159 QObject *contextObject;
161 // Any script blocks that exist on this context
162 QList<v8::Persistent<v8::Object> > importedScripts;
167 // List of imports that apply to this context
168 QDeclarativeTypeNameCache *imports;
171 QDeclarativeContextData *childContexts;
173 // My peers in parent's childContexts list
174 QDeclarativeContextData *nextChild;
175 QDeclarativeContextData **prevChild;
177 // Expressions that use this context
178 QDeclarativeAbstractExpression *expressions;
180 // Doubly-linked list of objects that are owned by this context
181 QDeclarativeData *contextObjects;
183 // Doubly-linked list of context guards (XXX merge with contextObjects)
184 QDeclarativeGuardedContextData *contextGuards;
187 struct ContextGuard : public QDeclarativeGuard<QObject>
189 inline ContextGuard();
190 inline ContextGuard &operator=(QObject *obj);
191 inline void objectDestroyed(QObject *);
193 inline bool wasSet() const;
195 QFlagPointer<QDeclarativeContextData> context;
196 QDeclarativeNotifier bindings;
198 ContextGuard *idValues;
200 void setIdProperty(int, QObject *);
201 void setIdPropertyData(QDeclarativeIntegerCache *);
203 // Linked contexts. this owns linkedContext.
204 QDeclarativeContextData *linkedContext;
206 // Linked list of uses of the Component attached property in this
208 QDeclarativeComponentAttached *componentAttached;
210 // Optimized binding objects. Needed for deferred properties.
211 QV4Bindings *v4bindings;
212 QV8Bindings *v8bindings;
214 // Return the outermost id for obj, if any.
215 QString findObjectId(const QObject *obj) const;
217 static QDeclarativeContextData *get(QDeclarativeContext *context) {
218 return QDeclarativeContextPrivate::get(context)->data;
222 void refreshExpressionsRecursive(bool isGlobal);
223 void refreshExpressionsRecursive(QDeclarativeAbstractExpression *);
224 ~QDeclarativeContextData() {}
227 class QDeclarativeGuardedContextData
230 inline QDeclarativeGuardedContextData();
231 inline QDeclarativeGuardedContextData(QDeclarativeContextData *);
232 inline ~QDeclarativeGuardedContextData();
234 inline QDeclarativeContextData *contextData();
235 inline void setContextData(QDeclarativeContextData *);
237 inline bool isNull() const { return !m_contextData; }
239 inline operator QDeclarativeContextData*() const { return m_contextData; }
240 inline QDeclarativeContextData* operator->() const { return m_contextData; }
241 inline QDeclarativeGuardedContextData &operator=(QDeclarativeContextData *d);
244 QDeclarativeGuardedContextData &operator=(const QDeclarativeGuardedContextData &);
245 QDeclarativeGuardedContextData(const QDeclarativeGuardedContextData &);
246 friend class QDeclarativeContextData;
250 QDeclarativeContextData *m_contextData;
251 QDeclarativeGuardedContextData *m_next;
252 QDeclarativeGuardedContextData **m_prev;
255 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData()
256 : m_contextData(0), m_next(0), m_prev(0)
260 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData(QDeclarativeContextData *data)
261 : m_contextData(0), m_next(0), m_prev(0)
263 setContextData(data);
266 QDeclarativeGuardedContextData::~QDeclarativeGuardedContextData()
271 void QDeclarativeGuardedContextData::setContextData(QDeclarativeContextData *contextData)
276 m_contextData = contextData;
277 m_next = contextData->contextGuards;
278 if (m_next) m_next->m_prev = &m_next;
279 m_prev = &contextData->contextGuards;
280 contextData->contextGuards = this;
284 QDeclarativeContextData *QDeclarativeGuardedContextData::contextData()
286 return m_contextData;
289 void QDeclarativeGuardedContextData::clear()
293 if (m_next) m_next->m_prev = m_prev;
300 QDeclarativeGuardedContextData &
301 QDeclarativeGuardedContextData::operator=(QDeclarativeContextData *d)
307 QDeclarativeContextData::ContextGuard::ContextGuard()
312 QDeclarativeContextData::ContextGuard &QDeclarativeContextData::ContextGuard::operator=(QObject *obj)
314 QDeclarativeGuard<QObject>::operator=(obj);
316 bindings.notify(); // For alias connections
320 void QDeclarativeContextData::ContextGuard::objectDestroyed(QObject *)
322 if (context->contextObject && !QObjectPrivate::get(context->contextObject)->wasDeleted)
326 bool QDeclarativeContextData::ContextGuard::wasSet() const
328 return context.flag();
333 #endif // QDECLARATIVECONTEXT_P_H