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)

This commit is contained in:
Mathew Sutcliffe
2013-04-30 00:09:32 +01:00
parent ba70a150e0
commit a05b85135c
5 changed files with 55 additions and 29 deletions

View File

@@ -97,7 +97,7 @@ template<class ImplMatrix, int Rows, int Columns> ImplMatrix &CMatrixBase<ImplMa
this->m_matrix(r, c) = CMath::roundEpsilon(this->m_matrix(r, c), 1E-10);
}
}
return static_cast<ImplMatrix &>(*this);
return *derived();
}
/*

View File

@@ -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<ImplMatrix const &>(*this);
return static_cast<ImplMatrix const *>(this);
}
/*!
* \brief Easy access to derived class (CRTP template parameter)
* \return
*/
ImplMatrix* derived()
{
return static_cast<ImplMatrix *>(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();
}
/*!

View File

@@ -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<ImplVector const &>(*this);
return static_cast<ImplVector const *>(this);
}
/*!
* \brief Easy access to derived class (CRTP template parameter)
* \return
*/
ImplVector* derived()
{
return static_cast<ImplVector *>(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<ImplVector &>(*this);
return *derived();
}
};

View File

@@ -190,7 +190,7 @@ template <class MU, class PQ> CPhysicalQuantity<MU, PQ> &CPhysicalQuantity<MU, P
*/
template <class MU, class PQ> PQ CPhysicalQuantity<MU, PQ>::operator -(const PQ &otherQuantity) const
{
PQ minus = this->clone();
PQ minus = *derived();
minus -= otherQuantity;
return minus;
}
@@ -209,7 +209,7 @@ template <class MU, class PQ> CPhysicalQuantity<MU, PQ> &CPhysicalQuantity<MU, P
*/
template <class MU, class PQ> PQ CPhysicalQuantity<MU, PQ>::operator *(double multiply) const
{
PQ times = this->clone();
PQ times = *derived();
times *= multiply;
return times;
}
@@ -228,7 +228,7 @@ template <class MU, class PQ> CPhysicalQuantity<MU, PQ> &CPhysicalQuantity<MU, P
*/
template <class MU, class PQ> PQ CPhysicalQuantity<MU, PQ>::operator /(double divide) const
{
PQ div = this->clone();
PQ div = *derived();
div /= divide;
return div;
}
@@ -281,7 +281,7 @@ template <class MU, class PQ> PQ &CPhysicalQuantity<MU, PQ>::switchUnit(const MU
this->m_unit = newUnit;
this->setUnitValue(cf);
}
return static_cast<PQ &>(*this);
return *derived();
}
/*

View File

@@ -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<PQ const &>(*this);
return static_cast<PQ const *>(this);
}
/*!
* \brief Easy access to derived class (CRTP template parameter)
* \return
*/
PQ* derived()
{
return static_cast<PQ *>(this);
}
/*!