Math: Use lvalue ref-qualifier for compound assignment operators

The goal is to make issues like the ones fixed in #138310 become visible
much earlier in the development cycle.

This PR covers the most obvious places where it could be applied. There
are probably more places, but they can be covered later.

Pull Request: https://projects.blender.org/blender/blender/pulls/138315
This commit is contained in:
Sergey Sharybin
2025-05-02 15:25:32 +02:00
committed by Sergey Sharybin
parent 705ba465bb
commit b1c8f90b32
6 changed files with 60 additions and 60 deletions

View File

@@ -121,25 +121,25 @@ template<typename T> struct AngleRadianBase {
return -a.value_;
}
AngleRadianBase &operator+=(const AngleRadianBase &b)
AngleRadianBase &operator+=(const AngleRadianBase &b) &
{
value_ += b.value_;
return *this;
}
AngleRadianBase &operator-=(const AngleRadianBase &b)
AngleRadianBase &operator-=(const AngleRadianBase &b) &
{
value_ -= b.value_;
return *this;
}
AngleRadianBase &operator*=(const AngleRadianBase &b)
AngleRadianBase &operator*=(const AngleRadianBase &b) &
{
value_ *= b.value_;
return *this;
}
AngleRadianBase &operator/=(const AngleRadianBase &b)
AngleRadianBase &operator/=(const AngleRadianBase &b) &
{
value_ /= b.value_;
return *this;
@@ -317,25 +317,25 @@ template<typename T> struct AngleCartesianBase {
return {a.cos_, -a.sin_};
}
AngleCartesianBase &operator+=(const AngleCartesianBase &b)
AngleCartesianBase &operator+=(const AngleCartesianBase &b) &
{
*this = *this + b;
return *this;
}
AngleCartesianBase &operator*=(const T &b)
AngleCartesianBase &operator*=(const T &b) &
{
*this = *this * b;
return *this;
}
AngleCartesianBase &operator-=(const AngleCartesianBase &b)
AngleCartesianBase &operator-=(const AngleCartesianBase &b) &
{
*this = *this - b;
return *this;
}
AngleCartesianBase &operator/=(const T &b)
AngleCartesianBase &operator/=(const T &b) &
{
*this = *this / b;
return *this;
@@ -538,32 +538,32 @@ template<typename T = float> struct AngleFraction {
return {-a.numerator_, a.denominator_};
}
AngleFraction &operator+=(const AngleFraction &b)
AngleFraction &operator+=(const AngleFraction &b) &
{
return *this = *this + b;
}
AngleFraction &operator-=(const AngleFraction &b)
AngleFraction &operator-=(const AngleFraction &b) &
{
return *this = *this - b;
}
AngleFraction &operator*=(const AngleFraction &b)
AngleFraction &operator*=(const AngleFraction &b) &
{
return *this = *this * b;
}
AngleFraction &operator/=(const AngleFraction &b)
AngleFraction &operator/=(const AngleFraction &b) &
{
return *this = *this / b;
}
AngleFraction &operator*=(const int64_t &b)
AngleFraction &operator*=(const int64_t &b) &
{
return *this = *this * b;
}
AngleFraction &operator/=(const int64_t &b)
AngleFraction &operator/=(const int64_t &b) &
{
return *this = *this / b;
}

View File

@@ -312,13 +312,13 @@ struct alignas(Alignment) MatBase : public vec_struct_base<VecBase<T, NumRow>, N
return b + a;
}
MatBase &operator+=(const MatBase &b)
MatBase &operator+=(const MatBase &b) &
{
unroll<NumCol>([&](auto i) { (*this)[i] += b[i]; });
return *this;
}
MatBase &operator+=(T b)
MatBase &operator+=(T b) &
{
unroll<NumCol>([&](auto i) { (*this)[i] += b; });
return *this;
@@ -352,13 +352,13 @@ struct alignas(Alignment) MatBase : public vec_struct_base<VecBase<T, NumRow>, N
return result;
}
MatBase &operator-=(const MatBase &b)
MatBase &operator-=(const MatBase &b) &
{
unroll<NumCol>([&](auto i) { (*this)[i] -= b[i]; });
return *this;
}
MatBase &operator-=(T b)
MatBase &operator-=(T b) &
{
unroll<NumCol>([&](auto i) { (*this)[i] -= b; });
return *this;
@@ -379,7 +379,7 @@ struct alignas(Alignment) MatBase : public vec_struct_base<VecBase<T, NumRow>, N
}
/** Multiply two matrices using matrix multiplication. */
MatBase &operator*=(const MatBase &b)
MatBase &operator*=(const MatBase &b) &
{
const MatBase &a = *this;
*this = a * b;
@@ -387,7 +387,7 @@ struct alignas(Alignment) MatBase : public vec_struct_base<VecBase<T, NumRow>, N
}
/** Multiply each component by a scalar. */
MatBase &operator*=(T b)
MatBase &operator*=(T b) &
{
unroll<NumCol>([&](auto i) { (*this)[i] *= b; });
return *this;
@@ -788,18 +788,18 @@ struct MutableMatView
OtherSrcNumRow,
OtherSrcStartCol,
OtherSrcStartRow,
OtherSrcAlignment> &b)
OtherSrcAlignment> &b) &
{
unroll<NumCol>([&](auto i) { (*this)[i] += b[i]; });
return *this;
}
MutableMatView &operator+=(const MatT &b)
MutableMatView &operator+=(const MatT &b) &
{
return *this += b.view();
}
MutableMatView &operator+=(T b)
MutableMatView &operator+=(T b) &
{
unroll<NumCol>([&](auto i) { (*this)[i] += b; });
return *this;
@@ -817,18 +817,18 @@ struct MutableMatView
OtherSrcNumRow,
OtherSrcStartCol,
OtherSrcStartRow,
OtherSrcAlignment> &b)
OtherSrcAlignment> &b) &
{
unroll<NumCol>([&](auto i) { (*this)[i] -= b[i]; });
return *this;
}
MutableMatView &operator-=(const MatT &b)
MutableMatView &operator-=(const MatT &b) &
{
return *this -= b.view();
}
MutableMatView &operator-=(T b)
MutableMatView &operator-=(T b) &
{
unroll<NumCol>([&](auto i) { (*this)[i] -= b; });
return *this;
@@ -847,19 +847,19 @@ struct MutableMatView
OtherSrcNumRow,
OtherSrcStartCol,
OtherSrcStartRow,
OtherSrcAlignment> &b)
OtherSrcAlignment> &b) &
{
*this = *static_cast<MatViewT *>(this) * b;
return *this;
}
MutableMatView &operator*=(const MatT &b)
MutableMatView &operator*=(const MatT &b) &
{
return *this *= b.view();
}
/** Multiply each component by a scalar. */
MutableMatView &operator*=(T b)
MutableMatView &operator*=(T b) &
{
unroll<NumCol>([&](auto i) { (*this)[i] *= b; });
return *this;

View File

@@ -362,7 +362,7 @@ DualQuaternionBase<T>::DualQuaternionBase(const QuaternionBase<T> &non_dual,
/* -------------- Operators -------------- */
template<typename T>
DualQuaternionBase<T> &DualQuaternionBase<T>::operator+=(const DualQuaternionBase<T> &b)
DualQuaternionBase<T> &DualQuaternionBase<T>::operator+=(const DualQuaternionBase<T> &b) &
{
DualQuaternionBase<T> &a = *this;
/* Sum rotation and translation. */
@@ -408,7 +408,7 @@ DualQuaternionBase<T> &DualQuaternionBase<T>::operator+=(const DualQuaternionBas
return *this;
}
template<typename T> DualQuaternionBase<T> &DualQuaternionBase<T>::operator*=(const T &t)
template<typename T> DualQuaternionBase<T> &DualQuaternionBase<T>::operator*=(const T &t) &
{
BLI_assert(t >= 0);
DualQuaternionBase<T> &q = *this;

View File

@@ -140,7 +140,7 @@ template<typename T> struct QuaternionBase {
a.w * b.z + a.z * b.w + a.x * b.y - a.y * b.x};
}
QuaternionBase &operator*=(const QuaternionBase &b)
QuaternionBase &operator*=(const QuaternionBase &b) &
{
*this = *this * b;
return *this;
@@ -233,10 +233,10 @@ template<typename T> struct DualQuaternionBase {
/** Operators. */
/** Apply a scalar weight to a dual quaternion. */
DualQuaternionBase &operator*=(const T &t);
DualQuaternionBase &operator*=(const T &t) &;
/** Add two weighted dual-quaternions rotations. */
DualQuaternionBase &operator+=(const DualQuaternionBase &b);
DualQuaternionBase &operator+=(const DualQuaternionBase &b) &;
/** Apply a scalar weight to a dual quaternion. */
friend DualQuaternionBase operator*(const DualQuaternionBase &a, const T &t)

View File

@@ -292,12 +292,12 @@ template<typename T, int Size> struct VecBase : public vec_struct_base<T, Size>
BLI_UNROLL_MATH_VEC_OP_SCALAR_VEC(+, a, b);
}
VecBase &operator+=(const VecBase &b)
VecBase &operator+=(const VecBase &b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_VEC(+=, b);
}
VecBase &operator+=(const T &b)
VecBase &operator+=(const T &b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_SCALAR(+=, b);
}
@@ -322,12 +322,12 @@ template<typename T, int Size> struct VecBase : public vec_struct_base<T, Size>
BLI_UNROLL_MATH_VEC_OP_SCALAR_VEC(-, a, b);
}
VecBase &operator-=(const VecBase &b)
VecBase &operator-=(const VecBase &b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_VEC(-=, b);
}
VecBase &operator-=(const T &b)
VecBase &operator-=(const T &b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_SCALAR(-=, b);
}
@@ -347,12 +347,12 @@ template<typename T, int Size> struct VecBase : public vec_struct_base<T, Size>
BLI_UNROLL_MATH_VEC_OP_SCALAR_VEC(*, a, b);
}
VecBase &operator*=(T b)
VecBase &operator*=(T b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_SCALAR(*=, b);
}
VecBase &operator*=(const VecBase &b)
VecBase &operator*=(const VecBase &b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_VEC(*=, b);
}
@@ -379,13 +379,13 @@ template<typename T, int Size> struct VecBase : public vec_struct_base<T, Size>
BLI_UNROLL_MATH_VEC_OP_SCALAR_VEC(/, a, b);
}
VecBase &operator/=(T b)
VecBase &operator/=(T b) &
{
BLI_assert(b != T(0));
BLI_UNROLL_MATH_VEC_OP_ASSIGN_SCALAR(/=, b);
}
VecBase &operator/=(const VecBase &b)
VecBase &operator/=(const VecBase &b) &
{
for (int i = 0; i < Size; i++) {
BLI_assert(b[i] != T(0));
@@ -410,12 +410,12 @@ template<typename T, int Size> struct VecBase : public vec_struct_base<T, Size>
BLI_UNROLL_MATH_VEC_OP_SCALAR_VEC(&, a, b);
}
BLI_INT_OP(T) VecBase &operator&=(T b)
BLI_INT_OP(T) VecBase &operator&=(T b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_SCALAR(&=, b);
}
BLI_INT_OP(T) VecBase &operator&=(const VecBase &b)
BLI_INT_OP(T) VecBase &operator&=(const VecBase &b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_VEC(&=, b);
}
@@ -435,12 +435,12 @@ template<typename T, int Size> struct VecBase : public vec_struct_base<T, Size>
BLI_UNROLL_MATH_VEC_OP_SCALAR_VEC(|, a, b);
}
BLI_INT_OP(T) VecBase &operator|=(T b)
BLI_INT_OP(T) VecBase &operator|=(T b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_SCALAR(|=, b);
}
BLI_INT_OP(T) VecBase &operator|=(const VecBase &b)
BLI_INT_OP(T) VecBase &operator|=(const VecBase &b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_VEC(|=, b);
}
@@ -460,12 +460,12 @@ template<typename T, int Size> struct VecBase : public vec_struct_base<T, Size>
BLI_UNROLL_MATH_VEC_OP_SCALAR_VEC(^, a, b);
}
BLI_INT_OP(T) VecBase &operator^=(T b)
BLI_INT_OP(T) VecBase &operator^=(T b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_SCALAR(^=, b);
}
BLI_INT_OP(T) VecBase &operator^=(const VecBase &b)
BLI_INT_OP(T) VecBase &operator^=(const VecBase &b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_VEC(^=, b);
}
@@ -487,12 +487,12 @@ template<typename T, int Size> struct VecBase : public vec_struct_base<T, Size>
BLI_UNROLL_MATH_VEC_OP_VEC_SCALAR(<<, a, b);
}
BLI_INT_OP(T) VecBase &operator<<=(T b)
BLI_INT_OP(T) VecBase &operator<<=(T b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_SCALAR(<<=, b);
}
BLI_INT_OP(T) VecBase &operator<<=(const VecBase &b)
BLI_INT_OP(T) VecBase &operator<<=(const VecBase &b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_VEC(<<=, b);
}
@@ -507,12 +507,12 @@ template<typename T, int Size> struct VecBase : public vec_struct_base<T, Size>
BLI_UNROLL_MATH_VEC_OP_VEC_SCALAR(>>, a, b);
}
BLI_INT_OP(T) VecBase &operator>>=(T b)
BLI_INT_OP(T) VecBase &operator>>=(T b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_SCALAR(>>=, b);
}
BLI_INT_OP(T) VecBase &operator>>=(const VecBase &b)
BLI_INT_OP(T) VecBase &operator>>=(const VecBase &b) &
{
BLI_UNROLL_MATH_VEC_OP_ASSIGN_VEC(>>=, b);
}

View File

@@ -170,7 +170,7 @@ template<class T, uint N> class Vec {
return *this;
}
template<class U> inline Vec<T, N> &operator+=(const Vec<U, N> &v)
template<class U> inline Vec<T, N> &operator+=(const Vec<U, N> &v) &
{
for (uint i = 0; i < N; i++) {
this->_coord[i] += (T)v[i];
@@ -178,7 +178,7 @@ template<class T, uint N> class Vec {
return *this;
}
template<class U> inline Vec<T, N> &operator-=(const Vec<U, N> &v)
template<class U> inline Vec<T, N> &operator-=(const Vec<U, N> &v) &
{
for (uint i = 0; i < N; i++) {
this->_coord[i] -= (T)v[i];
@@ -186,7 +186,7 @@ template<class T, uint N> class Vec {
return *this;
}
template<class U> inline Vec<T, N> &operator*=(const U r)
template<class U> inline Vec<T, N> &operator*=(const U r) &
{
for (uint i = 0; i < N; i++) {
this->_coord[i] *= r;
@@ -194,7 +194,7 @@ template<class T, uint N> class Vec {
return *this;
}
template<class U> inline Vec<T, N> &operator/=(const U r)
template<class U> inline Vec<T, N> &operator/=(const U r) &
{
if (r) {
for (uint i = 0; i < N; i++) {
@@ -688,7 +688,7 @@ template<class T, uint M, uint N> class Matrix {
return *this;
}
template<class U> inline Matrix<T, M, N> &operator+=(const Matrix<U, M, N> &m)
template<class U> inline Matrix<T, M, N> &operator+=(const Matrix<U, M, N> &m) &
{
for (uint i = 0; i < M; i++) {
for (uint j = 0; j < N; j++) {
@@ -698,7 +698,7 @@ template<class T, uint M, uint N> class Matrix {
return *this;
}
template<class U> inline Matrix<T, M, N> &operator-=(const Matrix<U, M, N> &m)
template<class U> inline Matrix<T, M, N> &operator-=(const Matrix<U, M, N> &m) &
{
for (uint i = 0; i < M; i++) {
for (uint j = 0; j < N; j++) {
@@ -708,7 +708,7 @@ template<class T, uint M, uint N> class Matrix {
return *this;
}
template<class U> inline Matrix<T, M, N> &operator*=(const U lambda)
template<class U> inline Matrix<T, M, N> &operator*=(const U lambda) &
{
for (uint i = 0; i < M; i++) {
for (uint j = 0; j < N; j++) {
@@ -718,7 +718,7 @@ template<class T, uint M, uint N> class Matrix {
return *this;
}
template<class U> inline Matrix<T, M, N> &operator/=(const U lambda)
template<class U> inline Matrix<T, M, N> &operator/=(const U lambda) &
{
if (lambda) {
for (uint i = 0; i < M; i++) {