1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-08-02 03:07:35 +00:00

AK: Reimplement comparisons on AK::Time using operator<=>

This allows us to make all comparision operators on the class constexpr
without pulling in a bunch of boilerplate. We don't use the `<compare>`
header because it doesn't compile in the main serenity cross-build due
to the include paths to LibC being incompatible with how libc++ expects
them to be for clang builds.
This commit is contained in:
Andrew Kaster 2023-01-02 00:27:05 -07:00 committed by Linus Groh
parent 83ad5bfba0
commit a8fcd39b88
3 changed files with 29 additions and 25 deletions

View file

@ -256,26 +256,6 @@ Time& Time::operator-=(Time const& other)
return *this;
}
bool Time::operator<(Time const& other) const
{
return m_seconds < other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds < other.m_nanoseconds);
}
bool Time::operator<=(Time const& other) const
{
return m_seconds < other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds <= other.m_nanoseconds);
}
bool Time::operator>(Time const& other) const
{
return m_seconds > other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds > other.m_nanoseconds);
}
bool Time::operator>=(Time const& other) const
{
return m_seconds > other.m_seconds || (m_seconds == other.m_seconds && m_nanoseconds >= other.m_nanoseconds);
}
Time Time::from_half_sanitized(i64 seconds, i32 extra_seconds, u32 nanoseconds)
{
VERIFY(nanoseconds < 1'000'000'000);

View file

@ -254,15 +254,24 @@ public:
[[nodiscard]] bool is_zero() const { return (m_seconds == 0) && (m_nanoseconds == 0); }
[[nodiscard]] bool is_negative() const { return m_seconds < 0; }
bool operator==(Time const& other) const { return this->m_seconds == other.m_seconds && this->m_nanoseconds == other.m_nanoseconds; }
Time operator+(Time const& other) const;
Time& operator+=(Time const& other);
Time operator-(Time const& other) const;
Time& operator-=(Time const& other);
bool operator<(Time const& other) const;
bool operator<=(Time const& other) const;
bool operator>(Time const& other) const;
bool operator>=(Time const& other) const;
constexpr bool operator==(Time const& other) const = default;
constexpr int operator<=>(Time const& other) const
{
if (int cmp = (m_seconds > other.m_seconds ? 1 : m_seconds < other.m_seconds ? -1
: 0);
cmp != 0)
return cmp;
if (int cmp = (m_nanoseconds > other.m_nanoseconds ? 1 : m_nanoseconds < other.m_nanoseconds ? -1
: 0);
cmp != 0)
return cmp;
return 0;
}
private:
constexpr explicit Time(i64 seconds, u32 nanoseconds)