diff --git a/src/blackmisc/coordinatened.h b/src/blackmisc/coordinatened.h index 5de3dba86..42ecdfa25 100644 --- a/src/blackmisc/coordinatened.h +++ b/src/blackmisc/coordinatened.h @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 VATSIM Community / authors +/* Copyright (C) 2013 VATSIM Community / contributors * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -58,6 +58,14 @@ public: */ CCoordinateNed(const CCoordinateGeodetic &referencePosition, double north, double east, double down) : CVector3DBase(north, east, down), m_referencePosition(referencePosition), m_hasReferencePosition(true) {} + /*! + * \brief Constructor by values + * \param north + * \param east + * \param down + */ + CCoordinateNed(double north, double east, double down) : CVector3DBase(north, east, down), m_referencePosition(), m_hasReferencePosition(false) {} + /*! * \brief Copy constructor * \param otherNed diff --git a/src/blackmisc/geolatlonbase.cpp b/src/blackmisc/geolatlonbase.cpp deleted file mode 100644 index 74d646c7d..000000000 --- a/src/blackmisc/geolatlonbase.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include "geolatitude.h" - -namespace BlackMisc -{ -namespace Geo -{ - - -// see here for the reason of thess forward instantiations -// http://www.parashift.com/c++-faq/separate-template-class-defn-from-decl.html -// template class CGeoLatLonBase; - -} - -} diff --git a/src/blackmisc/log.cpp b/src/blackmisc/log.cpp index 967bd86f9..146eb9ead 100644 --- a/src/blackmisc/log.cpp +++ b/src/blackmisc/log.cpp @@ -27,199 +27,200 @@ namespace BlackMisc { - CLog::CLog(IContext &context, TLogType logType) : m_context(context), m_logType(logType) +CLog::CLog(IContext &context, TLogType logType) : m_context(context), m_logType(logType) +{ +} + +CLog::~CLog(void) +{ +} + +void CLog::printWithNewLine(QString &message) +{ + //! If we have no displays, we have nothing to do. + if (hasNoDisplays()) { + return; } - CLog::~CLog(void) + QChar newLine = '\n'; + + //! We should print the message with a new line. So check + //! if one is already there. If not append it. + if (!message.endsWith(newLine, Qt::CaseInsensitive)) + message.append(newLine); + + printString(message); +} + +void CLog::print(QString &message) +{ + //! If we have no displays, we have nothing to do. + if (hasNoDisplays()) { + return; } - void CLog::printWithNewLine(QString &message) + printString(message); +} + +void CLog::printString(QString &message) +{ + QString logDisplay; + + //! Just in case, lets set the default name + m_context.setDefaultApplicationName(); + + //! If the current line is empty, then put some information as + //! the prefix. + //! Be aware: This information must be set by the \sa setLogInformation() + //! before. + if (m_logLine.isEmpty()) { - //! If we have no displays, we have nothing to do. - if (hasNoDisplays()) + m_logInformation.m_dateTime = QDateTime::currentDateTime(); + m_logInformation.m_logType = m_logType; + m_logInformation.m_applicationName = m_context.getApplicationName(); + m_logInformation.m_threadId = 0; //getThreadId(); + m_logInformation.m_sourceFile = m_sourceFile; + m_logInformation.m_line = m_line; + m_logInformation.m_methodName = m_methodName; + + m_logLine = message; + } + else + { + m_logLine += message; + } + + //! If this is not the end of the line, we are done for now. + if (! message.contains('\n')) + { + return; + } + + for (TLogDisplayList::iterator it = m_logDisplays.begin(); it != m_logDisplays.end(); ++it) + { + (*it)->print(m_logInformation, m_logLine); + } + + //! Reset everything for the next line + m_logLine.clear(); + resetLogInformation(); +} + +void CLog::attachDisplay(ILogDisplay *display) +{ + //! Display must be a valid pointer + if (display == NULL) + { + printf("Trying to add a NULL pointer\n"); + return; + } + + //! Check if it is already attached + if (!m_logDisplays.contains(display)) + { + m_logDisplays.push_back(display); + } + else + { + bWarning(m_context) << "Couldn't attach the display - already in the list!"; + } +} + +ILogDisplay *CLog::getDisplay(const QString &displayName) +{ + //! Must be a valid name + if (displayName.isEmpty()) + { + bWarning(m_context) << "Cannot return a display with empty name!"; + return NULL; + } + + TLogDisplayList::const_iterator it; + + //! Loop through the list and find the candidate + for (it = m_logDisplays.constBegin(); it != m_logDisplays.constEnd(); ++it) + { + //! Does it have the desired name? + if ((*it)->DisplayName == displayName) { - return; + return *it; } + } + return NULL; +} - QChar newLine = '\n'; - - //! We should print the message with a new line. So check - //! if one is already there. If not append it. - if (!message.endsWith(newLine, Qt::CaseInsensitive)) - message.append(newLine); - - printString(message); +void CLog::dettachDisplay(ILogDisplay *logDisplay) +{ + //! Must be a valid pointer + if (logDisplay == NULL) + { + bWarning(m_context) << "Cannot remove a NULL displayer!"; + return; } - void CLog::print(QString &message) - { - //! If we have no displays, we have nothing to do. - if (hasNoDisplays()) - { - return; - } + //! We should have only one, but just in case. + m_logDisplays.removeAll(logDisplay); - printString(message); +} + +void CLog::dettachDisplay(const QString &displayName) +{ + //! Must be a valid name + if (displayName.isEmpty()) + { + bWarning(m_context) << "Cannot remove displayer with empty name!"; + return; } - void CLog::printString(QString &message) + TLogDisplayList::iterator it; + + for (it = m_logDisplays.begin(); it != m_logDisplays.end();) { - QString logDisplay; - - //! Just in case, lets set the default name - m_context.setDefaultApplicationName(); - - //! If the current line is empty, then put some information as - //! the prefix. - //! Be aware: This information must be set by the \sa setLogInformation() - //! before. - if (m_logLine.isEmpty()) + if ((*it)->DisplayName == displayName) { - m_logInformation.m_dateTime = QDateTime::currentDateTime(); - m_logInformation.m_logType = m_logType; - m_logInformation.m_applicationName = m_context.getApplicationName(); - m_logInformation.m_threadId = 0; //getThreadId(); - m_logInformation.m_sourceFile = m_sourceFile; - m_logInformation.m_line = m_line; - m_logInformation.m_methodName = m_methodName; - - m_logLine = message; + it = m_logDisplays.erase(it); } else { - m_logLine += message; + ++it; } - - //! If this is not the end of the line, we are done for now. - if (! message.contains('\n')) - { - return; - } - - for (TLogDisplayList::iterator it = m_logDisplays.begin(); it != m_logDisplays.end(); ++it) - { - (*it)->print(m_logInformation, m_logLine); - } - - //! Reset everything for the next line - m_logLine.clear(); - resetLogInformation(); } +} - void CLog::attachDisplay(ILogDisplay *display) +bool CLog::isAttached(ILogDisplay *logDisplay) const +{ + return m_logDisplays.contains(logDisplay); +} + +void CLog::setLogInformation(int line, const char *sourceFile, const char *methodName) +{ + //! We have to make sure, we at least one display. + if (!hasNoDisplays()) { - //! Display must be a valid pointer - if (display == NULL) - { - printf("Trying to add a NULL pointer\n"); - return; - } - - //! Check if it is already attached - if (!m_logDisplays.contains(display)) - { - m_logDisplays.push_back(display); - } - else - { - bWarning(m_context) << "Couldn't attach the display - already in the list!"; - } + m_mutex.lock(); + m_posSet++; + m_sourceFile = sourceFile; + m_line = line; + m_methodName = methodName; } +} - ILogDisplay *CLog::getDisplay(const QString &displayName) +void CLog::resetLogInformation() +{ + //! It should be impossible that this gets called, withoud + //! having a attached display. + Q_ASSERT(!hasNoDisplays()); + + if (m_posSet > 0) { - //! Must be a valid name - if (displayName.isEmpty()) - { - bWarning(m_context) << "Cannot return a display with empty name!"; - return NULL; - } - - TLogDisplayList::const_iterator it; - - //! Loop through the list and find the candidate - for (it = m_logDisplays.constBegin(); it != m_logDisplays.constEnd(); ++it) - { - //! Does it have the desired name? - if ((*it)->DisplayName == displayName) - { - return *it; - } - } - } - - void CLog::dettachDisplay(ILogDisplay *logDisplay) - { - //! Must be a valid pointer - if (logDisplay == NULL) - { - bWarning(m_context) << "Cannot remove a NULL displayer!"; - return; - } - - //! We should have only one, but just in case. - m_logDisplays.removeAll(logDisplay); - - } - - void CLog::dettachDisplay(const QString &displayName) - { - //! Must be a valid name - if (displayName.isEmpty()) - { - bWarning(m_context) << "Cannot remove displayer with empty name!"; - return; - } - - TLogDisplayList::iterator it; - - for (it = m_logDisplays.begin(); it != m_logDisplays.end();) - { - if ((*it)->DisplayName == displayName) - { - it = m_logDisplays.erase(it); - } - else - { - ++it; - } - } - } - - bool CLog::isAttached(ILogDisplay *logDisplay) const - { - return m_logDisplays.contains(logDisplay); - } - - void CLog::setLogInformation(int line, const char *sourceFile, const char *methodName) - { - //! We have to make sure, we at least one display. - if (!hasNoDisplays()) - { - m_mutex.lock(); - m_posSet++; - m_sourceFile = sourceFile; - m_line = line; - m_methodName = methodName; - } - } - - void CLog::resetLogInformation() - { - //! It should be impossible that this gets called, withoud - //! having a attached display. - Q_ASSERT(!hasNoDisplays()); - - if (m_posSet > 0) - { - m_sourceFile = NULL; - m_line = -1; - m_methodName = NULL; - m_posSet--; - m_mutex.unlock(); - } + m_sourceFile = NULL; + m_line = -1; + m_methodName = NULL; + m_posSet--; + m_mutex.unlock(); } +} } // namespace BlackMisc diff --git a/src/blackmisc/mathmatrixbase.h b/src/blackmisc/mathmatrixbase.h index 8fd314ba3..2841a3385 100644 --- a/src/blackmisc/mathmatrixbase.h +++ b/src/blackmisc/mathmatrixbase.h @@ -31,6 +31,15 @@ protected: */ QString stringForConverter() const; + /*! + * \brief Clone as conrete implementation + * \return + */ + ImplMatrix clone() const + { + return static_cast(*this); + } + public: /*! * \brief Default constructor @@ -109,8 +118,7 @@ public: */ ImplMatrix operator *(double factor) const { - ImplMatrix m; - m += (*this); + ImplMatrix m = this->clone(); m *= factor; return m; } @@ -151,8 +159,7 @@ public: */ ImplMatrix operator /(double factor) const { - ImplMatrix m(0); - m += (*this); + ImplMatrix m = this->clone(); m /= factor; return m; } @@ -175,8 +182,7 @@ public: */ ImplMatrix operator +(const ImplMatrix &otherMatrix) const { - ImplMatrix m(0); - m += (*this); + ImplMatrix m = this->clone(); m += otherMatrix; return m; } @@ -199,8 +205,7 @@ public: */ ImplMatrix operator -(const ImplMatrix &otherMatrix) const { - ImplMatrix m(0); - m += (*this); + ImplMatrix m = this->clone(); m -= otherMatrix; return m; } @@ -220,9 +225,7 @@ public: */ bool isIdentityEpsilon() const { - ImplMatrix m(0.0); - m += (*this); - m.round(); + ImplMatrix m = this->clone().round(); return m.isIdentity(); } @@ -257,9 +260,7 @@ public: */ bool isZeroEpsilon() const { - ImplMatrix m(0.0); - m += (*this); - m.round(); + ImplMatrix m = this->clone().round(); return m.isZero(); } @@ -277,6 +278,7 @@ public: /*! * \brief Round all values + * \return */ ImplMatrix &round(); diff --git a/src/blackmisc/mathvector3dbase.cpp b/src/blackmisc/mathvector3dbase.cpp index 0dc6992a1..7d8c83bc0 100644 --- a/src/blackmisc/mathvector3dbase.cpp +++ b/src/blackmisc/mathvector3dbase.cpp @@ -16,7 +16,7 @@ namespace Math /* * Convert to string */ -template QString CVector3DBase::stringForConverter() const +template QString CVector3DBase::stringForConverter() const { QString s = ("{%1, %2, %3}"); s = s.arg(QString::number(this->m_i, 'f')). @@ -28,7 +28,7 @@ template QString CVector3DBase::stringForConverter( /* * Vector to zero */ -template void CVector3DBase::setZero() +template void CVector3DBase::setZero() { this->fill(0.0); } @@ -36,7 +36,7 @@ template void CVector3DBase::setZero() /* * Vector to zero */ -template void CVector3DBase::fill(double value) +template void CVector3DBase::fill(double value) { this->m_i = value; this->m_j = value; @@ -46,7 +46,7 @@ template void CVector3DBase::fill(double value) /* * Element */ -template double CVector3DBase::getElement(size_t row) const +template double CVector3DBase::getElement(size_t row) const { double d; switch (row) @@ -71,7 +71,7 @@ template double CVector3DBase::getElement(size_t ro /* * Set given element */ -template void CVector3DBase::setElement(size_t row, double value) +template void CVector3DBase::setElement(size_t row, double value) { switch (row) { @@ -94,9 +94,9 @@ template void CVector3DBase::setElement(size_t row, /* * Cross product */ -template ImplClass CVector3DBase::crossProduct(const ImplClass &otherVector) const +template ImplVector CVector3DBase::crossProduct(const ImplVector &otherVector) const { - ImplClass v(otherVector); + ImplVector v(otherVector); v.m_i = this->m_j * otherVector.m_k - this->m_k * otherVector.m_j; v.m_j = this->m_k * otherVector.m_i - this->m_i * otherVector.m_k; v.m_k = this->m_i * otherVector.m_j - this->m_j * otherVector.m_i; @@ -106,7 +106,7 @@ template ImplClass CVector3DBase::crossProduct(cons /* * Cross product */ -template double CVector3DBase::dotProduct(const ImplClass &otherVector) const +template double CVector3DBase::dotProduct(const ImplVector &otherVector) const { return this->m_i * otherVector.m_i + this->m_j * otherVector.m_j + this->m_k * otherVector.m_k; } @@ -115,7 +115,7 @@ template double CVector3DBase::dotProduct(const Imp /* * Multiply with matrix */ -template void CVector3DBase::matrixMultiplication(const CMatrix3x3 &matrix) +template void CVector3DBase::matrixMultiplication(const CMatrix3x3 &matrix) { CMatrix3x1 m = matrix * (this->toMatrix3x1()); this->m_i = m(0, 0); @@ -126,7 +126,7 @@ template void CVector3DBase::matrixMultiplication(c /* * Convert to matrix */ -template CMatrix3x1 CVector3DBase::toMatrix3x1() const +template CMatrix3x1 CVector3DBase::toMatrix3x1() const { return CMatrix3x1(this->m_i, this->m_j, this->m_k); } diff --git a/src/blackmisc/mathvector3dbase.h b/src/blackmisc/mathvector3dbase.h index 1dfd1f6ba..8183c325e 100644 --- a/src/blackmisc/mathvector3dbase.h +++ b/src/blackmisc/mathvector3dbase.h @@ -21,7 +21,7 @@ class CMatrix3x1; // forward declaration /*! * \brief 3D vector base (x, y, z) */ -template class CVector3DBase : public CBaseStreamStringifier +template class CVector3DBase : public CBaseStreamStringifier { protected: @@ -61,6 +61,16 @@ protected: */ virtual QString stringForConverter() const; + /*! + * \brief Clone as concrete implementation + * \return + */ + ImplVector clone() const + { + return static_cast(*this); + } + + public: // getter and setters are implemented in the derived classes @@ -90,7 +100,7 @@ public: */ bool isZeroEpsilon() const { - ImplClass v; + ImplVector v; v += (*this); v.round(); return v.isZero(); @@ -186,10 +196,9 @@ public: * \param otherVector * \return */ - ImplClass operator +(const ImplClass &otherVector) const + ImplVector operator +(const ImplVector &otherVector) const { - ImplClass v; - v += (*this); + ImplVector v = this->clone(); v += otherVector; return v; } @@ -212,10 +221,9 @@ public: * \param otherVector * \return */ - ImplClass operator -(const ImplClass &otherVector) const + ImplVector operator -(const ImplVector &otherVector) const { - ImplClass v; - v += (*this); + ImplVector v = this->clone(); v -= otherVector; return v; } @@ -238,10 +246,9 @@ public: * \param otherVector * \return */ - ImplClass operator *(const ImplClass &otherVector) const + ImplVector operator *(const ImplVector &otherVector) const { - ImplClass v; - v += (*this); + ImplVector v = this->clone(); v *= otherVector; return v; } @@ -264,10 +271,9 @@ public: * \param factor * \return */ - ImplClass operator *(double factor) const + ImplVector operator *(double factor) const { - ImplClass v; - v += (*this); + ImplVector v = this->clone(); v *= factor; return v; } @@ -278,7 +284,7 @@ public: * \param otherVector * \return */ - friend ImplClass operator *(double factor, const ImplClass &otherVector) + friend ImplVector operator *(double factor, const ImplVector &otherVector) { return otherVector * factor; } @@ -301,10 +307,9 @@ public: * \param divisor * \return */ - ImplClass operator /(double divisor) const + ImplVector operator /(double divisor) const { - ImplClass v; - v += (*this); + ImplVector v = this->clone(); v /= divisor; return v; } @@ -327,10 +332,9 @@ public: * \param otherVector * \return */ - ImplClass operator /(const ImplClass &otherVector) const + ImplVector operator /(const ImplVector &otherVector) const { - ImplClass v; - v += (*this); + ImplVector v = this->clone(); v /= otherVector; return v; } @@ -340,14 +344,14 @@ public: * \param otherVector * \return */ - double dotProduct(const ImplClass &otherVector) const; + double dotProduct(const ImplVector &otherVector) const; /*! * \brief Cross product * \param otherVector * \return */ - ImplClass crossProduct(const ImplClass &otherVector) const; + ImplVector crossProduct(const ImplVector &otherVector) const; /*! * \brief Matrix * this vector @@ -360,12 +364,9 @@ public: * \brief Reciprocal value * \return */ - ImplClass reciprocalValues() const + ImplVector reciprocalValues() const { - ImplClass v; - v.m_i = (1 / this->m_i); - v.m_j = (1 / this->m_j); - v.m_k = (1 / this->m_j); + ImplVector v(1 / this->m_i, 1 / this->m_j, 1 / this->m_j); return v; } @@ -406,13 +407,13 @@ public: * \brief Round this vector * \return */ - ImplClass &round() + ImplVector &round() { const double epsilon = 1E-10; this->m_i = BlackMisc::Math::CMath::roundEpsilon(this->m_i, epsilon); this->m_j = BlackMisc::Math::CMath::roundEpsilon(this->m_j, epsilon); this->m_k = BlackMisc::Math::CMath::roundEpsilon(this->m_k, epsilon); - return static_cast(*this); + return static_cast(*this); } }; diff --git a/src/blackmisc/pqphysicalquantity.cpp b/src/blackmisc/pqphysicalquantity.cpp index 40aefdd20..ff65846f8 100644 --- a/src/blackmisc/pqphysicalquantity.cpp +++ b/src/blackmisc/pqphysicalquantity.cpp @@ -1,4 +1,4 @@ -/* Copyright (C) 2013 VATSIM Community +/* Copyright (C) 2013 VATSIM Community / contributors * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ @@ -190,8 +190,7 @@ template CPhysicalQuantity &CPhysicalQuantity PQ CPhysicalQuantity::operator -(const PQ &otherQuantity) const { - PQ minus; - minus += (*this); + PQ minus = this->clone(); minus -= otherQuantity; return minus; } @@ -210,8 +209,7 @@ template CPhysicalQuantity &CPhysicalQuantity PQ CPhysicalQuantity::operator *(double multiply) const { - PQ times; - times += (*this); + PQ times = this->clone(); times *= multiply; return times; } @@ -230,8 +228,7 @@ template CPhysicalQuantity &CPhysicalQuantity PQ CPhysicalQuantity::operator /(double divide) const { - PQ div; - div += (*this); + PQ div = this->clone(); div /= divide; return div; } diff --git a/src/blackmisc/pqphysicalquantity.h b/src/blackmisc/pqphysicalquantity.h index 6db60b34c..7b62823cc 100644 --- a/src/blackmisc/pqphysicalquantity.h +++ b/src/blackmisc/pqphysicalquantity.h @@ -36,7 +36,7 @@ protected: MU m_conversionSiUnit; //!< corresponding SI base unit /*! - * \brief Constructor with int + * \brief Constructor by integer * \param baseValue * \param unit * \param siConversionUnit @@ -60,6 +60,15 @@ protected: return this->unitValueRoundedWithUnit(-1); } + /*! + * \brief Polymorphic clone as concrete class + * \return + */ + PQ clone() const + { + return static_cast(*this); + } + /*! * \brief Init by integer * \param baseValue @@ -72,6 +81,7 @@ protected: */ void setUnitValue(double baseValue); + /*! * \brief Set the SI value */