Skip to content

Commit

Permalink
let the compiler handle == and != (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
therjak authored Nov 3, 2023
1 parent 056bd33 commit a467385
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 38 deletions.
9 changes: 3 additions & 6 deletions src/base/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,9 @@ class CColor {
static_cast<uint8_t>(clamp(0, _a, 1) * 255));
}

constexpr bool operator==(const CColor& c) const {
return r == c.r && g == c.g && b == c.b && a == c.a;
}
constexpr bool operator!=(const CColor& c) const {
return r != c.r || g != c.g || b != c.b || a != c.a;
}
constexpr friend bool operator==(const CColor& lhs,
const CColor& rhs) = default;

[[nodiscard]] constexpr uint32_t argb() const {
return (a << 24) | (r << 16) | (g << 8) | b;
}
Expand Down
4 changes: 2 additions & 2 deletions src/base/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ class CMatrix4x4 {
constexpr CMatrix4x4 operator*(const float f) const;
constexpr CMatrix4x4 operator/(const float f) const;
constexpr friend CMatrix4x4 operator*(const float f, const CMatrix4x4& mat);
constexpr bool operator==(const CMatrix4x4& mat) const { return m == mat.m; }
constexpr bool operator!=(const CMatrix4x4& mat) const { return m != mat.m; }
constexpr friend bool operator==(const CMatrix4x4& lhs,
const CMatrix4x4& rhs) = default;

constexpr void Transpose();
[[nodiscard]] constexpr CMatrix4x4 Transposed() const;
Expand Down
9 changes: 3 additions & 6 deletions src/base/rectangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,9 @@ class CRect {
Move(-p);
return *this;
}
constexpr bool operator==(const CRect& r) const {
return x1 == r.x1 && y1 == r.y1 && x2 == r.x2 && y2 == r.y2;
}
constexpr bool operator!=(const CRect& r) const {
return x1 != r.x1 || y1 != r.y1 || x2 != r.x2 || y2 != r.y2;
}
constexpr friend bool operator==(const CRect& lhs,
const CRect& rhs) = default;

constexpr CRect operator|(const CRect& r) const {
if (!Intersects(r)) return CRect(1, 1, -1, -1);
return GetIntersection(r);
Expand Down
36 changes: 12 additions & 24 deletions src/base/vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,9 @@ class CVector2 {
const float fi = 1 / f;
return CVector2(x * fi, y * fi);
}
constexpr bool operator==(const CVector2& v) const {
return x == v.x && y == v.y;
}
constexpr bool operator!=(const CVector2& v) const {
return x != v.x || y != v.y;
}
constexpr friend bool operator==(const CVector2& lhs,
const CVector2& rhs) = default;

// dot product
constexpr float operator*(const CVector2& v) const {
return x * v.x + y * v.y;
Expand Down Expand Up @@ -135,12 +132,9 @@ class CVector2I {
constexpr CVector2I operator/(const int32_t f) const {
return CVector2I(x / f, y / f);
}
constexpr bool operator==(const CVector2I& v) const {
return x == v.x && y == v.y;
}
constexpr bool operator!=(const CVector2I& v) const {
return x != v.x || y != v.y;
}
constexpr friend bool operator==(const CVector2I& lhs,
const CVector2I& rhs) = default;

// dot product
constexpr int32_t operator*(const CVector2I& v) const {
return x * v.x + y * v.y;
Expand Down Expand Up @@ -216,12 +210,9 @@ class CVector3 {
const float fi = 1 / f;
return CVector3(x * fi, y * fi, z * fi);
}
constexpr bool operator==(const CVector3& v) {
return x == v.x && y == v.y && z == v.z;
}
constexpr bool operator!=(const CVector3& v) const {
return x != v.x || y != v.y || z != v.z;
}
constexpr friend bool operator==(const CVector3& lhs,
const CVector3& rhs) = default;

// dot product
constexpr float operator*(const CVector3& v) const {
return x * v.x + y * v.y + z * v.z;
Expand Down Expand Up @@ -307,12 +298,9 @@ class CVector4 {
const float fi = 1 / f;
return CVector4(x * fi, y * fi, z * fi, w * fi);
}
constexpr bool operator==(const CVector4& v) const {
return x == v.x && y == v.y && z == v.z && w == v.w;
}
constexpr bool operator!=(const CVector4& v) const {
return x != v.x || y != v.y || z != v.z || w != v.w;
}
constexpr friend bool operator==(const CVector4& lhs,
const CVector4& rhs) = default;

// dot product
constexpr float operator*(const CVector4& v) const {
return x * v.x + y * v.y + z * v.z + w * v.w;
Expand Down

0 comments on commit a467385

Please sign in to comment.