Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativeexpression_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEEXPRESSION_P_H
43 #define QDECLARATIVEEXPRESSION_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 "qdeclarativeexpression.h"
57
58 #include <private/qv8engine_p.h>
59 #include <private/qfieldlist_p.h>
60 #include <private/qdeletewatcher_p.h>
61 #include <private/qdeclarativeguard_p.h>
62 #include <private/qdeclarativeengine_p.h>
63
64 QT_BEGIN_NAMESPACE
65
66 class QDeclarativeAbstractExpression : public QDeleteWatchable
67 {
68 public:
69     QDeclarativeAbstractExpression();
70     virtual ~QDeclarativeAbstractExpression();
71
72     bool isValid() const;
73
74     QDeclarativeContextData *context() const;
75     void setContext(QDeclarativeContextData *);
76
77     virtual void refresh();
78
79 private:
80     friend class QDeclarativeContext;
81     friend class QDeclarativeContextData;
82     friend class QDeclarativeContextPrivate;
83     QDeclarativeContextData *m_context;
84     QDeclarativeAbstractExpression **m_prevExpression;
85     QDeclarativeAbstractExpression  *m_nextExpression;
86 };
87
88 class QDeclarativeDelayedError 
89 {
90 public:
91     inline QDeclarativeDelayedError() : nextError(0), prevError(0) {}
92     inline ~QDeclarativeDelayedError() { removeError(); }
93
94     QDeclarativeError error;
95
96     bool addError(QDeclarativeEnginePrivate *);
97
98     inline void removeError() {
99         if (!prevError) return;
100         if (nextError) nextError->prevError = prevError;
101         *prevError = nextError;
102         nextError = 0;
103         prevError = 0;
104     }
105
106 private:
107     QDeclarativeDelayedError  *nextError;
108     QDeclarativeDelayedError **prevError;
109 };
110
111 class QDeclarativeJavaScriptExpression : public QDeclarativeAbstractExpression, 
112                                          public QDeclarativeDelayedError
113 {
114 public:
115     QDeclarativeJavaScriptExpression();
116     virtual ~QDeclarativeJavaScriptExpression();
117
118     v8::Local<v8::Value> evaluate(v8::Handle<v8::Function>, bool *isUndefined);
119
120     inline bool requiresThisObject() const;
121     inline void setRequiresThisObject(bool v);
122     inline bool useSharedContext() const;
123     inline void setUseSharedContext(bool v);
124     inline bool notifyOnValueChanged() const;
125
126     void setNotifyOnValueChanged(bool v);
127     void resetNotifyOnValueChanged();
128
129     inline QObject *scopeObject() const;
130     inline void setScopeObject(QObject *v);
131
132     virtual void expressionChanged() {}
133
134 protected:
135     inline virtual QString expressionIdentifier();
136
137 private:
138     quint32 m_requiresThisObject:1;
139     quint32 m_useSharedContext:1;
140     quint32 m_notifyOnValueChanged:1;
141     quint32 m_dummy:29;
142
143     QObject *m_scopeObject;
144
145     typedef QDeclarativeJavaScriptExpressionGuard Guard;
146
147     struct GuardCapture : public QDeclarativeEnginePrivate::PropertyCapture {
148         GuardCapture(QDeclarativeJavaScriptExpression *e) : expression(e), errorString(0) {
149         }
150         ~GuardCapture()  {
151             Q_ASSERT(guards.isEmpty());
152             Q_ASSERT(errorString == 0);
153         }
154
155         virtual void captureProperty(QDeclarativeNotifier *);
156         virtual void captureProperty(QObject *, int, int);
157
158         QDeclarativeJavaScriptExpression *expression;
159         QFieldList<Guard, &Guard::next> guards;
160         QStringList *errorString;
161     };
162
163     QFieldList<Guard, &Guard::next> activeGuards;
164     GuardCapture *guardCapture;
165
166     void clearGuards();
167 };
168
169 class QDeclarativeExpression;
170 class QString;
171 class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeExpressionPrivate : public QObjectPrivate, public QDeclarativeJavaScriptExpression
172 {
173     Q_DECLARE_PUBLIC(QDeclarativeExpression)
174 public:
175     QDeclarativeExpressionPrivate();
176     ~QDeclarativeExpressionPrivate();
177
178     void init(QDeclarativeContextData *, const QString &, QObject *);
179     void init(QDeclarativeContextData *, v8::Handle<v8::Function>, QObject *);
180     void init(QDeclarativeContextData *, const QString &, bool, QObject *, const QString &, int, int);
181
182     QVariant value(QObject *secondaryScope = 0, bool *isUndefined = 0);
183
184     v8::Local<v8::Value> v8value(QObject *secondaryScope = 0, bool *isUndefined = 0);
185
186     static inline QDeclarativeExpressionPrivate *get(QDeclarativeExpression *expr);
187     static inline QDeclarativeExpression *get(QDeclarativeExpressionPrivate *expr);
188
189     void _q_notify();
190     virtual void expressionChanged();
191
192     static void exceptionToError(v8::Handle<v8::Message>, QDeclarativeError &);
193     static v8::Persistent<v8::Function> evalFunction(QDeclarativeContextData *ctxt, QObject *scope, 
194                                                      const QString &code, const QString &filename, int line,
195                                                      v8::Persistent<v8::Object> *qmlscope = 0);
196     static QDeclarativeExpression *create(QDeclarativeContextData *, QObject *, const QString &, bool,
197                                           const QString &, int, int);
198
199     bool expressionFunctionValid:1;
200     bool expressionFunctionRewritten:1;
201     bool extractExpressionFromFunction:1;
202
203     inline virtual QString expressionIdentifier();
204
205     QString expression;
206
207     v8::Persistent<v8::Object> v8qmlscope;
208     v8::Persistent<v8::Function> v8function;
209
210     QString url; // This is a QString for a reason.  QUrls are slooooooow...
211     int line;
212     int column;
213     QString name; //function name, hint for the debugger
214
215     QDeclarativeRefCount *dataRef;
216 };
217
218 bool QDeclarativeJavaScriptExpression::requiresThisObject() const 
219
220     return m_requiresThisObject; 
221 }
222
223 void QDeclarativeJavaScriptExpression::setRequiresThisObject(bool v) 
224
225     m_requiresThisObject = v; 
226 }
227
228 bool QDeclarativeJavaScriptExpression::useSharedContext() const 
229
230     return m_useSharedContext; 
231 }
232
233 void QDeclarativeJavaScriptExpression::setUseSharedContext(bool v) 
234
235     m_useSharedContext = v; 
236 }
237
238 bool QDeclarativeJavaScriptExpression::notifyOnValueChanged() const 
239
240     return m_notifyOnValueChanged; 
241 }
242
243 QObject *QDeclarativeJavaScriptExpression::scopeObject() const 
244
245     return m_scopeObject; 
246 }
247
248 void QDeclarativeJavaScriptExpression::setScopeObject(QObject *v) 
249
250     m_scopeObject = v; 
251 }
252
253 QString QDeclarativeJavaScriptExpression::expressionIdentifier() 
254
255     return QString();
256 }
257
258 QDeclarativeExpressionPrivate *QDeclarativeExpressionPrivate::get(QDeclarativeExpression *expr)
259 {
260     return static_cast<QDeclarativeExpressionPrivate *>(QObjectPrivate::get(expr));
261 }
262
263 QDeclarativeExpression *QDeclarativeExpressionPrivate::get(QDeclarativeExpressionPrivate *expr)
264 {
265     return expr->q_func();
266 }
267
268 QString QDeclarativeExpressionPrivate::expressionIdentifier()
269 {
270     return QLatin1String("\"") + expression + QLatin1String("\"");
271 }
272
273 QDeclarativeJavaScriptExpressionGuard::QDeclarativeJavaScriptExpressionGuard(QDeclarativeJavaScriptExpression *e)
274 : expression(e), next(0)
275
276     callback = &endpointCallback;
277 }
278
279 void QDeclarativeJavaScriptExpressionGuard::endpointCallback(QDeclarativeNotifierEndpoint *e)
280 {
281     static_cast<QDeclarativeJavaScriptExpressionGuard *>(e)->expression->expressionChanged();
282 }
283
284 QDeclarativeJavaScriptExpressionGuard *
285 QDeclarativeJavaScriptExpressionGuard::New(QDeclarativeJavaScriptExpression *e)
286 {
287     Q_ASSERT(e);
288     return QDeclarativeEnginePrivate::get(e->context()->engine)->jsExpressionGuardPool.New(e);
289 }
290
291 void QDeclarativeJavaScriptExpressionGuard::Delete()
292 {
293     QRecyclePool<QDeclarativeJavaScriptExpressionGuard>::Delete(this);
294 }
295
296 QT_END_NAMESPACE
297
298 #endif // QDECLARATIVEEXPRESSION_P_H