summaryrefslogtreecommitdiff
path: root/AK/Time.h
diff options
context:
space:
mode:
authorAndrew Kaster <akaster@serenityos.org>2023-01-02 00:27:05 -0700
committerLinus Groh <mail@linusgroh.de>2023-01-07 14:51:04 +0100
commita8fcd39b885129048652416aa2549183831704b1 (patch)
tree13c7cbc0e84bbacd01cfffad947c2c5bd7dbdcde /AK/Time.h
parent83ad5bfba0840dfbf2803f76bdf06110c254f9ad (diff)
downloadserenity-a8fcd39b885129048652416aa2549183831704b1.zip
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.
Diffstat (limited to 'AK/Time.h')
-rw-r--r--AK/Time.h19
1 files changed, 14 insertions, 5 deletions
diff --git a/AK/Time.h b/AK/Time.h
index 526e55f763..0b13b4a48e 100644
--- a/AK/Time.h
+++ b/AK/Time.h
@@ -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)