Issue #77 Break cyclic dependency between CVariant and CVariantList

This commit is contained in:
Mat Sutcliffe
2020-11-08 19:37:02 +00:00
parent 4df055afbe
commit 2435350e17
4 changed files with 17 additions and 16 deletions

View File

@@ -14,7 +14,6 @@
#include "blackmisc/logmessage.h"
#include "blackmisc/propertyindexref.h"
#include "blackmisc/statusmessage.h"
#include "blackmisc/variantlist.h"
#include "blackmisc/stringutils.h"
#include <QByteArray>
@@ -33,6 +32,9 @@
namespace BlackMisc
{
//! \private
int qMetaTypeId_CVariantList = -1; // referenced in variantlist.cpp
Private::IValueObjectMetaInfo *Private::getValueObjectMetaInfo(int typeId)
{
return getValueObjectMetaInfo(QVariant(typeId, nullptr));
@@ -49,11 +51,11 @@ namespace BlackMisc
{
return true;
}
if (typeId == qMetaTypeId<CVariantList>())
if (typeId == qMetaTypeId_CVariantList)
{
return m_v.canConvert<QVector<CVariant>>() || m_v.canConvert<QVariantList>();
}
if (userType() == qMetaTypeId<CVariantList>())
if (userType() == qMetaTypeId_CVariantList)
{
return QVariant::fromValue(QVector<CVariant>()).canConvert(typeId)
|| QVariant(typeId, nullptr).canConvert<QVariantList>();
@@ -66,7 +68,7 @@ namespace BlackMisc
if (!m_v.canConvert(typeId))
{
if (!canConvert(typeId)) { return false; }
if (typeId == qMetaTypeId<CVariantList>())
if (typeId == qMetaTypeId_CVariantList)
{
if (m_v.canConvert<QVector<CVariant>>())
{
@@ -74,11 +76,15 @@ namespace BlackMisc
}
else if (m_v.canConvert<QVariantList>())
{
m_v.setValue(CVariantList(m_v.value<QSequentialIterable>()));
QVector<CVariant> vec;
const auto seqit = m_v.value<QSequentialIterable>();
vec.reserve(seqit.size());
for (auto it = seqit.begin(); it != seqit.end(); ++it) { vec.push_back(*it); }
m_v.setValue(vec);
}
else { return false; }
}
if (userType() == qMetaTypeId<CVariantList>())
if (userType() == qMetaTypeId_CVariantList)
{
if (QVariant::fromValue(QVector<CVariant>()).canConvert(typeId))
{
@@ -92,7 +98,7 @@ namespace BlackMisc
bool CVariant::isVariantList() const
{
return userType() == qMetaTypeId<CVariantList>();
return userType() == qMetaTypeId_CVariantList;
}
QString CVariant::convertToQString(bool i18n) const

View File

@@ -39,7 +39,6 @@ namespace BlackMisc
{
class CIcon;
class CPropertyIndex;
class CVariantList;
/*!
* Wrapper around QVariant which provides transparent access to CValueObject methods

View File

@@ -12,6 +12,9 @@
namespace BlackMisc
{
//! \private
extern int qMetaTypeId_CVariantList; // defined in variant.cpp
CVariantList::CVariantList(const CSequence &other) : CSequence(other)
{}
@@ -25,16 +28,12 @@ namespace BlackMisc
std::move(other.begin(), other.end(), std::back_inserter(*this));
}
CVariantList::CVariantList(const QSequentialIterable &other)
{
for (auto it = other.begin(); it != other.end(); ++it) { push_back(*it); }
}
void CVariantList::registerMetadata()
{
Mixin::MetaType<CVariantList>::registerMetadata();
QMetaType::registerConverter<CVariantList, QVector<CVariant>>([](const CVariantList &list) { return list.toVector(); });
QMetaType::registerConverter<QVector<CVariant>, CVariantList>([](const QVector<CVariant> &list) { return CSequence(list); });
qMetaTypeId_CVariantList = qMetaTypeId<CVariantList>();
}
bool CVariantList::matches(const CVariant &event) const

View File

@@ -46,9 +46,6 @@ namespace BlackMisc
//! Construct from a moved QVariantList.
CVariantList(QVariantList &&other);
//! Construct from a QSequentialIterable.
CVariantList(const QSequentialIterable &other);
//! Convert to a sequence type by converting all elements.
template <typename T>
T to() const { return CVariant::from(*this).template to<T>(); }