Use V4 binding for non-final properties where possible
[profile/ivi/qtdeclarative.git] / src / qml / qml / v8 / qv8bindings_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 QtQml 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 QV8BINDINGS_P_H
43 #define QV8BINDINGS_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 <private/qpointervaluepair_p.h>
57 #include <private/qqmlpropertycache_p.h>
58 #include <private/qqmlinstruction_p.h>
59 #include <private/qqmlexpression_p.h>
60 #include <private/qqmlcompiler_p.h>
61 #include <private/qflagpointer_p.h>
62 #include <private/qqmlbinding_p.h>
63
64 QT_BEGIN_HEADER
65
66 QT_BEGIN_NAMESPACE
67
68 class QQmlCompiledData;
69
70 class QV8BindingsPrivate;
71 class QV8Bindings : public QQmlAbstractExpression
72 {
73 public:
74     QV8Bindings(QQmlCompiledData::V8Program *,
75                 int line,
76                 QQmlContextData *context);
77     virtual ~QV8Bindings();
78
79     QQmlAbstractBinding *configBinding(QObject *target, QObject *scope,
80                                                const QQmlInstruction::instr_assignBinding *);
81
82     // Inherited from QQmlAbstractExpression
83     virtual void refresh();
84
85     struct Binding : public QQmlJavaScriptExpression,
86                      public QQmlAbstractBinding {
87         Binding();
88
89         void update() { QQmlAbstractBinding::update(); }
90         void refresh();
91
92         // "Inherited" from QQmlJavaScriptExpression
93         static QString expressionIdentifier(QQmlJavaScriptExpression *);
94         static void expressionChanged(QQmlJavaScriptExpression *);
95
96         // "Inherited" from QQmlAbstractBinding
97         static void destroy(QQmlAbstractBinding *);
98         static int propertyIndex(const QQmlAbstractBinding *);
99         static QObject *object(const QQmlAbstractBinding *);
100         static void setEnabled(QQmlAbstractBinding *, bool, QQmlPropertyPrivate::WriteFlags);
101         static void update(QQmlAbstractBinding *, QQmlPropertyPrivate::WriteFlags);
102         static void retargetBinding(QQmlAbstractBinding *, QObject *, int);
103
104         QObject *object() const;
105         void update(QQmlPropertyPrivate::WriteFlags flags);
106
107         QV8Bindings *parent;
108
109         struct Retarget {
110             QObject *target;
111             int targetProperty;
112         };
113
114         // To save memory, we store flags inside the instruction pointer.
115         //    target.flag1: destroyed
116         //    instruction.flag1: enabled
117         //    instruction.flag2: updating
118         QPointerValuePair<QObject, Retarget> target;
119         QFlagPointer<const QQmlInstruction::instr_assignBinding> instruction;
120
121         inline bool destroyedFlag() const { return target.flag(); }
122         inline void setDestroyedFlag(bool v) { return target.setFlagValue(v); }
123         inline bool enabledFlag() const { return instruction.flag(); }
124         inline void setEnabledFlag(bool v) { instruction.setFlagValue(v); }
125         inline bool updatingFlag() const { return instruction.flag2(); }
126         inline void setUpdatingFlag(bool v) { instruction.setFlag2Value(v); }
127     };
128
129     inline void addref();
130     inline void release();
131
132     QQmlAbstractBinding *binding(int index) const { return bindings + index; }
133
134 private:
135     Q_DISABLE_COPY(QV8Bindings)
136
137     const QUrl &url() const;
138     const QString &urlString() const;
139     v8::Persistent<v8::Array> &functions() const;
140
141     QQmlCompiledData::V8Program *program;
142     Binding *bindings;
143     int refCount;
144 };
145
146 void QV8Bindings::addref()
147 {
148     ++refCount;
149 }
150
151 void QV8Bindings::release()
152 {
153     if (0 == --refCount)
154         delete this;
155 }
156
157 QT_END_NAMESPACE
158
159 QT_END_HEADER
160
161 #endif // QV8BINDINGS_P_H
162
163