PIP_ADD_COUNTER now almost unique across different cpp
add PIValueTree
This commit is contained in:
168
main.cpp
168
main.cpp
@@ -3,151 +3,10 @@
|
||||
#include "pibytearray.h"
|
||||
#include "pimathbase.h"
|
||||
#include "pijson.h"
|
||||
#include "piwaitevent_p.h"
|
||||
#include "pivaluetree.h"
|
||||
|
||||
using namespace PICoutManipulators;
|
||||
|
||||
typedef PIMap<PIString, PIVariant> PIVariantMap;
|
||||
typedef PIVector<PIVariant> PIVariantVector;
|
||||
|
||||
REGISTER_VARIANT(PIVariantMap);
|
||||
REGISTER_VARIANT(PIVariantVector);
|
||||
|
||||
|
||||
class PIValueTree {
|
||||
public:
|
||||
PIValueTree();
|
||||
PIValueTree(const PIVariant & v);
|
||||
PIValueTree(const PIString & n, const PIVariant & v);
|
||||
|
||||
enum FlagStandard {
|
||||
Hidden = 0x01,
|
||||
Editable = 0x02,
|
||||
Label = 0x04,
|
||||
ArrayReorder = 0x1001,
|
||||
ArrayResize = 0x1002,
|
||||
};
|
||||
|
||||
bool isNull() const {return _is_null;}
|
||||
bool isValid() const {return _value.isValid();}
|
||||
|
||||
const PIString & name() const {return _name;}
|
||||
void setName(const PIString & n);
|
||||
|
||||
const PIVariant & value() const {return _value;}
|
||||
void setValue(const PIVariant & v);
|
||||
|
||||
const PIVector<PIValueTree> & children() const {return _children;}
|
||||
|
||||
const PIVariantMap & attributes() const {return _attributes;}
|
||||
PIVariantMap & attributes() {return _attributes;}
|
||||
|
||||
PIFlags<FlagStandard> flagsStandard() {return _flags_standard;}
|
||||
uint flagsCustom() {return _flags_custom;}
|
||||
|
||||
bool contains(const PIString & name) const;
|
||||
PIValueTree child(const PIString & name) const;
|
||||
PIValueTree & child(const PIString & name);
|
||||
PIValueTree & insert(const PIValueTree & n);
|
||||
PIValueTree & remove(const PIString & name);
|
||||
|
||||
private:
|
||||
static PIValueTree & nullValue();
|
||||
PIString _name;
|
||||
PIVariantMap _attributes;
|
||||
PIVariant _value;
|
||||
PIVector<PIValueTree> _children;
|
||||
uint _flags_standard = Editable;
|
||||
uint _flags_custom = 0u;
|
||||
bool _is_null = false;
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
PIValueTree::PIValueTree() {
|
||||
}
|
||||
|
||||
|
||||
PIValueTree::PIValueTree(const PIVariant & v) {
|
||||
setValue(v);
|
||||
}
|
||||
|
||||
|
||||
PIValueTree::PIValueTree(const PIString & n, const PIVariant & v) {
|
||||
setName(n);
|
||||
setValue(v);
|
||||
}
|
||||
|
||||
|
||||
void PIValueTree::setName(const PIString & n) {
|
||||
if (_is_null) return;
|
||||
_name = n;
|
||||
}
|
||||
|
||||
|
||||
void PIValueTree::setValue(const PIVariant & v) {
|
||||
if (_is_null) return;
|
||||
_value = v;
|
||||
}
|
||||
|
||||
|
||||
bool PIValueTree::contains(const PIString & name) const {
|
||||
if (_is_null) return true;
|
||||
for (const auto & c: _children)
|
||||
if (c.name() == name)
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
PIValueTree PIValueTree::child(const PIString & name) const {
|
||||
if (_is_null) return PIValueTree();
|
||||
for (const auto & c: _children)
|
||||
if (c.name() == name)
|
||||
return c;
|
||||
return PIValueTree();
|
||||
}
|
||||
|
||||
|
||||
PIValueTree & PIValueTree::child(const PIString & name) {
|
||||
if (_is_null) return nullValue();
|
||||
for (auto & c: _children)
|
||||
if (c.name() == name)
|
||||
return c;
|
||||
return nullValue();
|
||||
}
|
||||
|
||||
|
||||
PIValueTree & PIValueTree::insert(const PIValueTree & n) {
|
||||
if (_is_null) return nullValue();
|
||||
for (auto & c: _children)
|
||||
if (c.name() == n.name()) {
|
||||
c = n;
|
||||
return *this;
|
||||
}
|
||||
_children << n;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
PIValueTree & PIValueTree::remove(const PIString & name) {
|
||||
if (_is_null) return nullValue();
|
||||
_children.removeWhere([name](const PIValueTree & i){return i.name() == name;});
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
PIValueTree & PIValueTree::nullValue() {
|
||||
static PIValueTree ret;
|
||||
ret._is_null = true;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
struct SomeType {
|
||||
int i;
|
||||
float f;
|
||||
@@ -169,18 +28,19 @@ REGISTER_VARIANT_CAST(PIString, SomeType) {
|
||||
|
||||
int main(int argc, char * argv[]) {
|
||||
PIValueTree root;
|
||||
root.insert({"bool", PIVariant(false)});
|
||||
root.insert({"integer", PIVariant(256)});
|
||||
root.insert({"string", PIVariant("str")});
|
||||
//for (auto it: root.children()) {
|
||||
// piCout << it.name() << it.value();
|
||||
//}
|
||||
PIVariant v = PIVariant::fromType("PIVariantTypes::IODevice");
|
||||
piCout << v;
|
||||
piCout << v.toString();
|
||||
v.setValueFromString("2;-0.1");
|
||||
piCout << v.value<PIVariantTypes::IODevice>();
|
||||
piCout << v.toString();
|
||||
root.addChild({"bool", PIVariant(false)});
|
||||
root.addChild({"integer", PIVariant(256)});
|
||||
root.addChild({"string", PIVariant("str")});
|
||||
for (auto it: root.children()) {
|
||||
piCout << it.name() << it.value();
|
||||
}
|
||||
piCout << piSerialize(root);
|
||||
//PIVariant v = PIVariant::fromType(PIVariant::typeID<double>());
|
||||
//piCout << v;
|
||||
//piCout << v.toString();
|
||||
//v.setValueFromString("2E-3");
|
||||
//piCout << v.value<double>();
|
||||
//piCout << v.toString();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user