source: trunk/examples/script/customclass/bytearrayclass.cpp@ 651

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

trunk: Merged in qt 4.6.2 sources.

File size: 8.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 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 examples of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
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** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include <QtScript/QScriptClassPropertyIterator>
43#include <QtScript/QScriptEngine>
44#include "bytearrayclass.h"
45#include "bytearrayprototype.h"
46
47#include <stdlib.h>
48
49Q_DECLARE_METATYPE(QByteArray*)
50Q_DECLARE_METATYPE(ByteArrayClass*)
51
52class ByteArrayClassPropertyIterator : public QScriptClassPropertyIterator
53{
54public:
55 ByteArrayClassPropertyIterator(const QScriptValue &object);
56 ~ByteArrayClassPropertyIterator();
57
58 bool hasNext() const;
59 void next();
60
61 bool hasPrevious() const;
62 void previous();
63
64 void toFront();
65 void toBack();
66
67 QScriptString name() const;
68 uint id() const;
69
70private:
71 int m_index;
72 int m_last;
73};
74
75//! [0]
76ByteArrayClass::ByteArrayClass(QScriptEngine *engine)
77 : QObject(engine), QScriptClass(engine)
78{
79 qScriptRegisterMetaType<QByteArray>(engine, toScriptValue, fromScriptValue);
80
81 length = engine->toStringHandle(QLatin1String("length"));
82
83 proto = engine->newQObject(new ByteArrayPrototype(this),
84 QScriptEngine::QtOwnership,
85 QScriptEngine::SkipMethodsInEnumeration
86 | QScriptEngine::ExcludeSuperClassMethods
87 | QScriptEngine::ExcludeSuperClassProperties);
88 QScriptValue global = engine->globalObject();
89 proto.setPrototype(global.property("Object").property("prototype"));
90
91 ctor = engine->newFunction(construct, proto);
92 ctor.setData(qScriptValueFromValue(engine, this));
93}
94//! [0]
95
96ByteArrayClass::~ByteArrayClass()
97{
98}
99
100//! [3]
101QScriptClass::QueryFlags ByteArrayClass::queryProperty(const QScriptValue &object,
102 const QScriptString &name,
103 QueryFlags flags, uint *id)
104{
105 QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
106 if (!ba)
107 return 0;
108 if (name == length) {
109 return flags;
110 } else {
111 bool isArrayIndex;
112 qint32 pos = name.toArrayIndex(&isArrayIndex);
113 if (!isArrayIndex)
114 return 0;
115 *id = pos;
116 if ((flags & HandlesReadAccess) && (pos >= ba->size()))
117 flags &= ~HandlesReadAccess;
118 return flags;
119 }
120}
121//! [3]
122
123//! [4]
124QScriptValue ByteArrayClass::property(const QScriptValue &object,
125 const QScriptString &name, uint id)
126{
127 QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
128 if (!ba)
129 return QScriptValue();
130 if (name == length) {
131 return ba->length();
132 } else {
133 qint32 pos = id;
134 if ((pos < 0) || (pos >= ba->size()))
135 return QScriptValue();
136 return uint(ba->at(pos)) & 255;
137 }
138 return QScriptValue();
139}
140//! [4]
141
142//! [5]
143void ByteArrayClass::setProperty(QScriptValue &object,
144 const QScriptString &name,
145 uint id, const QScriptValue &value)
146{
147 QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
148 if (!ba)
149 return;
150 if (name == length) {
151 ba->resize(value.toInt32());
152 } else {
153 qint32 pos = id;
154 if (pos < 0)
155 return;
156 if (ba->size() <= pos)
157 ba->resize(pos + 1);
158 (*ba)[pos] = char(value.toInt32());
159 }
160}
161//! [5]
162
163//! [6]
164QScriptValue::PropertyFlags ByteArrayClass::propertyFlags(
165 const QScriptValue &/*object*/, const QScriptString &name, uint /*id*/)
166{
167 if (name == length) {
168 return QScriptValue::Undeletable
169 | QScriptValue::SkipInEnumeration;
170 }
171 return QScriptValue::Undeletable;
172}
173//! [6]
174
175//! [7]
176QScriptClassPropertyIterator *ByteArrayClass::newIterator(const QScriptValue &object)
177{
178 return new ByteArrayClassPropertyIterator(object);
179}
180//! [7]
181
182QString ByteArrayClass::name() const
183{
184 return QLatin1String("ByteArray");
185}
186
187QScriptValue ByteArrayClass::prototype() const
188{
189 return proto;
190}
191
192QScriptValue ByteArrayClass::constructor()
193{
194 return ctor;
195}
196
197QScriptValue ByteArrayClass::newInstance(int size)
198{
199 return newInstance(QByteArray(size, /*ch=*/0));
200}
201
202//! [1]
203QScriptValue ByteArrayClass::newInstance(const QByteArray &ba)
204{
205 QScriptValue data = engine()->newVariant(qVariantFromValue(ba));
206 return engine()->newObject(this, data);
207}
208//! [1]
209
210//! [2]
211QScriptValue ByteArrayClass::construct(QScriptContext *ctx, QScriptEngine *)
212{
213 ByteArrayClass *cls = qscriptvalue_cast<ByteArrayClass*>(ctx->callee().data());
214 if (!cls)
215 return QScriptValue();
216 QScriptValue arg = ctx->argument(0);
217 if (arg.instanceOf(ctx->callee()))
218 return cls->newInstance(qscriptvalue_cast<QByteArray>(arg));
219 int size = arg.toInt32();
220 return cls->newInstance(size);
221}
222//! [2]
223
224QScriptValue ByteArrayClass::toScriptValue(QScriptEngine *eng, const QByteArray &ba)
225{
226 QScriptValue ctor = eng->globalObject().property("ByteArray");
227 ByteArrayClass *cls = qscriptvalue_cast<ByteArrayClass*>(ctor.data());
228 if (!cls)
229 return eng->newVariant(qVariantFromValue(ba));
230 return cls->newInstance(ba);
231}
232
233void ByteArrayClass::fromScriptValue(const QScriptValue &obj, QByteArray &ba)
234{
235 ba = qvariant_cast<QByteArray>(obj.data().toVariant());
236}
237
238
239
240ByteArrayClassPropertyIterator::ByteArrayClassPropertyIterator(const QScriptValue &object)
241 : QScriptClassPropertyIterator(object)
242{
243 toFront();
244}
245
246ByteArrayClassPropertyIterator::~ByteArrayClassPropertyIterator()
247{
248}
249
250//! [8]
251bool ByteArrayClassPropertyIterator::hasNext() const
252{
253 QByteArray *ba = qscriptvalue_cast<QByteArray*>(object().data());
254 return m_index < ba->size();
255}
256
257void ByteArrayClassPropertyIterator::next()
258{
259 m_last = m_index;
260 ++m_index;
261}
262
263bool ByteArrayClassPropertyIterator::hasPrevious() const
264{
265 return (m_index > 0);
266}
267
268void ByteArrayClassPropertyIterator::previous()
269{
270 --m_index;
271 m_last = m_index;
272}
273
274void ByteArrayClassPropertyIterator::toFront()
275{
276 m_index = 0;
277 m_last = -1;
278}
279
280void ByteArrayClassPropertyIterator::toBack()
281{
282 QByteArray *ba = qscriptvalue_cast<QByteArray*>(object().data());
283 m_index = ba->size();
284 m_last = -1;
285}
286
287QScriptString ByteArrayClassPropertyIterator::name() const
288{
289 return object().engine()->toStringHandle(QString::number(m_last));
290}
291
292uint ByteArrayClassPropertyIterator::id() const
293{
294 return m_last;
295}
296//! [8]
Note: See TracBrowser for help on using the repository browser.