String-ifier for streams as preparation for the GeoClasses

This commit is contained in:
Klaus Basan
2013-04-12 19:21:50 +02:00
parent 42712a2baf
commit 5f678dfe28
19 changed files with 372 additions and 235 deletions

View File

@@ -2,19 +2,28 @@
using namespace BlackMisc::PhysicalQuantities; using namespace BlackMisc::PhysicalQuantities;
namespace BlackMiscTest { namespace BlackMiscTest
{
/** /**
* Running the quantities * Running the quantities
*/ */
int CSamplesPhysicalQuantities::samples() { int CSamplesPhysicalQuantities::samples()
{
// cases which must not work // cases which must not work
// CMeasurementUnit mu; //must not work // CMeasurementUnit mu; //must not work
// CLengthUnit du1(CAngleUnit::rad()); // CLengthUnit du1(CAngleUnit::rad());
CMeasurementPrefix pf1 = CMeasurementPrefix::h();
CMeasurementPrefix pf2 = CMeasurementPrefix::M();
qDebug() << pf1 << pf2 << 1.0 * pf1;
CLengthUnit lu1(CLengthUnit::cm()); CLengthUnit lu1(CLengthUnit::cm());
CLengthUnit lu2(CLengthUnit::ft()); CLengthUnit lu2(CLengthUnit::ft());
qDebug() << lu1 << lu2; QString lu1s = lu1;
QString lu2s = lu2;
qDebug() << lu1 << lu2 << lu1s << lu2s;
const CLength l1(5.0, CLengthUnit::ft()); // 5 ft const CLength l1(5.0, CLengthUnit::ft()); // 5 ft
CLength l2(1, CLengthUnit::NM()); // 1NM CLength l2(1, CLengthUnit::NM()); // 1NM
CLength l3(1, CLengthUnit::km()); CLength l3(1, CLengthUnit::km());

View File

@@ -40,7 +40,6 @@ namespace BlackCore
CVector3D & operator +=(const CVector3D &rhs); CVector3D & operator +=(const CVector3D &rhs);
CVector3D & operator -=(const CVector3D &rhs); CVector3D & operator -=(const CVector3D &rhs);
CVector3D & operator = (const CVector3D &rhs); CVector3D & operator = (const CVector3D &rhs);
CVector3D operator +(const CVector3D &rhs); CVector3D operator +(const CVector3D &rhs);
@@ -51,6 +50,7 @@ namespace BlackCore
//double crossProduct(qint32 ); //double crossProduct(qint32 );
CVector3D & operator *=(const CVector3D &rhs); CVector3D & operator *=(const CVector3D &rhs);
CVector3D operator *( const CVector3D &rhs); CVector3D operator *( const CVector3D &rhs);

View File

@@ -16,9 +16,9 @@ namespace Aviation
/* /*
* Own implementation for streaming * Own implementation for streaming
*/ */
QString CAltitude::stringForStreamingOperator() const QString CAltitude::stringForConverter() const
{ {
QString s = CLength::stringForStreamingOperator(); QString s = CLength::stringForConverter();
return s.append(this->m_msl ? " MSL" : " AGL"); return s.append(this->m_msl ? " MSL" : " AGL");
} }

View File

@@ -26,7 +26,7 @@ protected:
* \brief Specific stream operation for Altitude * \brief Specific stream operation for Altitude
* \return * \return
*/ */
virtual QString stringForStreamingOperator() const; virtual QString stringForConverter() const;
public: public:
/*! /*!

View File

@@ -16,9 +16,9 @@ namespace Aviation
/* /*
* Own implementation for streaming * Own implementation for streaming
*/ */
QString CHeading::stringForStreamingOperator() const QString CHeading::stringForConverter() const
{ {
QString s = CAngle::stringForStreamingOperator(); QString s = CAngle::stringForConverter();
return s.append(this->m_magnetic ? " magnetic" : " true"); return s.append(this->m_magnetic ? " magnetic" : " true");
} }

View File

@@ -25,7 +25,7 @@ protected:
* \brief Specific stream operation for heading * \brief Specific stream operation for heading
* \return * \return
*/ */
virtual QString stringForStreamingOperator() const; virtual QString stringForConverter() const;
public: public:
/*! /*!

View File

@@ -16,9 +16,9 @@ namespace Aviation
/* /*
* Own implementation for streaming * Own implementation for streaming
*/ */
QString CTrack::stringForStreamingOperator() const QString CTrack::stringForConverter() const
{ {
QString s = CAngle::stringForStreamingOperator(); QString s = CAngle::stringForConverter();
return s.append(this->m_magnetic ? " magnetic" : " true"); return s.append(this->m_magnetic ? " magnetic" : " true");
} }

View File

@@ -27,7 +27,7 @@ protected:
* \brief Specific stream operation for Track * \brief Specific stream operation for Track
* \return * \return
*/ */
virtual QString stringForStreamingOperator() const; virtual QString stringForConverter() const;
public: public:
/*! /*!

View File

@@ -0,0 +1,79 @@
#ifndef BLACKMISC_BASESTREAMSTRINGIFIER_H
#define BLACKMISC_BASESTREAMSTRINGIFIER_H
#include "blackmisc/debug.h"
#include <QString>
#include <QtGlobal>
#include <QDebug>
namespace BlackMisc
{
/*!
* \brief Provides "to QString" and stream operators
*/
template <class UsingClass> class CBaseStreamStringifier
{
/*!
* \brief Stream << overload to be used in debugging messages
* \param debug
* \param uc
* \return
*/
friend QDebug operator<<(QDebug debug, const UsingClass &uc)
{
const CBaseStreamStringifier &s = uc;
debug << s.stringForStreaming();
return debug;
}
/*!
* \brief Stream operator << for log messages
* \param log
* \param uc
* \return
*/
friend CLogMessage operator<<(CLogMessage log, const UsingClass &uc)
{
const CBaseStreamStringifier &s = uc;
log << s.stringForStreaming();
return log;
}
public:
/*!
* \brief Virtual destructor
*/
virtual ~CBaseStreamStringifier() {}
/*!
* \brief Cast as QString
*/
operator QString() const
{
return this->stringForConverter();
}
protected:
/*!
* \brief String for streaming operators
* \return
*/
virtual QString stringForStreaming() const
{
// simplest default implementation requires only one method
return this->stringForConverter();
}
/*!
* \brief String for converter
* \return
*/
virtual QString stringForConverter() const = 0;
};
} // namespace
#endif // guard

View File

@@ -4,6 +4,11 @@
// just a dummy header, namespace documentation will go here // just a dummy header, namespace documentation will go here
/*! \file */ /*! \file */
/*!
* \namespace BlackMisc
* \brief Base and utility classes available in all other projects.
*/
/*! /*!
* \namespace BlackMisc::Aviation * \namespace BlackMisc::Aviation
* \brief Aviation and Avionics classes such as CHeading or CTransponder . * \brief Aviation and Avionics classes such as CHeading or CTransponder .

View File

@@ -59,7 +59,8 @@ HEADERS += \
avionavsystem.h \ avionavsystem.h \
aviotransponder.h \ aviotransponder.h \
avioadfsystem.h \ avioadfsystem.h \
aviation.h aviation.h \
basestreamstringifier.h
SOURCES += \ SOURCES += \
logmessage.cpp \ logmessage.cpp \
@@ -70,7 +71,6 @@ SOURCES += \
context.cpp \ context.cpp \
config.cpp \ config.cpp \
config_manager.cpp \ config_manager.cpp \
serialize.cpp \
com_client.cpp \ com_client.cpp \
com_server.cpp \ com_server.cpp \
com_client_buffer.cpp \ com_client_buffer.cpp \

View File

@@ -10,10 +10,12 @@
class QTcpSocket; class QTcpSocket;
const qint32 Sync_Marker = 0x1ACFFC1D;
namespace BlackMisc namespace BlackMisc
{ {
const qint32 Sync_Marker = 0x1ACFFC1D;
//! IComHandler Interface. //! IComHandler Interface.
/*! /*!
This interface implements the basic class for every InterCommunikation This interface implements the basic class for every InterCommunikation

View File

@@ -6,10 +6,10 @@
#ifndef MESSAGE_H #ifndef MESSAGE_H
#define MESSAGE_H #define MESSAGE_H
#include "blackmisc/serialize.h"
#include <QtGlobal> #include <QtGlobal>
#include <QDataStream> #include <QDataStream>
#include <QTextStream> #include <QTextStream>
#include "blackmisc/serialize.h"
namespace BlackMisc namespace BlackMisc
{ {
@@ -29,7 +29,6 @@ namespace BlackMisc
protected: protected:
QString m_message_id; QString m_message_id;
}; };

View File

@@ -78,24 +78,6 @@ bool CMeasurementPrefix::operator <(const CMeasurementPrefix &otherMultiplier) c
return this->m_factor < otherMultiplier.m_factor; return this->m_factor < otherMultiplier.m_factor;
} }
/*
* Stream to debug
*/
QDebug operator<<(QDebug d, const CMeasurementPrefix &multiplier)
{
d << multiplier.m_name;
return d;
}
/*
* Log to debug
*/
CLogMessage operator<<(CLogMessage log, const CMeasurementPrefix &multiplier)
{
log << multiplier.m_name;
return log;
}
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
// --- Measurement unit -------------------------------------------------- // --- Measurement unit --------------------------------------------------
// ----------------------------------------------------------------------- // -----------------------------------------------------------------------
@@ -155,24 +137,6 @@ bool CMeasurementUnit::operator ==(const CMeasurementUnit &otherUnit) const
&& this->m_isSiUnit == otherUnit.m_isSiUnit; && this->m_isSiUnit == otherUnit.m_isSiUnit;
} }
/*
* Stream to debug
*/
QDebug operator <<(QDebug d, const CMeasurementUnit &unit)
{
d << unit.m_name;
return d;
}
/*
* Stream to log
*/
CLogMessage operator<<(CLogMessage log, const CMeasurementUnit &unit)
{
log << unit.m_name;
return log;
}
/* /*
* Unequal operator * Unequal operator
*/ */

View File

@@ -6,6 +6,7 @@
#ifndef BLACKMISC_PQBASE_H #ifndef BLACKMISC_PQBASE_H
#define BLACKMISC_PQBASE_H #define BLACKMISC_PQBASE_H
#include "blackmisc/basestreamstringifier.h"
#include "blackmisc/debug.h" #include "blackmisc/debug.h"
#include <QString> #include <QString>
#include <QtGlobal> #include <QtGlobal>
@@ -22,23 +23,8 @@ namespace PhysicalQuantities
* Use the static values such CMeasurementMultiplier::k() as to specify values. * Use the static values such CMeasurementMultiplier::k() as to specify values.
* \author KWB * \author KWB
*/ */
class CMeasurementPrefix class CMeasurementPrefix : public CBaseStreamStringifier<CMeasurementPrefix>
{ {
/*!
* \brief Stream << overload to be used in debugging messages
* \param d
* \param multiplier
* \return
*/
friend QDebug operator<<(QDebug d, const CMeasurementPrefix &multiplier);
/*!
* \brief Stream operator << for log messages
* \param log
* \param multiplier
* \return
*/
friend CLogMessage operator<<(CLogMessage log, const CMeasurementPrefix &multiplier);
private: private:
QString m_name; //!< name, e.g. "kilo" QString m_name; //!< name, e.g. "kilo"
QString m_prefix; //!< prefix, e.g. "k" for kilo QString m_prefix; //!< prefix, e.g. "k" for kilo
@@ -51,73 +37,89 @@ private:
* \param factor * \param factor
*/ */
CMeasurementPrefix(const QString &name, const QString &prefixName, double factor); CMeasurementPrefix(const QString &name, const QString &prefixName, double factor);
protected:
/*!
* \brief Name as stringification
* \return
*/
virtual QString stringForConverter() const
{
return this->m_name;
}
public: public:
/*! /*!
* \brief Copy constructor * \brief Copy constructor
* \param otherMultiplier * \param otherMultiplier
*/ */
CMeasurementPrefix(const CMeasurementPrefix &otherMultiplier); CMeasurementPrefix(const CMeasurementPrefix &otherMultiplier);
/*! /*!
* \brief Assigmnet operator = * \brief Assigmnet operator =
* \param otherMultiplier * \param otherMultiplier
* \return * \return
*/ */
CMeasurementPrefix &operator =(const CMeasurementPrefix &otherMultiplier); CMeasurementPrefix &operator =(const CMeasurementPrefix &otherMultiplier);
/*! /*!
* \brief Equal operator == * \brief Equal operator ==
* \param otherMultiplier * \param otherMultiplier
* \return * \return
*/ */
bool operator == (const CMeasurementPrefix &otherMultiplier) const; bool operator == (const CMeasurementPrefix &otherMultiplier) const;
/*! /*!
* \brief Unequal operator != * \brief Unequal operator !=
* \param otherMultiplier * \param otherMultiplier
* \return * \return
*/ */
bool operator != (const CMeasurementPrefix &otherMultiplier) const; bool operator != (const CMeasurementPrefix &otherMultiplier) const;
/*! /*!
* \brief Greater operator > * \brief Greater operator >
* \param otherMultiplier * \param otherMultiplier
* \return * \return
*/ */
bool operator > (const CMeasurementPrefix &otherMultiplier) const; bool operator > (const CMeasurementPrefix &otherMultiplier) const;
/*! /*!
* \brief Less operator < * \brief Less operator <
* \param otherMultiplier * \param otherMultiplier
* \return * \return
*/ */
bool operator < (const CMeasurementPrefix &otherMultiplier) const; bool operator < (const CMeasurementPrefix &otherMultiplier) const;
/*! /*!
* \brief Cast as double * \brief Cast as double
*/ */
operator double() const { operator double() const
{
return this->m_factor; return this->m_factor;
} }
/*!
* \brief Cast as QString
*/
operator QString() const {
return this->m_name;
}
/*! /*!
* \brief Factor, e.g.1000 for "kilo" * \brief Factor, e.g.1000 for "kilo"
* \return * \return
*/ */
double getFactor() const { double getFactor() const
{
return this->m_factor; return this->m_factor;
} }
/*! /*!
* \brief Name, e.g. "kilo" * \brief Name, e.g. "kilo"
* \return * \return
*/ */
QString getName() const { QString getName() const
{
return this->m_name; return this->m_name;
} }
/*! /*!
* \brief Prefix, e.g. "k" for "kilo" * \brief Prefix, e.g. "k" for "kilo"
* \return * \return
*/ */
QString getPrefix() const { QString getPrefix() const
{
return this->m_prefix; return this->m_prefix;
} }
@@ -129,7 +131,8 @@ public:
* \brief Unit "None" * \brief Unit "None"
* \return * \return
*/ */
static const CMeasurementPrefix &None() { static const CMeasurementPrefix &None()
{
static CMeasurementPrefix none("", "", 0.0); static CMeasurementPrefix none("", "", 0.0);
return none; return none;
} }
@@ -137,7 +140,8 @@ public:
* \brief Unit "One" * \brief Unit "One"
* \return * \return
*/ */
static const CMeasurementPrefix &One() { static const CMeasurementPrefix &One()
{
static CMeasurementPrefix one("one", "", 1.0); static CMeasurementPrefix one("one", "", 1.0);
return one; return one;
} }
@@ -145,7 +149,8 @@ public:
* \brief Unit "mega" * \brief Unit "mega"
* \return * \return
*/ */
static const CMeasurementPrefix &M() { static const CMeasurementPrefix &M()
{
static CMeasurementPrefix mega("mega", "M", 1E6); static CMeasurementPrefix mega("mega", "M", 1E6);
return mega; return mega;
} }
@@ -153,7 +158,8 @@ public:
* \brief Unit "kilo" * \brief Unit "kilo"
* \return * \return
*/ */
static const CMeasurementPrefix &k() { static const CMeasurementPrefix &k()
{
static CMeasurementPrefix kilo("kilo", "k", 1000.0); static CMeasurementPrefix kilo("kilo", "k", 1000.0);
return kilo; return kilo;
} }
@@ -161,7 +167,8 @@ public:
* \brief Unit "giga" * \brief Unit "giga"
* \return * \return
*/ */
static const CMeasurementPrefix &G() { static const CMeasurementPrefix &G()
{
static CMeasurementPrefix giga("giga", "G", 1E9); static CMeasurementPrefix giga("giga", "G", 1E9);
return giga; return giga;
} }
@@ -169,7 +176,8 @@ public:
* \brief Unit "hecto" * \brief Unit "hecto"
* \return * \return
*/ */
static const CMeasurementPrefix &h() { static const CMeasurementPrefix &h()
{
static CMeasurementPrefix hecto("hecto", "h", 100.0); static CMeasurementPrefix hecto("hecto", "h", 100.0);
return hecto; return hecto;
} }
@@ -177,7 +185,8 @@ public:
* \brief Unit "centi" * \brief Unit "centi"
* \return * \return
*/ */
static const CMeasurementPrefix &c() { static const CMeasurementPrefix &c()
{
static CMeasurementPrefix centi("centi", "c", 0.01); static CMeasurementPrefix centi("centi", "c", 0.01);
return centi; return centi;
} }
@@ -185,7 +194,8 @@ public:
* \brief Unit "milli" * \brief Unit "milli"
* \return * \return
*/ */
static const CMeasurementPrefix &m() { static const CMeasurementPrefix &m()
{
static CMeasurementPrefix milli("milli", "m", 1E-03); static CMeasurementPrefix milli("milli", "m", 1E-03);
return milli; return milli;
} }
@@ -199,28 +209,12 @@ public:
/*! /*!
* \brief Base class for all units, such as meter, hertz. * \brief Base class for all units, such as meter, hertz.
*/ */
class CMeasurementUnit class CMeasurementUnit: public CBaseStreamStringifier<CMeasurementUnit>
{ {
/*!
* \brief Stream << overload to be used in debugging messages
* \param d
* \param unit
* \return
*/
friend QDebug operator<<(QDebug d, const CMeasurementUnit &unit);
/*!
* \brief Stream operator << for log messages
* \param log
* \param unit
* \return
*/
friend CLogMessage operator<<(CLogMessage log, const CMeasurementUnit &unit);
protected: protected:
/*! /*!
* Points to a individual converter method * \brief Points to an individual converter method
* Conversion as perobject, as required for CAnglewith sexagesimal conversion
*/ */
typedef double(*UnitConverter)(const CMeasurementUnit &, double); typedef double(*UnitConverter)(const CMeasurementUnit &, double);
@@ -238,7 +232,6 @@ private:
UnitConverter m_fromSiConverter; //! allows an arbitrary conversion method as per object UnitConverter m_fromSiConverter; //! allows an arbitrary conversion method as per object
protected: protected:
/*! /*!
* Constructor by parameter * Constructor by parameter
* \param name * \param name
@@ -256,41 +249,67 @@ protected:
CMeasurementUnit(const QString &name, const QString &unitName, const QString &type, bool isSiUnit, bool isSiBaseUnit, double conversionFactorToSI = 1, CMeasurementUnit(const QString &name, const QString &unitName, const QString &type, bool isSiUnit, bool isSiBaseUnit, double conversionFactorToSI = 1,
const CMeasurementPrefix &multiplier = CMeasurementPrefix::None(), qint32 displayDigits = 2, const CMeasurementPrefix &multiplier = CMeasurementPrefix::None(), qint32 displayDigits = 2,
double epsilon = 1E-10, UnitConverter toSiConverter = nullptr, UnitConverter fromSiConverter = nullptr); double epsilon = 1E-10, UnitConverter toSiConverter = nullptr, UnitConverter fromSiConverter = nullptr);
/*! /*!
* \brief Copy constructor * \brief Copy constructor
* \param otherUnit * \param otherUnit
*/ */
CMeasurementUnit(const CMeasurementUnit &otherUnit); CMeasurementUnit(const CMeasurementUnit &otherUnit);
/*! /*!
* \brief Assignment operator = * \brief Assignment operator =
* \param otherUnit * \param otherUnit
* \return * \return
*/ */
CMeasurementUnit &operator =(const CMeasurementUnit &otherUnit); CMeasurementUnit &operator =(const CMeasurementUnit &otherUnit);
protected: protected:
/*!
* \brief String for streaming operators is full name
* \return
*/
virtual QString stringForStreaming() const
{
return this->m_name;
}
/*!
* \brief String for converter is unit
* \return
*/
virtual QString stringForConverter() const
{
return this->m_unitName;
}
/*! /*!
* \brief Conversion factor to SI conversion unit * \brief Conversion factor to SI conversion unit
* \return * \return
*/ */
double getConversionFactorToSI() const { double getConversionFactorToSI() const
{
return this->m_conversionFactorToSIConversionUnit; return this->m_conversionFactorToSIConversionUnit;
} }
/*! /*!
* Given value to conversion SI conversion unit (e.g. meter, hertz). * Given value to conversion SI conversion unit (e.g. meter, hertz).
* Standard implementaion is simply factor based. * Standard implementaion is simply factor based.
* \param value * \param value
* \return * \return
*/ */
virtual double conversionToSiConversionUnit(double value) const { virtual double conversionToSiConversionUnit(double value) const
{
return value * this->m_conversionFactorToSIConversionUnit; return value * this->m_conversionFactorToSIConversionUnit;
} }
/*! /*!
* \brief Value from SI conversion unit to this unit. * \brief Value from SI conversion unit to this unit.
* Standard implementation is simply factor based. * Standard implementation is simply factor based.
* \param value * \param value
* \return * \return
*/ */
virtual double conversionFromSiConversionUnit(double value) const { virtual double conversionFromSiConversionUnit(double value) const
{
return value / this->m_conversionFactorToSIConversionUnit; return value / this->m_conversionFactorToSIConversionUnit;
} }
@@ -301,72 +320,90 @@ public:
* \return * \return
*/ */
bool operator == (const CMeasurementUnit &otherUnit) const; bool operator == (const CMeasurementUnit &otherUnit) const;
/*! /*!
* \brief Unequal operator != * \brief Unequal operator !=
* \param otherUnit * \param otherUnit
* \return * \return
*/ */
bool operator != (const CMeasurementUnit &otherUnit) const; bool operator != (const CMeasurementUnit &otherUnit) const;
/*! /*!
* \brief Representing an SI unit? Examples: kilometer, meter, hertz * \brief Representing an SI unit? Examples: kilometer, meter, hertz
* \return * \return
*/ */
bool isSiUnit() const { bool isSiUnit() const
{
return this->m_isSiUnit; return this->m_isSiUnit;
} }
/*! /*!
* \brief Representing an base SI unit? Examples: second, meter * \brief Representing an base SI unit? Examples: second, meter
* \return * \return
*/ */
bool isSiBaseUnit() const { bool isSiBaseUnit() const
{
return this->m_isSiUnit; return this->m_isSiUnit;
} }
/*! /*!
* \brief Representing an SI base unit? Example: meter * \brief Representing an SI base unit? Example: meter
* \return * \return
*/ */
bool isUnprefixedSiUnit() const { bool isUnprefixedSiUnit() const
{
return this->m_isSiUnit && this->m_multiplier.getFactor() == 1; return this->m_isSiUnit && this->m_multiplier.getFactor() == 1;
} }
/*! /*!
* \brief Name such as "meter" * \brief Name such as "meter"
* \return * \return
*/ */
QString getName() const { QString getName() const
{
return this->m_name; return this->m_name;
} }
/*! /*!
* \brief Unit name such as "m" * \brief Unit name such as "m"
* \return * \return
*/ */
QString getUnitName() const { QString getUnitName() const
{
return this->m_unitName; return this->m_unitName;
} }
/*! /*!
* \brief Type such as "distance", "frequency" * \brief Type such as "distance", "frequency"
* \return * \return
*/ */
QString getType() const { QString getType() const
{
return this->m_type; return this->m_type;
} }
/*! /*!
* Given value to conversion SI conversion unit (e.g. meter, hertz). * Given value to conversion SI conversion unit (e.g. meter, hertz).
* Standard implementation is simply factor based. * Standard implementation is simply factor based.
* \param value * \param value
* \return * \return
*/ */
double convertToSiConversionUnit(double value) const { double convertToSiConversionUnit(double value) const
{
return (this->m_toSiConverter) ? this->m_toSiConverter((*this), value) : this->conversionToSiConversionUnit(value); return (this->m_toSiConverter) ? this->m_toSiConverter((*this), value) : this->conversionToSiConversionUnit(value);
} }
/*! /*!
* Value from SI conversion unit to this unit. * Value from SI conversion unit to this unit.
* Standard implementation is simply factor based. * Standard implementation is simply factor based.
* \param value * \param value
* \return * \return
*/ */
double convertFromSiConversionUnit(double value) const { double convertFromSiConversionUnit(double value) const
{
return (this->m_fromSiConverter) ? this->m_fromSiConverter((*this), value) : this->conversionFromSiConversionUnit(value); return (this->m_fromSiConverter) ? this->m_fromSiConverter((*this), value) : this->conversionFromSiConversionUnit(value);
} }
/*! /*!
* Rounded string utility method, virtual so units can have * Rounded string utility method, virtual so units can have
* specialized formatting * specialized formatting
@@ -375,6 +412,7 @@ public:
* \return * \return
*/ */
virtual QString toQStringRounded(double value, int digits = -1) const; virtual QString toQStringRounded(double value, int digits = -1) const;
/*! /*!
* \brief Rounded value * \brief Rounded value
* \param value * \param value
@@ -382,6 +420,7 @@ public:
* \return * \return
*/ */
double valueRounded(double value, int digits = -1) const; double valueRounded(double value, int digits = -1) const;
/*! /*!
* \brief Value rounded with unit, e.g. "5.00m", "30kHz" * \brief Value rounded with unit, e.g. "5.00m", "30kHz"
* \param value * \param value
@@ -389,27 +428,34 @@ public:
* \return * \return
*/ */
virtual QString valueRoundedWithUnit(double value, int digits = -1) const; virtual QString valueRoundedWithUnit(double value, int digits = -1) const;
/*! /*!
* \brief Threshold for rounding * \brief Threshold for rounding
* \return * \return
*/ */
double getEpsilon() const { double getEpsilon() const
{
return this->m_epsilon; return this->m_epsilon;
} }
/*! /*!
* \brief getDisplayDigits * \brief getDisplayDigits
* \return * \return
*/ */
qint32 getDisplayDigits() const { qint32 getDisplayDigits() const
{
return this->m_displayDigits; return this->m_displayDigits;
} }
/*! /*!
* \brief Multiplier such as "kilo" * \brief Multiplier such as "kilo"
* \return * \return
*/ */
CMeasurementPrefix getMultiplier() const { CMeasurementPrefix getMultiplier() const
{
return this->m_multiplier; return this->m_multiplier;
} }
/*! /*!
* \brief Factor to convert to given unit * \brief Factor to convert to given unit
* \param value * \param value
@@ -442,7 +488,8 @@ public:
* \brief Unit is not specified * \brief Unit is not specified
* \return * \return
*/ */
static CMeasurementUnit &None() { static CMeasurementUnit &None()
{
static CMeasurementUnit none("none", "", "", false, false, 0.0, CMeasurementPrefix::None(), 0, 0); static CMeasurementUnit none("none", "", "", false, false, 0.0, CMeasurementPrefix::None(), 0, 0);
return none; return none;
} }

View File

@@ -38,7 +38,7 @@ template <class MU, class PQ> CPhysicalQuantity<MU, PQ>::CPhysicalQuantity(const
// void // void
} }
/*! /*
* Destructor * Destructor
*/ */
template <class MU, class PQ> CPhysicalQuantity<MU, PQ>::~CPhysicalQuantity() template <class MU, class PQ> CPhysicalQuantity<MU, PQ>::~CPhysicalQuantity()

View File

@@ -21,35 +21,8 @@ namespace PhysicalQuantities
* \brief A physical quantity such as "5m", "20s", "1500ft/s" * \brief A physical quantity such as "5m", "20s", "1500ft/s"
* \author KWB * \author KWB
*/ */
template <class MU, class PQ> class CPhysicalQuantity template <class MU, class PQ> class CPhysicalQuantity : public BlackMisc::CBaseStreamStringifier<PQ>
{ {
/*!
* Stream operator for debugging
* \brief operator <<
* \param debug
* \param quantity
* \return
* \remarks Has to be in the header files to avoid template link errors
*/
friend QDebug operator<<(QDebug debug, const CPhysicalQuantity &quantity) {
QString v = quantity.stringForStreamingOperator();
debug << v;
return debug;
}
/*!
* Stream operator for log messages
* \brief operator <<
* \param log
* \param quantity
* \return
* \remarks Has to be in the header files to avoid template link errors
*/
friend CLogMessage operator<<(CLogMessage log, const CPhysicalQuantity &quantity) {
QString v = quantity.stringForStreamingOperator();
log << v;
return log;
}
private: private:
qint32 m_unitValueI; //!< value backed by integer, allows sole integer arithmetic qint32 m_unitValueI; //!< value backed by integer, allows sole integer arithmetic
@@ -60,13 +33,6 @@ private:
protected: protected:
MU m_unit; //!< unit MU m_unit; //!< unit
MU m_conversionSiUnit; //!< corresponding SI base unit MU m_conversionSiUnit; //!< corresponding SI base unit
/*!
* \brief String for streaming operators
* \return
*/
virtual QString stringForStreamingOperator() const {
return this->unitValueRoundedWithUnit(-1);
}
/*! /*!
* \brief Constructor with int * \brief Constructor with int
@@ -75,6 +41,7 @@ protected:
* \param siConversionUnit * \param siConversionUnit
*/ */
CPhysicalQuantity(qint32 baseValue, const MU &unit, const MU &siConversionUnit); CPhysicalQuantity(qint32 baseValue, const MU &unit, const MU &siConversionUnit);
/*! /*!
* \brief Constructor with double * \brief Constructor with double
* \param baseValue * \param baseValue
@@ -82,77 +49,104 @@ protected:
* \param siConversionUnit * \param siConversionUnit
*/ */
CPhysicalQuantity(double baseValue, const MU &unit, const MU &siConversionUnit); CPhysicalQuantity(double baseValue, const MU &unit, const MU &siConversionUnit);
/*!
* \brief String for converter and streaming
* \return
*/
virtual QString stringForConverter() const
{
return this->unitValueRoundedWithUnit(-1);
}
/*! /*!
* \brief Init by integer * \brief Init by integer
* \param baseValue * \param baseValue
*/ */
void setUnitValue(qint32 baseValue); void setUnitValue(qint32 baseValue);
/*! /*!
* \brief Init by double * \brief Init by double
* \param baseValue * \param baseValue
*/ */
void setUnitValue(double baseValue); void setUnitValue(double baseValue);
/*! /*!
* \brief Set the SI value * \brief Set the SI value
*/ */
void setConversionSiUnitValue(); void setConversionSiUnitValue();
public: public:
/*! /*!
* \brief Copy constructor * \brief Copy constructor
* \param otherQuantity * \param otherQuantity
*/ */
CPhysicalQuantity(const CPhysicalQuantity &otherQuantity); CPhysicalQuantity(const CPhysicalQuantity &otherQuantity);
/*! /*!
* \brief Virtual destructor * \brief Virtual destructor
*/ */
virtual ~CPhysicalQuantity(); virtual ~CPhysicalQuantity();
/*! /*!
* \brief Unit of the distance * \brief Unit of the distance
* \return * \return
*/ */
MU getUnit() const { MU getUnit() const
{
return this->m_unit; return this->m_unit;
} }
/*! /*!
* \brief Conversion SI unit * \brief Conversion SI unit
* \return * \return
*/ */
MU getConversionSiUnit() const { MU getConversionSiUnit() const
{
return this->m_conversionSiUnit; return this->m_conversionSiUnit;
} }
/*! /*!
* \brief Switch unit, e.g. feet meter * \brief Switch unit, e.g. feet meter
* \param newUnit * \param newUnit
* \return * \return
*/ */
bool switchUnit(const MU &newUnit); bool switchUnit(const MU &newUnit);
/*! /*!
* \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 { bool isSiBaseUnit() const
{
return this->m_unit.isSiBaseUnit(); return this->m_unit.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 { bool isSiUnit() const
{
return this->m_unit.isSiUnit(); return this->m_unit.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 { bool isUnprefixedSiUnit() const
{
return this->m_unit.isUnprefixedSiUnit(); return this->m_unit.isUnprefixedSiUnit();
} }
/*! /*!
* \brief Value in given unit * \brief Value in given unit
* \param unit * \param unit
* @return * @return
*/ */
double value(const MU &unit) const; double value(const MU &unit) const;
/*! /*!
* \brief Rounded value in unit * \brief Rounded value in unit
* \param unit * \param unit
@@ -160,6 +154,7 @@ public:
* @return * @return
*/ */
double valueRounded(const MU &unit, int digits = -1) const; double valueRounded(const MU &unit, int digits = -1) const;
/*! /*!
* \brief Value to QString with unit, e.g. "5.00m" * \brief Value to QString with unit, e.g. "5.00m"
* \param unit * \param unit
@@ -167,186 +162,223 @@ public:
* @return * @return
*/ */
QString valueRoundedWithUnit(const MU &unit, int digits = -1) const; QString valueRoundedWithUnit(const MU &unit, int digits = -1) const;
/*! /*!
* \brief Value a int * \brief Value a int
* @return * @return
*/ */
qint32 unitValueToInteger() const { qint32 unitValueToInteger() const
{
return this->m_unitValueI; return this->m_unitValueI;
} }
/*! /*!
* \brief Value a double * \brief Value a double
* @return * @return
*/ */
double unitValueToDouble() const { double unitValueToDouble() const
{
return this->m_unitValueD; return this->m_unitValueD;
} }
/*! /*!
* \brief Value to QString with unit, e.g. "5.00m" * \brief Value to QString with unit, e.g. "5.00m"
* \param digits * \param digits
* @return * @return
*/ */
QString unitValueRoundedWithUnit(int digits = -1) const; QString unitValueRoundedWithUnit(int digits = -1) const;
/*! /*!
* \brief SI value to integer * \brief SI value to integer
* @return * @return
*/ */
qint32 siBaseUnitValueToInteger() const { qint32 siBaseUnitValueToInteger() const
{
return CMeasurementUnit::round(this->m_convertedSiUnitValueD, 0); return CMeasurementUnit::round(this->m_convertedSiUnitValueD, 0);
} }
/*! /*!
* \brief SI value to double * \brief SI value to double
* @return * @return
*/ */
double siBaseUnitValueToDouble() const { double siBaseUnitValueToDouble() const
{
return this->m_convertedSiUnitValueD; return this->m_convertedSiUnitValueD;
} }
/*! /*!
* \brief Rounded value by n digits * \brief Rounded value by n digits
* \param digits * \param digits
* @return * @return
*/ */
double unitValueToDoubleRounded(int digits = -1) const; double unitValueToDoubleRounded(int digits = -1) const;
/*! /*!
* \brief Rounded value by n digits * \brief Rounded value by n digits
* \param digits if no value is provided, unit rounding is taken * \param digits if no value is provided, unit rounding is taken
* @return * @return
*/ */
QString unitValueToQStringRounded(int digits = -1) const; QString unitValueToQStringRounded(int digits = -1) const;
/*! /*!
* \brief SI value as double * \brief SI value as double
* \return * \return
*/ */
double convertedSiValueToDouble() const { double convertedSiValueToDouble() const
{
return this->m_convertedSiUnitValueD; return this->m_convertedSiUnitValueD;
} }
/*! /*!
* \brief SI value as integer * \brief SI value as integer
* \return * \return
*/ */
qint32 convertedSiValueToInteger() const { qint32 convertedSiValueToInteger() const
{
return static_cast<qint32>(CMeasurementUnit::round(this->m_convertedSiUnitValueD, 0)); return static_cast<qint32>(CMeasurementUnit::round(this->m_convertedSiUnitValueD, 0));
} }
/*! /*!
* \brief Rounded SI value by n digits * \brief Rounded SI value by n digits
* \param digits * \param digits
* @return * @return
*/ */
double convertedSiValueToDoubleRounded(int digits = -1) const; double convertedSiValueToDoubleRounded(int digits = -1) const;
/*! /*!
* \brief Rounded value by n digits * \brief Rounded value by n digits
* \param digits if no value is provided, unit rounding is taken * \param digits if no value is provided, unit rounding is taken
* @return * @return
*/ */
QString convertedSiValueToQStringRounded(int digits = -1) const; QString convertedSiValueToQStringRounded(int digits = -1) const;
/*! /*!
* \brief SI Base unit value rounded * \brief SI Base unit value rounded
* \param digits * \param digits
* @return * @return
*/ */
QString convertedSiValueRoundedWithUnit(int digits = -1) const; QString convertedSiValueRoundedWithUnit(int digits = -1) const;
/*! /*!
* \brief Add to the unit value. * \brief Add to the unit value.
* \remarks Since overloading the + operator with double did lead to unintended conversions, as explicit method * \remarks Since overloading the + operator with double did lead to unintended conversions, as explicit method
* \param value * \param value
*/ */
void addUnitValue(double value); void addUnitValue(double value);
/*! /*!
* \brief Substratc to the unit value. * \brief Substratc to the unit value.
* \remarks Since overloading the - operator with double did lead to unintended conversions, as explicit method * \remarks Since overloading the - operator with double did lead to unintended conversions, as explicit method
* \param value * \param value
*/ */
void substractUnitValue(double value); void substractUnitValue(double value);
/*! /*!
* \brief Cast as QString * \brief Cast as QString
*/ */
operator QString() const { operator QString() const
{
return this->unitValueRoundedWithUnit(); return this->unitValueRoundedWithUnit();
} }
/*! /*!
* \brief Multiply operator *= * \brief Multiply operator *=
* \param multiply * \param multiply
* \return * \return
*/ */
CPhysicalQuantity &operator *=(double multiply); CPhysicalQuantity &operator *=(double multiply);
/*! /*!
* \brief Divide operator /= * \brief Divide operator /=
* \param divide * \param divide
* @return * @return
*/ */
CPhysicalQuantity &operator /=(double divide); CPhysicalQuantity &operator /=(double divide);
/*! /*!
* \brief Operator * * \brief Operator *
* \param multiply * \param multiply
* @return * @return
*/ */
PQ operator *(double multiply) const; PQ operator *(double multiply) const;
/*! /*!
* \brief Operator / * \brief Operator /
* \param divide * \param divide
* @return * @return
*/ */
PQ operator /(double divide) const; PQ operator /(double divide) const;
/*! /*!
* \brief Equal operator == * \brief Equal operator ==
* \param otherQuantity * \param otherQuantity
* @return * @return
*/ */
bool operator==(const CPhysicalQuantity &otherQuantity) const; bool operator==(const CPhysicalQuantity &otherQuantity) const;
/*! /*!
* \brief Not equal operator != * \brief Not equal operator !=
* \param otherQuantity * \param otherQuantity
* @return * @return
*/ */
bool operator!=(const CPhysicalQuantity &otherQuantity) const; bool operator!=(const CPhysicalQuantity &otherQuantity) const;
/*! /*!
* \brief Plus operator += * \brief Plus operator +=
* \param otherQuantity * \param otherQuantity
* @return * @return
*/ */
CPhysicalQuantity &operator +=(const CPhysicalQuantity &otherQuantity); CPhysicalQuantity &operator +=(const CPhysicalQuantity &otherQuantity);
/*! /*!
* \brief Minus operator-= * \brief Minus operator-=
* \param otherQuantity * \param otherQuantity
* @return * @return
*/ */
CPhysicalQuantity &operator -=(const CPhysicalQuantity &otherQuantity); CPhysicalQuantity &operator -=(const CPhysicalQuantity &otherQuantity);
/*! /*!
* \brief Greater operator > * \brief Greater operator >
* \param otherQuantity * \param otherQuantity
* @return * @return
*/ */
bool operator >(const CPhysicalQuantity &otherQuantity) const; bool operator >(const CPhysicalQuantity &otherQuantity) const;
/*! /*!
* \brief Less operator < * \brief Less operator <
* \param otherQuantity * \param otherQuantity
* @return * @return
*/ */
bool operator <(const CPhysicalQuantity &otherQuantity) const; bool operator <(const CPhysicalQuantity &otherQuantity) const;
/*! /*!
* \brief Less equal operator <= * \brief Less equal operator <=
* \param otherQuantity * \param otherQuantity
* @return * @return
*/ */
bool operator <=(const CPhysicalQuantity &otherQuantity) const; bool operator <=(const CPhysicalQuantity &otherQuantity) const;
/*! /*!
* \brief Greater equal operator >= * \brief Greater equal operator >=
* \param otherQuantity * \param otherQuantity
* @return * @return
*/ */
bool operator >=(const CPhysicalQuantity &otherQuantity) const; bool operator >=(const CPhysicalQuantity &otherQuantity) const;
/*! /*!
* \brief Assignment operator = * \brief Assignment operator =
* \param otherQuantity * \param otherQuantity
* @return * @return
*/ */
CPhysicalQuantity &operator =(const CPhysicalQuantity &otherQuantity); CPhysicalQuantity &operator =(const CPhysicalQuantity &otherQuantity);
/*! /*!
* \brief Plus operator + * \brief Plus operator +
* \param otherQuantity * \param otherQuantity
* @return * @return
*/ */
PQ operator +(const PQ &otherQuantity) const; PQ operator +(const PQ &otherQuantity) const;
/*! /*!
* \brief Minus operator - * \brief Minus operator -
* \param otherQuantity * \param otherQuantity

View File

@@ -1,5 +0,0 @@
#include "blackmisc/serialize.h"
ISerialize::ISerialize()
{
}

View File

@@ -5,17 +5,22 @@
#ifndef SERIALIZE_H #ifndef SERIALIZE_H
#define SERIALIZE_H #define SERIALIZE_H
#include <QDataStream>
class QDataStream; namespace BlackMisc
{
/*!
* \brief Serialize interface
*/
class ISerialize class ISerialize
{ {
public: public:
ISerialize(); ISerialize() {}
virtual ~ISerialize() {}; virtual ~ISerialize() {}
virtual QDataStream &operator<< (QDataStream &in) = 0; virtual QDataStream &operator<< (QDataStream &in) = 0;
virtual QDataStream &operator>> (QDataStream &out) const = 0; virtual QDataStream &operator>> (QDataStream &out) const = 0;
}; };
}
#endif // SERIALIZE_H #endif // SERIALIZE_H