AK: Add user defined literals for Time for ns, us, ms, and sec

We can use these to simplify Time constants throughout the codebase,
turning Time::from_milliseconds(10) into 10_ms, for example.
This commit is contained in:
Andrew Kaster 2023-01-01 13:16:23 -07:00 committed by Linus Groh
parent 8f4d2486dd
commit f7025435b2

View file

@ -383,6 +383,16 @@ inline bool operator!=(T const& a, T const& b)
return a.tv_sec != b.tv_sec || a.tv_nsec != b.tv_nsec;
}
// To use these, add a ``using namespace AK::TimeLiterals`` at block or file scope
namespace TimeLiterals {
constexpr Time operator""_ns(unsigned long long nanoseconds) { return Time::from_nanoseconds(static_cast<i64>(nanoseconds)); }
constexpr Time operator""_us(unsigned long long microseconds) { return Time::from_microseconds(static_cast<i64>(microseconds)); }
constexpr Time operator""_ms(unsigned long long milliseconds) { return Time::from_milliseconds(static_cast<i64>(milliseconds)); }
constexpr Time operator""_sec(unsigned long long seconds) { return Time::from_seconds(static_cast<i64>(seconds)); }
}
}
#if USING_AK_GLOBALLY