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 QDeclarativeV4Bindings;
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 *, bool parentTakesOwnership = false);
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();
138 quint32 isInternal:1;
139 quint32 ownedByParent:1; // unrelated to isInternal; parent context deletes children if true.
141 QDeclarativeContext *publicContext;
143 // Property name cache
144 QDeclarativeIntegerCache *propertyNames;
147 QObject *contextObject;
149 // Any script blocks that exist on this context
150 QList<QScriptValue> importedScripts;
155 // List of imports that apply to this context
156 QDeclarativeTypeNameCache *imports;
159 QDeclarativeContextData *childContexts;
161 // My peers in parent's childContexts list
162 QDeclarativeContextData *nextChild;
163 QDeclarativeContextData **prevChild;
165 // Expressions that use this context
166 QDeclarativeAbstractExpression *expressions;
168 // Doubly-linked list of objects that are owned by this context
169 QDeclarativeData *contextObjects;
171 // Doubly-linked list of context guards (XXX merge with contextObjects)
172 QDeclarativeGuardedContextData *contextGuards;
175 struct ContextGuard : public QDeclarativeGuard<QObject>
177 ContextGuard() : context(0) {}
178 inline ContextGuard &operator=(QObject *obj)
179 { QDeclarativeGuard<QObject>::operator=(obj); return *this; }
180 virtual void objectDestroyed(QObject *) {
181 if (context->contextObject && !QObjectPrivate::get(context->contextObject)->wasDeleted) bindings.notify();
183 QDeclarativeContextData *context;
184 QDeclarativeNotifier bindings;
186 ContextGuard *idValues;
188 void setIdProperty(int, QObject *);
189 void setIdPropertyData(QDeclarativeIntegerCache *);
191 // Optimized binding pointer
192 QDeclarativeV4Bindings *optimizedBindings;
194 // Linked contexts. this owns linkedContext.
195 QDeclarativeContextData *linkedContext;
197 // Linked list of uses of the Component attached property in this
199 QDeclarativeComponentAttached *componentAttached;
201 // Return the outermost id for obj, if any.
202 QString findObjectId(const QObject *obj) const;
204 static QDeclarativeContextData *get(QDeclarativeContext *context) {
205 return QDeclarativeContextPrivate::get(context)->data;
209 ~QDeclarativeContextData() {}
212 class QDeclarativeGuardedContextData
215 inline QDeclarativeGuardedContextData();
216 inline QDeclarativeGuardedContextData(QDeclarativeContextData *);
217 inline ~QDeclarativeGuardedContextData();
219 inline void setContextData(QDeclarativeContextData *);
221 inline QDeclarativeContextData *contextData();
223 inline operator QDeclarativeContextData*() const { return m_contextData; }
224 inline QDeclarativeContextData* operator->() const { return m_contextData; }
225 inline QDeclarativeGuardedContextData &operator=(QDeclarativeContextData *d);
228 QDeclarativeGuardedContextData &operator=(const QDeclarativeGuardedContextData &);
229 QDeclarativeGuardedContextData(const QDeclarativeGuardedContextData &);
230 friend class QDeclarativeContextData;
234 QDeclarativeContextData *m_contextData;
235 QDeclarativeGuardedContextData *m_next;
236 QDeclarativeGuardedContextData **m_prev;
239 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData()
240 : m_contextData(0), m_next(0), m_prev(0)
244 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData(QDeclarativeContextData *data)
245 : m_contextData(0), m_next(0), m_prev(0)
247 setContextData(data);
250 QDeclarativeGuardedContextData::~QDeclarativeGuardedContextData()
255 void QDeclarativeGuardedContextData::setContextData(QDeclarativeContextData *contextData)
260 m_contextData = contextData;
261 m_next = contextData->contextGuards;
262 if (m_next) m_next->m_prev = &m_next;
263 m_prev = &contextData->contextGuards;
264 contextData->contextGuards = this;
268 QDeclarativeContextData *QDeclarativeGuardedContextData::contextData()
270 return m_contextData;
273 void QDeclarativeGuardedContextData::clear()
277 if (m_next) m_next->m_prev = m_prev;
284 QDeclarativeGuardedContextData &
285 QDeclarativeGuardedContextData::operator=(QDeclarativeContextData *d)
293 #endif // QDECLARATIVECONTEXT_P_H