mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 00:50:22 +00:00
AK: Restrict timespec comparison operator overloads in AK::Time
The previous implementation was too generic, and would cause conflicting operator overload errors when included in certain code paths. Fix this by restricting the template parameters to types which have the same member names as `struct timespec`.
This commit is contained in:
parent
ccae0cae45
commit
a2a5af9745
Notes:
sideshowbarker
2024-07-18 08:52:17 +09:00
Author: https://github.com/bgianfo Commit: https://github.com/SerenityOS/serenity/commit/a2a5af97451 Pull-request: https://github.com/SerenityOS/serenity/pull/8830 Reviewed-by: https://github.com/alimpfard ✅
1 changed files with 21 additions and 12 deletions
33
AK/Time.h
33
AK/Time.h
|
@ -15,6 +15,14 @@
|
|||
struct timeval;
|
||||
struct timespec;
|
||||
|
||||
// Concept to detect types which look like timespec without requiring the type.
|
||||
template<typename T>
|
||||
concept TimeSpecType = requires(T t)
|
||||
{
|
||||
t.tv_sec;
|
||||
t.tv_nsec;
|
||||
};
|
||||
|
||||
namespace AK {
|
||||
|
||||
// Month and day start at 1. Month must be >= 1 and <= 12.
|
||||
|
@ -241,38 +249,39 @@ inline void timespec_to_timeval(const TimespecType& ts, TimevalType& tv)
|
|||
tv.tv_usec = ts.tv_nsec / 1000;
|
||||
}
|
||||
|
||||
template<typename TimespecType>
|
||||
inline bool operator>=(const TimespecType& a, const TimespecType& b)
|
||||
template<TimeSpecType T>
|
||||
inline bool operator>=(const T& a, const T& b)
|
||||
{
|
||||
return a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec >= b.tv_nsec);
|
||||
}
|
||||
|
||||
template<typename TimespecType>
|
||||
inline bool operator>(const TimespecType& a, const TimespecType& b)
|
||||
template<TimeSpecType T>
|
||||
inline bool operator>(const T& a, const T& b)
|
||||
{
|
||||
return a.tv_sec > b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec > b.tv_nsec);
|
||||
}
|
||||
|
||||
template<typename TimespecType>
|
||||
inline bool operator<(const TimespecType& a, const TimespecType& b)
|
||||
template<TimeSpecType T>
|
||||
inline bool operator<(const T& a, const T& b)
|
||||
{
|
||||
return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec < b.tv_nsec);
|
||||
}
|
||||
|
||||
template<typename TimespecType>
|
||||
inline bool operator<=(const TimespecType& a, const TimespecType& b)
|
||||
template<TimeSpecType T>
|
||||
inline bool operator<=(const T& a, const T& b)
|
||||
|
||||
{
|
||||
return a.tv_sec < b.tv_sec || (a.tv_sec == b.tv_sec && a.tv_nsec <= b.tv_nsec);
|
||||
}
|
||||
|
||||
template<typename TimespecType>
|
||||
inline bool operator==(const TimespecType& a, const TimespecType& b)
|
||||
template<TimeSpecType T>
|
||||
inline bool operator==(const T& a, const T& b)
|
||||
{
|
||||
return a.tv_sec == b.tv_sec && a.tv_nsec == b.tv_nsec;
|
||||
}
|
||||
|
||||
template<typename TimespecType>
|
||||
inline bool operator!=(const TimespecType& a, const TimespecType& b)
|
||||
template<TimeSpecType T>
|
||||
inline bool operator!=(const T& a, const T& b)
|
||||
{
|
||||
return a.tv_sec != b.tv_sec || a.tv_nsec != b.tv_nsec;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue