1
0
mirror of https://github.com/SerenityOS/serenity synced 2024-07-09 10:40:45 +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)

View File

@ -475,3 +475,18 @@ TEST_CASE(years_to_days_since_epoch_span)
EXPECT_EQ(actual_days, expected_days);
}
}
TEST_CASE(user_defined_literals)
{
using namespace AK::TimeLiterals;
static_assert(Time::from_nanoseconds(123) == 123_ns, "Factory is same as UDL");
static_assert(100_ms > 10_ms, "LT UDL");
static_assert(1000_ns == 1_us, "EQ UDL");
static_assert(1_sec > 1_ms, "GT UDL");
static_assert(100_ms >= 100'000_us, "GE UDL (eq)");
static_assert(100_ms >= 99'999_us, "GE UDL (gt)");
static_assert(100_ms <= 100'000_us, "LE UDL (eq)");
static_assert(100_ms <= 100'001_us, "LE UDL (lt)");
static_assert(1_sec != 2_sec, "NE UDL");
}