202 lines
6.1 KiB
C++
202 lines
6.1 KiB
C++
/*
|
||
PIP - Platform Independent Primitives
|
||
Storage of properties for GUI usage
|
||
Ivan Pelipenko peri4ko@yandex.ru, Andrey Bychkov work.a.b@yandex.ru
|
||
|
||
This program is free software: you can redistribute it and/or modify
|
||
it under the terms of the GNU Lesser General Public License as published by
|
||
the Free Software Foundation, either version 3 of the License, or
|
||
(at your option) any later version.
|
||
|
||
This program is distributed in the hope that it will be useful,
|
||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
GNU Lesser General Public License for more details.
|
||
|
||
You should have received a copy of the GNU Lesser General Public License
|
||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||
*/
|
||
|
||
#include "pipropertystorage.h"
|
||
|
||
|
||
//! \~\class PIPropertyStorage pipropertystorage.h
|
||
//! \~\details
|
||
//! \~english \section PIPropertyStorage_sec0 Synopsis
|
||
//! \~russian \section PIPropertyStorage_sec0 Краткий обзор
|
||
//!
|
||
//! \~english
|
||
//! Key-value storage, based on PIVector with PIPropertyStorage::Property elements. Each element in vector
|
||
//! contains unique name. You can access property by name with \a propertyValueByName() or \a propertyByName().
|
||
//! You can add or replace property by \a addProperty(const Property&) or \a addProperty(const PIString&, const PIVariant&, const PIString&, int).
|
||
//!
|
||
//! \~russian
|
||
//! Хранилище свойств ключ-значние, основанный на PIVector с элементами PIPropertyStorage::Property.
|
||
//! Каждый элемент имеет уникальное имя. Доступ к свойствам через \a propertyValueByName() или \a propertyByName().
|
||
//! Добавление и перезапись свойств через \a addProperty(const Property&) или \a addProperty(const PIString&, const PIVariant&, const PIString&, int).
|
||
//!
|
||
//! \~english Example:
|
||
//! \~russian Пример:
|
||
//! \~\code{.cpp}
|
||
//! \endcode
|
||
//!
|
||
|
||
|
||
//! \~\class PIPropertyStorage::Property pipropertystorage.h
|
||
//! \~\details
|
||
//!
|
||
|
||
|
||
bool PIPropertyStorage::isPropertyExists(const PIString & _name) const {
|
||
for (uint i = 0; i < props.size(); ++i)
|
||
if (props[i].name == _name)
|
||
return true;
|
||
return false;
|
||
}
|
||
|
||
|
||
//! \details
|
||
//! \~english Returns "true" if new property added, else if update existing property return "false"
|
||
//! \~russian Возвращает истину если новое свойство добавлено, в случае обновления "ложь"
|
||
bool PIPropertyStorage::addProperty(const PIPropertyStorage::Property & p) {
|
||
for (uint i = 0; i < props.size(); ++i)
|
||
if (props[i].name == p.name) {
|
||
props[i] = p;
|
||
return false;
|
||
}
|
||
props << p;
|
||
return true;
|
||
}
|
||
|
||
|
||
bool PIPropertyStorage::addProperty(PIPropertyStorage::Property && p) {
|
||
for (uint i = 0; i < props.size(); ++i)
|
||
if (props[i].name == p.name) {
|
||
props[i] = std::move(p);
|
||
return false;
|
||
}
|
||
props << std::move(p);
|
||
return true;
|
||
}
|
||
|
||
|
||
//! \details
|
||
//! \~english Returns "true" if new property added, else if update existing property return "false"
|
||
//! \~russian Возвращает истину если новое свойство добавлено, в случае обновления "ложь"
|
||
bool PIPropertyStorage::addProperty(const PIString & _name, const PIVariant & _def_value, const PIString & _comment, int _flags) {
|
||
return addProperty(Property(_name, _comment, _def_value, _flags));
|
||
}
|
||
|
||
|
||
bool PIPropertyStorage::removeProperty(const PIString & _name) {
|
||
for (uint i = 0; i < props.size(); ++i) {
|
||
if (props[i].name == _name) {
|
||
props.remove(i);
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
//! \details
|
||
//! \~english "flag" checked as bitfield
|
||
//! \~russian "flag" проверяется как битовое поле
|
||
int PIPropertyStorage::removePropertiesByFlag(int flag) {
|
||
int ret = 0;
|
||
for (int i = 0; i < props.size_s(); ++i) {
|
||
if ((props[i].flags & flag) == flag) {
|
||
props.remove(i);
|
||
--i;
|
||
ret++;
|
||
}
|
||
}
|
||
return ret;
|
||
}
|
||
|
||
|
||
//! \details
|
||
//! \~english "flag_ignore" is bitfield to ignore property in merge process
|
||
//! \~russian "flag_ignore" - битовое поле для исключения свойств из процесса слияния
|
||
void PIPropertyStorage::updateProperties(const PIVector<PIPropertyStorage::Property> & properties_, int flag_ignore) {
|
||
PIMap<PIString, PIVariant> values;
|
||
piForeachC(Property & p, props)
|
||
if (((p.flags & flag_ignore) != flag_ignore) || (flag_ignore == 0))
|
||
values[p.name] = p.value;
|
||
props = properties_;
|
||
for (uint 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();
|
||
}
|
||
|
||
|
||
bool PIPropertyStorage::setPropertyValue(const PIString & name, const PIVariant & value) {
|
||
for (uint i = 0; i < props.size(); ++i) {
|
||
if (props[i].name == name) {
|
||
props[i].value = value;
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
bool PIPropertyStorage::setPropertyComment(const PIString & name, const PIString & comment) {
|
||
for (uint i = 0; i < props.size(); ++i) {
|
||
if (props[i].name == name) {
|
||
props[i].comment = comment;
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
bool PIPropertyStorage::setPropertyFlags(const PIString & name, int flags) {
|
||
for (uint i = 0; i < props.size(); ++i) {
|
||
if (props[i].name == name) {
|
||
props[i].flags = flags;
|
||
return true;
|
||
}
|
||
}
|
||
return false;
|
||
}
|
||
|
||
|
||
PIPropertyStorage::Property & PIPropertyStorage::operator[](const PIString & name) {
|
||
piForeach (Property & p, props)
|
||
if (p.name == name)
|
||
return p;
|
||
addProperty(name, "");
|
||
return props.back();
|
||
}
|
||
|
||
|
||
const PIPropertyStorage::Property PIPropertyStorage::operator[](const PIString & name) const {
|
||
piForeachC (Property & p, props)
|
||
if (p.name == name)
|
||
return p;
|
||
return Property();
|
||
}
|