Files
pilotclient/src/blackmisc/avaltitude.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

104 lines
2.7 KiB
C++

/* Copyright (C) 2013 VATSIM Community
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "avaltitude.h"
using BlackMisc::PhysicalQuantities::CLength;
using BlackMisc::PhysicalQuantities::CLengthUnit;
namespace BlackMisc
{
namespace Aviation
{
/*
* Own implementation for streaming
*/
QString CAltitude::convertToQString(bool /* i18n */) const
{
QString s = this->CLength::convertToQString();
return s.append(this->isMeanSeaLevel() ? " MSL" : " AGL");
}
/*
* Marshall to DBus
*/
void CAltitude::marshallToDbus(QDBusArgument &argument) const
{
this->CLength::marshallToDbus(argument);
argument << qint32(this->m_datum);
}
/*
* Unmarshall from DBus
*/
void CAltitude::unmarshallFromDbus(const QDBusArgument &argument)
{
this->CLength::unmarshallFromDbus(argument);
qint32 datum;
argument >> datum;
this->m_datum = static_cast<ReferenceDatum>(datum);
}
/*
* Equal?
*/
bool CAltitude::operator ==(const CAltitude &other) const
{
return other.m_datum == this->m_datum && this->CLength::operator ==(other);
}
/*
* Unequal?
*/
bool CAltitude::operator !=(const CAltitude &other) const
{
return !((*this) == other);
}
/*
* metaTypeId
*/
int CAltitude::getMetaTypeId() const
{
return qMetaTypeId<CAltitude>();
}
/*
* is a
*/
bool CAltitude::isA(int metaTypeId) const
{
if (metaTypeId == qMetaTypeId<CAltitude>()) { return true; }
return this->CLength::isA(metaTypeId);
}
/*
* Compare
*/
int CAltitude::compareImpl(const CValueObject &otherBase) const
{
const auto &other = static_cast<const CAltitude &>(otherBase);
if (this->isMeanSeaLevel() && other.isAboveGroundLevel()) { return 1; }
if (this->isAboveGroundLevel() && other.isMeanSeaLevel()) { return -1; }
if (*this < other) { return -1; }
if (*this > other) { return 1; }
return 0;
}
/*
* Register metadata
*/
void CAltitude::registerMetadata()
{
qRegisterMetaType<CAltitude>();
qDBusRegisterMetaType<CAltitude>();
}
} // namespace
} // namespace