Files
pilotclient/src/blackmisc/avinformationmessage.cpp
Mathew Sutcliffe db4c05dd9f refs #84 removed the CValueObject::compare method and added a friend function BlackMisc::compare to replace it.
The new compare is implemented using "multimethods" described in the book Advanced C++ Programming Styles and Idioms by James Coplien.

First, the isA method is used to determine which of the values being compared is the most general. (For example, CLength is more general than CAltitude.)
Then the compareImpl method is called on the most general value, with the other value as an argument.
If there is not a direct inheritance relation between the two values (or they are the same class) then the comparison is invalid and a assert is triggered.
2014-01-17 01:38:27 +00:00

137 lines
3.6 KiB
C++

#include "avinformationmessage.h"
#include "blackmiscfreefunctions.h"
namespace BlackMisc
{
namespace Aviation
{
/*
* Convert to string
*/
QString CInformationMessage::convertToQString(bool /** i18n **/) const
{
return this->m_message;
}
/*
* metaTypeId
*/
int CInformationMessage::getMetaTypeId() const
{
return qMetaTypeId<CInformationMessage>();
}
/*
* is a
*/
bool CInformationMessage::isA(int metaTypeId) const
{
if (metaTypeId == qMetaTypeId<CInformationMessage>()) { return true; }
return this->CValueObject::isA(metaTypeId);
}
/*
* Compare
*/
int CInformationMessage::compareImpl(const CValueObject &otherBase) const
{
const auto &other = static_cast<const CInformationMessage &>(otherBase);
if (this->m_type < other.m_type) { return -1; }
if (this->m_type > other.m_type) { return 1; }
return this->m_message.compare(other.m_message);
}
/*
* Marshall to DBus
*/
void CInformationMessage::marshallToDbus(QDBusArgument &argument) const
{
argument << this->m_message;
argument << static_cast<int>(this->m_type);
argument << this->m_receivedTimestamp;
}
/*
* Unmarshall from DBus
*/
void CInformationMessage::unmarshallFromDbus(const QDBusArgument &argument)
{
uint type;
argument >> this->m_message;
argument >> type;
argument >> this->m_receivedTimestamp;
this->m_type = static_cast<InformationType>(type);
}
/*
* Equal?
*/
bool CInformationMessage::operator ==(const CInformationMessage &other) const
{
if (this == &other) return true;
return this->m_message == other.m_message &&
this->m_receivedTimestamp == other.m_receivedTimestamp &&
this->m_type == other.m_type;
}
/*
* Unequal?
*/
bool CInformationMessage::operator !=(const CInformationMessage &other) const
{
return !((*this) == other);
}
/*
* Register metadata
*/
void CInformationMessage::registerMetadata()
{
qRegisterMetaType<CInformationMessage>();
qDBusRegisterMetaType<CInformationMessage>();
}
/*
* Hash
*/
uint CInformationMessage::getValueHash() const
{
QList<uint> hashs;
hashs << qHash(this->m_message);
return BlackMisc::calculateHash(hashs, "CAtis");
}
/*
* Type as string
*/
const QString &CInformationMessage::getTypeAsString() const
{
switch (this->m_type)
{
case ATIS:
{
static const QString atis("ATIS");
return atis;
}
case METAR:
{
static const QString metar("METAR");
return metar;
}
case TAF:
{
static const QString taf("TAF");
return taf;
}
default:
{
static const QString ds("unknown");
return ds;
}
}
}
} // namespace
} // namespace