T73 Pass units by value.

Class types which are trivial to copy should be passed by value.
This commit is contained in:
Mathew Sutcliffe
2017-04-21 20:02:09 +01:00
parent c8d78ada65
commit 07ec7e6bc1
3 changed files with 17 additions and 20 deletions

View File

@@ -341,10 +341,9 @@ namespace BlackMisc
template <class U> static U unitFromSymbol(const QString &symbol, bool strict = true)
{
if (symbol.isEmpty()) return U::defaultUnit();
const QList<U> &units = U::allUnits();
for (int i = 0; i < units.size(); ++i)
for (const auto unit : U::allUnits())
{
if (units.at(i).getSymbol() == symbol) return units.at(i);
if (unit.getSymbol() == symbol) { return unit; }
}
if (strict) qFatal("Illegal unit name");
return U::defaultUnit();
@@ -358,11 +357,9 @@ namespace BlackMisc
template <class U> static bool isValidUnitSymbol(const QString &symbol, Qt::CaseSensitivity caseSensitivity = Qt::CaseSensitive)
{
if (symbol.isEmpty()) return false;
const QList<U> &units = U::allUnits();
for (int i = 0; i < units.size(); ++i)
for (const auto unit : U::allUnits())
{
if (caseSensitivity == Qt::CaseSensitive && units.at(i).getSymbol() == symbol) return true;
if (units.at(i).getSymbol().compare(symbol, Qt::CaseInsensitive) == 0) return 0;
if (QString::compare(unit.getSymbol(), symbol, caseSensitivity) == 0) { return true; }
}
return false;
}

View File

@@ -57,7 +57,7 @@ namespace BlackMisc
QString CPhysicalQuantity<MU, PQ>::getUnitSymbol() const { return this->m_unit.getSymbol(true); }
template <class MU, class PQ>
CPhysicalQuantity<MU, PQ>::CPhysicalQuantity(double value, const MU &unit) :
CPhysicalQuantity<MU, PQ>::CPhysicalQuantity(double value, MU unit) :
m_value(unit.isNull() ? 0.0 : value), m_unit(unit)
{ }
@@ -240,7 +240,7 @@ namespace BlackMisc
}
template <class MU, class PQ>
PQ &CPhysicalQuantity<MU, PQ>::switchUnit(const MU &newUnit)
PQ &CPhysicalQuantity<MU, PQ>::switchUnit(MU newUnit)
{
if (this->m_unit != newUnit)
{
@@ -286,7 +286,7 @@ namespace BlackMisc
}
template <class MU, class PQ>
QString CPhysicalQuantity<MU, PQ>::valueRoundedWithUnit(const MU &unit, int digits, bool i18n) const
QString CPhysicalQuantity<MU, PQ>::valueRoundedWithUnit(MU unit, int digits, bool i18n) const
{
Q_ASSERT_X(!unit.isNull(), Q_FUNC_INFO, "Cannot convert to null");
if (this->isNull()) { return this->convertToQString(i18n); }
@@ -301,14 +301,14 @@ namespace BlackMisc
}
template <class MU, class PQ>
double CPhysicalQuantity<MU, PQ>::valueRounded(const MU &unit, int digits) const
double CPhysicalQuantity<MU, PQ>::valueRounded(MU unit, int digits) const
{
Q_ASSERT_X(!unit.isNull(), Q_FUNC_INFO, "Cannot convert to null");
return unit.roundValue(this->value(unit), digits);
}
template <class MU, class PQ>
int CPhysicalQuantity<MU, PQ>::valueInteger(const MU &unit) const
int CPhysicalQuantity<MU, PQ>::valueInteger(MU unit) const
{
Q_ASSERT_X(!unit.isNull(), Q_FUNC_INFO, "Cannot convert to null");
double v = unit.roundValue(this->value(unit), 0);
@@ -322,7 +322,7 @@ namespace BlackMisc
}
template <class MU, class PQ>
double CPhysicalQuantity<MU, PQ>::value(const MU &unit) const
double CPhysicalQuantity<MU, PQ>::value(MU unit) const
{
Q_ASSERT_X(!unit.isNull(), Q_FUNC_INFO, "Cannot convert to null");
return unit.convertFrom(this->m_value, this->m_unit);

View File

@@ -74,7 +74,7 @@ namespace BlackMisc
//! Simply set unit, do no calclulate conversion
//! \sa switchUnit
void setUnit(const MU &unit) { this->m_unit = unit; }
void setUnit(MU unit) { this->m_unit = unit; }
//! Set unit by string
void setUnitBySymbol(const QString &unitName);
@@ -83,7 +83,7 @@ namespace BlackMisc
QString getUnitSymbol() const;
//! Change unit, and convert value to maintain the same quantity
PQ &switchUnit(const MU &newUnit);
PQ &switchUnit(MU newUnit);
//! Is quantity null?
bool isNull() const;
@@ -92,7 +92,7 @@ namespace BlackMisc
void setNull();
//! Value in given unit
double value(const MU &unit) const;
double value(MU unit) const;
//! Value in current unit
double value() const;
@@ -101,16 +101,16 @@ namespace BlackMisc
void setCurrentUnitValue(double value);
//! Rounded value in given unit
double valueRounded(const MU &unit, int digits = -1) const;
double valueRounded(MU unit, int digits = -1) const;
//! As integer value
int valueInteger(const MU &unit) const;
int valueInteger(MU unit) const;
//! Rounded value in current unit
double valueRounded(int digits = -1) const;
//! Value to QString with the given unit, e.g. "5.00m"
QString valueRoundedWithUnit(const MU &unit, int digits = -1, bool i18n = false) const;
QString valueRoundedWithUnit(MU unit, int digits = -1, bool i18n = false) const;
//! Value to QString with the current unit, e.g. "5.00m"
QString valueRoundedWithUnit(int digits = -1, bool i18n = false) const;
@@ -222,7 +222,7 @@ namespace BlackMisc
protected:
//! Constructor with double
CPhysicalQuantity(double value, const MU &unit);
CPhysicalQuantity(double value, MU unit);
//! Constructor by parsed string, e.g. 10m
CPhysicalQuantity(const QString &unitString);