From a05b85135cc5f21f4840c8b7b597b8f4758203bf Mon Sep 17 00:00:00 2001 From: Mathew Sutcliffe Date: Tue, 30 Apr 2013 00:09:32 +0100 Subject: [PATCH] replaced "clone" with "derived" in CRTP (more usual, more flexible, more clear in its intent, and avoids copying the object twice on compilers that don't optimise return by value) --- src/blackmisc/mathmatrixbase.cpp | 2 +- src/blackmisc/mathmatrixbase.h | 31 ++++++++++++++++++---------- src/blackmisc/mathvector3dbase.h | 28 ++++++++++++++++--------- src/blackmisc/pqphysicalquantity.cpp | 8 +++---- src/blackmisc/pqphysicalquantity.h | 15 +++++++++++--- 5 files changed, 55 insertions(+), 29 deletions(-) diff --git a/src/blackmisc/mathmatrixbase.cpp b/src/blackmisc/mathmatrixbase.cpp index 9d28779ca..99a1dadd3 100644 --- a/src/blackmisc/mathmatrixbase.cpp +++ b/src/blackmisc/mathmatrixbase.cpp @@ -97,7 +97,7 @@ template ImplMatrix &CMatrixBasem_matrix(r, c) = CMath::roundEpsilon(this->m_matrix(r, c), 1E-10); } } - return static_cast(*this); + return *derived(); } /* diff --git a/src/blackmisc/mathmatrixbase.h b/src/blackmisc/mathmatrixbase.h index 2841a3385..8b3b6152b 100644 --- a/src/blackmisc/mathmatrixbase.h +++ b/src/blackmisc/mathmatrixbase.h @@ -32,12 +32,21 @@ protected: QString stringForConverter() const; /*! - * \brief Clone as conrete implementation + * \brief Easy access to derived class (CRTP template parameter) * \return */ - ImplMatrix clone() const + ImplMatrix const* derived() const { - return static_cast(*this); + return static_cast(this); + } + + /*! + * \brief Easy access to derived class (CRTP template parameter) + * \return + */ + ImplMatrix* derived() + { + return static_cast(this); } public: @@ -118,7 +127,7 @@ public: */ ImplMatrix operator *(double factor) const { - ImplMatrix m = this->clone(); + ImplMatrix m = *derived(); m *= factor; return m; } @@ -159,7 +168,7 @@ public: */ ImplMatrix operator /(double factor) const { - ImplMatrix m = this->clone(); + ImplMatrix m = *derived(); m /= factor; return m; } @@ -182,7 +191,7 @@ public: */ ImplMatrix operator +(const ImplMatrix &otherMatrix) const { - ImplMatrix m = this->clone(); + ImplMatrix m = *derived(); m += otherMatrix; return m; } @@ -205,7 +214,7 @@ public: */ ImplMatrix operator -(const ImplMatrix &otherMatrix) const { - ImplMatrix m = this->clone(); + ImplMatrix m = *derived(); m -= otherMatrix; return m; } @@ -225,8 +234,8 @@ public: */ bool isIdentityEpsilon() const { - ImplMatrix m = this->clone().round(); - return m.isIdentity(); + ImplMatrix m = *derived(); + return m.round().isIdentity(); } /*! @@ -260,8 +269,8 @@ public: */ bool isZeroEpsilon() const { - ImplMatrix m = this->clone().round(); - return m.isZero(); + ImplMatrix m = *derived(); + return m.round().isZero(); } /*! diff --git a/src/blackmisc/mathvector3dbase.h b/src/blackmisc/mathvector3dbase.h index 8183c325e..1fe7d6133 100644 --- a/src/blackmisc/mathvector3dbase.h +++ b/src/blackmisc/mathvector3dbase.h @@ -62,14 +62,22 @@ protected: virtual QString stringForConverter() const; /*! - * \brief Clone as concrete implementation + * \brief Easy access to derived class (CRTP template parameter) * \return */ - ImplVector clone() const + ImplVector const* derived() const { - return static_cast(*this); + return static_cast(this); } + /*! + * \brief Easy access to derived class (CRTP template parameter) + * \return + */ + ImplVector* derived() + { + return static_cast(this); + } public: @@ -198,7 +206,7 @@ public: */ ImplVector operator +(const ImplVector &otherVector) const { - ImplVector v = this->clone(); + ImplVector v = *derived(); v += otherVector; return v; } @@ -223,7 +231,7 @@ public: */ ImplVector operator -(const ImplVector &otherVector) const { - ImplVector v = this->clone(); + ImplVector v = *derived(); v -= otherVector; return v; } @@ -248,7 +256,7 @@ public: */ ImplVector operator *(const ImplVector &otherVector) const { - ImplVector v = this->clone(); + ImplVector v = *derived(); v *= otherVector; return v; } @@ -273,7 +281,7 @@ public: */ ImplVector operator *(double factor) const { - ImplVector v = this->clone(); + ImplVector v = *derived(); v *= factor; return v; } @@ -309,7 +317,7 @@ public: */ ImplVector operator /(double divisor) const { - ImplVector v = this->clone(); + ImplVector v = *derived(); v /= divisor; return v; } @@ -334,7 +342,7 @@ public: */ ImplVector operator /(const ImplVector &otherVector) const { - ImplVector v = this->clone(); + ImplVector v = *derived(); v /= otherVector; return v; } @@ -413,7 +421,7 @@ public: 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 *derived(); } }; diff --git a/src/blackmisc/pqphysicalquantity.cpp b/src/blackmisc/pqphysicalquantity.cpp index ff65846f8..937d782ef 100644 --- a/src/blackmisc/pqphysicalquantity.cpp +++ b/src/blackmisc/pqphysicalquantity.cpp @@ -190,7 +190,7 @@ template CPhysicalQuantity &CPhysicalQuantity PQ CPhysicalQuantity::operator -(const PQ &otherQuantity) const { - PQ minus = this->clone(); + PQ minus = *derived(); minus -= otherQuantity; return minus; } @@ -209,7 +209,7 @@ template CPhysicalQuantity &CPhysicalQuantity PQ CPhysicalQuantity::operator *(double multiply) const { - PQ times = this->clone(); + PQ times = *derived(); times *= multiply; return times; } @@ -228,7 +228,7 @@ template CPhysicalQuantity &CPhysicalQuantity PQ CPhysicalQuantity::operator /(double divide) const { - PQ div = this->clone(); + PQ div = *derived(); div /= divide; return div; } @@ -281,7 +281,7 @@ template PQ &CPhysicalQuantity::switchUnit(const MU this->m_unit = newUnit; this->setUnitValue(cf); } - return static_cast(*this); + return *derived(); } /* diff --git a/src/blackmisc/pqphysicalquantity.h b/src/blackmisc/pqphysicalquantity.h index 2314e0e48..053fcf829 100644 --- a/src/blackmisc/pqphysicalquantity.h +++ b/src/blackmisc/pqphysicalquantity.h @@ -60,12 +60,21 @@ protected: } /*! - * \brief Polymorphic clone as concrete class + * \brief Easy access to derived class (CRTP template parameter) * \return */ - PQ clone() const + PQ const* derived() const { - return static_cast(*this); + return static_cast(this); + } + + /*! + * \brief Easy access to derived class (CRTP template parameter) + * \return + */ + PQ* derived() + { + return static_cast(this); } /*!