From 234bc0c2374d83936fdf27093615df6f32a08107 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 24 Nov 2022 16:04:45 +0000 Subject: [PATCH] LibGfx: Prevent calling `to_type()` on `Line/Point/Rect/Size` Also, add `Line::to_type()` since that was missing. Calling to_type() with the same type as the existing object accomplishes nothing except wasting some cycles and making the code more verbose, and it is hard to spot. Nobody does this in the code currently (yay!) but I made this mistake repeatedly when doing my step-by-step CSS Pixels conversion, so let's make it easier to catch them. --- Userland/Libraries/LibGfx/Line.h | 7 +++++++ Userland/Libraries/LibGfx/Point.h | 3 ++- Userland/Libraries/LibGfx/Rect.h | 3 ++- Userland/Libraries/LibGfx/Size.h | 3 ++- 4 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibGfx/Line.h b/Userland/Libraries/LibGfx/Line.h index a4b07f23f5..3697eb0a59 100644 --- a/Userland/Libraries/LibGfx/Line.h +++ b/Userland/Libraries/LibGfx/Line.h @@ -134,6 +134,13 @@ public: void set_a(Point const& a) { m_a = a; } void set_b(Point const& b) { m_b = b; } + template + requires(!IsSame) + [[nodiscard]] ALWAYS_INLINE constexpr Line to_type() const + { + return Line(*this); + } + String to_string() const; private: diff --git a/Userland/Libraries/LibGfx/Point.h b/Userland/Libraries/LibGfx/Point.h index 9ef99607fc..ce9fba7c28 100644 --- a/Userland/Libraries/LibGfx/Point.h +++ b/Userland/Libraries/LibGfx/Point.h @@ -229,7 +229,8 @@ public: [[nodiscard]] Point end_point_for_aspect_ratio(Point const& previous_end_point, float aspect_ratio) const; template - [[nodiscard]] Point to_type() const + requires(!IsSame) + [[nodiscard]] Point to_type() const { return Point(*this); } diff --git a/Userland/Libraries/LibGfx/Rect.h b/Userland/Libraries/LibGfx/Rect.h index 493fe2fa03..99e6566913 100644 --- a/Userland/Libraries/LibGfx/Rect.h +++ b/Userland/Libraries/LibGfx/Rect.h @@ -949,7 +949,8 @@ public: } template - [[nodiscard]] ALWAYS_INLINE Rect to_type() const + requires(!IsSame) + [[nodiscard]] ALWAYS_INLINE Rect to_type() const { return Rect(*this); } diff --git a/Userland/Libraries/LibGfx/Size.h b/Userland/Libraries/LibGfx/Size.h index 16f3a623e6..6e5931432c 100644 --- a/Userland/Libraries/LibGfx/Size.h +++ b/Userland/Libraries/LibGfx/Size.h @@ -176,7 +176,8 @@ public: } template - [[nodiscard]] ALWAYS_INLINE constexpr Size to_type() const + requires(!IsSame) + [[nodiscard]] ALWAYS_INLINE constexpr Size to_type() const { return Size(*this); }