1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
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 "private/qdeclarativedata_p.h"
59 #include "private/qdeclarativeintegercache_p.h"
60 #include "private/qdeclarativetypenamecache_p.h"
61 #include "private/qdeclarativenotifier_p.h"
62 #include "qdeclarativelist.h"
63 #include "private/qdeclarativeparser_p.h"
65 #include <QtCore/qhash.h>
66 #include <QtScript/qscriptvalue.h>
67 #include <QtCore/qset.h>
69 #include <private/qobject_p.h>
70 #include "private/qdeclarativeguard_p.h"
72 #include <private/qv8_p.h>
76 class QDeclarativeContext;
77 class QDeclarativeExpression;
78 class QDeclarativeEngine;
79 class QDeclarativeExpression;
80 class QDeclarativeExpressionPrivate;
81 class QDeclarativeAbstractExpression;
82 class QDeclarativeV4Bindings;
83 class QDeclarativeContextData;
85 class QDeclarativeContextPrivate : public QObjectPrivate
87 Q_DECLARE_PUBLIC(QDeclarativeContext)
89 QDeclarativeContextPrivate();
91 QDeclarativeContextData *data;
93 QList<QVariant> propertyValues;
96 static QDeclarativeContextPrivate *get(QDeclarativeContext *context) {
97 return static_cast<QDeclarativeContextPrivate *>(QObjectPrivate::get(context));
99 static QDeclarativeContext *get(QDeclarativeContextPrivate *context) {
100 return static_cast<QDeclarativeContext *>(context->q_func());
103 // Only used for debugging
104 QList<QPointer<QObject> > instances;
106 static int context_count(QDeclarativeListProperty<QObject> *);
107 static QObject *context_at(QDeclarativeListProperty<QObject> *, int);
110 class QDeclarativeComponentAttached;
111 class QDeclarativeGuardedContextData;
112 class Q_AUTOTEST_EXPORT QDeclarativeContextData
115 QDeclarativeContextData();
116 QDeclarativeContextData(QDeclarativeContext *);
121 inline bool isValid() const {
122 return engine && (!isInternal || !contextObject || !QObjectPrivate::get(contextObject)->wasDeleted);
125 // My parent context and engine
126 QDeclarativeContextData *parent;
127 QDeclarativeEngine *engine;
129 void setParent(QDeclarativeContextData *, bool parentTakesOwnership = false);
130 void refreshExpressions();
132 void addObject(QObject *);
134 QUrl resolvedUrl(const QUrl &);
136 // My containing QDeclarativeContext. If isInternal is true this owns publicContext.
137 // If internal is false publicContext owns this.
138 QDeclarativeContext *asQDeclarativeContext();
139 QDeclarativeContextPrivate *asQDeclarativeContextPrivate();
140 quint32 isInternal:1;
141 quint32 ownedByParent:1; // unrelated to isInternal; parent context deletes children if true.
142 quint32 isJSContext:1;
144 QDeclarativeContext *publicContext;
146 // Property name cache
147 QDeclarativeIntegerCache *propertyNames;
150 QObject *contextObject;
152 // Any script blocks that exist on this context
154 QList<v8::Persistent<v8::Object> > importedScripts;
155 // QList<QScriptValue> importedScripts;
160 // List of imports that apply to this context
161 QDeclarativeTypeNameCache *imports;
164 QDeclarativeContextData *childContexts;
166 // My peers in parent's childContexts list
167 QDeclarativeContextData *nextChild;
168 QDeclarativeContextData **prevChild;
170 // Expressions that use this context
171 QDeclarativeAbstractExpression *expressions;
173 // Doubly-linked list of objects that are owned by this context
174 QDeclarativeData *contextObjects;
176 // Doubly-linked list of context guards (XXX merge with contextObjects)
177 QDeclarativeGuardedContextData *contextGuards;
180 struct ContextGuard : public QDeclarativeGuard<QObject>
182 ContextGuard() : context(0) {}
183 inline ContextGuard &operator=(QObject *obj)
184 { QDeclarativeGuard<QObject>::operator=(obj); return *this; }
185 virtual void objectDestroyed(QObject *) {
186 if (context->contextObject && !QObjectPrivate::get(context->contextObject)->wasDeleted) bindings.notify();
188 QDeclarativeContextData *context;
189 QDeclarativeNotifier bindings;
191 ContextGuard *idValues;
193 void setIdProperty(int, QObject *);
194 void setIdPropertyData(QDeclarativeIntegerCache *);
196 // Optimized binding pointer
197 QDeclarativeV4Bindings *optimizedBindings;
199 // Linked contexts. this owns linkedContext.
200 QDeclarativeContextData *linkedContext;
202 // Linked list of uses of the Component attached property in this
204 QDeclarativeComponentAttached *componentAttached;
206 // Return the outermost id for obj, if any.
207 QString findObjectId(const QObject *obj) const;
209 static QDeclarativeContextData *get(QDeclarativeContext *context) {
210 return QDeclarativeContextPrivate::get(context)->data;
214 ~QDeclarativeContextData() {}
217 class QDeclarativeGuardedContextData
220 inline QDeclarativeGuardedContextData();
221 inline QDeclarativeGuardedContextData(QDeclarativeContextData *);
222 inline ~QDeclarativeGuardedContextData();
224 inline QDeclarativeContextData *contextData();
225 inline void setContextData(QDeclarativeContextData *);
227 inline bool isNull() const { return !m_contextData; }
229 inline operator QDeclarativeContextData*() const { return m_contextData; }
230 inline QDeclarativeContextData* operator->() const { return m_contextData; }
231 inline QDeclarativeGuardedContextData &operator=(QDeclarativeContextData *d);
234 QDeclarativeGuardedContextData &operator=(const QDeclarativeGuardedContextData &);
235 QDeclarativeGuardedContextData(const QDeclarativeGuardedContextData &);
236 friend class QDeclarativeContextData;
240 QDeclarativeContextData *m_contextData;
241 QDeclarativeGuardedContextData *m_next;
242 QDeclarativeGuardedContextData **m_prev;
245 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData()
246 : m_contextData(0), m_next(0), m_prev(0)
250 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData(QDeclarativeContextData *data)
251 : m_contextData(0), m_next(0), m_prev(0)
253 setContextData(data);
256 QDeclarativeGuardedContextData::~QDeclarativeGuardedContextData()
261 void QDeclarativeGuardedContextData::setContextData(QDeclarativeContextData *contextData)
266 m_contextData = contextData;
267 m_next = contextData->contextGuards;
268 if (m_next) m_next->m_prev = &m_next;
269 m_prev = &contextData->contextGuards;
270 contextData->contextGuards = this;
274 QDeclarativeContextData *QDeclarativeGuardedContextData::contextData()
276 return m_contextData;
279 void QDeclarativeGuardedContextData::clear()
283 if (m_next) m_next->m_prev = m_prev;
290 QDeclarativeGuardedContextData &
291 QDeclarativeGuardedContextData::operator=(QDeclarativeContextData *d)
299 #endif // QDECLARATIVECONTEXT_P_H