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

View File

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

View File

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