source: trunk/src/script/api/qscriptclass.cpp@ 618

Last change on this file since 618 was 561, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.1 sources.

  • Property svn:eol-style set to native
File size: 12.0 KB
RevLine 
[556]1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtScript module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL-ONLY$
10** GNU Lesser General Public License Usage
11** This file may be used under the terms of the GNU Lesser
12** General Public License version 2.1 as published by the Free Software
13** Foundation and appearing in the file LICENSE.LGPL included in the
14** packaging of this file. Please review the following information to
15** ensure the GNU Lesser General Public License version 2.1 requirements
16** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17**
18** If you have questions regarding the use of this file, please contact
19** Nokia at [email protected].
20** $QT_END_LICENSE$
21**
22****************************************************************************/
23
24#include "qscriptclass.h"
25#include "qscriptstring.h"
26
27/*!
28 \since 4.4
29 \class QScriptClass
30
31 \brief The QScriptClass class provides an interface for defining custom behavior of (a class of) Qt Script objects.
32
33 \ingroup script
34 \mainclass
35
36 The QScriptClass class defines an interface for handling various
37 aspects of interaction with the Qt Script objects associated with
38 the class. Such objects are created by calling
39 QScriptEngine::newObject(), passing a pointer to the QScriptClass as
40 argument.
41
42 By subclassing QScriptClass, you can define precisely how access to
43 properties of the objects that use your class is handled. This
44 enables a fully dynamic handling of properties, e.g. it's more
45 powerful than QScriptEngine::newQObject(). For example, you can use
46 QScriptClass to implement array-type objects (i.e. objects that
47 handle the \c{length} property, and properties whose names are valid
48 array indexes, in a special way), or to implement a "live"
49 (runtime-defined) proxy to an underlying object.
50
51 If you just need to handle access to a set of properties that are
52 known at the time an object is created (i.e. "semi-statically"), you
53 might consider using QScriptValue::setProperty() to define
54 getter/setter functions for the relevant properties, rather than
55 subclassing QScriptClass.
56
57 Reimplement queryProperty() to specify which properties are handled
58 in a custom way by your script class (i.e. should be
59 \bold{delegated} to the QScriptClass), and which properties should
60 be handled just like normal Qt Script object properties.
61
62 Reimplement property() and setProperty() to perform the actual
63 access (read or write) to the properties that your class
64 handles. Additionally, you can reimplement propertyFlags() to
65 specify custom flags for your properties.
66
67 Reimplement newIterator() to provide an iterator for objects of your
68 custom class. This is only necessary if objects of your class can
69 have custom properties that you want to be reported when an object
70 is used together with the QScriptValueIterator class, or when an
71 object is used in a for-in enumeration statement in a script.
72
73 When implementing custom classes of objects, you typically use
74 QScriptValue::setData() to store instance-specific data as part of
75 object initialization; the data won't be accessible from scripts
76 directly, but you can access it in e.g. your reimplementations of
77 property() and setProperty() (by calling QScriptValue::data()) to
78 perform custom processing.
79
80 Reimplement prototype() to provide a custom prototype object for
81 your script class.
82
83 Reimplement supportsExtension() and extension() if your custom
84 script class supports one or more of the extensions specified by the
85 Extension enum.
86
87 \sa QScriptClassPropertyIterator, QScriptEngine::newObject(), {Custom Script Class Example}
88*/
89
90/*!
91 \enum QScriptClass::Extension
92
93 This enum specifies the possible extensions to a QScriptClass.
94
95 \value Callable Instances of this class can be called as functions.
96
97 \value HasInstance Instances of this class implement [[HasInstance]].
98
99 \sa extension()
100*/
101
102/*!
103 \enum QScriptClass::QueryFlag
104
105 This enum describes flags that are used to query a QScriptClass
106 regarding how access to a property should be handled.
107
108 \value HandlesReadAccess The QScriptClass handles read access to this property.
109 \value HandlesWriteAccess The QScriptClass handles write access to this property.
110
111 \sa queryProperty()
112*/
113
114QT_BEGIN_NAMESPACE
115
116class QScriptClassPrivate
117{
118 Q_DECLARE_PUBLIC(QScriptClass)
119public:
120 QScriptClassPrivate() {}
121 virtual ~QScriptClassPrivate() {}
122
123 QScriptEngine *engine;
124
125 QScriptClass *q_ptr;
126};
127
128/*!
129 Constructs a QScriptClass object to be used in the given \a engine.
130
131 The engine does not take ownership of the QScriptClass object.
132*/
133QScriptClass::QScriptClass(QScriptEngine *engine)
134 : d_ptr(new QScriptClassPrivate)
135{
136 d_ptr->q_ptr = this;
137 d_ptr->engine = engine;
138}
139
140/*!
141 \internal
142*/
143QScriptClass::QScriptClass(QScriptEngine *engine, QScriptClassPrivate &dd)
144 : d_ptr(&dd)
145{
146 d_ptr->q_ptr = this;
147 d_ptr->engine = engine;
148}
149
150/*!
151 Destroys the QScriptClass object.
152
153 If a QScriptClass object is deleted before the associated engine(),
154 any Qt Script objects using the QScriptClass will be "demoted" to
155 normal Qt Script objects.
156*/
157QScriptClass::~QScriptClass()
158{
159}
160
161/*!
162 Returns the engine that this QScriptClass is associated with.
163*/
164QScriptEngine *QScriptClass::engine() const
165{
166 Q_D(const QScriptClass);
167 return d->engine;
168}
169
170/*!
171 Returns the object to be used as the prototype of new instances
172 of this class (created with QScriptEngine::newObject()).
173
174 The default implementation returns an invalid QScriptValue, meaning
175 that the standard Object prototype will be used. Reimplement this
176 function to provide your own custom prototype.
177
178 Typically you initialize your prototype object in the constructor of
179 your class, then return it in this function.
180
181 See the "Making Use of Prototype-Based Inheritance" section in the
182 QtScript documentation for more information on how prototypes are
183 used.
184*/
185QScriptValue QScriptClass::prototype() const
186{
187 return QScriptValue();
188}
189
190/*!
191 Returns the name of the script class.
192
193 Qt Script uses this name to generate a default string representation
194 of objects in case you do not provide a toString function.
195
196 The default implementation returns a null string.
197*/
198QString QScriptClass::name() const
199{
200 return QString();
201}
202
203/*!
204 Queries this script class for how access to the property with the
205 given \a name of the given \a object should be handled. The given \a
206 flags specify the aspects of interest. This function should return a
207 subset of \a flags to indicate which aspects of property access
208 should be further handled by the script class.
209
210 For example, if the \a flags contain HandlesReadAccess, and you
211 would like your class to handle the reading of the property (through
212 the property() function), the returned flags should include
213 HandlesReadAccess. If the returned flags do not contain
214 HandlesReadAccess, the property will be handled as a normal script
215 object property.
216
217 You can optionally use the \a id argument to store a value that will
218 subsequently be passed on to functions such as property() and
219 setProperty().
220
221 The default implementation of this function returns 0.
222
223 Note: This function is only called if the given property isn't
224 already a normal property of the object. For example, say you
225 advertise that you want to handle read access to property \c{foo},
226 but not write access; if \c{foo} is then assigned a value, it will
227 become a normal script object property, and subsequently you will no
228 longer be queried regarding read access to \c{foo}.
229
230 \sa property()
231*/
232QScriptClass::QueryFlags QScriptClass::queryProperty(
233 const QScriptValue &object, const QScriptString &name,
234 QueryFlags flags, uint *id)
235{
236 Q_UNUSED(object);
237 Q_UNUSED(name);
238 Q_UNUSED(flags);
239 Q_UNUSED(id);
240 return 0;
241}
242
243/*!
244 Returns the value of the property with the given \a name of the given
245 \a object.
246
247 The \a id argument is only useful if you assigned a value to it in
248 queryProperty().
249
250 The default implementation does nothing and returns an invalid QScriptValue.
251
252 \sa setProperty(), propertyFlags()
253*/
254QScriptValue QScriptClass::property(const QScriptValue &object,
255 const QScriptString &name, uint id)
256{
257 Q_UNUSED(object);
258 Q_UNUSED(name);
259 Q_UNUSED(id);
260 return QScriptValue();
261}
262
263/*!
264 Returns the flags of the property with the given \a name of the given
265 \a object.
266
267 The \a id argument is only useful if you assigned a value to it in
268 queryProperty().
269
270 The default implementation returns 0.