-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathValTree.h
More file actions
133 lines (99 loc) · 3.65 KB
/
ValTree.h
File metadata and controls
133 lines (99 loc) · 3.65 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
///
/// ValTree
///
#pragma once
#include <vector>
#include <string>
class ValTree
{
public:
/// Blank constructor is necessary for storing ValTree objects in a vector.
ValTree();
/// Construct a ValTree node using a key / value pair.
ValTree(const std::string& key, const std::string& val);
/// Copy constructor.
ValTree(const ValTree& rhs);
/// Destructor.
~ValTree();
/// Completely clear the ValTree of key, value, children and siblings.
void clear();
/// Return true if the key and value are empty.
bool isNull() const;
/// Getters.
const std::string& getKey() const;
const std::string& getStr() const;
long getInt() const;
double getFloat() const;
const std::vector<std::string> getStrs(char delim = ',') const;
std::vector<long> getInts(char delim = ',') const;
std::vector<double> getFloats(char delim = ',') const;
/// Set the key and value.
void set(const std::string& key, const std::string& val);
/// Assignment operators (these only work if this ValTree object already has a key).
ValTree& operator=(const ValTree& rhs);
ValTree& operator=(const std::string& rhs);
ValTree& operator=(int rhs);
ValTree& operator=(double rhs);
/// Comparison operators.
bool operator==(const ValTree& rhs);
bool operator!=(const ValTree& rhs);
bool operator<(const ValTree& rhs);
bool operator>(const ValTree& rhs);
/// Return the size of this ValTree (0 if null, 1 + number of children otherwise).
int size() const;
/// Return a sibling given index (0 is this ValTree, 1 is the first child).
ValTree& getIndex(int index);
const ValTree& getIndex(int index) const;
/// Return true if the tree has children.
bool hasChildren() const;
/// Return the first child object (returns a static null ValTree if no children).
ValTree& getFirstChild();
const ValTree& getFirstChild() const;
/// Return a child object given a key (returns a static null ValTree if no children).
ValTree& getChild(const std::string& key);
const ValTree& getChild(const std::string& key) const;
/// Return a child object using a dot-separated query string to navigate the tree.
ValTree& query(const std::string& query);
const ValTree& query(const std::string& query) const;
/// Add a child to the current ValTree.
void addChild(const ValTree& v);
/// Add child value and all necessary branches.
void addChild(const std::string& query, const std::string& val);
/// Remove any children with the given key.
void removeChild(const std::string& key);
/// Parse the given file into this ValTree object.
bool parse(const std::string& filename);
/// Parse the given data string into this ValTree object.
bool parseData(const std::string& data);
/// Save this ValTree object into the given file.
bool save(const std::string& filename) const;
/// Write this ValTree object into the given string buffer.
void write(std::string& buffer) const;
/// Log out this ValTree to cout / stdout.
void log() const;
/// Used to iterate over the ValTree's siblings, for example: `for (auto& sibling : tree) {}`
class Iterator
{
private:
int index;
const ValTree& tree;
public:
Iterator(const ValTree& tree, int index);
const ValTree& operator*();
Iterator& operator++(); // note this is pre-increment only (ie. ++it)
bool operator!=(const Iterator& rhs);
};
/// Iteration.
Iterator begin() const;
Iterator end() const;
private:
std::string key;
std::string val;
long valInt;
double valFloat;
std::vector<ValTree> children;
void setValInt();
void setValFloat();
static ValTree& null();
bool parse(const std::string& data, int& pos, int lastDepth);
};