PIValueTree improvements: methods with path (recursive), forEachRecursive()

PIValueTreeConvertions::fromTextFile now can include other files and handle ${} substitutions
This commit is contained in:
2024-07-09 21:44:30 +03:00
parent 903b320629
commit 0bafd3fa98
3 changed files with 92 additions and 14 deletions

View File

@@ -125,12 +125,24 @@ void PIValueTree::applyValues(const PIValueTree & root, bool recursive) {
PIVariant PIValueTree::childValue(const PIString & child_name, const PIVariant & default_value, bool * exists) const {
if (!contains(child_name)) {
const PIValueTree & node = child(child_name);
if (node.isNull()) {
if (exists) *exists = false;
return default_value;
}
return child(child_name).value();
if (exists) *exists = true;
return node.value();
}
PIVariant PIValueTree::childValue(const PIStringList & child_path, const PIVariant & default_value, bool * exists) const {
const PIValueTree & node = child(child_path);
if (node.isNull()) {
if (exists) *exists = false;
return default_value;
}
if (exists) *exists = true;
return node.value();
}
@@ -167,6 +179,15 @@ const PIValueTree & PIValueTree::child(const PIString & name) const {
}
const PIValueTree & PIValueTree::child(const PIStringList & path) const {
if (_is_null || path.isEmpty()) return *this;
const PIValueTree * ret = &child(path[0]);
for (int i = 1; i < path.size_s(); ++i)
ret = &child(path[i]);
return *ret;
}
PIValueTree & PIValueTree::child(const PIString & name) {
if (_is_null) return *this;
for (auto & c: _children)
@@ -175,6 +196,15 @@ PIValueTree & PIValueTree::child(const PIString & name) {
}
PIValueTree & PIValueTree::child(const PIStringList & path) {
if (_is_null || path.isEmpty()) return *this;
PIValueTree * ret = &child(path[0]);
for (int i = 1; i < path.size_s(); ++i)
ret = &child(path[i]);
return *ret;
}
PIValueTree & PIValueTree::insertChild(int index, const PIValueTree & n) {
if (_is_null) return *this;
for (auto & c: _children)
@@ -214,6 +244,11 @@ PIValueTree & PIValueTree::remove(const PIString & name) {
}
void PIValueTree::forEachRecursive(std::function<void(const PIValueTree &, const PIString &)> func) {
forEachRecursiveInternal(func);
}
const PIStringList & PIValueTree::standardAttributes() {
static PIStringList ret = {
// clang-format off
@@ -293,3 +328,11 @@ PIValueTree & PIValueTree::nullValue() {
ret._is_null = true;
return ret;
}
void PIValueTree::forEachRecursiveInternal(std::function<void(const PIValueTree &, const PIString &)> func, PIString prefix) {
for (auto & c: _children) {
func(c, prefix + c.name());
c.forEachRecursiveInternal(func, c.name() + ".");
}
}

View File

@@ -191,6 +191,17 @@ public:
//! \param exists Если не равно нулю, будет установлено в true, если дочерний узел существует, false в противном случае.
PIVariant childValue(const PIString & child_name, const PIVariant & default_value = PIVariant(), bool * exists = nullptr) const;
//! \~\brief
//! \~english Returns the value of a child node with a given path.
//! \param child_path The path of the child node.
//! \param default_value The default value to be returned if the child node is not found.
//! \param exists If not null, set to true if the child node exists, false otherwise.
//! \~russian Возвращает значение дочернего элемента с заданным путем.
//! \param child_path Путь дочернего узла.
//! \param default_value Значение по умолчанию, которое будет возвращено, если дочерний узел не найден.
//! \param exists Если не равно нулю, будет установлено в true, если дочерний узел существует, false в противном случае.
PIVariant childValue(const PIStringList & child_path, const PIVariant & default_value = PIVariant(), bool * exists = nullptr) const;
//! \~\brief
//! \~english Reads the value of a child node with a given name into "read_to". Returns a reference to the current %PIValueTree object.
//! \~russian Читает значение дочернего элемента с заданным именем в "read_to". Возвращает ссылку на текущий объект %PIValueTree.
@@ -220,11 +231,21 @@ public:
//! \~russian Возвращает константную ссылку на дочерний узел с заданным именем или пустой узел, если его нет.
const PIValueTree & child(const PIString & name) const;
//! \~\brief
//! \~english Returns a const reference to the child node with a given path, or null node if it doesn`t exists.
//! \~russian Возвращает константную ссылку на дочерний узел с заданным путем или пустой узел, если его нет.
const PIValueTree & child(const PIStringList & path) const;
//! \~\brief
//! \~english Returns a reference to the child node with a given name, or null node if it doesn`t exists.
//! \~russian Возвращает ссылку на дочерний узел с заданным именем или пустой узел, если его нет.
PIValueTree & child(const PIString & name);
//! \~\brief
//! \~english Returns a reference to the child node with a given path, or null node if it doesn`t exists.
//! \~russian Возвращает ссылку на дочерний узел с заданным путем или пустой узел, если его нет.
PIValueTree & child(const PIStringList & path);
//! \~\brief
//! \~english Inserts a node at a given index. Returns a reference to the current %PIValueTree object.
//! \~russian Вставляет узел в заданном индексе. Возвращает ссылку на текущий объект %PIValueTree.
@@ -260,6 +281,11 @@ public:
//! \~russian Возвращает константную ссылку на дочерний узел с заданным именем или пустой узел, если его нет.
const PIValueTree & operator[](const PIString & name) const { return child(name); }
//! \~\brief
//! \~english Recursive call "func" for every child. "full_name" is a name with path, joined with ".".
//! \~russian Рекурсивно выполняет "func" для каждого узла. "full_name" - это имя с путём, соединены ".".
void forEachRecursive(std::function<void(const PIValueTree & item, const PIString & full_name)> func);
//! \~\brief
//! \~english Returns a list of standard attribute names.
//! \~russian Возвращает список стандартных имен атрибутов.
@@ -268,6 +294,7 @@ public:
private:
static void print(PIString & s, const PIValueTree & v, PIString tab);
static PIValueTree & nullValue();
void forEachRecursiveInternal(std::function<void(const PIValueTree &, const PIString &)> func, PIString prefix = {});
PIString _name;
PIString _comment;