Removed "string trick" in CPropertyIndex

Summary:
Ref T187
Using QVector as this is Qt's recommended container type.

Reviewers: #swift_developers, kbasan

Reviewed By: #swift_developers, kbasan

Subscribers: jenkins

Tags: #swift_pilot_client

Maniphest Tasks: T187

Differential Revision: https://dev.swift-project.org/D62
This commit is contained in:
Mathew Sutcliffe
2017-11-23 22:15:37 +00:00
parent fcb6cf1a52
commit 3969245a55
2 changed files with 72 additions and 82 deletions

View File

@@ -8,140 +8,121 @@
*/
#include "blackmisc/propertyindex.h"
#include "blackmisc/verify.h"
#include <QStringList>
#include <QtGlobal>
namespace BlackMisc
{
CPropertyIndex::CPropertyIndex(int singleProperty)
CPropertyIndex::CPropertyIndex(int singleProperty) : m_indexes { singleProperty }
{
Q_ASSERT(singleProperty >= static_cast<int>(GlobalIndexCValueObject));
this->setIndexStringByList({singleProperty});
}
CPropertyIndex::CPropertyIndex(std::initializer_list<int> il)
{
this->setIndexStringByList(il);
}
CPropertyIndex::CPropertyIndex(std::initializer_list<int> il) : m_indexes(il)
{}
CPropertyIndex::CPropertyIndex(const QList<int> &indexes)
{
this->setIndexStringByList(indexes);
}
CPropertyIndex::CPropertyIndex(const QVector<int> &indexes) : m_indexes(indexes)
{}
CPropertyIndex::CPropertyIndex(const QString &indexes) : m_indexString(indexes)
CPropertyIndex::CPropertyIndex(const QList<int> &indexes) : m_indexes(indexes.toVector())
{}
CPropertyIndex::CPropertyIndex(const QString &indexes)
{
this->parseFromString(indexes);
}
CPropertyIndex CPropertyIndex::copyFrontRemoved() const
{
Q_ASSERT_X(!this->isEmpty(), Q_FUNC_INFO, "Empty index");
BLACK_VERIFY_X(!this->isEmpty(), Q_FUNC_INFO, "Empty index");
if (this->isEmpty()) { return CPropertyIndex(); }
const int p = this->m_indexString.indexOf(';');
if (p < 0) { return CPropertyIndex(); }
return CPropertyIndex(this->m_indexString.mid(p + 1));
CPropertyIndex copy = *this;
copy.m_indexes.pop_front();
return copy;
}
bool CPropertyIndex::isNested() const
{
return this->m_indexString.contains(';');
return m_indexes.size() > 1;
}
bool CPropertyIndex::isMyself() const
{
return this->m_indexString.isEmpty();
return m_indexes.isEmpty();
}
bool CPropertyIndex::isEmpty() const
{
return this->m_indexString.isEmpty();
return m_indexes.isEmpty();
}
QString CPropertyIndex::convertToQString(bool i18n) const
{
Q_UNUSED(i18n);
return this->m_indexString;
QString s;
for (const int i : m_indexes)
{
Q_ASSERT(i >= static_cast<int>(GlobalIndexCValueObject));
if (!s.isEmpty()) { s.append(";"); }
s.append(QString::number(i));
}
return s;
}
void CPropertyIndex::parseFromString(const QString &indexes)
{
if (indexes.isEmpty())
{
this->m_indexString.clear();
}
else
{
QString is(indexes.simplified().replace(" ", ";").replace(",", ";"));
if (is.endsWith(';'))
{
this->m_indexString = is.left(is.length() - 1);
}
else
{
this->m_indexString = is;
}
}
}
void CPropertyIndex::setIndexStringByList(const QList<int> &list)
{
QString l;
for (const int &i : list)
{
Q_ASSERT(i >= static_cast<int>(GlobalIndexCValueObject));
if (!l.isEmpty()) { l.append(";"); }
l.append(QString::number(i));
}
this->m_indexString = l;
}
QList<int> CPropertyIndex::indexList() const
{
QList<int> list;
if (this->m_indexString.isEmpty()) { return list; }
QStringList indexes = this->m_indexString.split(';');
for (const QString &index : indexes)
m_indexes.clear();
if (indexes.isEmpty()) { return; }
for (const QString &index : indexes.split(';'))
{
if (index.isEmpty()) { continue; }
bool ok;
int i = index.toInt(&ok);
Q_ASSERT(ok);
Q_ASSERT(i >= static_cast<int>(GlobalIndexCValueObject));
list.append(i);
m_indexes.append(i);
}
return list;
}
QJsonObject CPropertyIndex::toJson() const
{
QJsonObject json;
json.insert(QStringLiteral("indexes"), this->convertToQString());
return json;
}
void CPropertyIndex::convertFromJson(const QJsonObject &json)
{
const QJsonValue value = json.value(QLatin1String("indexes"));
if (!value.isString()) { throw CJsonException("'indexes' missing or not a string"); }
this->parseFromString(value.toString());
}
QVector<int> CPropertyIndex::indexVector() const
{
return m_indexes;
}
QList<int> CPropertyIndex::indexList() const
{
return m_indexes.toList();
}
void CPropertyIndex::prepend(int newLeftIndex)
{
QList<int> newList = indexList();
newList.prepend(newLeftIndex);
this->setIndexStringByList(newList);
m_indexes.push_front(newLeftIndex);
}
bool CPropertyIndex::contains(int index) const
{
return this->indexList().contains(index);
return m_indexes.contains(index);
}
int CPropertyIndex::frontToInt() const
{
Q_ASSERT_X(!this->isEmpty(), Q_FUNC_INFO, "No index");
int f = -1;
bool ok;
const int p = this->m_indexString.indexOf(';');
if (p < 0)
{
f = this->m_indexString.toInt(&ok);
}
else
{
f = this->m_indexString.leftRef(p).toInt(&ok);
}
Q_ASSERT_X(ok && f >= 0, Q_FUNC_INFO, "Invalid index");
return f;
return m_indexes.front();
}
bool CPropertyIndex::startsWith(int index) const

View File

@@ -63,7 +63,7 @@ namespace BlackMisc
public Mixin::MetaType<CPropertyIndex>,
public Mixin::HashByMetaClass<CPropertyIndex>,
public Mixin::DBusByMetaClass<CPropertyIndex>,
public Mixin::JsonByMetaClass<CPropertyIndex>,
public Mixin::JsonOperators<CPropertyIndex>,
public Mixin::EqualsByMetaClass<CPropertyIndex>,
public Mixin::LessThanByMetaClass<CPropertyIndex>,
public Mixin::CompareByMetaClass<CPropertyIndex>,
@@ -170,7 +170,10 @@ namespace BlackMisc
//! Initializer list constructor
CPropertyIndex(std::initializer_list<int> il);
//! Construct from a base class object.
//! Construct from a vector of indexes.
CPropertyIndex(const QVector<int> &indexes);
//! Construct from a list of indexes.
CPropertyIndex(const QList<int> &indexes);
//! From string
@@ -188,6 +191,9 @@ namespace BlackMisc
//! Empty?
bool isEmpty() const;
//! Index vector
QVector<int> indexVector() const;
//! Index list
QList<int> indexList() const;
@@ -237,19 +243,22 @@ namespace BlackMisc
//! \copydoc BlackMisc::Mixin::String::toQString
QString convertToQString(bool i18n = false) const;
//! \copydoc BlackMisc::CValueObject::toJson
QJsonObject toJson() const;
//! \copydoc BlackMisc::CValueObject::convertFromJson
void convertFromJson(const QJsonObject &json);
protected:
//! Parse indexes from string
void parseFromString(const QString &indexes);
private:
QString m_indexString; //! I use a little trick here, the string is used with the tupel system, as it provides all operators, hash ..
//! Convert list to string
void setIndexStringByList(const QList<int> &list);
QVector<int> m_indexes;
BLACK_METACLASS(
CPropertyIndex,
BLACK_METAMEMBER(indexString)
BLACK_METAMEMBER(indexes)
);
};
} //namespace