mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 09:34:57 +09: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:
parent
83ad5bfba0
commit
a8fcd39b88
Notes:
sideshowbarker
2024-07-17 11:30:05 +09:00
Author: https://github.com/ADKaster
Commit: a8fcd39b88
Pull-request: https://github.com/SerenityOS/serenity/pull/16771
Reviewed-by: https://github.com/kleinesfilmroellchen
Reviewed-by: https://github.com/linusg ✅
3 changed files with 29 additions and 25 deletions
19
AK/Time.h
19
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue