Files
pilotclient/src/blackmisc/propertyindexvariantmap.cpp
Mathew Sutcliffe d9faa031d2 refs #413 Decomposed metatype-related functions of CValueObject into a dedicated class Mixin::MetaType,
bypassing the old Policy::MetaType classes. The ultimate intention is to decompose all the aspects of
CValueObject into separate mixins in this manner, and have derived classes inherit from the mixins directly.
Splitting the responsibilities of CValueObject into separate classes will reduce the coupling between different
cross-cutting concerns and allow us to untangle the web of interdependencies and greatly simplify things.
2015-05-12 20:36:03 +01:00

188 lines
5.1 KiB
C++

/* Copyright (C) 2013
* swift project Community / Contributors
*
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
* directory of this distribution and at http://www.swift-project.org/license.html. No part of swift project,
* including this file, may be copied, modified, propagated, or distributed except according to the terms
* contained in the LICENSE file.
*/
#include "propertyindexvariantmap.h"
#include "propertyindexlist.h"
#include "blackmiscfreefunctions.h"
namespace BlackMisc
{
/*
* Constructor
*/
CPropertyIndexVariantMap::CPropertyIndexVariantMap(bool wildcard) : m_wildcard(wildcard) {}
/*
* Constructor single value
*/
CPropertyIndexVariantMap::CPropertyIndexVariantMap(const CPropertyIndex &index, const CVariant &value)
: m_wildcard(false)
{
this->addValue(index, value);
}
/*
* ==
*/
bool operator ==(const CPropertyIndexVariantMap &a, const CPropertyIndexVariantMap &b)
{
return a.m_wildcard == b.m_wildcard && a.m_values == b.m_values;
}
/*
* !=
*/
bool operator !=(const CPropertyIndexVariantMap &a, const CPropertyIndexVariantMap &b)
{
return !(b == a);
}
/*
* Compare with CVariant
*/
bool operator==(const CPropertyIndexVariantMap &indexMap, const CVariant &variant)
{
if (indexMap.isEmpty()) return indexMap.isWildcard();
const auto &map = indexMap.map();
for (auto it = map.begin(); it != map.end(); ++it)
{
// QVariant cannot be compared directly
CVariant p = variant.propertyByIndex(it.key()); // from value object
CVariant v = it.value().toCVariant(); // from map
if (p != v) return false;
}
return true;
}
/*
* Compare with CVariant
*/
bool operator!=(const CPropertyIndexVariantMap &indexMap, const CVariant &variant)
{
return !(indexMap == variant);
}
/*
* Compare with CVariant
*/
bool operator==(const CVariant &variant, const CPropertyIndexVariantMap &valueMap)
{
return valueMap == variant;
}
/*
* Compare with CVariant
*/
bool operator!=(const CVariant &variant, const CPropertyIndexVariantMap &valueMap)
{
return !(valueMap == variant);
}
/*
* Convert to string
*/
QString CPropertyIndexVariantMap::convertToQString(bool i18n) const
{
if (this->isEmpty()) return QString("{wildcard: %1}").arg(this->m_wildcard ? "true" : "false");
QString s;
foreach(CPropertyIndex index, this->m_values.keys())
{
CVariant v = this->m_values.value(index);
s.isEmpty() ?
s.append("{wildcard: ").append(this->m_wildcard ? "true" : "false").append(" ") :
s.append(", ");
s.append('{').append(index.toQString(i18n)).append(": ");
s.append("(").append(QString::number(v.userType())).append(") ");
QString vs = v.toQString(i18n);
s.append(vs);
s.append('}');
}
s = s.append("}");
return s;
}
/*
* Marshall to DBus
*/
void CPropertyIndexVariantMap::marshallToDbus(QDBusArgument &argument) const
{
argument << this->m_values.keys();
argument << this->m_values.values();
}
/*
* Unmarshall from DBus
*/
void CPropertyIndexVariantMap::unmarshallFromDbus(const QDBusArgument &argument)
{
QList<CPropertyIndex> indexes;
QList<CVariant> values;
argument >> indexes;
argument >> values;
Q_ASSERT(indexes.size() == values.size());
QMap<CPropertyIndex, CVariant> newMap;
for (int i = 0; i < indexes.size(); i++)
{
newMap.insert(indexes[i], values[i]);
}
// replace values in one step
this->m_values.swap(newMap);
}
/*
* Add value
*/
void CPropertyIndexVariantMap::addValue(const CPropertyIndex &index, const CVariant &value)
{
this->m_values.insert(index, value);
}
/*
* Add string by literal
*/
void CPropertyIndexVariantMap::addValue(const CPropertyIndex &index, const char *str)
{
this->addValue(index, QString(str));
}
void CPropertyIndexVariantMap::prependIndex(int index)
{
QMap<CPropertyIndex, CVariant> newMap;
for (const CPropertyIndex &pi : this->indexes())
{
CPropertyIndex newPi(pi);
newPi.prepend(index);
newMap.insert(newPi, this->m_values[pi]);
}
this->m_values = newMap;
}
/*
* Indexes
*/
CPropertyIndexList CPropertyIndexVariantMap::indexes() const
{
return CPropertyIndexList::fromImpl(this->m_values.keys());
}
/*
* Hash
*/
uint CPropertyIndexVariantMap::getValueHash() const
{
// there is no hash for map, so I use this workaround here
const QString s = this->toQString(false);
QList<uint> h;
h << qHash(s);
return BlackMisc::calculateHash(h, "CIndexVariantMap");
}
} // namespace