mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-16 18:35:35 +08:00
refs #327, CRTP for CValueObject classes
* using toMetaTuple, no jsonMembers function * no un/equal operator as discussed
This commit is contained in:
@@ -54,6 +54,7 @@
|
|||||||
template <> class TupleConverter<T> : TupleConverterBase \
|
template <> class TupleConverter<T> : TupleConverterBase \
|
||||||
{ \
|
{ \
|
||||||
friend class T; \
|
friend class T; \
|
||||||
|
friend class BlackMisc::CValueObjectStdTuple<T>; \
|
||||||
static_assert(Private::HasEnabledTupleConversion<T>::value, \
|
static_assert(Private::HasEnabledTupleConversion<T>::value, \
|
||||||
"Missing BLACK_ENABLE_TUPLE_CONVERSION macro in " #T); \
|
"Missing BLACK_ENABLE_TUPLE_CONVERSION macro in " #T); \
|
||||||
static auto toTuple(const T &o) -> decltype(BlackMisc::tie MEMBERS) \
|
static auto toTuple(const T &o) -> decltype(BlackMisc::tie MEMBERS) \
|
||||||
|
|||||||
@@ -16,6 +16,7 @@
|
|||||||
#include "tuple.h"
|
#include "tuple.h"
|
||||||
#include "json.h"
|
#include "json.h"
|
||||||
#include "variant.h"
|
#include "variant.h"
|
||||||
|
#include "blackmiscfreefunctions.h"
|
||||||
#include <QtDBus/QDBusMetaType>
|
#include <QtDBus/QDBusMetaType>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QtGlobal>
|
#include <QtGlobal>
|
||||||
@@ -270,6 +271,87 @@ namespace BlackMisc
|
|||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//! Standard tuple enabled value object
|
||||||
|
//! CRTP based approach to avoid repetitive code
|
||||||
|
template <class VO> class CValueObjectStdTuple : public BlackMisc::CValueObject
|
||||||
|
{
|
||||||
|
|
||||||
|
public:
|
||||||
|
|
||||||
|
//! Default constructor.
|
||||||
|
CValueObjectStdTuple() : CValueObject() {}
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::getValueHash()
|
||||||
|
virtual uint getValueHash() const override
|
||||||
|
{
|
||||||
|
return qHash(TupleConverter<VO>::toMetaTuple(*derived()));
|
||||||
|
}
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::toJson
|
||||||
|
virtual QJsonObject toJson() const override
|
||||||
|
{
|
||||||
|
return BlackMisc::serializeJson(TupleConverter<VO>::toMetaTuple(*derived()));
|
||||||
|
}
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::convertFromJson
|
||||||
|
virtual void convertFromJson(const QJsonObject &json) override
|
||||||
|
{
|
||||||
|
BlackMisc::deserializeJson(json, TupleConverter<VO>::toMetaTuple(*derived()));
|
||||||
|
}
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::toQVariant()
|
||||||
|
virtual QVariant toQVariant() const override { return QVariant::fromValue(*derived()); }
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::convertFromQVariant
|
||||||
|
virtual void convertFromQVariant(const QVariant &variant) override { BlackMisc::setFromQVariant(derived(), variant); }
|
||||||
|
|
||||||
|
//! Register metadata
|
||||||
|
static void registerMetadata()
|
||||||
|
{
|
||||||
|
qRegisterMetaType<VO>();
|
||||||
|
qDBusRegisterMetaType<VO>();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected:
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::getMetaTypeId
|
||||||
|
virtual int getMetaTypeId() const override { return qMetaTypeId<VO>(); }
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::isA
|
||||||
|
virtual bool isA(int metaTypeId) const override
|
||||||
|
{
|
||||||
|
if (metaTypeId == qMetaTypeId<VO>()) { return true; }
|
||||||
|
return this->CValueObject::isA(metaTypeId);
|
||||||
|
}
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::compareImpl(CValueObject &otherBase)
|
||||||
|
virtual int compareImpl(const CValueObject &otherBase) const override
|
||||||
|
{
|
||||||
|
const VO &other = static_cast<const VO &>(otherBase);
|
||||||
|
return compare(TupleConverter<VO>::toMetaTuple(*derived()), TupleConverter<VO>::toMetaTuple(other));
|
||||||
|
}
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::marshallToDbus()
|
||||||
|
virtual void marshallToDbus(QDBusArgument &argument) const override
|
||||||
|
{
|
||||||
|
argument << TupleConverter<VO>::toMetaTuple(*derived());
|
||||||
|
}
|
||||||
|
|
||||||
|
//! \copydoc CValueObject::unmarshallFromDbus()
|
||||||
|
virtual void unmarshallFromDbus(const QDBusArgument &argument) override
|
||||||
|
{
|
||||||
|
argument >> TupleConverter<VO>::toMetaTuple(*derived());
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
//! Easy access to derived class (CRTP template parameter)
|
||||||
|
VO const *derived() const { return static_cast<VO const *>(this); }
|
||||||
|
|
||||||
|
//! Easy access to derived class (CRTP template parameter)
|
||||||
|
VO *derived() { return static_cast<VO *>(this); }
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
* Non-member non-friend operator for streaming T objects to QDBusArgument.
|
* Non-member non-friend operator for streaming T objects to QDBusArgument.
|
||||||
* Needed because we can't rely on the friend operator in some cases due to
|
* Needed because we can't rely on the friend operator in some cases due to
|
||||||
|
|||||||
Reference in New Issue
Block a user