PIValueTree::readChildValue method

some child-oriented methods
allow to create JSON from single PIValueTree
This commit is contained in:
2023-04-28 14:40:55 +03:00
parent 3ab57eea88
commit f5392f8b63
5 changed files with 31 additions and 2 deletions

View File

@@ -3,7 +3,7 @@ cmake_policy(SET CMP0017 NEW) # need include() with .cmake
project(PIP)
set(PIP_MAJOR 3)
set(PIP_MINOR 10)
set(PIP_REVISION 2)
set(PIP_REVISION 3)
set(PIP_SUFFIX )
set(PIP_COMPANY SHS)
set(PIP_DOMAIN org.SHS)

View File

@@ -252,6 +252,7 @@ PIJSON toJSONTree(const PIValueTree & root, PIValueTreeConversions::Options opti
PIJSON PIValueTreeConversions::toJSON(const PIValueTree & root, Options options) {
if (options[IncludeRoot]) return toJSONTree(root, options);
PIJSON ret = PIJSON::newArray();
for (const auto & c: root.children())
ret << toJSONTree(c, options);

View File

@@ -38,7 +38,8 @@ enum Option {
WithAttributes = 0x1,
WithComment = 0x2,
WithType = 0x4,
WithAll = 0xFFFFFF,
WithAll = 0xFFF,
IncludeRoot = 0x1000,
Default = WithAll
};
typedef PIFlags<Option> Options;

View File

@@ -146,6 +146,14 @@ bool PIValueTree::contains(const PIStringList & path) const {
}
int PIValueTree::childIndex(const PIString & name) const {
if (_is_null) return -1;
for (int i = 0; i < _children.size_s(); ++i)
if (_children[i].name() == name) return i;
return -1;
}
const PIValueTree & PIValueTree::child(const PIString & name) const {
if (_is_null) return *this;
for (const auto & c: _children)
@@ -162,6 +170,18 @@ PIValueTree & PIValueTree::child(const PIString & name) {
}
PIValueTree & PIValueTree::insertChild(int index, const PIValueTree & n) {
if (_is_null) return *this;
for (auto & c: _children)
if (c.name() == n.name()) {
c = n;
return *this;
}
_children.insert(piClampi(index, 0, _children.size_s()), n);
return *this;
}
PIValueTree & PIValueTree::addChild(const PIValueTree & n) {
if (_is_null) return *this;
for (auto & c: _children)

View File

@@ -93,11 +93,18 @@ public:
const PIVector<PIValueTree> & children() const { return _children; }
void clearChildren() { _children.clear(); }
PIVariant childValue(const PIString & child_name, const PIVariant & default_value = PIVariant(), bool * exists = nullptr) const;
template<typename T>
PIValueTree & readChildValue(const PIString & child_name, T & read_to) {
if (contains(child_name)) read_to = child(child_name).value().value<T>();
return *this;
}
bool contains(const PIString & name) const;
bool contains(const PIStringList & path) const;
int childIndex(const PIString & name) const;
const PIValueTree & child(const PIString & name) const;
PIValueTree & child(const PIString & name);
PIValueTree & insertChild(int index, const PIValueTree & n);
PIValueTree & addChild(const PIValueTree & n);
PIValueTree & addChildren(const PIVector<PIValueTree> & n);
PIValueTree & remove(const PIString & name);