-
Notifications
You must be signed in to change notification settings - Fork 0
/
keyvaluesnode.h
85 lines (74 loc) · 2.79 KB
/
keyvaluesnode.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#ifndef KEYVALUESNODE_H
#define KEYVALUESNODE_H
#include <QObject>
#include <QString>
#include <QVariant>
#include <QList>
#include <QDebug>
#include <QDataStream>
#include <QPair>
#define KV_BOPEN "{"
#define KV_CBOPEN '{'
#define KV_BCLOSE "}"
#define KV_CBCLOSE '}'
// If a KeyValuesNode has children, its value should be ignored when writing to a file.
class KeyValuesNode : public QObject
{
Q_OBJECT
Q_PROPERTY(QString key READ key WRITE setKey)
Q_PROPERTY(QVariant value READ value WRITE setValue)
public:
explicit KeyValuesNode(const QString &key, const QVariant &value, KeyValuesNode* parent = NULL);
explicit KeyValuesNode(const QString &key, KeyValuesNode* parent = NULL);
explicit KeyValuesNode(const QPair<QString, QString> &pair, KeyValuesNode* parent = NULL);
explicit KeyValuesNode(KeyValuesNode* parent = NULL);
~KeyValuesNode();
QString key() const;
QVariant value() const;
int childCount() const;
KeyValuesNode* childAt(int i) const;
void addChild(int at, KeyValuesNode* node);
void appendChild(KeyValuesNode* node);
KeyValuesNode* removeChild(int at);
void clear(); // Removes key, value and all children recursively.
void deleteChildren(); // Only removes children recursively.
QList<KeyValuesNode*>& childNodes();
bool isRoot();
KeyValuesNode* parentNode() const;
bool writeKey() const;
void setWriteKey(bool b);
bool isEmpty() const;
bool contains(const QString &key) const;
KeyValuesNode* find(const QString &key) const;
// T must implement operator <<.
template<typename T>
T& writeToStream(T &stream, const QString &tab = "\t", int depth = 0) const;
void constructFrom(const QString &input);
signals:
public slots:
void setKey(const QString &key);
void setKey(const char* key);
void setValue(const QVariant &value);
void setValue(const QString &value);
void setValue(const char* value);
void setValue(int value);
void setValue(float value);
void setValue(bool value);
private:
typedef QList<KeyValuesNode*> KVList;
typedef QPair<QString, QString> KVPair;
int readFrom(const QString &input);
static int getToken(const QString &input, QString &tokenOut);
static int getKeyAndValue(const QString &input, KVPair &pair);
static bool isInvalid(const KVPair &pair);
static bool isLeaf(const KVPair &pair);
static bool isSectionCreator(const KVPair &pair);
static bool isSectionTerminator(const KVPair &pair);
QString m_szKey;
QVariant m_vValue;
KVList m_Children;
bool m_bWriteKey;
};
QDataStream& operator<<(QDataStream &stream, const KeyValuesNode &node);
QTextStream& operator<<(QTextStream &stream, const KeyValuesNode &node);
#endif // KEYVALUESNODE_H