|
| 1 | +#include "Util/InfoTree/InfoNode.h" |
| 2 | +#include "Util/InfoTree/InfoTree.h" |
| 3 | + |
| 4 | +InfoNode::InfoNode(std::size_t index, InfoTree *tree) |
| 5 | + : _index(index), _tree(tree) { |
| 6 | +} |
| 7 | + |
| 8 | +InfoKind InfoNode::GetKind() const { |
| 9 | + if (_index < _tree->_nodeOrInfos.size()) { |
| 10 | + return _tree->_nodeOrInfos[_index].Kind; |
| 11 | + } |
| 12 | + return InfoKind::None; |
| 13 | +} |
| 14 | + |
| 15 | +bool InfoNode::IsNull() const { |
| 16 | + return _index == 0; |
| 17 | +} |
| 18 | + |
| 19 | +bool InfoNode::IsObject() const { |
| 20 | + return GetKind() == InfoKind::Object; |
| 21 | +} |
| 22 | + |
| 23 | +bool InfoNode::IsArray() const { |
| 24 | + return GetKind() == InfoKind::Array; |
| 25 | +} |
| 26 | + |
| 27 | +bool InfoNode::IsNumber() const { |
| 28 | + return GetKind() == InfoKind::Number; |
| 29 | +} |
| 30 | + |
| 31 | +bool InfoNode::IsString() const { |
| 32 | + return GetKind() == InfoKind::String; |
| 33 | +} |
| 34 | + |
| 35 | +bool InfoNode::IsBool() const { |
| 36 | + return GetKind() == InfoKind::Bool; |
| 37 | +} |
| 38 | + |
| 39 | +void InfoNode::AddChild(InfoNode n) { |
| 40 | + if (_index < _tree->_nodeOrInfos.size()) { |
| 41 | + auto node = _tree->_nodeOrInfos[_index]; |
| 42 | + if (node.Kind == InfoKind::Array) { |
| 43 | + _tree->_arrayChildren[node.Data.ChildIndex].push_back(n._index); |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +void InfoNode::AddChild(std::string_view key, InfoNode n) { |
| 49 | + if (_index < _tree->_nodeOrInfos.size()) { |
| 50 | + auto node = _tree->_nodeOrInfos[_index]; |
| 51 | + if (node.Kind == InfoKind::Object) { |
| 52 | + _tree->_mapChildren[node.Data.ChildIndex].insert({std::string(key), n._index}); |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +std::string InfoNode::AsString() const { |
| 58 | + if (_index < _tree->_nodeOrInfos.size()) { |
| 59 | + if (_tree->_nodeOrInfos[_index].Kind == InfoKind::String) { |
| 60 | + return _tree->_stringValues[_tree->_nodeOrInfos[_index].Data.StringIndex]; |
| 61 | + } |
| 62 | + } |
| 63 | + return ""; |
| 64 | +} |
| 65 | + |
| 66 | +double InfoNode::AsDouble() const { |
| 67 | + if (_index < _tree->_nodeOrInfos.size()) { |
| 68 | + auto &node = _tree->_nodeOrInfos[_index]; |
| 69 | + if (node.Kind == InfoKind::Number) { |
| 70 | + return node.Data.NumberValue; |
| 71 | + } |
| 72 | + } |
| 73 | + return 0.0; |
| 74 | +} |
| 75 | + |
| 76 | +int InfoNode::AsInt() const { |
| 77 | + if (_index < _tree->_nodeOrInfos.size()) { |
| 78 | + auto &node = _tree->_nodeOrInfos[_index]; |
| 79 | + if (node.Kind == InfoKind::Number) { |
| 80 | + return node.Data.IntValue; |
| 81 | + } |
| 82 | + } |
| 83 | + return 0; |
| 84 | +} |
| 85 | + |
| 86 | +bool InfoNode::AsBool() const { |
| 87 | + if (_index < _tree->_nodeOrInfos.size()) { |
| 88 | + auto &node = _tree->_nodeOrInfos[_index]; |
| 89 | + if (node.Kind == InfoKind::Bool) { |
| 90 | + return node.Data.BoolValue; |
| 91 | + } |
| 92 | + } |
| 93 | + return false; |
| 94 | +} |
| 95 | + |
| 96 | +std::vector<InfoNode> InfoNode::GetChildren() const { |
| 97 | + std::vector<InfoNode> result; |
| 98 | + if (_index < _tree->_nodeOrInfos.size()) { |
| 99 | + auto &node = _tree->_nodeOrInfos[_index]; |
| 100 | + if (node.Kind == InfoKind::Array) { |
| 101 | + for (auto i: _tree->_arrayChildren[node.Data.ChildIndex]) { |
| 102 | + result.emplace_back(i, _tree); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + return result; |
| 107 | +} |
| 108 | + |
| 109 | +InfoNode InfoNode::GetValue(std::string_view key) const { |
| 110 | + if (_index < _tree->_nodeOrInfos.size()) { |
| 111 | + auto &node = _tree->_nodeOrInfos[_index]; |
| 112 | + if (node.Kind == InfoKind::Object) { |
| 113 | + auto &umap = _tree->_mapChildren[node.Data.ChildIndex]; |
| 114 | + std::string keyString(key); |
| 115 | + auto it = umap.find(keyString); |
| 116 | + if (it != umap.end()) { |
| 117 | + return InfoNode(it->second, _tree); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + return InfoNode(0, _tree); |
| 122 | +} |
| 123 | + |
0 commit comments