mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-08-09 19:16:01 +08:00
refs #356 Update remaining CValueObject derived classes to use CValueObjectStdTuple instead.
This commit is contained in:
@@ -42,12 +42,29 @@ namespace BlackMisc
|
||||
static QString stringify(QString str, bool /*i18n*/) { return str; }
|
||||
};
|
||||
|
||||
// forward declaration
|
||||
template <template <class> class C, class T, class CIt>
|
||||
class CContainerBase;
|
||||
|
||||
//! \private
|
||||
template <template <class> class C, class T, class CIt>
|
||||
struct CValueObjectStdTuplePolicy<CContainerBase<C, T, CIt>> : public CValueObjectLegacy
|
||||
{};
|
||||
|
||||
/*!
|
||||
* \brief Base class for CCollection and CSequence adding mutating operations and CValueObject facility on top of CRangeBase.
|
||||
*/
|
||||
template <template <class> class C, class T, class CIt>
|
||||
class CContainerBase : public CValueObject, public CRangeBase<C<T>, CIt>
|
||||
class CContainerBase : public CValueObjectStdTuple<CContainerBase<C, T, CIt>>, public CRangeBase<C<T>, CIt>
|
||||
{
|
||||
//! \copydoc BlackMisc::CValueObject::compare
|
||||
friend int compare(const C<T> &a, const C<T> &b)
|
||||
{
|
||||
if (a.size() < b.size()) { return -1; }
|
||||
if (a.size() > b.size()) { return 1; }
|
||||
return std::lexicographical_compare(a.cbegin(), a.cend(), b.cbegin(), b.cend());
|
||||
}
|
||||
|
||||
public:
|
||||
/*!
|
||||
* \brief Return a new container of a different type, containing the same elements as this one.
|
||||
@@ -127,7 +144,7 @@ namespace BlackMisc
|
||||
}
|
||||
}
|
||||
|
||||
protected: // CValueObject overrides
|
||||
protected: // CValueObjectStdTuple overrides
|
||||
//! \copydoc BlackMisc::CValueObject::convertToQString
|
||||
virtual QString convertToQString(bool i18n = false) const override
|
||||
{
|
||||
@@ -140,27 +157,6 @@ namespace BlackMisc
|
||||
//! \copydoc BlackMisc::CValueObject::getMetaTypeId
|
||||
virtual int getMetaTypeId() const override { return qMetaTypeId<C<T>>(); }
|
||||
|
||||
//! \copydoc BlackMisc::CValueObject::isA
|
||||
virtual bool isA(int metaTypeId) const override
|
||||
{
|
||||
if (metaTypeId == qMetaTypeId<C<T>>()) { return true; }
|
||||
return CValueObject::isA(metaTypeId);
|
||||
}
|
||||
|
||||
//! \copydoc BlackMisc::CValueObject::compareImpl
|
||||
virtual int compareImpl(const CValueObject &other) const override
|
||||
{
|
||||
const auto &o = static_cast<const CContainerBase &>(other);
|
||||
if (derived().size() < o.derived().size()) { return -1; }
|
||||
if (derived().size() > o.derived().size()) { return 1; }
|
||||
//for (auto i1 = derived().cbegin(), i2 = o.derived().cbegin(); i1 != derived().cend() && i2 != o.derived().cend(); ++i1, ++i2)
|
||||
//{
|
||||
// if (*i1 < *i2) { return -1; }
|
||||
// if (*i1 > *i2) { return 1; }
|
||||
//}
|
||||
return 0;
|
||||
}
|
||||
|
||||
//! \copydoc BlackMisc::CValueObject::marshallToDbus
|
||||
virtual void marshallToDbus(QDBusArgument &argument) const override
|
||||
{
|
||||
|
||||
@@ -74,10 +74,26 @@ namespace BlackMisc
|
||||
struct AssociativityTraits : public Private::AssociativityTraits<Private::ADL::SupportsQHash<Key>::value, Private::ADL::SupportsQMap<Key>::value>
|
||||
{};
|
||||
|
||||
//! Associative container with value semantics, chooses a sensible default implementation container type
|
||||
// forward declaration
|
||||
template<class Key, class Value, template <class...> class Impl = AssociativityTraits<Key>::template DefaultType>
|
||||
class CDictionary : public CValueObject
|
||||
class CDictionary;
|
||||
|
||||
//! \private
|
||||
template <class Key, class Value, template <class...> class Impl>
|
||||
struct CValueObjectStdTuplePolicy<CDictionary<Key, Value, Impl>> : public CValueObjectLegacy
|
||||
{};
|
||||
|
||||
//! Associative container with value semantics, chooses a sensible default implementation container type
|
||||
template<class Key, class Value, template <class...> class Impl /*= AssociativityTraits<Key>::template DefaultType*/>
|
||||
class CDictionary : public CValueObjectStdTuple<CDictionary<Key, Value, Impl>>
|
||||
{
|
||||
//! \copydoc BlackMisc::CValueObject::compare
|
||||
friend int compare(const CDictionary &a, const CDictionary &b)
|
||||
{
|
||||
if (a.m_impl.size() < b.m_impl.size()) { return -1; }
|
||||
if (a.m_impl.size() > b.m_impl.size()) { return 1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
public:
|
||||
//! The implementation container
|
||||
@@ -408,22 +424,6 @@ namespace BlackMisc
|
||||
//! \copydoc BlackMisc::CValueObject::getMetaTypeId
|
||||
virtual int getMetaTypeId() const override { return qMetaTypeId<CDictionary>(); }
|
||||
|
||||
//! \copydoc BlackMisc::CValueObject::isA
|
||||
virtual bool isA(int metaTypeId) const override
|
||||
{
|
||||
if (metaTypeId == qMetaTypeId<CDictionary>()) { return true; }
|
||||
return CValueObject::isA(metaTypeId);
|
||||
}
|
||||
|
||||
//! \copydoc BlackMisc::CValueObject::compareImpl
|
||||
virtual int compareImpl(const CValueObject &other) const override
|
||||
{
|
||||
const auto &o = static_cast<const CDictionary &>(other);
|
||||
if (m_impl.size() < o.m_impl.size()) { return -1; }
|
||||
if (m_impl.size() > o.m_impl.size()) { return 1; }
|
||||
return 0;
|
||||
}
|
||||
|
||||
//! \copydoc BlackMisc::CValueObject::marshallToDbus
|
||||
virtual void marshallToDbus(QDBusArgument &argument) const override
|
||||
{
|
||||
|
||||
@@ -117,24 +117,6 @@ namespace BlackMisc
|
||||
return qMetaTypeId<CPropertyIndexVariantMap>();
|
||||
}
|
||||
|
||||
/*
|
||||
* is a
|
||||
*/
|
||||
bool CPropertyIndexVariantMap::isA(int metaTypeId) const
|
||||
{
|
||||
if (metaTypeId == qMetaTypeId<CPropertyIndexVariantMap>()) { return true; }
|
||||
return this->CValueObject::isA(metaTypeId);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compare
|
||||
*/
|
||||
int CPropertyIndexVariantMap::compareImpl(const CValueObject &/*otherBase*/) const
|
||||
{
|
||||
qFatal("not implemented");
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Marshall to DBus
|
||||
*/
|
||||
|
||||
@@ -27,12 +27,16 @@ namespace BlackMisc
|
||||
// forward declaration
|
||||
class CPropertyIndex;
|
||||
class CPropertyIndexList;
|
||||
class CPropertyIndexVariantMap;
|
||||
|
||||
//! \private
|
||||
template <> struct CValueObjectStdTuplePolicy<CPropertyIndexVariantMap> : public CValueObjectLegacy {};
|
||||
|
||||
/*!
|
||||
* Specialized value object compliant map for variants,
|
||||
* based on indexes
|
||||
*/
|
||||
class CPropertyIndexVariantMap : public CValueObject
|
||||
class CPropertyIndexVariantMap : public CValueObjectStdTuple<CPropertyIndexVariantMap>
|
||||
{
|
||||
|
||||
public:
|
||||
@@ -155,12 +159,6 @@ namespace BlackMisc
|
||||
//! \copydoc CValueObject::getMetaTypeId
|
||||
virtual int getMetaTypeId() const override;
|
||||
|
||||
//! \copydoc CValueObject::isA
|
||||
virtual bool isA(int metaTypeId) const override;
|
||||
|
||||
//! \copydoc CValueObject::compareImpl
|
||||
virtual int compareImpl(const CValueObject &other) const override;
|
||||
|
||||
//! \copydoc CValueObject::marshallToDbus
|
||||
virtual void marshallToDbus(QDBusArgument &argument) const override;
|
||||
|
||||
|
||||
@@ -168,6 +168,22 @@ namespace BlackMisc
|
||||
using PropertyIndex = Policy::PropertyIndex::Default; //!< PropertyIndex policy
|
||||
};
|
||||
|
||||
/*!
|
||||
* Policy classes for use by classes with incomplete migration to CValueObjectStdTuple.
|
||||
*
|
||||
* This is to make it easier to apply the necessary changes to these classes for #356.
|
||||
* \todo Remove this and finish migrating classes that use it.
|
||||
*/
|
||||
struct CValueObjectLegacy : public CValueObjectStdTuplePolicy<CEmpty>
|
||||
{
|
||||
using Equals = Policy::Equals::None; //!< Equals policy
|
||||
using LessThan = Policy::LessThan::None; //!< Less than policy
|
||||
using Compare = Policy::Compare::None; //!< Compare policy
|
||||
using Hash = Policy::Hash::Own; //!< Hash policy
|
||||
using DBus = Policy::DBus::Own; //!< DBus policy
|
||||
using Json = Policy::Json::Own; //!< JSon policy
|
||||
};
|
||||
|
||||
/*!
|
||||
* Standard implementation of CValueObject using meta tuple system.
|
||||
*
|
||||
|
||||
@@ -23,84 +23,6 @@ namespace BlackMisc
|
||||
return s;
|
||||
}
|
||||
|
||||
/*
|
||||
* metaTypeId
|
||||
*/
|
||||
int CTestValueObject::getMetaTypeId() const
|
||||
{
|
||||
return qMetaTypeId<CTestValueObject>();
|
||||
}
|
||||
|
||||
/*
|
||||
* is a
|
||||
*/
|
||||
bool CTestValueObject::isA(int metaTypeId) const
|
||||
{
|
||||
if (metaTypeId == qMetaTypeId<CTestValueObject>()) { return true; }
|
||||
|
||||
return this->CValueObject::isA(metaTypeId);
|
||||
}
|
||||
|
||||
/*
|
||||
* Compare
|
||||
*/
|
||||
int CTestValueObject::compareImpl(const CValueObject &otherBase) const
|
||||
{
|
||||
const auto &other = static_cast<const CTestValueObject &>(otherBase);
|
||||
|
||||
return compare(TupleConverter<CTestValueObject>::toTuple(*this), TupleConverter<CTestValueObject>::toTuple(other));
|
||||
}
|
||||
|
||||
/*
|
||||
* Marshall to DBus
|
||||
*/
|
||||
void CTestValueObject::marshallToDbus(QDBusArgument &argument) const
|
||||
{
|
||||
argument << TupleConverter<CTestValueObject>::toTuple(*this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Unmarshall from DBus
|
||||
*/
|
||||
void CTestValueObject::unmarshallFromDbus(const QDBusArgument &argument)
|
||||
{
|
||||
argument >> TupleConverter<CTestValueObject>::toTuple(*this);
|
||||
}
|
||||
|
||||
/*
|
||||
* Equal?
|
||||
*/
|
||||
bool CTestValueObject::operator ==(const CTestValueObject &other) const
|
||||
{
|
||||
if (this == &other) return true;
|
||||
return TupleConverter<CTestValueObject>::toTuple(*this) == TupleConverter<CTestValueObject>::toTuple(other);
|
||||
}
|
||||
|
||||
/*
|
||||
* Unequal?
|
||||
*/
|
||||
bool CTestValueObject::operator !=(const CTestValueObject &other) const
|
||||
{
|
||||
return !((*this) == other);
|
||||
}
|
||||
|
||||
/*
|
||||
* Less than?
|
||||
*/
|
||||
bool CTestValueObject::operator <(const CTestValueObject &other) const
|
||||
{
|
||||
if (this == &other) return false;
|
||||
return TupleConverter<CTestValueObject>::toTuple(*this) < TupleConverter<CTestValueObject>::toTuple(other);
|
||||
}
|
||||
|
||||
/*
|
||||
* Hash
|
||||
*/
|
||||
uint CTestValueObject::getValueHash() const
|
||||
{
|
||||
return qHash(TupleConverter<CTestValueObject>::toTuple(*this));
|
||||
}
|
||||
|
||||
/*
|
||||
* Property by index
|
||||
*/
|
||||
@@ -115,7 +37,7 @@ namespace BlackMisc
|
||||
case IndexName:
|
||||
return CVariant::fromValue(this->m_name);
|
||||
default:
|
||||
return CValueObject::propertyByIndex(index);
|
||||
return CValueObjectStdTuple::propertyByIndex(index);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,42 +61,9 @@ namespace BlackMisc
|
||||
this->setName(variant.value<QString>());
|
||||
break;
|
||||
default:
|
||||
CValueObject::setPropertyByIndex(variant, index);
|
||||
CValueObjectStdTuple::setPropertyByIndex(variant, index);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Register metadata
|
||||
*/
|
||||
void CTestValueObject::registerMetadata()
|
||||
{
|
||||
qRegisterMetaType<CTestValueObject>();
|
||||
qDBusRegisterMetaType<CTestValueObject>();
|
||||
}
|
||||
|
||||
/*
|
||||
* Members
|
||||
*/
|
||||
const QStringList &CTestValueObject::jsonMembers()
|
||||
{
|
||||
return TupleConverter<CTestValueObject>::jsonMembers();
|
||||
}
|
||||
|
||||
/*
|
||||
* To JSON
|
||||
*/
|
||||
QJsonObject CTestValueObject::toJson() const
|
||||
{
|
||||
return BlackMisc::serializeJson(CTestValueObject::jsonMembers(), TupleConverter<CTestValueObject>::toTuple(*this));
|
||||
}
|
||||
|
||||
/*
|
||||
* From Json
|
||||
*/
|
||||
void CTestValueObject::convertFromJson(const QJsonObject &json)
|
||||
{
|
||||
BlackMisc::deserializeJson(json, CTestValueObject::jsonMembers(), TupleConverter<CTestValueObject>::toTuple(*this));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace BlackMisc
|
||||
/*!
|
||||
* Value object encapsulating information of a server
|
||||
*/
|
||||
class CTestValueObject : public BlackMisc::CValueObject
|
||||
class CTestValueObject : public BlackMisc::CValueObjectStdTuple<CTestValueObject>
|
||||
{
|
||||
public:
|
||||
//! Properties by index
|
||||
@@ -51,71 +51,16 @@ namespace BlackMisc
|
||||
//! Set description
|
||||
void setDescription(const QString &description) { m_description = description; }
|
||||
|
||||
//! Equal operator ==
|
||||
bool operator ==(const CTestValueObject &other) const;
|
||||
|
||||
//! Unequal operator !=
|
||||
bool operator !=(const CTestValueObject &other) const;
|
||||
|
||||
//! Less than operator <
|
||||
bool operator <(const CTestValueObject &other) const;
|
||||
|
||||
//! \copydoc CValueObject::getValueHash()
|
||||
virtual uint getValueHash() const override;
|
||||
|
||||
//! \copydoc CValueObject::toJson
|
||||
virtual QJsonObject toJson() const override;
|
||||
|
||||
//! \copydoc CValueObject::fromJson
|
||||
void convertFromJson(const QJsonObject &json) override;
|
||||
|
||||
//! Register metadata
|
||||
static void registerMetadata();
|
||||
|
||||
//! \copydoc TupleConverter<>::jsonMembers()
|
||||
static const QStringList &jsonMembers();
|
||||
|
||||
//! \copydoc CValueObject::propertyByIndex
|
||||
virtual CVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const override;
|
||||
|
||||
//! \copydoc CValueObject::setPropertyByIndex
|
||||
void setPropertyByIndex(const CVariant &variant, const BlackMisc::CPropertyIndex &index) override;
|
||||
|
||||
//! \copydoc CValueObject::toQVariant
|
||||
virtual QVariant toQVariant() const override
|
||||
{
|
||||
return QVariant::fromValue(*this);
|
||||
}
|
||||
|
||||
//! \copydoc CValueObject::fromQVariant
|
||||
virtual void convertFromQVariant(const QVariant &variant) override
|
||||
{
|
||||
Q_ASSERT(variant.canConvert<CTestValueObject>());
|
||||
if (variant.canConvert<CTestValueObject>())
|
||||
{
|
||||
(*this) = variant.value<CTestValueObject>();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
//! \copydoc CValueObject::convertToQString()
|
||||
virtual QString convertToQString(bool i18n = false) const override;
|
||||
|
||||
//! \copydoc CValueObject::getMetaTypeId
|
||||
virtual int getMetaTypeId() const override;
|
||||
|
||||
//! \copydoc CValueObject::isA
|
||||
virtual bool isA(int metaTypeId) const override;
|
||||
|
||||
//! \copydoc CValueObject::compareImpl
|
||||
virtual int compareImpl(const CValueObject &other) const override;
|
||||
|
||||
//! \copydoc CValueObject::marshallToDbus
|
||||
virtual void marshallToDbus(QDBusArgument &argument) const override;
|
||||
|
||||
//! \copydoc CValueObject::unmarshallFromDbus
|
||||
virtual void unmarshallFromDbus(const QDBusArgument &argument) override;
|
||||
|
||||
private:
|
||||
BLACK_ENABLE_TUPLE_CONVERSION(CTestValueObject)
|
||||
QString m_name;
|
||||
|
||||
Reference in New Issue
Block a user