refs #434 CPropertyIndexVariantMap: removed operator templates and replaced CVariant comparison operators with a named method, matches().

This commit is contained in:
Mathew Sutcliffe
2015-06-01 21:06:43 +01:00
parent e76015004b
commit ada03ee513
5 changed files with 35 additions and 52 deletions

View File

@@ -70,7 +70,7 @@ namespace BlackMiscTest
// now Jane's time is over // now Jane's time is over
CPropertyIndexVariantMap anotherController; CPropertyIndexVariantMap anotherController;
anotherController.addValue(CAtcStation::IndexController, CVariant::fromValue(CUser("445566", "Fuzzy"))); anotherController.addValue(CAtcStation::IndexController, CVariant::fromValue(CUser("445566", "Fuzzy")));
atcList.applyIf(BlackMisc::Predicates::Equals(newController), anotherController); atcList.applyIf(BlackMisc::Predicates::Matches(newController), anotherController);
qDebug() << "-- after update via value map"; qDebug() << "-- after update via value map";
qDebug() << atcList.toQString(); qDebug() << atcList.toQString();

View File

@@ -17,6 +17,8 @@
namespace BlackMisc namespace BlackMisc
{ {
class CPropertyIndexVariantMap;
namespace Predicates namespace Predicates
{ {
@@ -98,6 +100,14 @@ namespace BlackMisc
template <class U> bool operator ()(const U &other) const { return other == m_value; } template <class U> bool operator ()(const U &other) const { return other == m_value; }
}; };
//! \private
struct Matches
{
const CPropertyIndexVariantMap &m_map;
Matches(const CPropertyIndexVariantMap &map) : m_map(map) {}
template <class T> bool operator()(const T &value) const;
};
} //namespace Private } //namespace Private
/*! /*!
@@ -173,6 +183,14 @@ namespace BlackMisc
return { std::forward<T>(value), 0 }; return { std::forward<T>(value), 0 };
} }
/*!
* Predicate which is true if its argument matches a captured CPropertyIndexVariantMap.
*/
inline auto Matches(const CPropertyIndexVariantMap &map) -> Private::Matches
{
return { map };
}
} //namespace Predicates } //namespace Predicates
} //namespace BlackMisc } //namespace BlackMisc

View File

@@ -31,10 +31,10 @@ namespace BlackMisc
return !(b == a); return !(b == a);
} }
bool operator==(const CPropertyIndexVariantMap &indexMap, const CVariant &variant) bool CPropertyIndexVariantMap::matchesVariant(const CVariant &variant) const
{ {
if (indexMap.isEmpty()) return indexMap.isWildcard(); if (this->isEmpty()) return this->isWildcard();
const auto &map = indexMap.map(); const auto &map = this->map();
for (auto it = map.begin(); it != map.end(); ++it) for (auto it = map.begin(); it != map.end(); ++it)
{ {
// QVariant cannot be compared directly // QVariant cannot be compared directly
@@ -45,21 +45,6 @@ namespace BlackMisc
return true; return true;
} }
bool operator!=(const CPropertyIndexVariantMap &indexMap, const CVariant &variant)
{
return !(indexMap == variant);
}
bool operator==(const CVariant &variant, const CPropertyIndexVariantMap &valueMap)
{
return valueMap == variant;
}
bool operator!=(const CVariant &variant, const CPropertyIndexVariantMap &valueMap)
{
return !(valueMap == variant);
}
QString CPropertyIndexVariantMap::convertToQString(bool i18n) const QString CPropertyIndexVariantMap::convertToQString(bool i18n) const
{ {
if (this->isEmpty()) return QString("{wildcard: %1}").arg(this->m_wildcard ? "true" : "false"); if (this->isEmpty()) return QString("{wildcard: %1}").arg(this->m_wildcard ? "true" : "false");

View File

@@ -178,37 +178,11 @@ namespace BlackMisc
//! Equal operator, required if maps are directly compared, not with CValueObject //! Equal operator, required if maps are directly compared, not with CValueObject
BLACKMISC_EXPORT friend bool operator !=(const CPropertyIndexVariantMap &a, const CPropertyIndexVariantMap &b); BLACKMISC_EXPORT friend bool operator !=(const CPropertyIndexVariantMap &a, const CPropertyIndexVariantMap &b);
//! Operator == with CVariant //! True if this map matches the value contained in the variant
BLACKMISC_EXPORT friend bool operator ==(const CPropertyIndexVariantMap &valueMap, const CVariant &variant); bool matchesVariant(const CVariant &value) const;
//! Operator != with CVariant //! True if this map matches the value
BLACKMISC_EXPORT friend bool operator !=(const CPropertyIndexVariantMap &valueMap, const CVariant &variant); template <typename T> bool matches(const T &value) const { return matchesVariant(CVariant::from(value)); }
//! Operator == with CVariant
BLACKMISC_EXPORT friend bool operator ==(const CVariant &variant, const CPropertyIndexVariantMap &valueMap);
//! Operator != with CVariant
BLACKMISC_EXPORT friend bool operator !=(const CVariant &variant, const CPropertyIndexVariantMap &valueMap);
//! Operator == with CValueObject
//! \todo Still needed?
template <class T, class = typename std::enable_if<QMetaTypeId<T>::Defined>::type>
friend bool operator ==(const CPropertyIndexVariantMap &valueMap, const T &valueObject) { return valueMap == CVariant::from(valueObject); }
//! Operator != with CValueObject
//! \todo Still needed?
template <class T, class = typename std::enable_if<QMetaTypeId<T>::Defined>::type>
friend bool operator !=(const CPropertyIndexVariantMap &valueMap, const T &valueObject) { return valueMap != CVariant::from(valueObject); }
//! Operator == with CValueObject
//! \todo Still needed?
template <class T, class = typename std::enable_if<QMetaTypeId<T>::Defined>::type>
friend bool operator ==(const T &valueObject, const CPropertyIndexVariantMap &valueMap) { return valueMap == CVariant::from(valueObject); }
//! Operator != with CValueObject
//! \todo Still needed?
template <class T, class = typename std::enable_if<QMetaTypeId<T>::Defined>::type>
friend bool operator !=(const T &valueObject, const CPropertyIndexVariantMap &valueMap) { return valueMap != CVariant::from(valueObject); }
//! Map //! Map
const QMap<CPropertyIndex, CVariant> &map() const { return this->m_values; } const QMap<CPropertyIndex, CVariant> &map() const { return this->m_values; }
@@ -302,6 +276,12 @@ namespace BlackMisc
} }
} // Mixin } // Mixin
template <class T>
bool Predicates::Private::Matches::operator()(const T &value) const
{
return m_map.matches(value);
}
} }
Q_DECLARE_METATYPE(BlackMisc::CPropertyIndexVariantMap) Q_DECLARE_METATYPE(BlackMisc::CPropertyIndexVariantMap)

View File

@@ -100,9 +100,9 @@ namespace BlackMiscTest
// compare // compare
QVERIFY2(vmWildcard == station1, "Station should be equal to wildcard"); QVERIFY2(vmWildcard.matches(station1), "Station should be equal to wildcard");
QVERIFY2(station1 != vmNoWildcard, "Station should not be equal to empty list"); QVERIFY2(!vmNoWildcard.matches(station1), "Station should not be equal to empty list");
QVERIFY2(station1 == vm, "Controller should match"); QVERIFY2(vm.matches(station1), "Controller should match");
QVERIFY2(vmWildcard == vmCopy, "Maps should be equal"); QVERIFY2(vmWildcard == vmCopy, "Maps should be equal");
QVERIFY2(qHash(vmWildcard) == qHash(vmCopy), "Hashs should be equal (simple)"); QVERIFY2(qHash(vmWildcard) == qHash(vmCopy), "Hashs should be equal (simple)");