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 ** 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 "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
153 QList<v8::Persistent<v8::Object> > importedScripts;
158 // List of imports that apply to this context
159 QDeclarativeTypeNameCache *imports;
162 QDeclarativeContextData *childContexts;
164 // My peers in parent's childContexts list
165 QDeclarativeContextData *nextChild;
166 QDeclarativeContextData **prevChild;
168 // Expressions that use this context
169 QDeclarativeAbstractExpression *expressions;
171 // Doubly-linked list of objects that are owned by this context
172 QDeclarativeData *contextObjects;
174 // Doubly-linked list of context guards (XXX merge with contextObjects)
175 QDeclarativeGuardedContextData *contextGuards;
178 struct ContextGuard : public QDeclarativeGuard<QObject>
180 ContextGuard() : context(0) {}
181 inline ContextGuard &operator=(QObject *obj)
182 { QDeclarativeGuard<QObject>::operator=(obj); return *this; }
183 virtual void objectDestroyed(QObject *) {
184 if (context->contextObject && !QObjectPrivate::get(context->contextObject)->wasDeleted) bindings.notify();
186 QDeclarativeContextData *context;
187 QDeclarativeNotifier bindings;
189 ContextGuard *idValues;
191 void setIdProperty(int, QObject *);
192 void setIdPropertyData(QDeclarativeIntegerCache *);
194 // Optimized binding pointer
195 QDeclarativeV4Bindings *optimizedBindings;
197 // Linked contexts. this owns linkedContext.
198 QDeclarativeContextData *linkedContext;
200 // Linked list of uses of the Component attached property in this
202 QDeclarativeComponentAttached *componentAttached;
204 // Return the outermost id for obj, if any.
205 QString findObjectId(const QObject *obj) const;
207 static QDeclarativeContextData *get(QDeclarativeContext *context) {
208 return QDeclarativeContextPrivate::get(context)->data;
212 ~QDeclarativeContextData() {}
215 class QDeclarativeGuardedContextData
218 inline QDeclarativeGuardedContextData();
219 inline QDeclarativeGuardedContextData(QDeclarativeContextData *);
220 inline ~QDeclarativeGuardedContextData();
222 inline QDeclarativeContextData *contextData();
223 inline void setContextData(QDeclarativeContextData *);
225 inline bool isNull() const { return !m_contextData; }
227 inline operator QDeclarativeContextData*() const { return m_contextData; }
228 inline QDeclarativeContextData* operator->() const { return m_contextData; }
229 inline QDeclarativeGuardedContextData &operator=(QDeclarativeContextData *d);
232 QDeclarativeGuardedContextData &operator=(const QDeclarativeGuardedContextData &);
233 QDeclarativeGuardedContextData(const QDeclarativeGuardedContextData &);
234 friend class QDeclarativeContextData;
238 QDeclarativeContextData *m_contextData;
239 QDeclarativeGuardedContextData *m_next;
240 QDeclarativeGuardedContextData **m_prev;
243 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData()
244 : m_contextData(0), m_next(0), m_prev(0)
248 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData(QDeclarativeContextData *data)
249 : m_contextData(0), m_next(0), m_prev(0)
251 setContextData(data);
254 QDeclarativeGuardedContextData::~QDeclarativeGuardedContextData()
259 void QDeclarativeGuardedContextData::setContextData(QDeclarativeContextData *contextData)
264 m_contextData = contextData;
265 m_next = contextData->contextGuards;
266 if (m_next) m_next->m_prev = &m_next;
267 m_prev = &contextData->contextGuards;
268 contextData->contextGuards = this;
272 QDeclarativeContextData *QDeclarativeGuardedContextData::contextData()
274 return m_contextData;
277 void QDeclarativeGuardedContextData::clear()
281 if (m_next) m_next->m_prev = m_prev;
288 QDeclarativeGuardedContextData &
289 QDeclarativeGuardedContextData::operator=(QDeclarativeContextData *d)
297 #endif // QDECLARATIVECONTEXT_P_H