From e0d054d6bcf75d7248665ad8cb2a416b96fbf335 Mon Sep 17 00:00:00 2001 From: wmayer Date: Mon, 27 Feb 2023 20:06:01 +0100 Subject: [PATCH] Base: handle several Lint warnings --- src/Base/Quantity.cpp | 47 ++++++++++++++++++++++++++++--------------- src/Base/Quantity.h | 2 +- src/Base/Unit.h | 4 +++- 3 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/Base/Quantity.cpp b/src/Base/Quantity.cpp index 0517a990e3..1f79f59e27 100644 --- a/src/Base/Quantity.cpp +++ b/src/Base/Quantity.cpp @@ -49,6 +49,7 @@ using namespace Base; // ====== Static attributes ========================= +// NOLINTNEXTLINE int QuantityFormat::defaultDenominator = 8; // for 1/8" @@ -71,22 +72,24 @@ QuantityFormat::QuantityFormat(QuantityFormat::NumberFormat format, int decimals // ---------------------------------------------------------------------------- Quantity::Quantity() + : _Value{0.0} { - this->_Value = 0.0; } Quantity::Quantity(const Quantity& that) + : _Value{that._Value} + , _Unit{that._Unit} { - *this = that ; } Quantity::Quantity(double value, const Unit& unit) + : _Value{value} + , _Unit{unit} { - this->_Unit = unit; - this->_Value = value; } Quantity::Quantity(double value, const QString& unit) + : _Value{0.0} { if (unit.isEmpty()) { this->_Value = value; @@ -105,9 +108,9 @@ Quantity::Quantity(double value, const QString& unit) } } -double Quantity::getValueAs(const Quantity &q)const +double Quantity::getValueAs(const Quantity& other)const { - return _Value/q.getValue(); + return _Value / other.getValue(); } bool Quantity::operator ==(const Quantity& that) const @@ -122,32 +125,36 @@ bool Quantity::operator !=(const Quantity& that) const bool Quantity::operator <(const Quantity& that) const { - if (this->_Unit != that._Unit) + if (this->_Unit != that._Unit) { throw Base::UnitsMismatchError("Quantity::operator <(): quantities need to have same unit to compare"); + } return (this->_Value < that._Value) ; } bool Quantity::operator >(const Quantity& that) const { - if (this->_Unit != that._Unit) + if (this->_Unit != that._Unit) { throw Base::UnitsMismatchError("Quantity::operator >(): quantities need to have same unit to compare"); + } return (this->_Value > that._Value) ; } bool Quantity::operator <=(const Quantity& that) const { - if (this->_Unit != that._Unit) + if (this->_Unit != that._Unit) { throw Base::UnitsMismatchError("Quantity::operator <=(): quantities need to have same unit to compare"); + } return (this->_Value <= that._Value) ; } bool Quantity::operator >=(const Quantity& that) const { - if (this->_Unit != that._Unit) + if (this->_Unit != that._Unit) { throw Base::UnitsMismatchError("Quantity::operator >=(): quantities need to have same unit to compare"); + } return (this->_Value >= that._Value) ; } @@ -174,8 +181,10 @@ Quantity Quantity::operator /(double p) const Quantity Quantity::pow(const Quantity &p) const { - if (!p._Unit.isEmpty()) + if (!p._Unit.isEmpty()) { throw Base::UnitsMismatchError("Quantity::pow(): exponent must not have a unit"); + } + return Quantity( std::pow(this->_Value, p._Value), this->_Unit.pow(static_cast(p._Value)) @@ -192,15 +201,18 @@ Quantity Quantity::pow(double p) const Quantity Quantity::operator +(const Quantity &p) const { - if (this->_Unit != p._Unit) + if (this->_Unit != p._Unit) { throw Base::UnitsMismatchError("Quantity::operator +(): Unit mismatch in plus operation"); + } + return Quantity(this->_Value + p._Value,this->_Unit); } Quantity& Quantity::operator +=(const Quantity &p) { - if (this->_Unit != p._Unit) + if (this->_Unit != p._Unit) { throw Base::UnitsMismatchError("Quantity::operator +=(): Unit mismatch in plus operation"); + } _Value += p._Value; @@ -209,15 +221,18 @@ Quantity& Quantity::operator +=(const Quantity &p) Quantity Quantity::operator -(const Quantity &p) const { - if (this->_Unit != p._Unit) + if (this->_Unit != p._Unit) { throw Base::UnitsMismatchError("Quantity::operator -(): Unit mismatch in minus operation"); + } + return Quantity(this->_Value - p._Value,this->_Unit); } Quantity& Quantity::operator -=(const Quantity &p) { - if (this->_Unit != p._Unit) + if (this->_Unit != p._Unit) { throw Base::UnitsMismatchError("Quantity::operator -=(): Unit mismatch in minus operation"); + } _Value -= p._Value; @@ -226,7 +241,7 @@ Quantity& Quantity::operator -=(const Quantity &p) Quantity Quantity::operator -() const { - return Quantity(-(this->_Value),this->_Unit); + return Quantity(-(this->_Value), this->_Unit); } Quantity& Quantity::operator = (const Quantity &New) diff --git a/src/Base/Quantity.h b/src/Base/Quantity.h index 30ee1818d2..94f457fcb3 100644 --- a/src/Base/Quantity.h +++ b/src/Base/Quantity.h @@ -75,7 +75,7 @@ struct BaseExport QuantityFormat { denominator = denom; } QuantityFormat(); - QuantityFormat(NumberFormat format, int decimals=-1); + explicit QuantityFormat(NumberFormat format, int decimals=-1); inline char toFormat() const { switch (format) { case Fixed: diff --git a/src/Base/Unit.h b/src/Base/Unit.h index bc2e31d128..368abc786c 100644 --- a/src/Base/Unit.h +++ b/src/Base/Unit.h @@ -59,7 +59,9 @@ class BaseExport Unit { public: /// default constructor - Unit(int8_t Length,int8_t Mass=0,int8_t Time=0,int8_t ElectricCurrent=0,int8_t ThermodynamicTemperature=0,int8_t AmountOfSubstance=0,int8_t LuminousIntensity=0,int8_t Angle=0); + explicit Unit(int8_t Length,int8_t Mass=0,int8_t Time=0,int8_t ElectricCurrent=0, + int8_t ThermodynamicTemperature=0, int8_t AmountOfSubstance=0, + int8_t LuminousIntensity=0, int8_t Angle=0); Unit(); Unit(const Unit&); explicit Unit(const QString& expr);