1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** 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 are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department 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 |
|
---|
49 | Q_DECLARE_METATYPE(QByteArray*)
|
---|
50 | Q_DECLARE_METATYPE(ByteArrayClass*)
|
---|
51 |
|
---|
52 | class ByteArrayClassPropertyIterator : public QScriptClassPropertyIterator
|
---|
53 | {
|
---|
54 | public:
|
---|
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 |
|
---|
70 | private:
|
---|
71 | int m_index;
|
---|
72 | int m_last;
|
---|
73 | };
|
---|
74 |
|
---|
75 | static qint32 toArrayIndex(const QString &str)
|
---|
76 | {
|
---|
77 | QByteArray bytes = str.toUtf8();
|
---|
78 | char *eptr;
|
---|
79 | quint32 pos = strtoul(bytes.constData(), &eptr, 10);
|
---|
80 | if ((eptr == bytes.constData() + bytes.size())
|
---|
81 | && (QByteArray::number(pos) == bytes)) {
|
---|
82 | return pos;
|
---|
83 | }
|
---|
84 | return -1;
|
---|
85 | }
|
---|
86 |
|
---|
87 | //! [0]
|
---|
88 | ByteArrayClass::ByteArrayClass(QScriptEngine *engine)
|
---|
89 | : QObject(engine), QScriptClass(engine)
|
---|
90 | {
|
---|
91 | qScriptRegisterMetaType<QByteArray>(engine, toScriptValue, fromScriptValue);
|
---|
92 |
|
---|
93 | length = engine->toStringHandle(QLatin1String("length"));
|
---|
94 |
|
---|
95 | proto = engine->newQObject(new ByteArrayPrototype(this),
|
---|
96 | QScriptEngine::QtOwnership,
|
---|
97 | QScriptEngine::SkipMethodsInEnumeration
|
---|
98 | | QScriptEngine::ExcludeSuperClassMethods
|
---|
99 | | QScriptEngine::ExcludeSuperClassProperties);
|
---|
100 | QScriptValue global = engine->globalObject();
|
---|
101 | proto.setPrototype(global.property("Object").property("prototype"));
|
---|
102 |
|
---|
103 | ctor = engine->newFunction(construct);
|
---|
104 | ctor.setData(qScriptValueFromValue(engine, this));
|
---|
105 | }
|
---|
106 | //! [0]
|
---|
107 |
|
---|
108 | ByteArrayClass::~ByteArrayClass()
|
---|
109 | {
|
---|
110 | }
|
---|
111 |
|
---|
112 | //! [3]
|
---|
113 | QScriptClass::QueryFlags ByteArrayClass::queryProperty(const QScriptValue &object,
|
---|
114 | const QScriptString &name,
|
---|
115 | QueryFlags flags, uint *id)
|
---|
116 | {
|
---|
117 | QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
|
---|
118 | if (!ba)
|
---|
119 | return 0;
|
---|
120 | if (name == length) {
|
---|
121 | return flags;
|
---|
122 | } else {
|
---|
123 | qint32 pos = toArrayIndex(name);
|
---|
124 | if (pos == -1)
|
---|
125 | return 0;
|
---|
126 | *id = pos;
|
---|
127 | if ((flags & HandlesReadAccess) && (pos >= ba->size()))
|
---|
128 | flags &= ~HandlesReadAccess;
|
---|
129 | return flags;
|
---|
130 | }
|
---|
131 | }
|
---|
132 | //! [3]
|
---|
133 |
|
---|
134 | //! [4]
|
---|
135 | QScriptValue ByteArrayClass::property(const QScriptValue &object,
|
---|
136 | const QScriptString &name, uint id)
|
---|
137 | {
|
---|
138 | QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
|
---|
139 | if (!ba)
|
---|
140 | return QScriptValue();
|
---|
141 | if (name == length) {
|
---|
142 | return ba->length();
|
---|
143 | } else {
|
---|
144 | qint32 pos = id;
|
---|
145 | if ((pos < 0) || (pos >= ba->size()))
|
---|
146 | return QScriptValue();
|
---|
147 | return uint(ba->at(pos)) & 255;
|
---|
148 | }
|
---|
149 | return QScriptValue();
|
---|
150 | }
|
---|
151 | //! [4]
|
---|
152 |
|
---|
153 | //! [5]
|
---|
154 | void ByteArrayClass::setProperty(QScriptValue &object,
|
---|
155 | const QScriptString &name,
|
---|
156 | uint id, const QScriptValue &value)
|
---|
157 | {
|
---|
158 | QByteArray *ba = qscriptvalue_cast<QByteArray*>(object.data());
|
---|
159 | if (!ba)
|
---|
160 | return;
|
---|
161 | if (name == length) {
|
---|
162 | ba->resize(value.toInt32());
|
---|
163 | } else {
|
---|
164 | qint32 pos = id;
|
---|
165 | if (pos < 0)
|
---|
166 | return;
|
---|
167 | if (ba->size() <= pos)
|
---|
168 | ba->resize(pos + 1);
|
---|
169 | (*ba)[pos] = char(value.toInt32());
|
---|
170 | }
|
---|
171 | }
|
---|
172 | //! [5]
|
---|
173 |
|
---|
174 | //! [6]
|
---|
175 | QScriptValue::PropertyFlags ByteArrayClass::propertyFlags(
|
---|
176 | const QScriptValue &/*object*/, const QScriptString &name, uint /*id*/)
|
---|
177 | {
|
---|
178 | if (name == length) {
|
---|
179 | return QScriptValue::Undeletable
|
---|
180 | | QScriptValue::SkipInEnumeration;
|
---|
181 | }
|
---|
182 | return QScriptValue::Undeletable;
|
---|
183 | }
|
---|
184 | //! [6]
|
---|
185 |
|
---|
186 | //! [7]
|
---|
187 | QScriptClassPropertyIterator *ByteArrayClass::newIterator(const QScriptValue &object)
|
---|
188 | {
|
---|
189 | return new ByteArrayClassPropertyIterator(object);
|
---|
190 | }
|
---|
191 | //! [7]
|
---|
192 |
|
---|
193 | QString ByteArrayClass::name() const
|
---|
194 | {
|
---|
195 | return QLatin1String("ByteArray");
|
---|
196 | }
|
---|
197 |
|
---|
198 | QScriptValue ByteArrayClass::prototype() const
|
---|
199 | {
|
---|
200 | return proto;
|
---|
201 | }
|
---|
202 |
|
---|
203 | QScriptValue ByteArrayClass::constructor()
|
---|
204 | {
|
---|
205 | return ctor;
|
---|
206 | }
|
---|
207 |
|
---|
208 | QScriptValue ByteArrayClass::newInstance(int size)
|
---|
209 | {
|
---|
210 | return newInstance(QByteArray(size, /*ch=*/0));
|
---|
211 | }
|
---|
212 |
|
---|
213 | //! [1]
|
---|
214 | QScriptValue ByteArrayClass::newInstance(const QByteArray &ba)
|
---|
215 | {
|
---|
216 | QScriptValue data = engine()->newVariant(qVariantFromValue(ba));
|
---|
217 | return engine()->newObject(this, data);
|
---|
218 | }
|
---|
219 | //! [1]
|
---|
220 |
|
---|
221 | //! [2]
|
---|
222 | QScriptValue ByteArrayClass::construct(QScriptContext *ctx, QScriptEngine *)
|
---|
223 | {
|
---|
224 | ByteArrayClass *cls = qscriptvalue_cast<ByteArrayClass*>(ctx->callee().data());
|
---|
225 | if (!cls)
|
---|
226 | return QScriptValue();
|
---|
227 | int size = ctx->argument(0).toInt32();
|
---|
228 | return cls->newInstance(size);
|
---|
229 | }
|
---|
230 | //! [2]
|
---|
231 |
|
---|
232 | QScriptValue ByteArrayClass::toScriptValue(QScriptEngine *eng, const QByteArray &ba)
|
---|
233 | {
|
---|
234 | QScriptValue ctor = eng->globalObject().property("ByteArray");
|
---|
235 | ByteArrayClass *cls = qscriptvalue_cast<ByteArrayClass*>(ctor.data());
|
---|
236 | if (!cls)
|
---|
237 | return eng->newVariant(qVariantFromValue(ba));
|
---|
238 | return cls->newInstance(ba);
|
---|
239 | }
|
---|
240 |
|
---|
241 | void ByteArrayClass::fromScriptValue(const QScriptValue &obj, QByteArray &ba)
|
---|
242 | {
|
---|
243 | ba = qscriptvalue_cast<QByteArray>(obj.data());
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 |
|
---|
248 | ByteArrayClassPropertyIterator::ByteArrayClassPropertyIterator(const QScriptValue &object)
|
---|
249 | : QScriptClassPropertyIterator(object)
|
---|
250 | {
|
---|
251 | toFront();
|
---|
252 | }
|
---|
253 |
|
---|
254 | ByteArrayClassPropertyIterator::~ByteArrayClassPropertyIterator()
|
---|
255 | {
|
---|
256 | }
|
---|
257 |
|
---|
258 | //! [8]
|
---|
259 | bool ByteArrayClassPropertyIterator::hasNext() const
|
---|
260 | {
|
---|
261 | QByteArray *ba = qscriptvalue_cast<QByteArray*>(object().data());
|
---|
262 | return m_index < ba->size();
|
---|
263 | }
|
---|
264 |
|
---|
265 | void ByteArrayClassPropertyIterator::next()
|
---|
266 | {
|
---|
267 | m_last = m_index;
|
---|
268 | ++m_index;
|
---|
269 | }
|
---|
270 |
|
---|
271 | bool ByteArrayClassPropertyIterator::hasPrevious() const
|
---|
272 | {
|
---|
273 | return (m_index > 0);
|
---|
274 | }
|
---|
275 |
|
---|
276 | void ByteArrayClassPropertyIterator::previous()
|
---|
277 | {
|
---|
278 | --m_index;
|
---|
279 | m_last = m_index;
|
---|
280 | }
|
---|
281 |
|
---|
282 | void ByteArrayClassPropertyIterator::toFront()
|
---|
283 | {
|
---|
284 | m_index = 0;
|
---|
285 | m_last = -1;
|
---|
286 | }
|
---|
287 |
|
---|
288 | void ByteArrayClassPropertyIterator::toBack()
|
---|
289 | {
|
---|
290 | QByteArray *ba = qscriptvalue_cast<QByteArray*>(object().data());
|
---|
291 | m_index = ba->size();
|
---|
292 | m_last = -1;
|
---|
293 | }
|
---|
294 |
|
---|
295 | QScriptString ByteArrayClassPropertyIterator::name() const
|
---|
296 | {
|
---|
297 | return QScriptString();
|
---|
298 | }
|
---|
299 |
|
---|
300 | uint ByteArrayClassPropertyIterator::id() const
|
---|
301 | {
|
---|
302 | return m_last;
|
---|
303 | }
|
---|
304 | //! [8]
|
---|