Merge branch 'master' of git://scm.dev.nokia.troll.no/qt/qtdeclarative
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativescarceresourcescriptclass_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 QDECLARATIVESCARCERESOURCESCRIPTCLASS_P_H
43 #define QDECLARATIVESCARCERESOURCESCRIPTCLASS_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/qdeclarativepropertycache_p.h"
57 #include "private/qdeclarativetypenamecache_p.h"
58
59 #include <private/qscriptdeclarativeclass_p.h>
60 #include <QtScript/qscriptengine.h>
61
62 QT_BEGIN_NAMESPACE
63
64 class QDeclarativeEngine;
65
66 /*
67    Scarce resources (like pixmaps and textures) are managed manually
68    in that the variant will be set to the invalid variant once the
69    JavaScript engine has finished using the JavaScript object whose
70    instance data is the ScarceResourceData (but before the garbage
71    collector frees the JavaScript object itself).
72
73    The engine stores a doubly-linked-list of scarce resources which
74    will to be cleaned up after a binding is successfully evaluated
75    (unless the user explicitly preserves the scarce resource).
76
77    A ScarceResourceData pointer should not be deleted manually, as
78    all instances of a ScarceResourceData should be owned by the
79    JavaScript engine.
80  */
81 struct ScarceResourceData : public QScriptDeclarativeClass::Object {
82     ScarceResourceData(const QVariant &v) : resource(v), prev(0), next(0)
83     {
84     }
85
86     virtual ~ScarceResourceData()
87     {
88         releaseResource();
89     }
90
91     // Insert this resource into the given list of resources.
92     void insertInto(ScarceResourceData **list)
93     {
94         // This node becomes the head of the list.
95         next = *list; // so our next = old list head
96         *list = this; // list now points to us (we're the head)
97         prev = list;  // as we're the head, our prev ptr becomes the list ptr.
98
99         // and the next node's prev pointer must contain a ptr to our next ptr,
100         // since per definition, prev always contains a pointer to the previous node's "next" ptr,
101         // and the "this" node is the "this->next" node's "prev" node.
102         if (next) next->prev = &next;
103     }
104
105     // Remove this resource from the list of resources, without releasing the resource.
106     void removeNode()
107     {
108         // whatever previously pointed to this node (ie, as that node's "next" node)
109         // should now point to our next node (since we no longer exist in the list).
110         // and the next node's prev ptr should point to our prev node.
111         if (prev) *prev = next;
112         if (next) next->prev = prev;
113         prev = 0;
114         next = 0;
115     }
116
117     // Release this resource, and remove from the list.
118     void releaseResource()
119     {
120         resource = QVariant();
121         removeNode();
122     }
123
124     QVariant resource;
125
126     // prev always contains a pointer to the previous node's "next" ptr.
127     // :. for the head node, [*prev] will be engine->scarceResources
128     // :. for every other node, [*prev] will be the previous node's "next" ptr.
129     ScarceResourceData **prev;
130     ScarceResourceData  *next;
131 };
132
133 class Q_AUTOTEST_EXPORT QDeclarativeScarceResourceScriptClass : public QScriptDeclarativeClass
134 {
135 public:
136     QDeclarativeScarceResourceScriptClass(QDeclarativeEngine *);
137     ~QDeclarativeScarceResourceScriptClass();
138
139     // Creates a new JavaScript object whose instance data is the scarce resource v
140     QScriptValue newScarceResource(const QVariant &v);
141
142     // inherited from QScriptDeclarativeClass
143     virtual QScriptClass::QueryFlags queryProperty(Object *, const Identifier &,
144                                                    QScriptClass::QueryFlags flags);
145     virtual Value property(Object *, const Identifier &);
146     virtual QVariant toVariant(Object *, bool *ok = 0);
147     QVariant toVariant(const QScriptValue &value);
148
149 private:
150     PersistentIdentifier m_preserveId;
151     PersistentIdentifier m_destroyId;
152     QScriptValue m_preserve;
153     QScriptValue m_destroy;
154
155     static QScriptValue preserve(QScriptContext *context, QScriptEngine *engine);
156     static QScriptValue destroy(QScriptContext *context, QScriptEngine *engine);
157
158     QDeclarativeEngine *engine;
159 };
160
161 QT_END_NAMESPACE
162
163 #endif // QDECLARATIVESCARCERESOURCESCRIPTCLASS_P_H