refs #327, CStatusMessage based on CRTP (proof of concept)

This commit is contained in:
Klaus Basan
2014-09-11 17:50:14 +02:00
parent 6aba823f4f
commit c5eb1151d4
2 changed files with 26 additions and 144 deletions

View File

@@ -23,6 +23,23 @@ namespace BlackMisc
: m_type(type), m_severity(severity), m_message(message), m_timestamp(QDateTime::currentDateTimeUtc())
{ }
/*
* Equal?
*/
bool CStatusMessage::operator ==(const CStatusMessage &other) const
{
if (this == &other) return true;
return TupleConverter<CStatusMessage>::toTuple(*this) == TupleConverter<CStatusMessage>::toTuple(other);
}
/*
* Unequal?
*/
bool CStatusMessage::operator !=(const CStatusMessage &other) const
{
return !((*this) == other);
}
/*
* Constructor
*/
@@ -49,32 +66,6 @@ namespace BlackMisc
return s;
}
/*
* metaTypeId
*/
int CStatusMessage::getMetaTypeId() const
{
return qMetaTypeId<CStatusMessage>();
}
/*
* is a
*/
bool CStatusMessage::isA(int metaTypeId) const
{
if (metaTypeId == qMetaTypeId<CStatusMessage>()) { return true; }
return this->CValueObject::isA(metaTypeId);
}
/*
* Metadata
*/
void CStatusMessage::registerMetadata()
{
qRegisterMetaType<CStatusMessage>();
qDBusRegisterMetaType<CStatusMessage>();
}
/*
* Validation Error
*/
@@ -121,30 +112,6 @@ namespace BlackMisc
}
}
/*
* To JSON
*/
QJsonObject CStatusMessage::toJson() const
{
return BlackMisc::serializeJson(CStatusMessage::jsonMembers(), TupleConverter<CStatusMessage>::toTuple(*this));
}
/*
* From JSON
*/
void CStatusMessage::convertFromJson(const QJsonObject &json)
{
BlackMisc::deserializeJson(json, CStatusMessage::jsonMembers(), TupleConverter<CStatusMessage>::toTuple(*this));
}
/*
* Members
*/
const QStringList &CStatusMessage::jsonMembers()
{
return TupleConverter<CStatusMessage>::jsonMembers();
}
/*
* Type
*/
@@ -238,56 +205,6 @@ namespace BlackMisc
}
}
/*
* Compare
*/
int CStatusMessage::compareImpl(const CValueObject &otherBase) const
{
const auto &other = static_cast<const CStatusMessage &>(otherBase);
return compare(TupleConverter<CStatusMessage>::toTuple(*this), TupleConverter<CStatusMessage>::toTuple(other));
}
/*
* Marshall to DBus
*/
void CStatusMessage::marshallToDbus(QDBusArgument &argument) const
{
argument << TupleConverter<CStatusMessage>::toTuple(*this);
}
/*
* Unmarshall from DBus
*/
void CStatusMessage::unmarshallFromDbus(const QDBusArgument &argument)
{
argument >> TupleConverter<CStatusMessage>::toTuple(*this);
}
/*
* Hash
*/
uint CStatusMessage::getValueHash() const
{
return qHash(TupleConverter<CStatusMessage>::toTuple(*this));
}
/*
* Equal?
*/
bool CStatusMessage::operator ==(const CStatusMessage &other) const
{
if (this == &other) return true;
return TupleConverter<CStatusMessage>::toTuple(*this) == TupleConverter<CStatusMessage>::toTuple(other);
}
/*
* Unequal
*/
bool CStatusMessage::operator !=(const CStatusMessage &other) const
{
return !(other == (*this));
}
/*
* Property by index
*/

View File

@@ -22,7 +22,7 @@ namespace BlackMisc
/*!
* Streamable status message, e.g. from Core -> GUI
*/
class CStatusMessage : public CValueObject
class CStatusMessage : public CValueObjectStdTuple<CStatusMessage>
{
public:
//! Status types
@@ -69,6 +69,12 @@ namespace BlackMisc
//! Constructor
CStatusMessage(StatusType type, StatusSeverity severity, const QString &message);
//! Equal operator ==
bool operator ==(const CStatusMessage &other) const;
//! Unequal operator !=
bool operator !=(const CStatusMessage &other) const;
//! Status type
StatusType getType() const { return this->m_type; }
@@ -81,15 +87,6 @@ namespace BlackMisc
//! Message empty
bool isEmpty() const { return this->m_message.isEmpty(); }
//! \copydoc CValueObject::getValueHash()
virtual uint getValueHash() const override;
//! \copydoc CValueObject::toQVariant()
virtual QVariant toQVariant() const override { return QVariant::fromValue(*this); }
//! \copydoc CValueObject::convertFromQVariant
virtual void convertFromQVariant(const QVariant &variant) override { BlackMisc::setFromQVariant(this, variant); }
//! Type as string
const QString &getTypeAsString() const;
@@ -102,12 +99,6 @@ namespace BlackMisc
//! Type as string
const QString &getSeverityAsString() const;
//! \copydoc CValueObject::toJson
virtual QJsonObject toJson() const override;
//! \copydoc CValueObject::convertFromJson
virtual void convertFromJson(const QJsonObject &json) override;
//! \copydoc CValueObject::propertyByIndex(int)
virtual QVariant propertyByIndex(const BlackMisc::CPropertyIndex &index) const override;
@@ -117,18 +108,6 @@ namespace BlackMisc
//! To HTML
QString toHtml() const;
//! Equal operator ==
bool operator ==(const CStatusMessage &other) const;
//! Unequal operator !=
bool operator !=(const CStatusMessage &other) const;
//! Register metadata
static void registerMetadata();
//! JSON member names
static const QStringList &jsonMembers();
//! Validation error
static CStatusMessage getValidationError(const QString &message);
@@ -145,24 +124,9 @@ namespace BlackMisc
static const CIcon &convertToIcon(const CStatusMessage &statusMessage);
protected:
//! \copydoc CValueObject::marshallToDbus
virtual void marshallToDbus(QDBusArgument &arg) const override;
//! \copydoc CValueObject::unmarshallFromDbus
virtual void unmarshallFromDbus(const QDBusArgument &arg) override;
//! \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;
private:
BLACK_ENABLE_TUPLE_CONVERSION(CStatusMessage)
StatusType m_type;
@@ -171,7 +135,8 @@ namespace BlackMisc
QDateTime m_timestamp;
};
}
} // namespace
BLACK_DECLARE_TUPLE_CONVERSION(BlackMisc::CStatusMessage, (o.m_type, o.m_severity, o.m_message, o.m_timestamp))
Q_DECLARE_METATYPE(BlackMisc::CStatusMessage)