PIVariant and PIValueTree changes

This commit is contained in:
2022-11-29 18:08:06 +03:00
parent a516acbd44
commit 1327e46f61
4 changed files with 87 additions and 32 deletions

View File

@@ -28,10 +28,16 @@
//! //!
//! \~russian //! \~russian
//! //!
//!
const char PIValueTree::attributeHidden [] = "hidden" ;
const char PIValueTree::attributeReadOnly [] = "readonly" ;
const char PIValueTree::attributeIsLabel [] = "label" ;
const char PIValueTree::attributeArrayType [] = "arrayType" ; const char PIValueTree::attributeArrayType [] = "arrayType" ;
const char PIValueTree::attributeArrayMinCount[] = "arrayMinCount"; const char PIValueTree::attributeArrayMinCount[] = "arrayMinCount";
const char PIValueTree::attributeArrayMaxCount[] = "arrayMaxCount"; const char PIValueTree::attributeArrayMaxCount[] = "arrayMaxCount";
const char PIValueTree::attributeArrayReorder [] = "arrayReorder" ;
const char PIValueTree::attributeArrayResize [] = "arrayResize" ;
const char PIValueTree::attributeMinimum [] = "minimum" ; const char PIValueTree::attributeMinimum [] = "minimum" ;
const char PIValueTree::attributeMaximum [] = "maximum" ; const char PIValueTree::attributeMaximum [] = "maximum" ;
@@ -91,12 +97,6 @@ void PIValueTree::mergeAttributes(const PIVariantMap & a) {
} }
void PIValueTree::setStandardFlag(FlagStandard flag, bool on) {
if (on) _flags[Standard] |= flag;
else _flags[Standard] &= ~flag;
}
bool PIValueTree::contains(const PIString & name) const { bool PIValueTree::contains(const PIString & name) const {
if (_is_null) return true; if (_is_null) return true;
for (const auto & c: _children) for (const auto & c: _children)
@@ -151,6 +151,31 @@ PIValueTree & PIValueTree::remove(const PIString & name) {
} }
void PIValueTree::print(PIString & s, const PIValueTree & v, PIString tab) {
PIString ntab = tab + " ", nntab = ntab + " ";
s += tab + v.name() + " {\n";
if (v.value().isValid())
s += ntab + "value: " + v.value().toString() + "\n";
if (v.comment().isNotEmpty())
s += ntab + "comment: " + v.comment() + "\n";
if (v.attributes().isNotEmpty()) {
s += ntab + "attributes: [\n";
auto ait = v.attributes().makeIterator();
while (ait.next())
s += nntab + ait.key() + ": " + ait.value().toString() + "\n";
s += ntab + "]\n";
}
if (v.hasChildren()) {
s += ntab + "children: [\n";
for (const auto & c: v.children()) {
print(s, c, nntab);
}
s += ntab + "]\n";
}
s += tab + "}\n";
}
PIValueTree & PIValueTree::nullValue() { PIValueTree & PIValueTree::nullValue() {
static PIValueTree ret; static PIValueTree ret;
ret._is_null = true; ret._is_null = true;

View File

@@ -32,28 +32,21 @@
class PIP_EXPORT PIValueTree { class PIP_EXPORT PIValueTree {
BINARY_STREAM_FRIEND(PIValueTree); BINARY_STREAM_FRIEND(PIValueTree);
friend PICout operator <<(PICout s, const PIValueTree & v);
public: public:
PIValueTree(); PIValueTree();
PIValueTree(const PIVariant & v); PIValueTree(const PIVariant & v);
PIValueTree(const PIString & n, const PIVariant & v, const PIVariantMap & a = PIVariantMap()); PIValueTree(const PIString & n, const PIVariant & v, const PIVariantMap & a = PIVariantMap());
enum FlagsRole { static const char attributeHidden [];
Standard, static const char attributeReadOnly [];
Custom, static const char attributeIsLabel [];
FlagsRoleCount
};
enum FlagStandard {
Hidden = 0x01,
Editable = 0x02,
Label = 0x04,
ArrayReorder = 0x1001,
ArrayResize = 0x1002,
};
static const char attributeArrayType []; static const char attributeArrayType [];
static const char attributeArrayMinCount[]; static const char attributeArrayMinCount[];
static const char attributeArrayMaxCount[]; static const char attributeArrayMaxCount[];
static const char attributeArrayReorder [];
static const char attributeArrayResize [];
static const char attributeMinimum []; static const char attributeMinimum [];
static const char attributeMaximum []; static const char attributeMaximum [];
@@ -79,15 +72,12 @@ public:
const PIVariantMap & attributes() const {return _attributes;} const PIVariantMap & attributes() const {return _attributes;}
PIVariantMap & attributes() {return _attributes;} PIVariantMap & attributes() {return _attributes;}
PIVariant attribute(const PIString & an, const PIVariant & def = PIVariant()) const {return _attributes.value(an, def);}
void setAttribute(const PIString & n, const PIVariant & a); void setAttribute(const PIString & n, const PIVariant & a);
void mergeAttributes(const PIVariantMap & a); void mergeAttributes(const PIVariantMap & a);
PIFlags<FlagStandard> flagsStandard() {return _flags[Standard];}
uint flags(FlagsRole role) {return _flags[role];}
void setStandardFlag(FlagStandard flag, bool on = true);
void setFlags(FlagsRole role, uint value) {_flags[role] = value;}
const PIVector<PIValueTree> & children() const {return _children;} const PIVector<PIValueTree> & children() const {return _children;}
void clearChildren() {_children.clear();}
bool contains(const PIString & name) const; bool contains(const PIString & name) const;
PIValueTree child(const PIString & name) const; PIValueTree child(const PIString & name) const;
@@ -97,6 +87,7 @@ public:
PIValueTree & remove(const PIString & name); PIValueTree & remove(const PIString & name);
private: private:
static void print(PIString & s, const PIValueTree & v, PIString tab);
static PIValueTree & nullValue(); static PIValueTree & nullValue();
PIString _name; PIString _name;
@@ -104,7 +95,6 @@ private:
PIVariantMap _attributes; PIVariantMap _attributes;
PIVariant _value; PIVariant _value;
PIVector<PIValueTree> _children; PIVector<PIValueTree> _children;
uint _flags[FlagsRoleCount] = {Editable, 0};
bool _is_null = false; bool _is_null = false;
}; };
@@ -121,8 +111,6 @@ BINARY_STREAM_WRITE(PIValueTree) {
.add(4, v._value) .add(4, v._value)
.add(5, v._name) .add(5, v._name)
.add(6, v._children); .add(6, v._children);
for (int i = 0; i < PIValueTree::FlagsRoleCount; ++i)
cs.add(100 + i, v._flags[i]);
s << cs.data(); s << cs.data();
return s; return s;
} }
@@ -141,14 +129,23 @@ BINARY_STREAM_READ (PIValueTree) {
case 4: cs.get(v._value); case 4: cs.get(v._value);
case 5: cs.get(v._name); case 5: cs.get(v._name);
case 6: cs.get(v._children); case 6: cs.get(v._children);
default:
if (cs.getID() >= 100 && cs.getID() < (100 + PIValueTree::FlagsRoleCount)) {
v._flags[cs.getID() - 100] = cs.getData<uint>();
}
} }
} }
return s; return s;
} }
//! \relatesalso PICout
//! \~english Output operator to \a PICout.
//! \~russian Оператор вывода в \a PICout.
inline PICout operator <<(PICout s, const PIValueTree & v) {
s.space();
s.saveAndSetControls(0);
PIString str;
PIValueTree::print(str, v, "");
s << str;
s.restoreControls();
return s;
}
#endif // PIVALUETREE_H #endif // PIVALUETREE_H

View File

@@ -236,7 +236,10 @@ PIVariant::Type PIVariant::typeFromID(uint type_id) {
uint PIVariant::typeIDFromName(const PIString & tname) { uint PIVariant::typeIDFromName(const PIString & tname) {
return tname.hash(); PIVariant::Type t = typeFromName(tname);
if (t == pivInvalid) return 0;
if (t == pivCustom) return tname.hash();
return typeIDFromType(t);
} }
@@ -344,9 +347,13 @@ PIVariant PIVariant::fromValue(const PIByteArray & c, const PIString & type) {
PIVariant PIVariant::fromType(uint type_id) { PIVariant PIVariant::fromType(uint type_id) {
#ifdef CUSTOM_PIVARIANT
__PIVariantInfo__ * vi = __PIVariantInfoStorage__::get()->map->value(type_id); __PIVariantInfo__ * vi = __PIVariantInfoStorage__::get()->map->value(type_id);
if (!vi) return PIVariant(); if (!vi) return PIVariant();
return PIVariant::fromValue(vi->empty, type_id); return PIVariant::fromValue(vi->empty, type_id);
#else
return PIVariant();
#endif
} }
@@ -390,6 +397,17 @@ PIString PIVariant::typeName(PIVariant::Type type) {
} }
PIString PIVariant::typeNameFromID(uint type_id) {
#ifdef CUSTOM_PIVARIANT
__PIVariantInfo__ * vi = __PIVariantInfoStorage__::get()->map->value(type_id);
if (!vi) return PIString();
return vi->typeName;
#else
return PIString();
#endif
}
//! \~\brief //! \~\brief
//! \~english Returns variant content as boolean //! \~english Returns variant content as boolean
//! \~russian Возвращает содержимое как boolean //! \~russian Возвращает содержимое как boolean

View File

@@ -749,6 +749,21 @@ public:
//! \~russian Возвращает имя типа. //! \~russian Возвращает имя типа.
static PIString typeName(PIVariant::Type type); static PIString typeName(PIVariant::Type type);
//! \~english Returns type name.
//! \~russian Возвращает имя типа.
template <typename T>
static PIString typeName() {
#ifdef CUSTOM_PIVARIANT
return __PIVariantFunctions__<T>::typeNameHelper();
#else
return PIString();
#endif
}
//! \~english Returns type name from its ID.
//! \~russian Возвращает имя типа из его ID.
static PIString typeNameFromID(uint type_id);
template <typename T> template <typename T>
static uint typeID() { static uint typeID() {
#ifdef CUSTOM_PIVARIANT #ifdef CUSTOM_PIVARIANT