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"
74 class QDeclarativeContext;
75 class QDeclarativeExpression;
76 class QDeclarativeEngine;
77 class QDeclarativeExpression;
78 class QDeclarativeExpressionPrivate;
79 class QDeclarativeAbstractExpression;
80 class QDeclarativeCompiledBindings;
81 class QDeclarativeContextData;
83 class QDeclarativeContextPrivate : public QObjectPrivate
85 Q_DECLARE_PUBLIC(QDeclarativeContext)
87 QDeclarativeContextPrivate();
89 QDeclarativeContextData *data;
91 QList<QVariant> propertyValues;
94 static QDeclarativeContextPrivate *get(QDeclarativeContext *context) {
95 return static_cast<QDeclarativeContextPrivate *>(QObjectPrivate::get(context));
97 static QDeclarativeContext *get(QDeclarativeContextPrivate *context) {
98 return static_cast<QDeclarativeContext *>(context->q_func());
101 // Only used for debugging
102 QList<QPointer<QObject> > instances;
104 static int context_count(QDeclarativeListProperty<QObject> *);
105 static QObject *context_at(QDeclarativeListProperty<QObject> *, int);
108 class QDeclarativeComponentAttached;
109 class QDeclarativeGuardedContextData;
110 class Q_AUTOTEST_EXPORT QDeclarativeContextData
113 QDeclarativeContextData();
114 QDeclarativeContextData(QDeclarativeContext *);
119 inline bool isValid() const {
120 return engine && (!isInternal || !contextObject || !QObjectPrivate::get(contextObject)->wasDeleted);
123 // My parent context and engine
124 QDeclarativeContextData *parent;
125 QDeclarativeEngine *engine;
127 void setParent(QDeclarativeContextData *);
128 void refreshExpressions();
130 void addObject(QObject *);
132 QUrl resolvedUrl(const QUrl &);
134 // My containing QDeclarativeContext. If isInternal is true this owns publicContext.
135 // If internal is false publicContext owns this.
136 QDeclarativeContext *asQDeclarativeContext();
137 QDeclarativeContextPrivate *asQDeclarativeContextPrivate();
139 QDeclarativeContext *publicContext;
141 // Property name cache
142 QDeclarativeIntegerCache *propertyNames;
145 QObject *contextObject;
147 // Any script blocks that exist on this context
148 QList<QScriptValue> importedScripts;
149 void addImportedScript(const QDeclarativeParser::Object::ScriptBlock &script);
154 // List of imports that apply to this context
155 QDeclarativeTypeNameCache *imports;
158 QDeclarativeContextData *childContexts;
160 // My peers in parent's childContexts list
161 QDeclarativeContextData *nextChild;
162 QDeclarativeContextData **prevChild;
164 // Expressions that use this context
165 QDeclarativeAbstractExpression *expressions;
167 // Doubly-linked list of objects that are owned by this context
168 QDeclarativeData *contextObjects;
170 // Doubly-linked list of context guards (XXX merge with contextObjects)
171 QDeclarativeGuardedContextData *contextGuards;
174 struct ContextGuard : public QDeclarativeGuard<QObject>
176 ContextGuard() : context(0) {}
177 inline ContextGuard &operator=(QObject *obj)
178 { QDeclarativeGuard<QObject>::operator=(obj); return *this; }
179 virtual void objectDestroyed(QObject *) {
180 if (context->contextObject && !QObjectPrivate::get(context->contextObject)->wasDeleted) bindings.notify();
182 QDeclarativeContextData *context;
183 QDeclarativeNotifier bindings;
185 ContextGuard *idValues;
187 void setIdProperty(int, QObject *);
188 void setIdPropertyData(QDeclarativeIntegerCache *);
190 // Optimized binding pointer
191 QDeclarativeCompiledBindings *optimizedBindings;
193 // Linked contexts. this owns linkedContext.
194 QDeclarativeContextData *linkedContext;
196 // Linked list of uses of the Component attached property in this
198 QDeclarativeComponentAttached *componentAttached;
200 // Return the outermost id for obj, if any.
201 QString findObjectId(const QObject *obj) const;
203 static QDeclarativeContextData *get(QDeclarativeContext *context) {
204 return QDeclarativeContextPrivate::get(context)->data;
208 ~QDeclarativeContextData() {}
211 class QDeclarativeGuardedContextData
214 inline QDeclarativeGuardedContextData();
215 inline QDeclarativeGuardedContextData(QDeclarativeContextData *);
216 inline ~QDeclarativeGuardedContextData();
218 inline void setContextData(QDeclarativeContextData *);
220 inline QDeclarativeContextData *contextData();
222 inline operator QDeclarativeContextData*() const { return m_contextData; }
223 inline QDeclarativeContextData* operator->() const { return m_contextData; }
224 inline QDeclarativeGuardedContextData &operator=(QDeclarativeContextData *d);
227 QDeclarativeGuardedContextData &operator=(const QDeclarativeGuardedContextData &);
228 QDeclarativeGuardedContextData(const QDeclarativeGuardedContextData &);
229 friend class QDeclarativeContextData;
233 QDeclarativeContextData *m_contextData;
234 QDeclarativeGuardedContextData *m_next;
235 QDeclarativeGuardedContextData **m_prev;
238 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData()
239 : m_contextData(0), m_next(0), m_prev(0)
243 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData(QDeclarativeContextData *data)
244 : m_contextData(0), m_next(0), m_prev(0)
246 setContextData(data);
249 QDeclarativeGuardedContextData::~QDeclarativeGuardedContextData()
254 void QDeclarativeGuardedContextData::setContextData(QDeclarativeContextData *contextData)
259 m_contextData = contextData;
260 m_next = contextData->contextGuards;
261 if (m_next) m_next->m_prev = &m_next;
262 m_prev = &contextData->contextGuards;
263 contextData->contextGuards = this;
267 QDeclarativeContextData *QDeclarativeGuardedContextData::contextData()
269 return m_contextData;
272 void QDeclarativeGuardedContextData::clear()
276 if (m_next) m_next->m_prev = m_prev;
283 QDeclarativeGuardedContextData &
284 QDeclarativeGuardedContextData::operator=(QDeclarativeContextData *d)
292 #endif // QDECLARATIVECONTEXT_P_H