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 QtScript module 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 "qscriptvalueiterator.h"
|
---|
43 |
|
---|
44 | #ifndef QT_NO_SCRIPT
|
---|
45 |
|
---|
46 | #include "qscriptvalueiterator_p.h"
|
---|
47 | #include "qscriptvalueiteratorimpl_p.h"
|
---|
48 | #include "qscriptengine_p.h"
|
---|
49 | #include "qscriptcontext_p.h"
|
---|
50 | #include "qscriptvalueimpl_p.h"
|
---|
51 | #include "qscriptmember_p.h"
|
---|
52 | #include "qscriptobject_p.h"
|
---|
53 |
|
---|
54 | QT_BEGIN_NAMESPACE
|
---|
55 |
|
---|
56 | /*!
|
---|
57 | \since 4.3
|
---|
58 | \class QScriptValueIterator
|
---|
59 |
|
---|
60 | \brief The QScriptValueIterator class provides a Java-style iterator for QScriptValue.
|
---|
61 |
|
---|
62 | \ingroup script
|
---|
63 | \mainclass
|
---|
64 |
|
---|
65 | The QScriptValueIterator constructor takes a QScriptValue as
|
---|
66 | argument. After construction, the iterator is located at the very
|
---|
67 | beginning of the sequence of properties. Here's how to iterate over
|
---|
68 | all the properties of a QScriptValue:
|
---|
69 |
|
---|
70 | \snippet doc/src/snippets/code/src_script_qscriptvalueiterator.cpp 0
|
---|
71 |
|
---|
72 | The next() advances the iterator. The name(), value() and flags()
|
---|
73 | functions return the name, value and flags of the last item that was
|
---|
74 | jumped over.
|
---|
75 |
|
---|
76 | If you want to remove properties as you iterate over the
|
---|
77 | QScriptValue, use remove(). If you want to modify the value of a
|
---|
78 | property, use setValue().
|
---|
79 |
|
---|
80 | Note that QScriptValueIterator only iterates over the QScriptValue's
|
---|
81 | own properties; i.e. it does not follow the prototype chain. You can
|
---|
82 | use a loop like this to follow the prototype chain:
|
---|
83 |
|
---|
84 | \snippet doc/src/snippets/code/src_script_qscriptvalueiterator.cpp 1
|
---|
85 |
|
---|
86 | Note that QScriptValueIterator will not automatically skip over
|
---|
87 | properties that have the QScriptValue::SkipInEnumeration flag set;
|
---|
88 | that flag only affects iteration in script code. If you want, you
|
---|
89 | can skip over such properties with code like the following:
|
---|
90 |
|
---|
91 | \snippet doc/src/snippets/code/src_script_qscriptvalueiterator.cpp 2
|
---|
92 |
|
---|
93 | \sa QScriptValue::property()
|
---|
94 | */
|
---|
95 |
|
---|
96 | /*!
|
---|
97 | \internal
|
---|
98 | */
|
---|
99 | QScriptValueIteratorPrivate::QScriptValueIteratorPrivate()
|
---|
100 | : q_ptr(0), it(0)
|
---|
101 | {
|
---|
102 | }
|
---|
103 |
|
---|
104 | /*!
|
---|
105 | \internal
|
---|
106 | */
|
---|
107 | QScriptValueIteratorPrivate::~QScriptValueIteratorPrivate()
|
---|
108 | {
|
---|
109 | }
|
---|
110 |
|
---|
111 | /*!
|
---|
112 | Constructs an iterator for traversing \a object. The iterator is
|
---|
113 | set to be at the front of the sequence of properties (before the
|
---|
114 | first property).
|
---|
115 | */
|
---|
116 | QScriptValueIterator::QScriptValueIterator(const QScriptValue &object)
|
---|
117 | {
|
---|
118 | QScriptValueImpl val = QScriptValuePrivate::valueOf(object);
|
---|
119 | if (!val.isObject()) {
|
---|
120 | d_ptr = 0;
|
---|
121 | } else {
|
---|
122 | d_ptr = new QScriptValueIteratorPrivate();
|
---|
123 | d_ptr->it = new QScriptValueIteratorImpl(val);
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | /*!
|
---|
128 | Destroys the iterator.
|
---|
129 | */
|
---|
130 | QScriptValueIterator::~QScriptValueIterator()
|
---|
131 | {
|
---|
132 | if (d_ptr) {
|
---|
133 | delete d_ptr->it;
|
---|
134 | delete d_ptr;
|
---|
135 | d_ptr = 0;
|
---|
136 | }
|
---|
137 | }
|
---|
138 |
|
---|
139 | /*!
|
---|
140 | Returns true if there is at least one item ahead of the iterator
|
---|
141 | (i.e. the iterator is \e not at the back of the property sequence);
|
---|
142 | otherwise returns false.
|
---|
143 |
|
---|
144 | \sa next(), hasPrevious()
|
---|
145 | */
|
---|
146 | bool QScriptValueIterator::hasNext() const
|
---|
147 | {
|
---|
148 | Q_D(const QScriptValueIterator);
|
---|
149 | return (d && d->it->hasNext());
|
---|
150 | }
|
---|
151 |
|
---|
152 | /*!
|
---|
153 | Advances the iterator by one position.
|
---|
154 |
|
---|
155 | Calling this function on an iterator located at the back of the
|
---|
156 | container leads to undefined results.
|
---|
157 |
|
---|
158 | \sa hasNext(), previous(), name()
|
---|
159 | */
|
---|
160 | void QScriptValueIterator::next()
|
---|
161 | {
|
---|
162 | Q_D(QScriptValueIterator);
|
---|
163 | if (d)
|
---|
164 | d->it->next();
|
---|
165 | }
|
---|
166 |
|
---|
167 | /*!
|
---|
168 | Returns true if there is at least one item behind the iterator
|
---|
169 | (i.e. the iterator is \e not at the front of the property sequence);
|
---|
170 | otherwise returns false.
|
---|
171 |
|
---|
172 | \sa previous(), hasNext()
|
---|
173 | */
|
---|
174 | bool QScriptValueIterator::hasPrevious() const
|
---|
175 | {
|
---|
176 | Q_D(const QScriptValueIterator);
|
---|
177 | return (d && d->it->hasPrevious());
|
---|
178 | }
|
---|
179 |
|
---|
180 | /*!
|
---|
181 | Moves the iterator back by one position.
|
---|
182 |
|
---|
183 | Calling this function on an iterator located at the front of the
|
---|
184 | container leads to undefined results.
|
---|
185 |
|
---|
186 | \sa hasPrevious(), next(), name()
|
---|
187 | */
|
---|
188 | void QScriptValueIterator::previous()
|
---|
189 | {
|
---|
190 | Q_D(QScriptValueIterator);
|
---|
191 | if (d)
|
---|
192 | d->it->previous();
|
---|
193 | }
|
---|
194 |
|
---|
195 | /*!
|
---|
196 | Moves the iterator to the front of the QScriptValue (before the
|
---|
197 | first property).
|
---|
198 |
|
---|
199 | \sa toBack(), next()
|
---|
200 | */
|
---|
201 | void QScriptValueIterator::toFront()
|
---|
202 | {
|
---|
203 | Q_D(QScriptValueIterator);
|
---|
204 | if (d)
|
---|
205 | d->it->toFront();
|
---|
206 | }
|
---|
207 |
|
---|
208 | /*!
|
---|
209 | Moves the iterator to the back of the QScriptValue (after the
|
---|
210 | last property).
|
---|
211 |
|
---|
212 | \sa toFront(), previous()
|
---|
213 | */
|
---|
214 | void QScriptValueIterator::toBack()
|
---|
215 | {
|
---|
216 | Q_D(QScriptValueIterator);
|
---|
217 | if (d)
|
---|
218 | d->it->toBack();
|
---|
219 | }
|
---|
220 |
|
---|
221 | /*!
|
---|
222 | Returns the name of the last property that was jumped over using
|
---|
223 | next() or previous().
|
---|
224 |
|
---|
225 | \sa value(), flags()
|
---|
226 | */
|
---|
227 | QString QScriptValueIterator::name() const
|
---|
228 | {
|
---|
229 | Q_D(const QScriptValueIterator);
|
---|
230 | if (!d)
|
---|
231 | return QString();
|
---|
232 | return d->it->name();
|
---|
233 | }
|
---|
234 |
|
---|
235 | /*!
|
---|
236 | \since 4.4
|
---|
237 |
|
---|
238 | Returns the name of the last property that was jumped over using
|
---|
239 | next() or previous().
|
---|
240 | */
|
---|
241 | QScriptString QScriptValueIterator::scriptName() const
|
---|
242 | {
|
---|
243 | Q_D(const QScriptValueIterator);
|
---|
244 | if (!d)
|
---|
245 | return QScriptString();
|
---|
246 | QScriptEnginePrivate *eng = d->it->object().engine();
|
---|
247 | return eng->internedString(d->it->nameId());
|
---|
248 | }
|
---|
249 |
|
---|
250 | /*!
|
---|
251 | Returns the value of the last property that was jumped over using
|
---|
252 | next() or previous().
|
---|
253 |
|
---|
254 | \sa setValue(), name()
|
---|
255 | */
|
---|
256 | QScriptValue QScriptValueIterator::value() const
|
---|
257 | {
|
---|
258 | Q_D(const QScriptValueIterator);
|
---|
259 | if (!d)
|
---|
260 | return QScriptValue();
|
---|
261 | QScriptEnginePrivate *eng = d->it->object().engine();
|
---|
262 | return eng->toPublic(d->it->value());
|
---|
263 | }
|
---|
264 |
|
---|
265 | /*!
|
---|
266 | Sets the \a value of the last property that was jumped over using
|
---|
267 | next() or previous().
|
---|
268 |
|
---|
269 | \sa value(), name()
|
---|
270 | */
|
---|
271 | void QScriptValueIterator::setValue(const QScriptValue &value)
|
---|
272 | {
|
---|
273 | Q_D(const QScriptValueIterator);
|
---|
274 | if (d) {
|
---|
275 | QScriptEnginePrivate *eng = d->it->object().engine();
|
---|
276 | d->it->setValue(eng->toImpl(value));
|
---|
277 | }
|
---|
278 | }
|
---|
279 |
|
---|
280 | /*!
|
---|
281 | Returns the flags of the last property that was jumped over using
|
---|
282 | next() or previous().
|
---|
283 |
|
---|
284 | \sa value()
|
---|
285 | */
|
---|
286 | QScriptValue::PropertyFlags QScriptValueIterator::flags() const
|
---|
287 | {
|
---|
288 | Q_D(const QScriptValueIterator);
|
---|
289 | if (!d)
|
---|
290 | return 0;
|
---|
291 | return QScriptValue::PropertyFlags(d->it->flags() & ~QScript::Member::InternalRange);
|
---|
292 | }
|
---|
293 |
|
---|
294 | /*!
|
---|
295 | Removes the last property that was jumped over using next()
|
---|
296 | or previous().
|
---|
297 |
|
---|
298 | \sa setValue()
|
---|
299 | */
|
---|
300 | void QScriptValueIterator::remove()
|
---|
301 | {
|
---|
302 | Q_D(const QScriptValueIterator);
|
---|
303 | if (d)
|
---|
304 | d->it->remove();
|
---|
305 | }
|
---|
306 |
|
---|
307 | /*!
|
---|
308 | Makes the iterator operate on \a object. The iterator is set to be
|
---|
309 | at the front of the sequence of properties (before the first
|
---|
310 | property).
|
---|
311 | */
|
---|
312 | QScriptValueIterator& QScriptValueIterator::operator=(QScriptValue &object)
|
---|
313 | {
|
---|
314 | if (d_ptr) {
|
---|
315 | delete d_ptr->it;
|
---|
316 | d_ptr = 0;
|
---|
317 | }
|
---|
318 | QScriptValueImpl val = QScriptValuePrivate::valueOf(object);
|
---|
319 | if (val.isObject()) {
|
---|
320 | d_ptr = new QScriptValueIteratorPrivate();
|
---|
321 | d_ptr->it = new QScriptValueIteratorImpl(val);
|
---|
322 | }
|
---|
323 | return *this;
|
---|
324 | }
|
---|
325 |
|
---|
326 | QT_END_NAMESPACE
|
---|
327 |
|
---|
328 | #endif // QT_NO_SCRIPT
|
---|