From b1c8f90b3218e8251761a7ac2caa278086271ac6 Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Fri, 2 May 2025 15:25:32 +0200 Subject: [PATCH] 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 --- .../blender/blenlib/BLI_math_angle_types.hh | 28 +++++++-------- .../blender/blenlib/BLI_math_matrix_types.hh | 30 ++++++++-------- source/blender/blenlib/BLI_math_quaternion.hh | 4 +-- .../blenlib/BLI_math_quaternion_types.hh | 6 ++-- .../blender/blenlib/BLI_math_vector_types.hh | 36 +++++++++---------- .../freestyle/intern/geometry/VecMat.h | 16 ++++----- 6 files changed, 60 insertions(+), 60 deletions(-) diff --git a/source/blender/blenlib/BLI_math_angle_types.hh b/source/blender/blenlib/BLI_math_angle_types.hh index c00a4500695..2ab008eb29d 100644 --- a/source/blender/blenlib/BLI_math_angle_types.hh +++ b/source/blender/blenlib/BLI_math_angle_types.hh @@ -121,25 +121,25 @@ template 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 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 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; } diff --git a/source/blender/blenlib/BLI_math_matrix_types.hh b/source/blender/blenlib/BLI_math_matrix_types.hh index 3a48cbd5a93..b3392831b83 100644 --- a/source/blender/blenlib/BLI_math_matrix_types.hh +++ b/source/blender/blenlib/BLI_math_matrix_types.hh @@ -312,13 +312,13 @@ struct alignas(Alignment) MatBase : public vec_struct_base, N return b + a; } - MatBase &operator+=(const MatBase &b) + MatBase &operator+=(const MatBase &b) & { unroll([&](auto i) { (*this)[i] += b[i]; }); return *this; } - MatBase &operator+=(T b) + MatBase &operator+=(T b) & { unroll([&](auto i) { (*this)[i] += b; }); return *this; @@ -352,13 +352,13 @@ struct alignas(Alignment) MatBase : public vec_struct_base, N return result; } - MatBase &operator-=(const MatBase &b) + MatBase &operator-=(const MatBase &b) & { unroll([&](auto i) { (*this)[i] -= b[i]; }); return *this; } - MatBase &operator-=(T b) + MatBase &operator-=(T b) & { unroll([&](auto i) { (*this)[i] -= b; }); return *this; @@ -379,7 +379,7 @@ struct alignas(Alignment) MatBase : public vec_struct_base, 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, N } /** Multiply each component by a scalar. */ - MatBase &operator*=(T b) + MatBase &operator*=(T b) & { unroll([&](auto i) { (*this)[i] *= b; }); return *this; @@ -788,18 +788,18 @@ struct MutableMatView OtherSrcNumRow, OtherSrcStartCol, OtherSrcStartRow, - OtherSrcAlignment> &b) + OtherSrcAlignment> &b) & { unroll([&](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([&](auto i) { (*this)[i] += b; }); return *this; @@ -817,18 +817,18 @@ struct MutableMatView OtherSrcNumRow, OtherSrcStartCol, OtherSrcStartRow, - OtherSrcAlignment> &b) + OtherSrcAlignment> &b) & { unroll([&](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([&](auto i) { (*this)[i] -= b; }); return *this; @@ -847,19 +847,19 @@ struct MutableMatView OtherSrcNumRow, OtherSrcStartCol, OtherSrcStartRow, - OtherSrcAlignment> &b) + OtherSrcAlignment> &b) & { *this = *static_cast(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([&](auto i) { (*this)[i] *= b; }); return *this; diff --git a/source/blender/blenlib/BLI_math_quaternion.hh b/source/blender/blenlib/BLI_math_quaternion.hh index 0592b1272a3..985d05eef1f 100644 --- a/source/blender/blenlib/BLI_math_quaternion.hh +++ b/source/blender/blenlib/BLI_math_quaternion.hh @@ -362,7 +362,7 @@ DualQuaternionBase::DualQuaternionBase(const QuaternionBase &non_dual, /* -------------- Operators -------------- */ template -DualQuaternionBase &DualQuaternionBase::operator+=(const DualQuaternionBase &b) +DualQuaternionBase &DualQuaternionBase::operator+=(const DualQuaternionBase &b) & { DualQuaternionBase &a = *this; /* Sum rotation and translation. */ @@ -408,7 +408,7 @@ DualQuaternionBase &DualQuaternionBase::operator+=(const DualQuaternionBas return *this; } -template DualQuaternionBase &DualQuaternionBase::operator*=(const T &t) +template DualQuaternionBase &DualQuaternionBase::operator*=(const T &t) & { BLI_assert(t >= 0); DualQuaternionBase &q = *this; diff --git a/source/blender/blenlib/BLI_math_quaternion_types.hh b/source/blender/blenlib/BLI_math_quaternion_types.hh index 481c0275cd2..c09d03ba60e 100644 --- a/source/blender/blenlib/BLI_math_quaternion_types.hh +++ b/source/blender/blenlib/BLI_math_quaternion_types.hh @@ -140,7 +140,7 @@ template 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 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) diff --git a/source/blender/blenlib/BLI_math_vector_types.hh b/source/blender/blenlib/BLI_math_vector_types.hh index 2278722d8c3..7f6ab96c92c 100644 --- a/source/blender/blenlib/BLI_math_vector_types.hh +++ b/source/blender/blenlib/BLI_math_vector_types.hh @@ -292,12 +292,12 @@ template struct VecBase : public vec_struct_base 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 struct VecBase : public vec_struct_base 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 struct VecBase : public vec_struct_base 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 struct VecBase : public vec_struct_base 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 struct VecBase : public vec_struct_base 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 struct VecBase : public vec_struct_base 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 struct VecBase : public vec_struct_base 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 struct VecBase : public vec_struct_base 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 struct VecBase : public vec_struct_base 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); } diff --git a/source/blender/freestyle/intern/geometry/VecMat.h b/source/blender/freestyle/intern/geometry/VecMat.h index d788a83f226..05b794a89db 100644 --- a/source/blender/freestyle/intern/geometry/VecMat.h +++ b/source/blender/freestyle/intern/geometry/VecMat.h @@ -170,7 +170,7 @@ template class Vec { return *this; } - template inline Vec &operator+=(const Vec &v) + template inline Vec &operator+=(const Vec &v) & { for (uint i = 0; i < N; i++) { this->_coord[i] += (T)v[i]; @@ -178,7 +178,7 @@ template class Vec { return *this; } - template inline Vec &operator-=(const Vec &v) + template inline Vec &operator-=(const Vec &v) & { for (uint i = 0; i < N; i++) { this->_coord[i] -= (T)v[i]; @@ -186,7 +186,7 @@ template class Vec { return *this; } - template inline Vec &operator*=(const U r) + template inline Vec &operator*=(const U r) & { for (uint i = 0; i < N; i++) { this->_coord[i] *= r; @@ -194,7 +194,7 @@ template class Vec { return *this; } - template inline Vec &operator/=(const U r) + template inline Vec &operator/=(const U r) & { if (r) { for (uint i = 0; i < N; i++) { @@ -688,7 +688,7 @@ template class Matrix { return *this; } - template inline Matrix &operator+=(const Matrix &m) + template inline Matrix &operator+=(const Matrix &m) & { for (uint i = 0; i < M; i++) { for (uint j = 0; j < N; j++) { @@ -698,7 +698,7 @@ template class Matrix { return *this; } - template inline Matrix &operator-=(const Matrix &m) + template inline Matrix &operator-=(const Matrix &m) & { for (uint i = 0; i < M; i++) { for (uint j = 0; j < N; j++) { @@ -708,7 +708,7 @@ template class Matrix { return *this; } - template inline Matrix &operator*=(const U lambda) + template inline Matrix &operator*=(const U lambda) & { for (uint i = 0; i < M; i++) { for (uint j = 0; j < N; j++) { @@ -718,7 +718,7 @@ template class Matrix { return *this; } - template inline Matrix &operator/=(const U lambda) + template inline Matrix &operator/=(const U lambda) & { if (lambda) { for (uint i = 0; i < M; i++) {