Files
pilotclient/src/blackmisc/pqbase.cpp
Mathew Sutcliffe e12d7fbd7a Had a bit of spare time so tried out the policy design pattern I've been learning about, applying it to PQ converters.
Some interesting results:
* Minor simplification of PQ converters and unit ctors; may not seem like much, but I think it enables possible future simplifications of a similar kind.
* CMeasurementPrefix is gone, no longer needed. Now prefixes are applied at the template stage.
* Apart from that, PQs public API is unchanged.
* Discovered that ft/s^2 conversion factor was wrong: fixed.
2013-09-15 22:01:11 +01:00

79 lines
2.0 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;
}
} // namespace
} // namespace