Initial V8 integration
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativecontext_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVECONTEXT_P_H
43 #define QDECLARATIVECONTEXT_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
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.
52 //
53 // We mean it.
54 //
55
56 #include "qdeclarativecontext.h"
57
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"
64
65 #include <QtCore/qhash.h>
66 #include <QtScript/qscriptvalue.h>
67 #include <QtCore/qset.h>
68
69 #include <private/qobject_p.h>
70 #include "private/qdeclarativeguard_p.h"
71
72 #include <private/qv8_p.h>
73
74 QT_BEGIN_NAMESPACE
75
76 class QDeclarativeContext;
77 class QDeclarativeExpression;
78 class QDeclarativeEngine;
79 class QDeclarativeExpression;
80 class QDeclarativeExpressionPrivate;
81 class QDeclarativeAbstractExpression;
82 class QDeclarativeV4Bindings;
83 class QDeclarativeContextData;
84
85 class QDeclarativeContextPrivate : public QObjectPrivate
86 {
87     Q_DECLARE_PUBLIC(QDeclarativeContext)
88 public:
89     QDeclarativeContextPrivate();
90
91     QDeclarativeContextData *data;
92
93     QList<QVariant> propertyValues;
94     int notifyIndex;
95
96     static QDeclarativeContextPrivate *get(QDeclarativeContext *context) {
97         return static_cast<QDeclarativeContextPrivate *>(QObjectPrivate::get(context));
98     }
99     static QDeclarativeContext *get(QDeclarativeContextPrivate *context) {
100         return static_cast<QDeclarativeContext *>(context->q_func());
101     }
102
103     // Only used for debugging
104     QList<QPointer<QObject> > instances;
105
106     static int context_count(QDeclarativeListProperty<QObject> *);
107     static QObject *context_at(QDeclarativeListProperty<QObject> *, int);
108 };
109
110 class QDeclarativeComponentAttached;
111 class QDeclarativeGuardedContextData;
112 class Q_AUTOTEST_EXPORT QDeclarativeContextData
113 {
114 public:
115     QDeclarativeContextData();
116     QDeclarativeContextData(QDeclarativeContext *);
117     void clearContext();
118     void destroy();
119     void invalidate();
120
121     inline bool isValid() const {
122         return engine && (!isInternal || !contextObject || !QObjectPrivate::get(contextObject)->wasDeleted);
123     }
124
125     // My parent context and engine
126     QDeclarativeContextData *parent;
127     QDeclarativeEngine *engine;
128
129     void setParent(QDeclarativeContextData *, bool parentTakesOwnership = false);
130     void refreshExpressions();
131
132     void addObject(QObject *);
133
134     QUrl resolvedUrl(const QUrl &);
135
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;
143     quint32 dummy:29;
144     QDeclarativeContext *publicContext;
145
146     // Property name cache
147     QDeclarativeIntegerCache *propertyNames;
148
149     // Context object
150     QObject *contextObject;
151
152     // Any script blocks that exist on this context
153     // XXX aakenned
154     QList<v8::Persistent<v8::Object> > importedScripts;
155 //    QList<QScriptValue> importedScripts;
156
157     // Context base url
158     QUrl url;
159
160     // List of imports that apply to this context
161     QDeclarativeTypeNameCache *imports;
162
163     // My children
164     QDeclarativeContextData *childContexts;
165
166     // My peers in parent's childContexts list
167     QDeclarativeContextData  *nextChild;
168     QDeclarativeContextData **prevChild;
169
170     // Expressions that use this context
171     QDeclarativeAbstractExpression *expressions;
172
173     // Doubly-linked list of objects that are owned by this context
174     QDeclarativeData *contextObjects;
175
176     // Doubly-linked list of context guards (XXX merge with contextObjects)
177     QDeclarativeGuardedContextData *contextGuards;
178
179     // id guards
180     struct ContextGuard : public QDeclarativeGuard<QObject>
181     {
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(); 
187         }
188         QDeclarativeContextData *context;
189         QDeclarativeNotifier bindings;
190     };
191     ContextGuard *idValues;
192     int idValueCount;
193     void setIdProperty(int, QObject *);
194     void setIdPropertyData(QDeclarativeIntegerCache *);
195
196     // Optimized binding pointer
197     QDeclarativeV4Bindings *optimizedBindings;
198
199     // Linked contexts. this owns linkedContext.
200     QDeclarativeContextData *linkedContext;
201
202     // Linked list of uses of the Component attached property in this
203     // context
204     QDeclarativeComponentAttached *componentAttached;
205
206     // Return the outermost id for obj, if any.
207     QString findObjectId(const QObject *obj) const;
208
209     static QDeclarativeContextData *get(QDeclarativeContext *context) {
210         return QDeclarativeContextPrivate::get(context)->data;
211     }
212
213 private:
214     ~QDeclarativeContextData() {}
215 };
216
217 class QDeclarativeGuardedContextData
218 {
219 public:
220     inline QDeclarativeGuardedContextData();
221     inline QDeclarativeGuardedContextData(QDeclarativeContextData *);
222     inline ~QDeclarativeGuardedContextData();
223
224     inline QDeclarativeContextData *contextData();
225     inline void setContextData(QDeclarativeContextData *);
226
227     inline bool isNull() const { return !m_contextData; }
228
229     inline operator QDeclarativeContextData*() const { return m_contextData; }
230     inline QDeclarativeContextData* operator->() const { return m_contextData; }
231     inline QDeclarativeGuardedContextData &operator=(QDeclarativeContextData *d);
232
233 private:
234     QDeclarativeGuardedContextData &operator=(const QDeclarativeGuardedContextData &);
235     QDeclarativeGuardedContextData(const QDeclarativeGuardedContextData &);
236     friend class QDeclarativeContextData;
237
238     inline void clear();
239
240     QDeclarativeContextData *m_contextData;
241     QDeclarativeGuardedContextData  *m_next;
242     QDeclarativeGuardedContextData **m_prev;
243 };
244
245 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData()
246 : m_contextData(0), m_next(0), m_prev(0)
247 {
248 }
249
250 QDeclarativeGuardedContextData::QDeclarativeGuardedContextData(QDeclarativeContextData *data)
251 : m_contextData(0), m_next(0), m_prev(0)
252 {
253     setContextData(data);
254 }
255
256 QDeclarativeGuardedContextData::~QDeclarativeGuardedContextData()
257 {
258     clear();
259 }
260
261 void QDeclarativeGuardedContextData::setContextData(QDeclarativeContextData *contextData)
262 {
263     clear();
264
265     if (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;
271     }
272 }
273
274 QDeclarativeContextData *QDeclarativeGuardedContextData::contextData()
275 {
276     return m_contextData;
277 }
278
279 void QDeclarativeGuardedContextData::clear()
280 {
281     if (m_prev) {
282         *m_prev = m_next;
283         if (m_next) m_next->m_prev = m_prev;
284         m_contextData = 0;
285         m_next = 0;
286         m_prev = 0;
287     }
288 }
289
290 QDeclarativeGuardedContextData &
291 QDeclarativeGuardedContextData::operator=(QDeclarativeContextData *d)
292 {
293     setContextData(d);
294     return *this;
295 }
296
297 QT_END_NAMESPACE
298
299 #endif // QDECLARATIVECONTEXT_P_H