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/propertyindex.h"
#include "blackmisc/verify.h"
#include <QStringList> #include <QStringList>
#include <QtGlobal> #include <QtGlobal>
namespace BlackMisc namespace BlackMisc
{ {
CPropertyIndex::CPropertyIndex(int singleProperty) CPropertyIndex::CPropertyIndex(int singleProperty) : m_indexes { singleProperty }
{ {
Q_ASSERT(singleProperty >= static_cast<int>(GlobalIndexCValueObject)); Q_ASSERT(singleProperty >= static_cast<int>(GlobalIndexCValueObject));
this->setIndexStringByList({singleProperty});
} }
CPropertyIndex::CPropertyIndex(std::initializer_list<int> il) CPropertyIndex::CPropertyIndex(std::initializer_list<int> il) : m_indexes(il)
{ {}
this->setIndexStringByList(il);
}
CPropertyIndex::CPropertyIndex(const QList<int> &indexes) CPropertyIndex::CPropertyIndex(const QVector<int> &indexes) : m_indexes(indexes)
{ {}
this->setIndexStringByList(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); this->parseFromString(indexes);
} }
CPropertyIndex CPropertyIndex::copyFrontRemoved() const 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(); } if (this->isEmpty()) { return CPropertyIndex(); }
const int p = this->m_indexString.indexOf(';'); CPropertyIndex copy = *this;
if (p < 0) { return CPropertyIndex(); } copy.m_indexes.pop_front();
return CPropertyIndex(this->m_indexString.mid(p + 1)); return copy;
} }
bool CPropertyIndex::isNested() const bool CPropertyIndex::isNested() const
{ {
return this->m_indexString.contains(';'); return m_indexes.size() > 1;
} }
bool CPropertyIndex::isMyself() const bool CPropertyIndex::isMyself() const
{ {
return this->m_indexString.isEmpty(); return m_indexes.isEmpty();
} }
bool CPropertyIndex::isEmpty() const bool CPropertyIndex::isEmpty() const
{ {
return this->m_indexString.isEmpty(); return m_indexes.isEmpty();
} }
QString CPropertyIndex::convertToQString(bool i18n) const QString CPropertyIndex::convertToQString(bool i18n) const
{ {
Q_UNUSED(i18n); 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) void CPropertyIndex::parseFromString(const QString &indexes)
{ {
if (indexes.isEmpty()) m_indexes.clear();
{ if (indexes.isEmpty()) { return; }
this->m_indexString.clear(); for (const QString &index : indexes.split(';'))
}
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)
{ {
if (index.isEmpty()) { continue; } if (index.isEmpty()) { continue; }
bool ok; bool ok;
int i = index.toInt(&ok); int i = index.toInt(&ok);
Q_ASSERT(ok); Q_ASSERT(ok);
Q_ASSERT(i >= static_cast<int>(GlobalIndexCValueObject)); 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) void CPropertyIndex::prepend(int newLeftIndex)
{ {
QList<int> newList = indexList(); m_indexes.push_front(newLeftIndex);
newList.prepend(newLeftIndex);
this->setIndexStringByList(newList);
} }
bool CPropertyIndex::contains(int index) const bool CPropertyIndex::contains(int index) const
{ {
return this->indexList().contains(index); return m_indexes.contains(index);
} }
int CPropertyIndex::frontToInt() const int CPropertyIndex::frontToInt() const
{ {
Q_ASSERT_X(!this->isEmpty(), Q_FUNC_INFO, "No index"); Q_ASSERT_X(!this->isEmpty(), Q_FUNC_INFO, "No index");
int f = -1; return m_indexes.front();
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;
} }
bool CPropertyIndex::startsWith(int index) const bool CPropertyIndex::startsWith(int index) const

View File

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