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 QDECLARATIVESCARCERESOURCESCRIPTCLASS_P_H
43 #define QDECLARATIVESCARCERESOURCESCRIPTCLASS_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 "private/qdeclarativepropertycache_p.h"
57 #include "private/qdeclarativetypenamecache_p.h"
59 #include <private/qscriptdeclarativeclass_p.h>
60 #include <QtScript/qscriptengine.h>
64 class QDeclarativeEngine;
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).
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).
77 A ScarceResourceData pointer should not be deleted manually, as
78 all instances of a ScarceResourceData should be owned by the
81 struct ScarceResourceData : public QScriptDeclarativeClass::Object {
82 ScarceResourceData(const QVariant &v) : resource(v), prev(0), next(0)
86 virtual ~ScarceResourceData()
91 // Insert this resource into the given list of resources.
92 void insertInto(ScarceResourceData **list)
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.
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;
105 // Remove this resource from the list of resources, without releasing the resource.
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;
117 // Release this resource, and remove from the list.
118 void releaseResource()
120 resource = QVariant();
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;
133 class Q_AUTOTEST_EXPORT QDeclarativeScarceResourceScriptClass : public QScriptDeclarativeClass
136 QDeclarativeScarceResourceScriptClass(QDeclarativeEngine *);
137 ~QDeclarativeScarceResourceScriptClass();
139 // Creates a new JavaScript object whose instance data is the scarce resource v
140 QScriptValue newScarceResource(const QVariant &v);
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);
150 PersistentIdentifier m_preserveId;
151 PersistentIdentifier m_destroyId;
152 QScriptValue m_preserve;
153 QScriptValue m_destroy;
155 static QScriptValue preserve(QScriptContext *context, QScriptEngine *engine);
156 static QScriptValue destroy(QScriptContext *context, QScriptEngine *engine);
158 QDeclarativeEngine *engine;
163 #endif // QDECLARATIVESCARCERESOURCESCRIPTCLASS_P_H