remove integer quantities

This commit is contained in:
Mathew Sutcliffe
2013-08-14 03:29:03 +01:00
parent 586be27502
commit baa0ac147e
12 changed files with 10 additions and 165 deletions

View File

@@ -23,13 +23,6 @@ public:
*/
CAcceleration() : CPhysicalQuantity(0, CAccelerationUnit::m_s2(), CAccelerationUnit::m_s2()) {}
/*!
* \brief Init by int value
* \param value
* \param unit
*/
CAcceleration(qint32 value, const CAccelerationUnit &unit) : CPhysicalQuantity(value, unit, CAccelerationUnit::m_s2()) {}
/*!
* \brief Init by double value
* \param value

View File

@@ -29,13 +29,6 @@ public:
*/
CAngle(const CPhysicalQuantity &base) : CPhysicalQuantity(base) {}
/*!
* \brief Init by int value
* \param value
* \param unit
*/
CAngle(qint32 value, const CAngleUnit &unit): CPhysicalQuantity(value, unit, CAngleUnit::rad()) {}
/*!
* \brief Init by double value
* \param value

View File

@@ -29,13 +29,6 @@ public:
*/
CFrequency(const CPhysicalQuantity &base) : CPhysicalQuantity(base) {}
/*!
* \brief Init by int value
* \param value
* \param unit
*/
CFrequency(qint32 value, const CFrequencyUnit &unit) : CPhysicalQuantity(value, unit, CFrequencyUnit::Hz()) {}
/*!
* \brief Init by double value
* \param value

View File

@@ -29,13 +29,6 @@ public:
*/
CLength(const CPhysicalQuantity &base) : CPhysicalQuantity(base) {}
/*!
* \brief Init by int value
* \param value
* \param unit
*/
CLength(qint32 value, const CLengthUnit &unit) : CPhysicalQuantity(value, unit, CLengthUnit::m()) {}
/*!
*\brief Init by double value
* \param value

View File

@@ -23,13 +23,6 @@ public:
*/
CMass() : CPhysicalQuantity(0, CMassUnit::kg(), CMassUnit::kg()) {}
/*!
* \brief Init by int value
* \param value
* \param unit
*/
CMass(qint32 value, const CMassUnit &unit) : CPhysicalQuantity(value, unit, CMassUnit::kg()) {}
/*!
* \brief Init by double value
* \param value

View File

@@ -10,15 +10,6 @@ namespace BlackMisc
namespace PhysicalQuantities
{
/*
* Constructor by integer
*/
template <class MU, class PQ> CPhysicalQuantity<MU, PQ>::CPhysicalQuantity(qint32 baseValue, const MU &unit, const MU &siConversionUnit) :
m_unit(unit), m_conversionSiUnit(siConversionUnit)
{
this->setUnitValue(baseValue);
}
/*
* Constructor by double
*/
@@ -32,8 +23,8 @@ template <class MU, class PQ> CPhysicalQuantity<MU, PQ>::CPhysicalQuantity(doubl
* Copy constructor
*/
template <class MU, class PQ> CPhysicalQuantity<MU, PQ>::CPhysicalQuantity(const CPhysicalQuantity &other) :
m_unitValueD(other.m_unitValueD), m_unitValueI(other.m_unitValueI), m_convertedSiUnitValueD(other.m_convertedSiUnitValueD),
m_isIntegerBaseValue(other.m_isIntegerBaseValue), m_unit(other.m_unit), m_conversionSiUnit(other.m_conversionSiUnit)
m_unitValueD(other.m_unitValueD), m_convertedSiUnitValueD(other.m_convertedSiUnitValueD),
m_unit(other.m_unit), m_conversionSiUnit(other.m_conversionSiUnit)
{
}
@@ -58,18 +49,9 @@ template <class MU, class PQ> bool CPhysicalQuantity<MU, PQ>::operator ==(const
bool eq = false;
if (this->m_unit == other.m_unit)
{
// same unit
if (this->m_isIntegerBaseValue && other.m_isIntegerBaseValue)
{
// pure integer comparison, no rounding issues
eq = this->m_unitValueI == other.m_unitValueI;
}
else
{
// same unit, comparison based on double
diff = qAbs(this->m_unitValueD - other.m_unitValueD);
eq = diff <= (lenient * this->m_unit.getEpsilon());
}
// same unit, comparison based on double
diff = qAbs(this->m_unitValueD - other.m_unitValueD);
eq = diff <= (lenient * this->m_unit.getEpsilon());
}
else
{
@@ -95,10 +77,8 @@ template <class MU, class PQ> CPhysicalQuantity<MU, PQ>& CPhysicalQuantity<MU, P
{
if (this == &other) return *this;
this->m_unitValueI = other.m_unitValueI;
this->m_unitValueD = other.m_unitValueD;
this->m_convertedSiUnitValueD = other.m_convertedSiUnitValueD;
this->m_isIntegerBaseValue = other.m_isIntegerBaseValue;
this->m_unit = other.m_unit;
this->m_conversionSiUnit = other.m_conversionSiUnit;
return *this;
@@ -111,16 +91,7 @@ template <class MU, class PQ> CPhysicalQuantity<MU, PQ> &CPhysicalQuantity<MU, P
{
if (this->m_unit == other.m_unit)
{
// same unit
if (this->m_isIntegerBaseValue && other.m_isIntegerBaseValue)
{
// pure integer, no rounding issues
this->setUnitValue(other.m_unitValueI + this->m_unitValueI);
}
else
{
this->setUnitValue(other.m_unitValueD + this->m_unitValueD);
}
this->setUnitValue(other.m_unitValueD + this->m_unitValueD);
}
else
{
@@ -163,16 +134,7 @@ template <class MU, class PQ> CPhysicalQuantity<MU, PQ> &CPhysicalQuantity<MU, P
{
if (this->m_unit == other.m_unit)
{
// same unit
if (this->m_isIntegerBaseValue && other.m_isIntegerBaseValue)
{
// pure integer, no rounding issues
this->setUnitValue(other.m_unitValueI - this->m_unitValueI);
}
else
{
this->setUnitValue(other.m_unitValueD - this->m_unitValueD);
}
this->setUnitValue(other.m_unitValueD - this->m_unitValueD);
}
else
{
@@ -283,25 +245,12 @@ template <class MU, class PQ> PQ &CPhysicalQuantity<MU, PQ>::switchUnit(const MU
return *derived();
}
/*
* Init by integer
*/
template <class MU, class PQ> void CPhysicalQuantity<MU, PQ>::setUnitValue(qint32 baseValue)
{
this->m_unitValueI = baseValue;
this->m_unitValueD = double(baseValue);
this->m_isIntegerBaseValue = true;
this->setConversionSiUnitValue();
}
/*
* Init by double
*/
template <class MU, class PQ> void CPhysicalQuantity<MU, PQ>::setUnitValue(double baseValue)
{
this->m_unitValueD = baseValue;
this->m_unitValueI = qRound(baseValue);
this->m_isIntegerBaseValue = false;
this->setConversionSiUnitValue();
}

View File

@@ -28,9 +28,7 @@ template <class MU, class PQ> class CPhysicalQuantity : public BlackMisc::CBaseS
{
private:
double m_unitValueD; //!< value backed by double
qint32 m_unitValueI; //!< value backed by integer, allows sole integer arithmetic
double m_convertedSiUnitValueD; //!< SI unit value
bool m_isIntegerBaseValue; //!< flag integer? / double?
/*!
* \brief Easy access to derived class (CRTP template parameter)
@@ -54,14 +52,6 @@ protected:
MU m_unit; //!< unit
MU m_conversionSiUnit; //!< corresponding SI base unit
/*!
* \brief Constructor by integer
* \param baseValue
* \param unit
* \param siConversionUnit
*/
CPhysicalQuantity(qint32 baseValue, const MU &unit, const MU &siConversionUnit);
/*!
* \brief Constructor with double
* \param baseValue
@@ -80,12 +70,6 @@ protected:
return this->unitValueRoundedWithUnit(-1, i18n);
}
/*!
* \brief Init by integer
* \param baseValue
*/
void setUnitValue(qint32 baseValue);
/*!
* \brief Init by double
* \param baseValue
@@ -186,15 +170,6 @@ public:
*/
QString valueRoundedWithUnit(const MU &unit, int digits = -1, bool i18n = false) const;
/*!
* \brief Value as int
* \return
*/
qint32 unitValueToInteger() const
{
return this->m_unitValueI;
}
/*!
* \brief Value as double
* \return
@@ -235,16 +210,6 @@ public:
return this->m_convertedSiUnitValueD;
}
/*!
* \brief SI value as integer
* \return
*/
qint32 convertedSiValueToInteger() const
{
return static_cast<qint32>(
BlackMisc::Math::CMath::round(this->m_convertedSiUnitValueD, 0));
}
/*!
* \brief Rounded SI value by n digits
* \param digits
@@ -403,7 +368,6 @@ public:
*/
bool isZeroEpsilon() const
{
if (this->m_isIntegerBaseValue) return this->m_unitValueI == 0;
return this->m_unit.isEpsilon(this->m_unitValueD);
}
@@ -432,9 +396,7 @@ public:
virtual void marshallToDbus(QDBusArgument &argument) const
{
argument << this->m_unitValueD;
argument << this->m_unitValueI;
argument << this->m_convertedSiUnitValueD;
argument << this->m_isIntegerBaseValue;
argument << this->m_unit;
argument << this->m_conversionSiUnit;
}
@@ -446,9 +408,7 @@ public:
virtual void unmarshallFromDbus(const QDBusArgument &argument)
{
argument >> this->m_unitValueD;
argument >> this->m_unitValueI;
argument >> this->m_convertedSiUnitValueD;
argument >> this->m_isIntegerBaseValue;
argument >> this->m_unit;
argument >> this->m_conversionSiUnit;
}

View File

@@ -30,13 +30,6 @@ public:
*/
CPressure(const CPhysicalQuantity &base) : CPhysicalQuantity(base) {}
/*!
* \brief Init by int value
* \param value
* \param unit
*/
CPressure(qint32 value, const CPressureUnit &unit) : CPhysicalQuantity(value, unit, CPressureUnit::Pa()) {}
/*!
*\brief Init by double value
* \param value

View File

@@ -30,13 +30,6 @@ public:
*/
CSpeed(const CPhysicalQuantity &base): CPhysicalQuantity(base) {}
/*!
* \brief Init by int value
* \param value
* \param unit
*/
CSpeed(qint32 value, const CSpeedUnit &unit) : CPhysicalQuantity(value, unit, CSpeedUnit::m_s()) {}
/*!
*\brief Init by double value
* \param value

View File

@@ -29,13 +29,6 @@ public:
*/
CTemperature(const CPhysicalQuantity &base) : CPhysicalQuantity(base) {}
/*!
* \brief Init by int value
* \param value
* \param unit
*/
CTemperature(qint32 value, const CTemperatureUnit &unit): CPhysicalQuantity(value, unit, CTemperatureUnit::K()) {}
/*!
* \brief Init by double value
* \param value

View File

@@ -30,13 +30,6 @@ public:
*/
CTime(const CPhysicalQuantity &base): CPhysicalQuantity(base) {}
/*!
* \brief Init by int value
* \param value
* \param unit
*/
CTime(qint32 value, const CTimeUnit &unit) : CPhysicalQuantity(value, unit, CTimeUnit::s()) {}
/*!
*\brief Init by double value
* \param value

View File

@@ -97,7 +97,6 @@ void CTestPhysicalQuantities::angleTests()
CAngle a2(1.5 * CAngle::PI(), CAngleUnit::rad());
CAngle a3(35.4336, CAngleUnit::sexagesimalDeg()); // 35.72666
a2.switchUnit(CAngleUnit::deg());
QVERIFY2(a2.unitValueToInteger() == 270, qPrintable(QString("1.5Pi should be 270deg, not %1 deg").arg(a2.unitValueToInteger())));
QVERIFY2(a1.piFactor() == 1, qPrintable(QString("Pi should be 1PI, not %1").arg(a1.piFactor())));
QVERIFY2(a3.valueRounded(CAngleUnit::deg()) == 35.73, "Expecting 35.73");
}
@@ -108,9 +107,9 @@ void CTestPhysicalQuantities::angleTests()
void CTestPhysicalQuantities::massTests()
{
CMass w1(1000, CMassUnit::kg());
CMass w2(w1.unitValueToInteger(), CMassUnit::kg());
CMass w2(w1.unitValueToDouble(), CMassUnit::kg());
w2.switchUnit(CMassUnit::t());
QVERIFY2(w2.unitValueToInteger() == 1, "1tonne shall be 1000kg");
QVERIFY2(w2.unitValueToDouble() == 1, "1tonne shall be 1000kg");
w2.switchUnit(CMassUnit::lb());
QVERIFY2(w2.unitValueToDoubleRounded(2) == 2204.62, "1tonne shall be 2204pounds");
QVERIFY2(w1 == w2, "Masses shall be equal");
@@ -154,7 +153,7 @@ void CTestPhysicalQuantities::temperatureTests()
void CTestPhysicalQuantities::timeTests()
{
CTime t1(1, CTimeUnit::h());
QVERIFY2(t1.convertedSiValueToInteger() == 3600, "1hour shall be 3600s");
QVERIFY2(t1.convertedSiValueToDouble() == 3600, "1hour shall be 3600s");
}
/*