mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-09 05:28:09 +08:00
refs #876 Utility to examine which members of value objects compare equal.
This commit is contained in:
@@ -15,6 +15,10 @@
|
||||
#include "blackmisc/metaclass.h"
|
||||
#include "blackmisc/inheritancetraits.h"
|
||||
#include "blackmisc/typetraits.h"
|
||||
#include "blackmisc/range.h"
|
||||
#include <QList>
|
||||
#include <QPair>
|
||||
#include <QString>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
@@ -180,6 +184,60 @@ namespace BlackMisc
|
||||
};
|
||||
|
||||
} // Mixin
|
||||
|
||||
/*!
|
||||
* Helps with debugging Mixin::EqualsByMetaClass.
|
||||
*/
|
||||
class CMetaMemberComparator
|
||||
{
|
||||
public:
|
||||
//! Return list of pairs containing member name and bool indicating if member is equal.
|
||||
template <typename T, std::enable_if_t<THasMetaClass<T>::value, int> = 0>
|
||||
QList<QPair<QString, bool>> operator ()(const T &a, const T &b) const
|
||||
{
|
||||
auto meta = introspect<T>().without(MetaFlags<DisabledForComparison>());
|
||||
auto result = baseEquals(static_cast<const TBaseOfT<T> *>(&a), static_cast<const TBaseOfT<T> *>(&b));
|
||||
meta.forEachMember([ & ](auto member)
|
||||
{
|
||||
constexpr auto flags = decltype(member.m_flags)();
|
||||
result.append(qMakePair(member.latin1Name(), membersEqual(member.in(a), member.in(b), flags & MetaFlags<CaseInsensitiveComparison>())));
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
//! Recurse into one of the submembers of T, comparing members of that member.
|
||||
template <typename T, std::enable_if_t<THasMetaClass<T>::value, int> = 0>
|
||||
QList<QPair<QString, bool>> operator ()(const T &a, const T &b, QStringList memberNames) const
|
||||
{
|
||||
if (memberNames.isEmpty()) { return CMetaMemberComparator()(a, b); }
|
||||
auto meta = introspect<T>().without(MetaFlags<DisabledForComparison>());
|
||||
auto result = baseEquals(static_cast<const TBaseOfT<T> *>(&a), static_cast<const TBaseOfT<T> *>(&b), memberNames);
|
||||
const auto memberName = memberNames.takeFirst();
|
||||
meta.forEachMember([ & ](auto member)
|
||||
{
|
||||
const auto p = [ & ](const auto &m) { return qMakePair(memberName + '.' + m.first, m.second); };
|
||||
if (member.latin1Name() == memberName) { result.append(makeRange(CMetaMemberComparator()(member.in(a), member.in(b), memberNames)).transform(p)); }
|
||||
});
|
||||
return result;
|
||||
}
|
||||
|
||||
//! Return empty list if T doesn't have a metaclass.
|
||||
template <typename T, std::enable_if_t<!THasMetaClass<T>::value, int> = 0>
|
||||
QList<QPair<QString, bool>> operator ()(const T &, const T &) const { return {}; }
|
||||
|
||||
//! Return empty list if T doesn't have a metaclass.
|
||||
template <typename T, std::enable_if_t<!THasMetaClass<T>::value, int> = 0>
|
||||
QList<QPair<QString, bool>> operator ()(const T &, const T &, QStringList) const { return {}; }
|
||||
|
||||
private:
|
||||
template <typename T> static auto baseEquals(const T *a, const T *b) { return CMetaMemberComparator()(*a, *b); }
|
||||
template <typename T> static auto baseEquals(const T *a, const T *b, const QStringList &memberNames) { return CMetaMemberComparator()(*a, *b, memberNames); }
|
||||
static auto baseEquals(const void *, const void *, const QStringList & = {}) { return QList<QPair<QString, bool>>(); }
|
||||
static auto baseEquals(const CEmpty *, const CEmpty *, const QStringList & = {}) { return QList<QPair<QString, bool>>(); }
|
||||
|
||||
template <typename T> static bool membersEqual(const T &a, const T &b, std::true_type) { return a.compare(b, Qt::CaseInsensitive) == 0; }
|
||||
template <typename T> static bool membersEqual(const T &a, const T &b, std::false_type) { return a == b; }
|
||||
};
|
||||
} // BlackMisc
|
||||
|
||||
#endif
|
||||
|
||||
@@ -263,6 +263,12 @@ namespace BlackMisc
|
||||
{
|
||||
return getIntrospector<T>(Private::make_index_sequence<T::MetaClass::getMemberList().c_size>());
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
static std::true_type hasMetaClass(int, typename T::MetaClass * = nullptr) { return {}; }
|
||||
|
||||
template <typename T>
|
||||
static std::false_type hasMetaClass(...) { return {}; }
|
||||
};
|
||||
}
|
||||
|
||||
@@ -277,6 +283,13 @@ namespace BlackMisc
|
||||
return Private::CMetaClassAccessor::getIntrospector<T>();
|
||||
}
|
||||
|
||||
/*!
|
||||
* Trait that is true if T has a metaclass.
|
||||
* \ingroup MetaClass
|
||||
*/
|
||||
template <typename T>
|
||||
struct THasMetaClass : public decltype(Private::CMetaClassAccessor::hasMetaClass<T>(0)) {};
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user