mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-01 21:56:43 +08:00
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.
117 lines
3.3 KiB
C++
117 lines
3.3 KiB
C++
/* Copyright (C) 2013 VATSIM Community / contributors
|
|
* 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 "blackmisc/pqbase.h"
|
|
#include "blackmisc/mathematics.h"
|
|
|
|
using namespace BlackMisc::Math;
|
|
|
|
namespace BlackMisc
|
|
{
|
|
namespace PhysicalQuantities
|
|
{
|
|
|
|
// -----------------------------------------------------------------------
|
|
// --- Measurement unit --------------------------------------------------
|
|
// -----------------------------------------------------------------------
|
|
|
|
/*
|
|
* Equal operator
|
|
*/
|
|
bool CMeasurementUnit::operator ==(const CMeasurementUnit &other) const
|
|
{
|
|
if (this == &other) return true;
|
|
return this->m_name == other.m_name;
|
|
}
|
|
|
|
/*
|
|
* Unequal operator
|
|
*/
|
|
bool CMeasurementUnit::operator !=(const CMeasurementUnit &other) const
|
|
{
|
|
return !(other == *this);
|
|
}
|
|
|
|
/*
|
|
* Conversion
|
|
*/
|
|
double CMeasurementUnit::convertFrom(double value, const CMeasurementUnit &unit) const
|
|
{
|
|
Q_ASSERT(this->m_converter);
|
|
Q_ASSERT(unit.m_converter);
|
|
if (this->m_converter == unit.m_converter) return value;
|
|
return this->m_converter->fromDefault(unit.m_converter->toDefault(value));
|
|
}
|
|
|
|
/*
|
|
* Value to QString with unit, e.g. "5.00m"
|
|
* @return
|
|
*/
|
|
QString CMeasurementUnit::makeRoundedQStringWithUnit(double value, int digits, bool i18n) const
|
|
{
|
|
return this->makeRoundedQString(value, digits).append(this->getSymbol(i18n));
|
|
}
|
|
|
|
/*
|
|
* Value rounded
|
|
*/
|
|
double CMeasurementUnit::roundValue(double value, int digits) const
|
|
{
|
|
if (digits < 0) digits = this->m_displayDigits;
|
|
return CMath::round(value, digits);
|
|
}
|
|
|
|
/*
|
|
* Rounded to QString
|
|
*/
|
|
QString CMeasurementUnit::makeRoundedQString(double value, int digits, bool /* i18n */) const
|
|
{
|
|
if (digits < 0) digits = this->m_displayDigits;
|
|
double v = CMath::round(value, digits);
|
|
QString s = QLocale::system().toString(v, 'f', digits);
|
|
return s;
|
|
}
|
|
|
|
/*
|
|
* metaTypeId
|
|
*/
|
|
int CMeasurementUnit::getMetaTypeId() const
|
|
{
|
|
return qMetaTypeId<CMeasurementUnit>();
|
|
}
|
|
|
|
/*
|
|
* is a
|
|
*/
|
|
bool CMeasurementUnit::isA(int metaTypeId) const
|
|
{
|
|
if (metaTypeId == qMetaTypeId<CMeasurementUnit>()) { return true; }
|
|
|
|
return this->CValueObject::isA(metaTypeId);
|
|
}
|
|
|
|
/*
|
|
* Compare
|
|
*/
|
|
int CMeasurementUnit::compareImpl(const CValueObject &/*otherBase*/) const
|
|
{
|
|
qFatal("not implemented");
|
|
return 0;
|
|
}
|
|
|
|
/*!
|
|
* \brief Register metadata of unit and quantity
|
|
*/
|
|
void CMeasurementUnit::registerMetadata()
|
|
{
|
|
qRegisterMetaType<CMeasurementUnit>();
|
|
qDBusRegisterMetaType<CMeasurementUnit>();
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
} // namespace
|