diff --git a/src/core/pipropertystorage.cpp b/src/core/pipropertystorage.cpp new file mode 100644 index 00000000..765a8ee1 --- /dev/null +++ b/src/core/pipropertystorage.cpp @@ -0,0 +1,97 @@ +#include "pipropertystorage.h" + + +bool PIPropertyStorage::isPropertyExists(const PIString & _name) const { + for (int i = 0; i < props.size(); ++i) + if (props[i].name == _name) + return true; + return false; +} + + +void PIPropertyStorage::addProperty(const PIPropertyStorage::Property & p) { + for (int i = 0; i < props.size(); ++i) + if (props[i].name == p.name) { + props[i] = p; + return; + } + props << p; +} + + +void PIPropertyStorage::removeProperty(const PIString & _name) { + for (int i = 0; i < props.size(); ++i) + if (props[i].name == _name) { + props.remove(i); + return; + } +} + + +void PIPropertyStorage::removePropertiesByFlag(int flag) { + for (int i = 0; i < props.size(); ++i) + if ((props[i].flags & flag) == flag) { + props.remove(i); + --i; + } +} + + +void PIPropertyStorage::updateProperties(const PIVector & properties_, int flag_ignore) { + PIMap values; + piForeachC(Property & p, props) + if (((p.flags & flag_ignore) != flag_ignore) || (flag_ignore == 0)) + values[p.name] = p.value; + props = properties_; + for (int i = 0; i < props.size(); ++i) { + Property & p(props[i]); + if (values.contains(p.name)) { + PIVariant pv = values[p.name]; + if (pv.type() == p.value.type()) + p.value = pv; + } + } +} + + +PIPropertyStorage::Property PIPropertyStorage::propertyByName(const PIString & name) const { + piForeachC(Property & p, props) + if (p.name == name) + return p; + return Property(); +} + + +PIVariant PIPropertyStorage::propertyValueByName(const PIString & name) const { + piForeachC(Property & p, props) + if (p.name == name) + return p.value; + return PIVariant(); +} + + +void PIPropertyStorage::setPropertyValue(const PIString & name, const PIVariant & value) { + for (int i = 0; i < props.size(); ++i) + if (props[i].name == name) { + props[i].value = value; + return; + } +} + + +void PIPropertyStorage::setPropertyComment(const PIString & name, const PIString & comment) { + for (int i = 0; i < props.size(); ++i) + if (props[i].name == name) { + props[i].comment = comment; + return; + } +} + + +void PIPropertyStorage::setPropertyFlags(const PIString & name, int flags) { + for (int i = 0; i < props.size(); ++i) + if (props[i].name == name) { + props[i].flags = flags; + return; + } +} diff --git a/src/core/pipropertystorage.h b/src/core/pipropertystorage.h new file mode 100644 index 00000000..53da16d5 --- /dev/null +++ b/src/core/pipropertystorage.h @@ -0,0 +1,70 @@ +#ifndef PIPROPERTYSTORAGE_H +#define PIPROPERTYSTORAGE_H + +#include "pivariant.h" + +class PIPropertyStorage { +public: + PIPropertyStorage() {} + + struct Property { + Property(const PIString & n = PIString(), const PIString & c = PIString(), const PIVariant & v = PIVariant(), int f = 0): + name(n), comment(c), value(v), flags(f) {} + PIString name; + PIString comment; + PIVariant value; + int flags; + }; + + PIPropertyStorage(const PIVector & pl) {props = pl;} + + typedef PIVector::const_iterator const_iterator; + typedef PIVector::iterator iterator; + + iterator begin() {return props.begin();} + const_iterator begin() const {return props.begin();} + iterator end() {return props.end();} + const_iterator end() const {return props.end();} + + int length() const {return props.length();} + int size() const {return props.size();} + bool isEmpty() const {return props.isEmpty();} + Property & front() {return props.front();} + const Property & front() const {return props.front();} + Property & back() {return props.back();} + const Property & back() const {return props.back();} + void removeAt(int i) {props.remove(i);} + void clear() {props.clear();} + + PIPropertyStorage copy() const {return PIPropertyStorage(*this);} + int propertiesCount() const {return props.size();} + PIVector & properties() {return props;} + const PIVector & properties() const {return props;} + const PIPropertyStorage & propertyStorage() const {return *this;} + bool isPropertyExists(const PIString & _name) const; + void clearProperties() {props.clear();} + void addProperty(const Property & p); + void addProperty(const PIString & _name, const PIVariant & _def_value, const PIString & _comment = PIString(), int _flags = 0) {addProperty(Property(_name, _comment, _def_value, _flags));} + void removeProperty(const PIString & _name); + void removePropertiesByFlag(int flag); + void updateProperties(const PIVector & properties_, int flag_ignore = 0); + Property propertyByName(const PIString & name) const; + PIVariant propertyValueByName(const PIString & name) const; + void setPropertyValue(const PIString & name, const PIVariant & value); + void setPropertyComment(const PIString & name, const PIString & comment); + void setPropertyFlags(const PIString & name, int flags); + + PIPropertyStorage & operator <<(const PIPropertyStorage::Property & p) {props << p; return *this;} + PIPropertyStorage & operator <<(const PIVector & p) {props << p; return *this;} + PIPropertyStorage & operator <<(const PIPropertyStorage & p) {props << p.props; return *this;} + Property & operator[](int i) {return props[i];} + const Property & operator[](int i) const {return props[i];} + + static Property parsePropertyLine(PIString l); + +protected: + PIVector props; + +}; + +#endif // PIPROPERTYSTORAGE_H