-
Notifications
You must be signed in to change notification settings - Fork 23
/
jsvalueiterator.h
50 lines (41 loc) · 1.33 KB
/
jsvalueiterator.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Copyright 2016 Cutehacks AS. All rights reserved.
// License can be found in the LICENSE file.
#include <QtQml/QJSValue>
class JSValueIterator
{
public:
JSValueIterator(const QJSValue &value)
: m_value(value)
{
if (value.isArray()) {
int length = value.property("length").toInt();
for (int i = 0; i < length; ++i)
m_keys << QString::number(i);
m_keys << QString::number(length);
} else {
QJSValue object = value.property("constructor");
QJSValue keysMethod = object.property("keys");
if (keysMethod.isCallable()) {
QJSValue ret = keysMethod.callWithInstance(object, QJSValueList() << value);
foreach (QVariant key, ret.toVariant().toList())
m_keys << key.toString();
}
}
}
bool next()
{
if (m_keys.isEmpty())
return false;
m_currentName = m_keys.takeFirst();
m_currentValue = m_value.property(m_currentName);
return true;
}
bool hasNext() { return !m_keys.isEmpty(); }
QString name() const { return m_currentName; }
QJSValue value() const { return m_currentValue; }
private:
QString m_currentName;
QJSValue m_currentValue;
QJSValue m_value;
QList<QString> m_keys;
};