Virtual destructor for polymorphic classes, const correctness, removed virtual method call in constructor

This commit is contained in:
Klaus Basan
2013-03-24 17:15:08 +01:00
parent 3b99954bdc
commit b3439ea3e4
18 changed files with 320 additions and 183 deletions

View File

@@ -58,10 +58,18 @@ int main(int argc, char *argv[])
qDebug() << t1 << t2 << t2.convertedSiValueRoundedWithUnit(); qDebug() << t1 << t2 << t2.convertedSiValueRoundedWithUnit();
qDebug() << t3 << t3.valueRoundedWithUnit(CTemperatureUnit::C()); qDebug() << t3 << t3.valueRoundedWithUnit(CTemperatureUnit::C());
// some logging wit CLogMessage // some logging with CLogMessage
bDebug << p1; bDebug << p1;
bDebug << p1.getUnit() << p1.getUnit().getMultiplier(); bDebug << p1.getUnit() << p1.getUnit().getMultiplier();
// some of the faults Mathew has pointed out,not longer possible
// CAngleUnit::rad() = CAngleUnit::deg();
// qDebug() << CAngleUnit::rad(); // wrong
(t1 - t2).switchUnit(CTemperatureUnit::F()); // was not working since wrong return type const
// CDistanceUnit duA(CSpeedUnit::ft_min()); // no longer possible
CDistanceUnit duB(CDistanceUnit::cm());
qDebug() << duB;
// bye // bye
return a.exec(); return a.exec();
} }

View File

@@ -34,4 +34,12 @@ CAngle::CAngle(double value, const CAngleUnit &unit) : CPhysicalQuantity(value,
// void // void
} }
/**
* Destructor
*/
CAngle::~CAngle()
{
// void
}
} // namespace } // namespace

View File

@@ -10,6 +10,13 @@ namespace BlackMisc {
* \author KWB * \author KWB
*/ */
class CAngleUnit : public CMeasurementUnit { class CAngleUnit : public CMeasurementUnit {
friend class CAngle;
private:
/*!
* Downcast copy constructor, allows to implement methods in base class
* \param otherUnit
*/
CAngleUnit(const CMeasurementUnit &otherUnit) : CMeasurementUnit(otherUnit) {}
public: public:
/*! /*!
* Constructor * Constructor
@@ -24,21 +31,16 @@ public:
*/ */
CAngleUnit(const QString &name, const QString &unitName, bool isSIUnit, double conversionFactorToSI = 1.0, const CMeasurementPrefix &mulitplier = CMeasurementPrefix::One(), qint32 displayDigits = 2, double epsilon = 1E-9) : CAngleUnit(const QString &name, const QString &unitName, bool isSIUnit, double conversionFactorToSI = 1.0, const CMeasurementPrefix &mulitplier = CMeasurementPrefix::One(), qint32 displayDigits = 2, double epsilon = 1E-9) :
CMeasurementUnit(name, unitName, "angle", isSIUnit, false, conversionFactorToSI, mulitplier, displayDigits, epsilon) {} CMeasurementUnit(name, unitName, "angle", isSIUnit, false, conversionFactorToSI, mulitplier, displayDigits, epsilon) {}
/*!
* Downcast copy constructor, allows to implement methods in base class
* \param otherUnit
*/
CAngleUnit(const CMeasurementUnit &otherUnit) : CMeasurementUnit(otherUnit) {}
/*! /*!
* \brief Meter m * \brief Meter m
* \return * \return
*/ */
static CAngleUnit& rad() { static CAngleUnit rad("radian", "rad", true); return rad;} static const CAngleUnit& rad() { static CAngleUnit rad("radian", "rad", true); return rad;}
/*! /*!
* \brief Nautical miles NM * \brief Nautical miles NM
* \return * \return
*/ */
static CAngleUnit& deg() { static CAngleUnit deg("degree", "°", false, M_PI/180); return deg;} static const CAngleUnit& deg() { static CAngleUnit deg("degree", "°", false, M_PI/180); return deg;}
}; };
/*! /*!
@@ -68,16 +70,20 @@ public:
* \param unit * \param unit
*/ */
CAngle(double value, const CAngleUnit &unit = CAngleUnit::rad()); CAngle(double value, const CAngleUnit &unit = CAngleUnit::rad());
/*!
* \brief Virtual destructor
*/
virtual ~CAngle();
/*! /*!
* \brief Unit of the distance * \brief Unit of the distance
* \return * \return
*/ */
CAngleUnit getUnit() const { return this->_unit; } CAngleUnit getUnit() const { return this->_pUnit; }
/*! /*!
* \brief Conversion SI unit * \brief Conversion SI unit
* \return * \return
*/ */
CAngleUnit getConversionSiUnit() const { return this->_conversionSiUnit; } CAngleUnit getConversionSiUnit() const { return this->_pConversionSiUnit; }
/*! /*!
* \brief Convenience method PI * \brief Convenience method PI
* \return * \return

View File

@@ -19,22 +19,22 @@ public:
* \brief Temperature absolute Zero in °C * \brief Temperature absolute Zero in °C
* \return * \return
*/ */
static CTemperature& TemperatureAbsoluteZero() { static CTemperature t(-273.15, CTemperatureUnit::C()); return t;} static const CTemperature& TemperatureAbsoluteZero() { static CTemperature t(-273.15, CTemperatureUnit::C()); return t;}
/*! /*!
* \brief Tripe point of purified water, 0.01°C * \brief Tripe point of purified water, 0.01°C
* \return * \return
*/ */
static CTemperature& TemperatureTriplePointOfVSMOW() { static CTemperature t(-273.16, CTemperatureUnit::K()); return t;} static const CTemperature& TemperatureTriplePointOfVSMOW() { static CTemperature t(-273.16, CTemperatureUnit::K()); return t;}
/*! /*!
* \brief Temperature absolute Zero in °C * \brief Temperature absolute Zero in °C
* \return * \return
*/ */
static CTemperature& TemperatureAbsoluteZeroC() { static CTemperature t(-273.15, CTemperatureUnit::C()); return t;} static const CTemperature& TemperatureAbsoluteZeroC() { static CTemperature t(-273.15, CTemperatureUnit::C()); return t;}
/*! /*!
* \brief Standard pressure 1013,25mbar / 29.92inHg * \brief Standard pressure 1013,25mbar / 29.92inHg
* \return * \return
*/ */
static CPressure& InternationalStandardSeaLevelPressure() { static CPressure p(1013.25, CPressureUnit::hPa()); return p;} static const CPressure& InternationalStandardSeaLevelPressure() { static CPressure p(1013.25, CPressureUnit::hPa()); return p;}
}; };
} // namespace } // namespace
#endif // PQCONSTANTS_H #endif // PQCONSTANTS_H

View File

@@ -34,4 +34,12 @@ CDistance::CDistance(double value, const CDistanceUnit &unit) : CPhysicalQuantit
// void // void
} }
/**
* Destructor
*/
CDistance::~CDistance()
{
// void
}
} // namespace BlackCore } // namespace BlackCore

View File

@@ -9,6 +9,13 @@ namespace BlackMisc {
* \author KWB * \author KWB
*/ */
class CDistanceUnit : public CMeasurementUnit { class CDistanceUnit : public CMeasurementUnit {
friend class CDistance;
private:
/*!
* Downcast copy constructor, allows to implement methods in base class
* \param otherUnit
*/
CDistanceUnit(const CMeasurementUnit &otherUnit) : CMeasurementUnit(otherUnit) {}
public: public:
/*! /*!
* Constructor * Constructor
@@ -24,36 +31,31 @@ public:
*/ */
CDistanceUnit(const QString &name, const QString &unitName, bool isSIUnit, bool isSIBaseUnit, double conversionFactorToSI = 1.0, const CMeasurementPrefix &mulitplier = CMeasurementPrefix::One(), qint32 displayDigits = 2, double epsilon = 1E-9) : CDistanceUnit(const QString &name, const QString &unitName, bool isSIUnit, bool isSIBaseUnit, double conversionFactorToSI = 1.0, const CMeasurementPrefix &mulitplier = CMeasurementPrefix::One(), qint32 displayDigits = 2, double epsilon = 1E-9) :
CMeasurementUnit(name, unitName, "distance", isSIUnit, isSIBaseUnit, conversionFactorToSI, mulitplier, displayDigits, epsilon) {} CMeasurementUnit(name, unitName, "distance", isSIUnit, isSIBaseUnit, conversionFactorToSI, mulitplier, displayDigits, epsilon) {}
/*!
* Downcast copy constructor, allows to implement methods in base class
* \param otherUnit
*/
CDistanceUnit(const CMeasurementUnit &otherUnit) : CMeasurementUnit(otherUnit) {}
/*! /*!
* \brief Meter m * \brief Meter m
* \return * \return
*/ */
static CDistanceUnit& m() { static CDistanceUnit m("meter", "m", true, true); return m;} static const CDistanceUnit& m() { static CDistanceUnit m("meter", "m", true, true); return m;}
/*! /*!
* \brief Nautical miles NM * \brief Nautical miles NM
* \return * \return
*/ */
static CDistanceUnit& NM() { static CDistanceUnit NM("nautical miles", "NM", false, false, 1000.0*1.85200, CMeasurementPrefix::One(), 3);return NM;} static const CDistanceUnit& NM() { static CDistanceUnit NM("nautical miles", "NM", false, false, 1000.0*1.85200, CMeasurementPrefix::One(), 3);return NM;}
/*! /*!
* \brief Foot ft * \brief Foot ft
* \return * \return
*/ */
static CDistanceUnit& ft() { static CDistanceUnit ft("foot", "ft", false, false, 0.3048, CMeasurementPrefix::One(), 0); return ft;} static const CDistanceUnit& ft() { static CDistanceUnit ft("foot", "ft", false, false, 0.3048, CMeasurementPrefix::One(), 0); return ft;}
/*! /*!
* \brief Kilometer km * \brief Kilometer km
* \return * \return
*/ */
static CDistanceUnit& km() { static CDistanceUnit km("kilometer", "km", true, false, CMeasurementPrefix::k().getFactor(), CMeasurementPrefix::k(), 3);return km;} static const CDistanceUnit& km() { static CDistanceUnit km("kilometer", "km", true, false, CMeasurementPrefix::k().getFactor(), CMeasurementPrefix::k(), 3);return km;}
/*! /*!
* \brief Centimeter cm * \brief Centimeter cm
* \return * \return
*/ */
static CDistanceUnit& cm() { static CDistanceUnit cm("centimeter", "cm", true, false, CMeasurementPrefix::c().getFactor(), CMeasurementPrefix::c(), 1);return cm;} static const CDistanceUnit& cm() { static CDistanceUnit cm("centimeter", "cm", true, false, CMeasurementPrefix::c().getFactor(), CMeasurementPrefix::c(), 1);return cm;}
}; };
/*! /*!
@@ -83,16 +85,20 @@ public:
* \param unit * \param unit
*/ */
CDistance(double value, const CDistanceUnit &unit = CDistanceUnit::m()); CDistance(double value, const CDistanceUnit &unit = CDistanceUnit::m());
/*!
* \brief Virtual destructor
*/
virtual ~CDistance();
/*! /*!
* \brief Unit of the distance * \brief Unit of the distance
* \return * \return
*/ */
CDistanceUnit getUnit() const { return this->_unit; } CDistanceUnit getUnit() const { return this->_pUnit; }
/*! /*!
* \brief Conversion SI unit * \brief Conversion SI unit
* \return * \return
*/ */
CDistanceUnit getConversionSiUnit() const { return this->_conversionSiUnit; } CDistanceUnit getConversionSiUnit() const { return this->_pConversionSiUnit; }
}; };
} // namespace blackCore } // namespace blackCore
#endif // PQDISTANCE_H #endif // PQDISTANCE_H

View File

@@ -34,5 +34,13 @@ CFrequency::CFrequency(double value, const CFrequencyUnit &unit) : CPhysicalQuan
// void // void
} }
/**
* Destructor
*/
CFrequency::~CFrequency()
{
// void
}
} // namespace } // namespace

View File

@@ -9,6 +9,13 @@ namespace BlackMisc {
* \author KWB * \author KWB
*/ */
class CFrequencyUnit : public CMeasurementUnit { class CFrequencyUnit : public CMeasurementUnit {
friend class CFrequency;
private:
/*!
* Downcast copy constructor, allows to implement methods in base class
* \param otherUnit
*/
CFrequencyUnit(const CMeasurementUnit &otherUnit) : CMeasurementUnit(otherUnit) {}
public: public:
/*! /*!
* Constructor * Constructor
@@ -23,31 +30,26 @@ public:
*/ */
CFrequencyUnit(const QString &name, const QString &unitName, bool isSIUnit, double conversionFactorToSI = 1.0, const CMeasurementPrefix &mulitplier = CMeasurementPrefix::One(), qint32 displayDigits = 2, double epsilon = 1E-9) : CFrequencyUnit(const QString &name, const QString &unitName, bool isSIUnit, double conversionFactorToSI = 1.0, const CMeasurementPrefix &mulitplier = CMeasurementPrefix::One(), qint32 displayDigits = 2, double epsilon = 1E-9) :
CMeasurementUnit(name, unitName, "frequency", isSIUnit, false, conversionFactorToSI, mulitplier, displayDigits, epsilon) {} CMeasurementUnit(name, unitName, "frequency", isSIUnit, false, conversionFactorToSI, mulitplier, displayDigits, epsilon) {}
/*!
* Downcast copy constructor, allows to implement methods in base class
* \param otherUnit
*/
CFrequencyUnit(const CMeasurementUnit &otherUnit) : CMeasurementUnit(otherUnit) {}
/*! /*!
* \brief Hertz * \brief Hertz
* \return * \return
*/ */
static CFrequencyUnit& Hz() { static CFrequencyUnit Hz("hertz", "Hz", true); return Hz;} static const CFrequencyUnit& Hz() { static CFrequencyUnit Hz("hertz", "Hz", true); return Hz;}
/*! /*!
* \brief Kilohertz * \brief Kilohertz
* \return * \return
*/ */
static CFrequencyUnit& kHz() { static CFrequencyUnit kHz("kilohertz", "kHz", true, CMeasurementPrefix::k().getFactor(), CMeasurementPrefix::k(), 0);return kHz;} static const CFrequencyUnit& kHz() { static CFrequencyUnit kHz("kilohertz", "kHz", true, CMeasurementPrefix::k().getFactor(), CMeasurementPrefix::k(), 0);return kHz;}
/*! /*!
* \brief Megahertz * \brief Megahertz
* \return * \return
*/ */
static CFrequencyUnit& MHz() { static CFrequencyUnit MHz("megahertz", "MHz", false, CMeasurementPrefix::M().getFactor(), CMeasurementPrefix::M(), 0); return MHz;} static const CFrequencyUnit& MHz() { static CFrequencyUnit MHz("megahertz", "MHz", false, CMeasurementPrefix::M().getFactor(), CMeasurementPrefix::M(), 0); return MHz;}
/*! /*!
* \brief Gigahertz * \brief Gigahertz
* \return * \return
*/ */
static CFrequencyUnit& GHz() { static CFrequencyUnit GHz("gigahertz", "GHz", true, CMeasurementPrefix::G().getFactor(), CMeasurementPrefix::G(), 0);return GHz;} static const CFrequencyUnit& GHz() { static CFrequencyUnit GHz("gigahertz", "GHz", true, CMeasurementPrefix::G().getFactor(), CMeasurementPrefix::G(), 0);return GHz;}
}; };
/*! /*!
@@ -72,21 +74,25 @@ public:
*/ */
CFrequency(qint32 value, const CFrequencyUnit &unit = CFrequencyUnit::Hz()); CFrequency(qint32 value, const CFrequencyUnit &unit = CFrequencyUnit::Hz());
/*! /*!
*\brief Init by double value * \brief Init by double value
* \param value * \param value
* \param unit * \param unit
*/ */
CFrequency(double value, const CFrequencyUnit &unit = CFrequencyUnit::Hz()); CFrequency(double value, const CFrequencyUnit &unit = CFrequencyUnit::Hz());
/*!
* \brief Virtual destructor
*/
virtual ~CFrequency();
/*! /*!
* \brief Unit of the distance * \brief Unit of the distance
* \return * \return
*/ */
CFrequencyUnit getUnit() const { return this->_unit; } CFrequencyUnit getUnit() const { return this->_pUnit; }
/*! /*!
* \brief Conversion SI unit * \brief Conversion SI unit
* \return * \return
*/ */
CFrequencyUnit getConversionSiUnit() const { return this->_conversionSiUnit; } CFrequencyUnit getConversionSiUnit() const { return this->_pConversionSiUnit; }
}; };
} // namespace blackCore } // namespace blackCore
#endif // PQFREQUENCY_H #endif // PQFREQUENCY_H

View File

@@ -7,13 +7,13 @@ namespace BlackMisc {
*/ */
CMass::CMass(): CPhysicalQuantity(0, CMassUnit::kg(),CMassUnit::kg()) CMass::CMass(): CPhysicalQuantity(0, CMassUnit::kg(),CMassUnit::kg())
{ {
//void // void
} }
/** /**
* Constructor * Constructor
*/ */
CMass::CMass(const CPhysicalQuantity &weight): CPhysicalQuantity(weight) CMass::CMass(const CPhysicalQuantity &mass): CPhysicalQuantity(mass)
{ {
// void // void
} }
@@ -34,4 +34,12 @@ CMass::CMass(double value, const CMassUnit &unit) : CPhysicalQuantity(value, uni
// void // void
} }
} // namespace BlackCore /**
* Destructor
*/
CMass::~CMass()
{
// void
}
} // namespace BlackMisc

View File

@@ -9,6 +9,13 @@ namespace BlackMisc {
* \author KWB * \author KWB
*/ */
class CMassUnit : public CMeasurementUnit { class CMassUnit : public CMeasurementUnit {
friend class CMass;
private:
/*!
* Downcast copy constructor, allows to implement methods in base class
* \param otherUnit
*/
CMassUnit(const CMeasurementUnit &otherUnit) : CMeasurementUnit(otherUnit) {}
public: public:
/*! /*!
* Constructor * Constructor
@@ -24,31 +31,26 @@ public:
*/ */
CMassUnit(const QString &name, const QString &unitName, bool isSIUnit, bool isSIBaseUnit, double conversionFactorToSI = 1.0, const CMeasurementPrefix &mulitplier = CMeasurementPrefix::One(), qint32 displayDigits = 2, double epsilon = 1E-9) : CMassUnit(const QString &name, const QString &unitName, bool isSIUnit, bool isSIBaseUnit, double conversionFactorToSI = 1.0, const CMeasurementPrefix &mulitplier = CMeasurementPrefix::One(), qint32 displayDigits = 2, double epsilon = 1E-9) :
CMeasurementUnit(name, unitName, "mass", isSIUnit, isSIBaseUnit, conversionFactorToSI, mulitplier, displayDigits, epsilon) {} CMeasurementUnit(name, unitName, "mass", isSIUnit, isSIBaseUnit, conversionFactorToSI, mulitplier, displayDigits, epsilon) {}
/*!
* Downcast copy constructor, allows to implement methods in base class
* \param otherUnit
*/
CMassUnit(const CMeasurementUnit &otherUnit) : CMeasurementUnit(otherUnit) {}
/*! /*!
* \brief Kilogram, SI base unit * \brief Kilogram, SI base unit
* \return * \return
*/ */
static CMassUnit& kg() { static CMassUnit kg("kilogram", "kg", true, true, 1.0, CMeasurementPrefix::k(), 1); return kg;} static const CMassUnit& kg() { static CMassUnit kg("kilogram", "kg", true, true, 1.0, CMeasurementPrefix::k(), 1); return kg;}
/*! /*!
* \brief Gram, SI unit * \brief Gram, SI unit
* \return * \return
*/ */
static CMassUnit& g() { static CMassUnit g("gram", "g", true, false, 1.0/1000.0, CMeasurementPrefix::One(), 0); return g;} static const CMassUnit& g() { static CMassUnit g("gram", "g", true, false, 1.0/1000.0, CMeasurementPrefix::One(), 0); return g;}
/*! /*!
* \brief Tonne, aka metric tonne (1000kg) * \brief Tonne, aka metric tonne (1000kg)
* \return * \return
*/ */
static CMassUnit& t() { static CMassUnit t("tonne", "t", true, false, 1000.0, CMeasurementPrefix::One(), 3); return t;} static const CMassUnit& t() { static CMassUnit t("tonne", "t", true, false, 1000.0, CMeasurementPrefix::One(), 3); return t;}
/*! /*!
* \brief Pound, aka mass pound * \brief Pound, aka mass pound
* \return * \return
*/ */
static CMassUnit& lb() { static CMassUnit lbs("pound", "lb", false, false, 0.45359237, CMeasurementPrefix::One(), 1); return lbs;} static const CMassUnit& lb() { static CMassUnit lbs("pound", "lb", false, false, 0.45359237, CMeasurementPrefix::One(), 1); return lbs;}
}; };
/*! /*!
@@ -62,10 +64,6 @@ public:
* \brief Default constructor * \brief Default constructor
*/ */
CMass(); CMass();
/**
*\brief downcast copy constructor
*/
CMass(const CPhysicalQuantity &mass);
/*! /*!
* \brief Init by int value * \brief Init by int value
* \param value * \param value
@@ -78,16 +76,25 @@ public:
* \param unit * \param unit
*/ */
CMass(double value, const CMassUnit &unit = CMassUnit::kg()); CMass(double value, const CMassUnit &unit = CMassUnit::kg());
/*!
* \brief Copyconstructor
* \param mass
*/
CMass(const CPhysicalQuantity &mass);
/*!
* \brief Virtual destructor
*/
virtual ~CMass();
/*! /*!
* \brief Unit of the mass * \brief Unit of the mass
* \return * \return
*/ */
CMassUnit getUnit() const { return this->_unit; } CMassUnit getUnit() const { return this->_pUnit; }
/*! /*!
* \brief Conversion SI unit * \brief Conversion SI unit
* \return * \return
*/ */
CMassUnit getConversionSiUnit() const { return this->_conversionSiUnit; } CMassUnit getConversionSiUnit() const { return this->_pConversionSiUnit; }
}; };
} // namespace blackCore } // namespace blackCore

View File

@@ -5,8 +5,8 @@ namespace BlackMisc {
/** /**
* Constructor by integer * Constructor by integer
*/ */
CPhysicalQuantity::CPhysicalQuantity(qint32 baseValue, const CMeasurementUnit &unit, const CMeasurementUnit &siConversionUnit) : CPhysicalQuantity::CPhysicalQuantity(qint32 baseValue, const CMeasurementUnit &unit, const CMeasurementUnit &siConversionUnit, const CPhysicalQuantityUnitConverter unitConverter) :
_unit(unit), _conversionSiUnit(siConversionUnit) _pUnit(unit), _pConversionSiUnit(siConversionUnit), _unitConverter(unitConverter)
{ {
this->setUnitValue(baseValue); this->setUnitValue(baseValue);
} }
@@ -14,8 +14,8 @@ CPhysicalQuantity::CPhysicalQuantity(qint32 baseValue, const CMeasurementUnit &u
/** /**
* Constructor by double * Constructor by double
*/ */
CPhysicalQuantity::CPhysicalQuantity(double baseValue, const CMeasurementUnit &unit, const CMeasurementUnit &siConversionUnit) : CPhysicalQuantity::CPhysicalQuantity(double baseValue, const CMeasurementUnit &unit, const CMeasurementUnit &siConversionUnit, const CPhysicalQuantityUnitConverter unitConverter) :
_unit(unit), _conversionSiUnit(siConversionUnit) _pUnit(unit), _pConversionSiUnit(siConversionUnit),_unitConverter(unitConverter)
{ {
this->setUnitValue(baseValue); this->setUnitValue(baseValue);
} }
@@ -25,7 +25,15 @@ CPhysicalQuantity::CPhysicalQuantity(double baseValue, const CMeasurementUnit &u
*/ */
CPhysicalQuantity::CPhysicalQuantity(const CPhysicalQuantity &otherQuantity) : CPhysicalQuantity::CPhysicalQuantity(const CPhysicalQuantity &otherQuantity) :
_unitValueD(otherQuantity._unitValueD), _unitValueI(otherQuantity._unitValueI), _convertedSiUnitValueD(otherQuantity._convertedSiUnitValueD), _unitValueD(otherQuantity._unitValueD), _unitValueI(otherQuantity._unitValueI), _convertedSiUnitValueD(otherQuantity._convertedSiUnitValueD),
_isIntegerBaseValue(otherQuantity._isIntegerBaseValue), _unit(otherQuantity._unit), _conversionSiUnit(otherQuantity._conversionSiUnit) _isIntegerBaseValue(otherQuantity._isIntegerBaseValue), _pUnit(otherQuantity._pUnit), _pConversionSiUnit(otherQuantity._pConversionSiUnit), _unitConverter(otherQuantity._unitConverter)
{
// void
}
/*!
* Destructor
*/
CPhysicalQuantity::~CPhysicalQuantity()
{ {
// void // void
} }
@@ -56,24 +64,24 @@ CLogMessage operator <<(CLogMessage d, const CPhysicalQuantity &quantity)
bool CPhysicalQuantity::operator ==(const CPhysicalQuantity &otherQuantity) const bool CPhysicalQuantity::operator ==(const CPhysicalQuantity &otherQuantity) const
{ {
if(this == &otherQuantity) return true; if(this == &otherQuantity) return true;
if(this->_unit.getType()!= otherQuantity._unit.getType()) return false; if(this->_pUnit.getType()!= otherQuantity._pUnit.getType()) return false;
// some special case for best quality // some special case for best quality
double diff; double diff;
const double lenient = 1.001; // even diff alread has a round issue const double lenient = 1.001; // even diff alread has a round issue
if (this->_unit == otherQuantity._unit) { if (this->_pUnit == otherQuantity._pUnit) {
// same unit // same unit
if (this->_isIntegerBaseValue && otherQuantity._isIntegerBaseValue) { if (this->_isIntegerBaseValue && otherQuantity._isIntegerBaseValue) {
// pure integer comparison, no rounding issues // pure integer comparison, no rounding issues
return this->_unitValueI == otherQuantity._unitValueI; return this->_unitValueI == otherQuantity._unitValueI;
} else { } else {
diff = abs(this->_unitValueD - otherQuantity._unitValueD); diff = abs(this->_unitValueD - otherQuantity._unitValueD);
return diff <= (lenient * this->_unit.getEpsilon()); return diff <= (lenient * this->_pUnit.getEpsilon());
} }
} else { } else {
// based on SI value // based on SI value
diff = abs(this->_convertedSiUnitValueD - otherQuantity._convertedSiUnitValueD); diff = abs(this->_convertedSiUnitValueD - otherQuantity._convertedSiUnitValueD);
return diff <= (lenient * this->_unit.getEpsilon()); return diff <= (lenient * this->_pUnit.getEpsilon());
} }
} }
@@ -97,8 +105,9 @@ CPhysicalQuantity& CPhysicalQuantity::operator=(const CPhysicalQuantity &otherQu
CPhysicalQuantity::_unitValueD = otherQuantity._unitValueD; CPhysicalQuantity::_unitValueD = otherQuantity._unitValueD;
CPhysicalQuantity::_convertedSiUnitValueD = otherQuantity._convertedSiUnitValueD; CPhysicalQuantity::_convertedSiUnitValueD = otherQuantity._convertedSiUnitValueD;
CPhysicalQuantity::_isIntegerBaseValue = otherQuantity._isIntegerBaseValue; CPhysicalQuantity::_isIntegerBaseValue = otherQuantity._isIntegerBaseValue;
CPhysicalQuantity::_unit = otherQuantity._unit; CPhysicalQuantity::_pUnit = otherQuantity._pUnit;
CPhysicalQuantity::_conversionSiUnit = otherQuantity._conversionSiUnit; CPhysicalQuantity::_pConversionSiUnit = otherQuantity._pConversionSiUnit;
CPhysicalQuantity::_unitConverter = otherQuantity._unitConverter;
return *this; return *this;
} }
@@ -107,7 +116,7 @@ CPhysicalQuantity& CPhysicalQuantity::operator=(const CPhysicalQuantity &otherQu
*/ */
CPhysicalQuantity &CPhysicalQuantity::operator +=(const CPhysicalQuantity &otherQuantity) CPhysicalQuantity &CPhysicalQuantity::operator +=(const CPhysicalQuantity &otherQuantity)
{ {
if (this->_unit == otherQuantity._unit) { if (this->_pUnit == otherQuantity._pUnit) {
// same unit // same unit
if (this->_isIntegerBaseValue && otherQuantity._isIntegerBaseValue) { if (this->_isIntegerBaseValue && otherQuantity._isIntegerBaseValue) {
// pure integer, no rounding issues // pure integer, no rounding issues
@@ -116,7 +125,7 @@ CPhysicalQuantity &CPhysicalQuantity::operator +=(const CPhysicalQuantity &other
this->setUnitValue(otherQuantity._unitValueI + this->_unitValueI); this->setUnitValue(otherQuantity._unitValueI + this->_unitValueI);
} }
} else { } else {
double v = otherQuantity.value(this->_unit); double v = otherQuantity.value(this->_pUnit);
this->setUnitValue(v + this->_unitValueD); this->setUnitValue(v + this->_unitValueD);
} }
return *this; return *this;
@@ -128,7 +137,7 @@ CPhysicalQuantity &CPhysicalQuantity::operator +=(const CPhysicalQuantity &other
CPhysicalQuantity &CPhysicalQuantity::operator +=(double unprefixedSiUnitValue) CPhysicalQuantity &CPhysicalQuantity::operator +=(double unprefixedSiUnitValue)
{ {
if (!this->isUnprefixedSiUnit()) { if (!this->isUnprefixedSiUnit()) {
this->switchUnit(this->_conversionSiUnit); this->switchUnit(this->_pConversionSiUnit);
} }
this->setUnitValue(this->_unitValueD + unprefixedSiUnitValue); this->setUnitValue(this->_unitValueD + unprefixedSiUnitValue);
return *this; return *this;
@@ -137,7 +146,7 @@ CPhysicalQuantity &CPhysicalQuantity::operator +=(double unprefixedSiUnitValue)
/** /**
* Plus operator * Plus operator
*/ */
const CPhysicalQuantity CPhysicalQuantity::operator +(const CPhysicalQuantity &otherQuantity) const CPhysicalQuantity CPhysicalQuantity::operator +(const CPhysicalQuantity &otherQuantity) const
{ {
CPhysicalQuantity pq = (*this); CPhysicalQuantity pq = (*this);
return pq+= otherQuantity; return pq+= otherQuantity;
@@ -148,7 +157,7 @@ const CPhysicalQuantity CPhysicalQuantity::operator +(const CPhysicalQuantity &o
*/ */
CPhysicalQuantity &CPhysicalQuantity::operator -=(const CPhysicalQuantity &otherQuantity) CPhysicalQuantity &CPhysicalQuantity::operator -=(const CPhysicalQuantity &otherQuantity)
{ {
if (this->_unit == otherQuantity._unit) { if (this->_pUnit == otherQuantity._pUnit) {
// same unit // same unit
if (this->_isIntegerBaseValue && otherQuantity._isIntegerBaseValue) { if (this->_isIntegerBaseValue && otherQuantity._isIntegerBaseValue) {
// pure integer, no rounding issues // pure integer, no rounding issues
@@ -157,7 +166,7 @@ CPhysicalQuantity &CPhysicalQuantity::operator -=(const CPhysicalQuantity &other
this->setUnitValue(otherQuantity._unitValueI - this->_unitValueI); this->setUnitValue(otherQuantity._unitValueI - this->_unitValueI);
} }
} else { } else {
double v = otherQuantity.value(this->_unit); double v = otherQuantity.value(this->_pUnit);
this->setUnitValue(v - this->_unitValueD); this->setUnitValue(v - this->_unitValueD);
} }
return *this; return *this;
@@ -175,7 +184,7 @@ CPhysicalQuantity &CPhysicalQuantity::operator -=(double unprefixedSiUnitValue)
/** /**
* Minus operator * Minus operator
*/ */
const CPhysicalQuantity CPhysicalQuantity::operator -(const CPhysicalQuantity &otherQuantity) const CPhysicalQuantity CPhysicalQuantity::operator -(const CPhysicalQuantity &otherQuantity) const
{ {
CPhysicalQuantity pq = (*this); CPhysicalQuantity pq = (*this);
return pq-= otherQuantity; return pq-= otherQuantity;
@@ -193,7 +202,7 @@ CPhysicalQuantity &CPhysicalQuantity::operator *=(double multiply)
/** /**
* Multiply operator * Multiply operator
*/ */
const CPhysicalQuantity CPhysicalQuantity::operator *(double multiply) const CPhysicalQuantity CPhysicalQuantity::operator *(double multiply) const
{ {
CPhysicalQuantity pq= (*this); CPhysicalQuantity pq= (*this);
return pq *= multiply; return pq *= multiply;
@@ -211,7 +220,7 @@ CPhysicalQuantity &CPhysicalQuantity::operator /=(double divide)
/** /**
* Divide operator /= * Divide operator /=
*/ */
const CPhysicalQuantity CPhysicalQuantity::operator /(double divide) const CPhysicalQuantity CPhysicalQuantity::operator /(double divide) const
{ {
CPhysicalQuantity pq= (*this); CPhysicalQuantity pq= (*this);
return pq /= divide; return pq /= divide;
@@ -223,7 +232,7 @@ const CPhysicalQuantity CPhysicalQuantity::operator /(double divide) const
bool CPhysicalQuantity::operator <(const CPhysicalQuantity &otherQuantity) const { bool CPhysicalQuantity::operator <(const CPhysicalQuantity &otherQuantity) const {
if(this == &otherQuantity) return false; if(this == &otherQuantity) return false;
double diff = this->_convertedSiUnitValueD - otherQuantity._convertedSiUnitValueD; double diff = this->_convertedSiUnitValueD - otherQuantity._convertedSiUnitValueD;
return (diff < 0 && abs(diff) >= this->_unit.getEpsilon()); return (diff < 0 && abs(diff) >= this->_pUnit.getEpsilon());
} }
bool CPhysicalQuantity::operator >(const CPhysicalQuantity &otherQuantity) const { bool CPhysicalQuantity::operator >(const CPhysicalQuantity &otherQuantity) const {
@@ -246,10 +255,10 @@ bool CPhysicalQuantity::operator <=(const CPhysicalQuantity &otherQuantity) cons
*/ */
bool CPhysicalQuantity::switchUnit(const CMeasurementUnit &newUnit) bool CPhysicalQuantity::switchUnit(const CMeasurementUnit &newUnit)
{ {
if (this->_unit == newUnit) return true; if (this->_pUnit == newUnit) return true;
if (this->_unit.getType() != newUnit.getType()) return false; // not possible if (this->_pUnit.getType() != newUnit.getType()) return false; // not possible
double cf = this->calculateValueInOtherUnit(newUnit); double cf = this->_unitConverter(this, newUnit);
this->_unit = newUnit; this->_pUnit = newUnit;
this->setUnitValue(cf); this->setUnitValue(cf);
return true; return true;
} }
@@ -280,17 +289,17 @@ void CPhysicalQuantity::setUnitValue(double baseValue)
* Set SI value * Set SI value
*/ */
void CPhysicalQuantity::setConversionSiUnitValue() { void CPhysicalQuantity::setConversionSiUnitValue() {
this->_convertedSiUnitValueD = this->calculateValueInOtherUnit(this->_conversionSiUnit); this->_convertedSiUnitValueD = this->_unitConverter(this, this->_pConversionSiUnit);
} }
/** /**
* Standard conversion by factor, used in most cases,in some cases overridden (e.g.CTemperature) * Standard conversion by factor, used in most cases, in some cases (e.g. CTemperature) arbitrary converter
*/ */
double CPhysicalQuantity::calculateValueInOtherUnit(const CMeasurementUnit &otherUnit) const { double CPhysicalQuantity::standardUnitFactorValueConverter(const CPhysicalQuantity *quantity, const CMeasurementUnit &otherUnit) {
if (this->_unit == CMeasurementUnit::None() || this->_unitValueD == 0.0) return 0.0; if (quantity->_pUnit == CMeasurementUnit::None() || quantity->_unitValueD == 0.0) return 0.0;
if (this->_unit == otherUnit) return this->_unitValueD; if (quantity->_pUnit == otherUnit) return quantity->_unitValueD;
double f = this->_unit.conversionFactor(otherUnit); double f = quantity->_pUnit.conversionFactor(otherUnit);
return f * this->_unitValueD; return f * quantity->_unitValueD;
} }
/** /**
@@ -298,7 +307,7 @@ double CPhysicalQuantity::calculateValueInOtherUnit(const CMeasurementUnit &othe
*/ */
double CPhysicalQuantity::unitValueToDoubleRounded(int digits) const double CPhysicalQuantity::unitValueToDoubleRounded(int digits) const
{ {
if (digits < 1) digits = this->_unit.getDisplayDigits(); if (digits < 1) digits = this->_pUnit.getDisplayDigits();
return CPhysicalQuantity::round(this->_unitValueD, digits); return CPhysicalQuantity::round(this->_unitValueD, digits);
} }
@@ -317,7 +326,7 @@ QString CPhysicalQuantity::toQStringRounded(double value, int digits)
*/ */
QString CPhysicalQuantity::unitValueToQStringRounded(int digits) const QString CPhysicalQuantity::unitValueToQStringRounded(int digits) const
{ {
if (digits < 1) digits = this->_unit.getDisplayDigits(); if (digits < 1) digits = this->_pUnit.getDisplayDigits();
return CPhysicalQuantity::toQStringRounded(this->_unitValueD, digits); return CPhysicalQuantity::toQStringRounded(this->_unitValueD, digits);
} }
@@ -326,7 +335,7 @@ QString CPhysicalQuantity::unitValueToQStringRounded(int digits) const
*/ */
QString CPhysicalQuantity::convertedSiValueToQStringRounded(int digits) const QString CPhysicalQuantity::convertedSiValueToQStringRounded(int digits) const
{ {
if (digits < 1) digits = this->_conversionSiUnit.getDisplayDigits(); if (digits < 1) digits = this->_pConversionSiUnit.getDisplayDigits();
return CPhysicalQuantity::toQStringRounded(this->_convertedSiUnitValueD, digits); return CPhysicalQuantity::toQStringRounded(this->_convertedSiUnitValueD, digits);
} }
@@ -334,16 +343,16 @@ QString CPhysicalQuantity::convertedSiValueToQStringRounded(int digits) const
* Value rounded in original unit * Value rounded in original unit
*/ */
QString CPhysicalQuantity::unitValueRoundedWithUnit(int digits) const { QString CPhysicalQuantity::unitValueRoundedWithUnit(int digits) const {
if (digits < 1) digits = this->_unit.getDisplayDigits(); if (digits < 1) digits = this->_pUnit.getDisplayDigits();
return this->unitValueToQStringRounded(digits).append(this->_unit.getUnitName()); return this->unitValueToQStringRounded(digits).append(this->_pUnit.getUnitName());
} }
/** /**
* SI base unit value with unit * SI base unit value with unit
*/ */
QString CPhysicalQuantity::convertedSiValueRoundedWithUnit(int digits) const { QString CPhysicalQuantity::convertedSiValueRoundedWithUnit(int digits) const {
if (digits < 1) digits = this->_conversionSiUnit.getDisplayDigits(); if (digits < 1) digits = this->_pConversionSiUnit.getDisplayDigits();
return this->convertedSiValueToQStringRounded(digits).append(this->_conversionSiUnit.getUnitName()); return this->convertedSiValueToQStringRounded(digits).append(this->_pConversionSiUnit.getUnitName());
} }
/** /**
@@ -351,8 +360,8 @@ QString CPhysicalQuantity::convertedSiValueRoundedWithUnit(int digits) const {
*/ */
QString CPhysicalQuantity::valueRoundedWithUnit(const CMeasurementUnit &unit, int digits) const QString CPhysicalQuantity::valueRoundedWithUnit(const CMeasurementUnit &unit, int digits) const
{ {
if (unit == this->_unit) return this->unitValueRoundedWithUnit(digits); if (unit == this->_pUnit) return this->unitValueRoundedWithUnit(digits);
if (unit == this->_conversionSiUnit) return this->convertedSiValueRoundedWithUnit(digits); if (unit == this->_pConversionSiUnit) return this->convertedSiValueRoundedWithUnit(digits);
if (digits < 0) digits = unit.getDisplayDigits(); if (digits < 0) digits = unit.getDisplayDigits();
return CPhysicalQuantity::toQStringRounded(this->value(unit), digits).append(unit.getUnitName()); return CPhysicalQuantity::toQStringRounded(this->value(unit), digits).append(unit.getUnitName());
} }
@@ -371,8 +380,8 @@ double CPhysicalQuantity::valueRounded(const CMeasurementUnit &unit, int digits)
*/ */
double CPhysicalQuantity::value(const CMeasurementUnit &unit) const double CPhysicalQuantity::value(const CMeasurementUnit &unit) const
{ {
if (unit == this->_conversionSiUnit) return this->_convertedSiUnitValueD; if (unit == this->_pConversionSiUnit) return this->_convertedSiUnitValueD;
double v = this->calculateValueInOtherUnit(unit); double v = this->_unitConverter(this, unit);
return v; return v;
} }
@@ -381,7 +390,7 @@ double CPhysicalQuantity::value(const CMeasurementUnit &unit) const
*/ */
double CPhysicalQuantity::convertedSiValueToDoubleRounded(int digits) const double CPhysicalQuantity::convertedSiValueToDoubleRounded(int digits) const
{ {
if (digits < 1) digits = this->_conversionSiUnit.getDisplayDigits(); if (digits < 1) digits = this->_pConversionSiUnit.getDisplayDigits();
return CPhysicalQuantity::round(this->_convertedSiUnitValueD, digits); return CPhysicalQuantity::round(this->_convertedSiUnitValueD, digits);
} }

View File

@@ -9,6 +9,14 @@
namespace BlackMisc { namespace BlackMisc {
class CPhysicalQuantity;
/*!
* Our converter function, should be implemented as static method of the quantity
* classes for clarity
*/
typedef double (*CPhysicalQuantityUnitConverter)(const CPhysicalQuantity *quantity, const CMeasurementUnit &unit);
/*! /*!
* \brief A physical quantity such as "5m", "20s", "1500ft/s" * \brief A physical quantity such as "5m", "20s", "1500ft/s"
* \author KWB * \author KWB
@@ -37,28 +45,43 @@ private:
double _unitValueD; //!< value backed by double double _unitValueD; //!< value backed by double
double _convertedSiUnitValueD; //!< SI unit value double _convertedSiUnitValueD; //!< SI unit value
bool _isIntegerBaseValue; //!< flag integer? / double? bool _isIntegerBaseValue; //!< flag integer? / double?
CPhysicalQuantityUnitConverter _unitConverter; //! <! converts values between units
/*!
* Convert value in another unit, normally just by a factor, but in some cases
* (e.g. CTemperature)overridden because arbitrary conversion is required
* \sa CMeasurementUnit::conversionFactor(CMeasurementUnit)
* \param quantity quanity
* \param otherUnit
* \return
*/
static double standardUnitFactorValueConverter(const CPhysicalQuantity *quantity, const CMeasurementUnit &otherUnit);
protected: protected:
CMeasurementUnit _unit; //!< unit CMeasurementUnit _pUnit; //!< unit
CMeasurementUnit _conversionSiUnit; //!< corresponding SI base unit CMeasurementUnit _pConversionSiUnit; //!< corresponding SI base unit
/*! /*!
* \brief Constructor with int * \brief Constructor with int
* \param baseValue * \param baseValue
* \param unit * \param unit
* \param siBaseUnit * \param siBaseUnit
* \param unitConverter
*/ */
CPhysicalQuantity(qint32 baseValue, const CMeasurementUnit &unit, const CMeasurementUnit &siConversionUnit); CPhysicalQuantity(qint32 baseValue, const CMeasurementUnit &unit, const CMeasurementUnit &siConversionUnit,
const CPhysicalQuantityUnitConverter unitConverter = CPhysicalQuantity::standardUnitFactorValueConverter);
/*! /*!
* \brief Constructor with double * \brief Constructor with double
* \param baseValue * \param baseValue
* \param unit * \param unit
* \param siBaseUnit * \param siBaseUnit
*/ */
CPhysicalQuantity(double baseValue, const CMeasurementUnit &unit, const CMeasurementUnit &siConversionUnit); CPhysicalQuantity(double baseValue, const CMeasurementUnit &unit, const CMeasurementUnit &siConversionUnit,
const CPhysicalQuantityUnitConverter unitConverter = CPhysicalQuantity::standardUnitFactorValueConverter);
/*! /*!
* \brief Init by integer * \brief Init by integer
* \param baseValue * \param baseValue
* \param unitConverter
*/ */
void setUnitValue(qint32 baseValue); void setUnitValue(qint32 baseValue);
/*! /*!
@@ -70,14 +93,6 @@ protected:
* \brief Set the SI value * \brief Set the SI value
*/ */
void setConversionSiUnitValue(); void setConversionSiUnitValue();
/*!
* Convert value in another unit, normally just by a factor, but in some cases
* (e.g. CTemperature)overridden because arbitrary conversion is required
* \sa CMeasurementUnit::conversionFactor(CMeasurementUnit)
* \param otherUnit
* \return
*/
virtual double calculateValueInOtherUnit(const CMeasurementUnit &otherUnit) const;
public: public:
/*! /*!
@@ -85,6 +100,11 @@ public:
* \param otherQuantity * \param otherQuantity
*/ */
CPhysicalQuantity(const CPhysicalQuantity &otherQuantity); CPhysicalQuantity(const CPhysicalQuantity &otherQuantity);
/*!
* \brief Virtual destructor
*/
virtual ~CPhysicalQuantity();
/*! /*!
* \brief Switch unit, e.g. feet meter * \brief Switch unit, e.g. feet meter
* \param newUnit * \param newUnit
@@ -95,17 +115,17 @@ public:
* \brief Value in SI base unit? Meter is an SI base unit, hertz not! * \brief Value in SI base unit? Meter is an SI base unit, hertz not!
* \return * \return
*/ */
bool isSiBaseUnit() const { return this->_unit.isSiBaseUnit(); } bool isSiBaseUnit() const { return this->_pUnit.isSiBaseUnit(); }
/*! /*!
* \brief Value in SI unit? Hertz is an derived SI unit, NM not! * \brief Value in SI unit? Hertz is an derived SI unit, NM not!
* \return * \return
*/ */
bool isSiUnit() const { return this->_unit.isSiUnit(); } bool isSiUnit() const { return this->_pUnit.isSiUnit(); }
/*! /*!
* \brief Value in unprefixed SI unit? Meter is a unprefixed, kilometer a prefixed SI Unit * \brief Value in unprefixed SI unit? Meter is a unprefixed, kilometer a prefixed SI Unit
* \return * \return
*/ */
bool isUnprefixedSiUnit() const { return this->_unit.isUnprefixedSiUnit(); } bool isUnprefixedSiUnit() const { return this->_pUnit.isUnprefixedSiUnit(); }
/*! /*!
* \brief Value to QString with unit, e.g. "5.00m" * \brief Value to QString with unit, e.g. "5.00m"
* \param digits * \param digits
@@ -235,13 +255,13 @@ public:
* \param otherQuantity * \param otherQuantity
* @return * @return
*/ */
const CPhysicalQuantity operator +(const CPhysicalQuantity &otherQuantity) const; CPhysicalQuantity operator +(const CPhysicalQuantity &otherQuantity) const;
/*! /*!
* \brief Minus operator - * \brief Minus operator -
* \param otherQuantity * \param otherQuantity
* @return * @return
*/ */
const CPhysicalQuantity operator -(const CPhysicalQuantity &otherQuantity) const; CPhysicalQuantity operator -(const CPhysicalQuantity &otherQuantity) const;
/*! /*!
* \brief Multiply operator *= * \brief Multiply operator *=
* \param multiply * \param multiply
@@ -259,13 +279,13 @@ public:
* \param multiply * \param multiply
* @return * @return
*/ */
const CPhysicalQuantity operator *(double multiply) const; CPhysicalQuantity operator *(double multiply) const;
/*! /*!
* \brief Operator / * \brief Operator /
* \param divide * \param divide
* @return * @return
*/ */
const CPhysicalQuantity operator /(double divide) const; CPhysicalQuantity operator /(double divide) const;
/*! /*!
* \brief Equal operator == * \brief Equal operator ==
* \param otherQuantity * \param otherQuantity

View File

@@ -34,5 +34,13 @@ CPressure::CPressure(double value, const CPressureUnit &unit) : CPhysicalQuantit
// void // void
} }
/**
* Destructor
*/
CPressure::~CPressure()
{
// void
}
} // namespace } // namespace

View File

@@ -10,6 +10,14 @@ namespace BlackMisc {
* \author KWB * \author KWB
*/ */
class CPressureUnit : public CMeasurementUnit { class CPressureUnit : public CMeasurementUnit {
friend class CPressure;
private:
/*!
* Downcast copy constructor, allows to implement methods in base class
* and cast unit
* \param otherUnit
*/
CPressureUnit(const CMeasurementUnit &otherUnit) : CMeasurementUnit(otherUnit) {}
public: public:
/*! /*!
* Constructor * Constructor
@@ -24,46 +32,41 @@ public:
*/ */
CPressureUnit(const QString &name, const QString &unitName, bool isSIUnit, double conversionFactorToSI = 1.0, const CMeasurementPrefix &mulitplier = CMeasurementPrefix::One(), qint32 displayDigits = 2, double epsilon = 1E-9) : CPressureUnit(const QString &name, const QString &unitName, bool isSIUnit, double conversionFactorToSI = 1.0, const CMeasurementPrefix &mulitplier = CMeasurementPrefix::One(), qint32 displayDigits = 2, double epsilon = 1E-9) :
CMeasurementUnit(name, unitName, "frequency", isSIUnit, false, conversionFactorToSI, mulitplier, displayDigits, epsilon) {} CMeasurementUnit(name, unitName, "frequency", isSIUnit, false, conversionFactorToSI, mulitplier, displayDigits, epsilon) {}
/*!
* Downcast copy constructor, allows to implement methods in base class
* \param otherUnit
*/
CPressureUnit(const CMeasurementUnit &otherUnit) : CMeasurementUnit(otherUnit) {}
/*! /*!
* \brief Pascal * \brief Pascal
* \return * \return
*/ */
static CPressureUnit& Pa() { static CPressureUnit Pa("pascal", "Pa", true); return Pa;} static const CPressureUnit& Pa() { static CPressureUnit Pa("pascal", "Pa", true); return Pa;}
/*! /*!
* \brief Hectopascal * \brief Hectopascal
* \return * \return
*/ */
static CPressureUnit& hPa() { static CPressureUnit hPa("hectopascal", "hPa", true, CMeasurementPrefix::h().getFactor(), CMeasurementPrefix::h()); return hPa;} static const CPressureUnit& hPa() { static CPressureUnit hPa("hectopascal", "hPa", true, CMeasurementPrefix::h().getFactor(), CMeasurementPrefix::h()); return hPa;}
/*! /*!
* \brief Pounds per square inch * \brief Pounds per square inch
* \return * \return
*/ */
static CPressureUnit& psi() { static CPressureUnit psi("pounds per square inch", "psi", false, 6894.8, CMeasurementPrefix::One(), 2); return psi;} static const CPressureUnit& psi() { static CPressureUnit psi("pounds per square inch", "psi", false, 6894.8, CMeasurementPrefix::One(), 2); return psi;}
/*! /*!
* \brief Bar * \brief Bar
* \return * \return
*/ */
static CPressureUnit& bar() { static CPressureUnit bar("bar", "bar", false, 1E5);return bar;} static const CPressureUnit& bar() { static CPressureUnit bar("bar", "bar", false, 1E5);return bar;}
/*! /*!
* \brief Millibar, actually the same as hPa * \brief Millibar, actually the same as hPa
* \return * \return
*/ */
static CPressureUnit& mbar() { static CPressureUnit bar("bar", "bar", false, 1E2);return bar;} static const CPressureUnit& mbar() { static CPressureUnit bar("bar", "bar", false, 1E2);return bar;}
/*! /*!
* \brief Inch of mercury at 0°C * \brief Inch of mercury at 0°C
* \return * \return
*/ */
static CPressureUnit& inHg() { static CPressureUnit inhg("Inch of mercury 0°C", "inHg", false, 3386.389);return inhg;} static const CPressureUnit& inHg() { static CPressureUnit inhg("Inch of mercury 0°C", "inHg", false, 3386.389);return inhg;}
/*! /*!
* \brief Inch of mercury for flight level 29,92inHg = 1013,25mbar = 1013,25hPa * \brief Inch of mercury for flight level 29,92inHg = 1013,25mbar = 1013,25hPa
* \return * \return
*/ */
static CPressureUnit& inHgFL() { static CPressureUnit inhg("Inch of mercury ", "inHg", false, 3386.5307486631);return inhg;} static const CPressureUnit& inHgFL() { static CPressureUnit inhg("Inch of mercury ", "inHg", false, 3386.5307486631);return inhg;}
}; };
/*! /*!
@@ -93,16 +96,20 @@ public:
* \param unit * \param unit
*/ */
CPressure(double value, const CPressureUnit &unit = CPressureUnit::Pa()); CPressure(double value, const CPressureUnit &unit = CPressureUnit::Pa());
/*!
* \brief Virtual destructor
*/
virtual ~CPressure();
/*! /*!
* \brief Unit of the distance * \brief Unit of the distance
* \return * \return
*/ */
CPressureUnit getUnit() const { return this->_unit; } CPressureUnit getUnit() const { return this->_pUnit; }
/*! /*!
* \brief Conversion SI unit * \brief Conversion SI unit
* \return * \return
*/ */
CPressureUnit getConversionSiUnit() const { return this->_conversionSiUnit; } CPressureUnit getConversionSiUnit() const { return this->_pConversionSiUnit; }
}; };
} // namespace blackCore } // namespace blackCore
#endif // PQPRESSURE_H #endif // PQPRESSURE_H

View File

@@ -10,13 +10,13 @@ CSpeed::CSpeed() : CPhysicalQuantity(0, CSpeedUnit::m_s(), CSpeedUnit::m_s())
// void // void
} }
/** ///**
* Constructor // * Constructor
*/ // */
CSpeed::CSpeed(const CPhysicalQuantity &speed): CPhysicalQuantity(speed) //CSpeed::CSpeed(const CPhysicalQuantity &speed): CPhysicalQuantity(speed)
{ //{
//void // //void
} //}
/** /**
* Constructor * Constructor
@@ -34,5 +34,12 @@ CSpeed::CSpeed(double value, const CSpeedUnit &unit) : CPhysicalQuantity(value,
// void // void
} }
/*!
* \brief Destructor
*/
CSpeed::~CSpeed()
{
// void
}
} // namespace } // namespace

View File

@@ -33,27 +33,27 @@ public:
* \brief Meter/second m/s * \brief Meter/second m/s
* \return * \return
*/ */
static CSpeedUnit& m_s() { static CSpeedUnit ms("meter/second", "m/s", true, false); return ms;} static const CSpeedUnit& m_s() { static CSpeedUnit ms("meter/second", "m/s", true, false); return ms;}
/*! /*!
* \brief Nautical miles per hour NM/h * \brief Nautical miles per hour NM/h
* \return * \return
*/ */
static CSpeedUnit& NM_h() { static CSpeedUnit NMh("nautical miles/hour", "NM/h", false, false, 1852.0/3600.0, CMeasurementPrefix::One(), 1);return NMh;} static const CSpeedUnit& NM_h() { static CSpeedUnit NMh("nautical miles/hour", "NM/h", false, false, 1852.0/3600.0, CMeasurementPrefix::One(), 1);return NMh;}
/*! /*!
* \brief Feet/second ft/s * \brief Feet/second ft/s
* \return * \return
*/ */
static CSpeedUnit& ft_s() { static CSpeedUnit fts("feet/seconds", "ft/s", false, false, 0.3048, CMeasurementPrefix::One(), 0); return fts;} static const CSpeedUnit& ft_s() { static CSpeedUnit fts("feet/seconds", "ft/s", false, false, 0.3048, CMeasurementPrefix::One(), 0); return fts;}
/*! /*!
* \brief Feet/min ft/min * \brief Feet/min ft/min
* \return * \return
*/ */
static CSpeedUnit& ft_min() { static CSpeedUnit ftmin("feet/minute", "ft/min", false, false, 0.3048 / 60.0, CMeasurementPrefix::One(), 0); return ftmin;} static const CSpeedUnit& ft_min() { static CSpeedUnit ftmin("feet/minute", "ft/min", false, false, 0.3048 / 60.0, CMeasurementPrefix::One(), 0); return ftmin;}
/*! /*!
* \brief Kilometer/hour km/h * \brief Kilometer/hour km/h
* \return * \return
*/ */
static CSpeedUnit& km_h() { static CSpeedUnit kmh("kilometer/hour", "km/h", false, false, 1.0/3.6, CMeasurementPrefix::One(), 1);return kmh;} static const CSpeedUnit& km_h() { static CSpeedUnit kmh("kilometer/hour", "km/h", false, false, 1.0/3.6, CMeasurementPrefix::One(), 1);return kmh;}
}; };
/*! /*!
@@ -62,15 +62,17 @@ public:
*/ */
class CSpeed : public CPhysicalQuantity class CSpeed : public CPhysicalQuantity
{ {
friend class CSpeedUnit;
private:
/**
*\brief Downcast copy constructor, allows casting units
*/
CSpeed(const CPhysicalQuantity &speed);
public: public:
/*! /*!
* \brief Default constructor * \brief Default constructor
*/ */
CSpeed(); CSpeed();
/**
*\brief downcast copy constructor
*/
CSpeed(const CPhysicalQuantity &speed);
/*! /*!
* \brief Init by int value * \brief Init by int value
* \param value * \param value
@@ -83,16 +85,20 @@ public:
* \param unit * \param unit
*/ */
CSpeed(double value, const CSpeedUnit &unit = CSpeedUnit::m_s()); CSpeed(double value, const CSpeedUnit &unit = CSpeedUnit::m_s());
/*!
* \brief Destructor
*/
virtual ~CSpeed();
/*! /*!
* \brief Unit of the distance * \brief Unit of the distance
* \return * \return
*/ */
CSpeedUnit getUnit() const { return this->_unit; } CSpeedUnit getUnit() const { return this->_pUnit; }
/*! /*!
* \brief Conversion SI unit * \brief Conversion SI unit
* \return * \return
*/ */
CSpeedUnit getConversionSiUnit() const { return this->_conversionSiUnit; } CSpeedUnit getConversionSiUnit() const { return this->_pConversionSiUnit; }
}; };
} // namespace } // namespace

View File

@@ -5,9 +5,9 @@ namespace BlackMisc {
/** /**
* Default Constructor * Default Constructor
*/ */
CTemperature::CTemperature(): CPhysicalQuantity(0, CTemperatureUnit::K(),CTemperatureUnit::K()) CTemperature::CTemperature(): CPhysicalQuantity(0, CTemperatureUnit::K(),CTemperatureUnit::K(), CTemperature::temperaturUnitConverter)
{ {
this->setUnitValue(this->unitValueToDouble()); // I have to recall, since virtual method overriding does not work in constructor // void
} }
/** /**
@@ -15,53 +15,62 @@ CTemperature::CTemperature(): CPhysicalQuantity(0, CTemperatureUnit::K(),CTemper
*/ */
CTemperature::CTemperature(const CPhysicalQuantity &temperature): CPhysicalQuantity(temperature) CTemperature::CTemperature(const CPhysicalQuantity &temperature): CPhysicalQuantity(temperature)
{ {
this->setUnitValue(this->unitValueToDouble()); // I have to recall, since virtual method overriding does not work in constructor // void
} }
/** /**
* Constructor * Constructor
*/ */
CTemperature::CTemperature(qint32 value, const CTemperatureUnit &unit) : CPhysicalQuantity(value, unit, CTemperatureUnit::K()) CTemperature::CTemperature(qint32 value, const CTemperatureUnit &unit) : CPhysicalQuantity(value, unit, CTemperatureUnit::K(), CTemperature::temperaturUnitConverter)
{ {
this->setUnitValue(value); // I have to recall, since virtual method overriding does not work in constructor // void
} }
/** /**
* Constructor * Constructor
*/ */
CTemperature::CTemperature(double value, const CTemperatureUnit &unit) : CPhysicalQuantity(value, unit, CTemperatureUnit::K()) CTemperature::CTemperature(double value, const CTemperatureUnit &unit) : CPhysicalQuantity(value, unit, CTemperatureUnit::K(), CTemperature::temperaturUnitConverter)
{ {
this->setUnitValue(value); // I have to recall, since virtual method overriding does not work in constructor // void
} }
/** /**
* Overriden specializedmethodfortemperture * Destructor
*/ */
double CTemperature::calculateValueInOtherUnit(const CMeasurementUnit &otherUnit) const CTemperature::~CTemperature()
{ {
if (this->getUnit()==otherUnit) return this->siBaseUnitValueToDouble(); // void
}
/**
* Specialized method for temperture
*/
double CTemperature::temperaturUnitConverter(const CPhysicalQuantity *quantity, const CMeasurementUnit &otherUnit)
{
CTemperature *me = (CTemperature*) quantity; // allow me access to protected
if (me->_pUnit == otherUnit) return me->siBaseUnitValueToDouble();
double siValue; double siValue;
// I always convert via SI Unit, other I would need too many conversions // I always convert via SI Unit, other I would need too many conversions
if(otherUnit == this->_conversionSiUnit) { if(otherUnit == me->_pConversionSiUnit) {
// here I expect a conversion to SI is required and not done yet // here I expect a conversion to SI is required and not done yet
if(this->_unit == CTemperatureUnit::C()) { if(me->_pUnit == CTemperatureUnit::C()) {
siValue = this->unitValueToDouble() + 273.15; siValue = quantity->unitValueToDouble() + 273.15;
} else if(this->_unit == CTemperatureUnit::F()) { } else if(me->_pUnit == CTemperatureUnit::F()) {
siValue = (this->unitValueToDouble() + 459.67) *5.0 / 9.0; siValue = (me->unitValueToDouble() + 459.67) *5.0 / 9.0;
} else{ } else{
// TODO: EXCEPTION // TODO: EXCEPTION
} }
} else { } else {
// here I expect the SI value is already set // here I expect the SI value is already set
siValue = this->siBaseUnitValueToDouble(); siValue = quantity->siBaseUnitValueToDouble();
} }
// from SI // from SI
if (otherUnit == this->_conversionSiUnit) return siValue; if (otherUnit == me->_pConversionSiUnit) return siValue;
if(otherUnit == CTemperatureUnit::C()) { if(otherUnit == CTemperatureUnit::C()) {
return siValue - 273.15; return siValue - 273.15;
} else if(this->_unit == CTemperatureUnit::F()) { } else if(me->_pUnit == CTemperatureUnit::F()) {
return (siValue * 9.0 / 5.0) - 459.67; return (siValue * 9.0 / 5.0) - 459.67;
} }
// TODO: Exception // TODO: Exception

View File

@@ -9,6 +9,13 @@ namespace BlackMisc {
* \author KWB * \author KWB
*/ */
class CTemperatureUnit : public CMeasurementUnit { class CTemperatureUnit : public CMeasurementUnit {
friend class CTemperature;
private:
/*!
* Downcast copy constructor, allows to implement methods in base class
* \param otherUnit
*/
CTemperatureUnit(const CMeasurementUnit &otherUnit) : CMeasurementUnit(otherUnit) {}
public: public:
/*! /*!
* Constructor * Constructor
@@ -24,26 +31,21 @@ public:
*/ */
CTemperatureUnit(const QString &name, const QString &unitName, bool isSIUnit, bool isSIBaseUnit, double conversionFactorToSI = 1.0, const CMeasurementPrefix &mulitplier = CMeasurementPrefix::One(), qint32 displayDigits = 2, double epsilon = 1E-9) : CTemperatureUnit(const QString &name, const QString &unitName, bool isSIUnit, bool isSIBaseUnit, double conversionFactorToSI = 1.0, const CMeasurementPrefix &mulitplier = CMeasurementPrefix::One(), qint32 displayDigits = 2, double epsilon = 1E-9) :
CMeasurementUnit(name, unitName, "temperature", isSIUnit, isSIBaseUnit, conversionFactorToSI, mulitplier, displayDigits, epsilon) {} CMeasurementUnit(name, unitName, "temperature", isSIUnit, isSIBaseUnit, conversionFactorToSI, mulitplier, displayDigits, epsilon) {}
/*!
* Downcast copy constructor, allows to implement methods in base class
* \param otherUnit
*/
CTemperatureUnit(const CMeasurementUnit &otherUnit) : CMeasurementUnit(otherUnit) {}
/*! /*!
* \brief Kelvin * \brief Kelvin
* \return * \return
*/ */
static CTemperatureUnit& K() { static CTemperatureUnit K("Kelvin", "K", true, true); return K;} static const CTemperatureUnit& K() { static CTemperatureUnit K("Kelvin", "K", true, true); return K;}
/*! /*!
* \brief Centigrade C * \brief Centigrade C
* \return * \return
*/ */
static CTemperatureUnit& C() { static CTemperatureUnit C("centigrade", "°C", false, false);return C;} static const CTemperatureUnit& C() { static CTemperatureUnit C("centigrade", "°C", false, false);return C;}
/*! /*!
* \brief Fahrenheit F * \brief Fahrenheit F
* \return * \return
*/ */
static CTemperatureUnit& F() { static CTemperatureUnit F("Fahrenheit", "°F", false, false, 5.0/9.0);return F;} static const CTemperatureUnit& F() { static CTemperatureUnit F("Fahrenheit", "°F", false, false, 5.0/9.0);return F;}
}; };
/*! /*!
@@ -52,14 +54,14 @@ public:
*/ */
class CTemperature : public CPhysicalQuantity class CTemperature : public CPhysicalQuantity
{ {
protected: private:
/*! /*!
* Specific method for temperature, a normal factor conversion is not sufficient. * \brief Convert into another temperature unit
* \param quantity
* \param otherUnit * \param otherUnit
* \return * \return
*/ */
virtual double calculateValueInOtherUnit(const CMeasurementUnit &otherUnit) const; static double temperaturUnitConverter(const CPhysicalQuantity *quantity, const CMeasurementUnit &otherUnit);
public: public:
/*! /*!
* \brief Default constructor * \brief Default constructor
@@ -81,16 +83,20 @@ public:
* \param unit * \param unit
*/ */
CTemperature(double value, const CTemperatureUnit &unit = CTemperatureUnit::K()); CTemperature(double value, const CTemperatureUnit &unit = CTemperatureUnit::K());
/*!
* \brief Destructor
*/
virtual ~CTemperature();
/*! /*!
* \brief Unit of the temperature * \brief Unit of the temperature
* \return * \return
*/ */
CTemperatureUnit getUnit() const { return this->_unit; } CTemperatureUnit getUnit() const { return this->_pUnit; }
/*! /*!
* \brief Conversion SI unit * \brief Conversion SI unit
* \return * \return
*/ */
CTemperatureUnit getConversionSiUnit() const { return this->_conversionSiUnit; } CTemperatureUnit getConversionSiUnit() const { return this->_pConversionSiUnit; }
}; };
} // namespace } // namespace