Skip to content

Commit

Permalink
Implement operator +=, -=, *= and /= to return a reference to self.
Browse files Browse the repository at this point in the history
This resolves problems when calling these operators in Python.
  • Loading branch information
codereader committed Jul 20, 2017
1 parent e0c870e commit 4f54941
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 18 deletions.
18 changes: 12 additions & 6 deletions libs/math/Vector2.h
Expand Up @@ -153,9 +153,10 @@ class BasicVector2 {
}

template<typename OtherElement>
void operator+= (const BasicVector2<OtherElement>& other) {
BasicVector2<Element>& operator+= (const BasicVector2<OtherElement>& other) {
_v[0] += static_cast<Element>(other.x());
_v[1] += static_cast<Element>(other.y());
return *this;
}

/* Define the substraction operators - and -= with any other BasicVector2 of type OtherElement
Expand All @@ -170,9 +171,10 @@ class BasicVector2 {
}

template<typename OtherElement>
void operator-= (const BasicVector2<OtherElement>& other) {
BasicVector2<Element>& operator-= (const BasicVector2<OtherElement>& other) {
_v[0] -= static_cast<Element>(other.x());
_v[1] -= static_cast<Element>(other.y());
return *this;
}

/* Define the multiplication operators * and *= with another Vector2 of type OtherElement
Expand All @@ -192,9 +194,10 @@ class BasicVector2 {
}

template<typename OtherElement>
void operator*= (const BasicVector2<OtherElement>& other) {
BasicVector2<Element>& operator*= (const BasicVector2<OtherElement>& other) {
_v[0] *= static_cast<Element>(other.x());
_v[1] *= static_cast<Element>(other.y());
return *this;
}


Expand All @@ -210,10 +213,11 @@ class BasicVector2 {
}

template<typename OtherElement>
void operator*= (const OtherElement& other) {
BasicVector2<Element>& operator*= (const OtherElement& other) {
Element factor = static_cast<Element>(other);
_v[0] *= factor;
_v[1] *= factor;
return *this;
}

/* Define the division operators / and /= with another Vector2 of type OtherElement
Expand All @@ -228,9 +232,10 @@ class BasicVector2 {
}

template<typename OtherElement>
void operator/= (const BasicVector2<OtherElement>& other) {
BasicVector2<Element>& operator/= (const BasicVector2<OtherElement>& other) {
_v[0] /= static_cast<Element>(other.x());
_v[1] /= static_cast<Element>(other.y());
return *this;
}

/* Define the scalar divisions / and /=
Expand All @@ -245,10 +250,11 @@ class BasicVector2 {
}

template<typename OtherElement>
void operator/= (const OtherElement& other) {
BasicVector2<Element>& operator/= (const OtherElement& other) {
Element divisor = static_cast<Element>(other);
_v[0] /= divisor;
_v[1] /= divisor;
return *this;
}

/** Cast to std::string
Expand Down
18 changes: 12 additions & 6 deletions libs/math/Vector3.h
Expand Up @@ -136,10 +136,11 @@ class BasicVector3
}

template<typename OtherElement>
void operator+= (const BasicVector3<OtherElement>& other) {
BasicVector3<Element>& operator+= (const BasicVector3<OtherElement>& other) {
_v[0] += static_cast<Element>(other.x());
_v[1] += static_cast<Element>(other.y());
_v[2] += static_cast<Element>(other.z());
return *this;
}

/* Define the substraction operators - and -= with any other BasicVector3 of type OtherElement
Expand All @@ -155,10 +156,11 @@ class BasicVector3
}

template<typename OtherElement>
void operator-= (const BasicVector3<OtherElement>& other) {
BasicVector3<Element>& operator-= (const BasicVector3<OtherElement>& other) {
_v[0] -= static_cast<Element>(other.x());
_v[1] -= static_cast<Element>(other.y());
_v[2] -= static_cast<Element>(other.z());
return *this;
}

/* Define the multiplication operators * and *= with another Vector3 of type OtherElement
Expand All @@ -179,10 +181,11 @@ class BasicVector3
}

template<typename OtherElement>
void operator*= (const BasicVector3<OtherElement>& other) {
BasicVector3<Element>& operator*= (const BasicVector3<OtherElement>& other) {
_v[0] *= static_cast<Element>(other.x());
_v[1] *= static_cast<Element>(other.y());
_v[2] *= static_cast<Element>(other.z());
return *this;
}

/* Define the multiplications * and *= with a scalar
Expand All @@ -198,11 +201,12 @@ class BasicVector3
}

template<typename OtherElement>
void operator*= (const OtherElement& other) {
BasicVector3<Element>& operator*= (const OtherElement& other) {
Element factor = static_cast<Element>(other);
_v[0] *= factor;
_v[1] *= factor;
_v[2] *= factor;
return *this;
}

/* Define the division operators / and /= with another Vector3 of type OtherElement
Expand All @@ -218,10 +222,11 @@ class BasicVector3
}

template<typename OtherElement>
void operator/= (const BasicVector3<OtherElement>& other) {
BasicVector3<Element>& operator/= (const BasicVector3<OtherElement>& other) {
_v[0] /= static_cast<Element>(other.x());
_v[1] /= static_cast<Element>(other.y());
_v[2] /= static_cast<Element>(other.z());
return *this;
}

/* Define the scalar divisions / and /=
Expand All @@ -237,11 +242,12 @@ class BasicVector3
}

template<typename OtherElement>
void operator/= (const OtherElement& other) {
BasicVector3<Element>& operator/= (const OtherElement& other) {
Element divisor = static_cast<Element>(other);
_v[0] /= divisor;
_v[1] /= divisor;
_v[2] /= divisor;
return *this;
}

/*
Expand Down
18 changes: 12 additions & 6 deletions libs/math/Vector4.h
Expand Up @@ -116,11 +116,12 @@ class BasicVector4
}

template<typename OtherElement>
void operator+= (const BasicVector4<OtherElement>& other) {
BasicVector4<Element>& operator+= (const BasicVector4<OtherElement>& other) {
_v[0] += static_cast<Element>(other.x());
_v[1] += static_cast<Element>(other.y());
_v[2] += static_cast<Element>(other.z());
_v[3] += static_cast<Element>(other.w());
return *this;
}

/* Define the substraction operators - and -= with any other BasicVector4 of type OtherElement
Expand All @@ -137,11 +138,12 @@ class BasicVector4
}

template<typename OtherElement>
void operator-= (const BasicVector4<OtherElement>& other) {
BasicVector4<Element>& operator-= (const BasicVector4<OtherElement>& other) {
_v[0] -= static_cast<Element>(other.x());
_v[1] -= static_cast<Element>(other.y());
_v[2] -= static_cast<Element>(other.z());
_v[3] -= static_cast<Element>(other.w());
return *this;
}

/* Define the multiplication operators * and *= with another Vector4 of type OtherElement
Expand All @@ -163,11 +165,12 @@ class BasicVector4
}

template<typename OtherElement>
void operator*= (const BasicVector4<OtherElement>& other) {
BasicVector4<Element>& operator*= (const BasicVector4<OtherElement>& other) {
_v[0] *= static_cast<Element>(other.x());
_v[1] *= static_cast<Element>(other.y());
_v[2] *= static_cast<Element>(other.z());
_v[3] *= static_cast<Element>(other.w());
return *this;
}


Expand All @@ -185,12 +188,13 @@ class BasicVector4
}

template<typename OtherElement>
void operator*= (const OtherElement& other) {
BasicVector4<Element>& operator*= (const OtherElement& other) {
Element factor = static_cast<Element>(other);
_v[0] *= factor;
_v[1] *= factor;
_v[2] *= factor;
_v[3] *= factor;
return *this;
}

/* Define the division operators / and /= with another Vector4 of type OtherElement
Expand All @@ -207,11 +211,12 @@ class BasicVector4
}

template<typename OtherElement>
void operator/= (const BasicVector4<OtherElement>& other) {
BasicVector4<Element>& operator/= (const BasicVector4<OtherElement>& other) {
_v[0] /= static_cast<Element>(other.x());
_v[1] /= static_cast<Element>(other.y());
_v[2] /= static_cast<Element>(other.z());
_v[3] /= static_cast<Element>(other.w());
return *this;
}

/* Define the scalar divisions / and /=
Expand All @@ -228,12 +233,13 @@ class BasicVector4
}

template<typename OtherElement>
void operator/= (const OtherElement& other) {
BasicVector4<Element>& operator/= (const OtherElement& other) {
Element divisor = static_cast<Element>(other);
_v[0] /= divisor;
_v[1] /= divisor;
_v[2] /= divisor;
_v[3] /= divisor;
return *this;
}

/* Scalar product this vector with another Vector4,
Expand Down

0 comments on commit 4f54941

Please sign in to comment.