mirror of
https://github.com/SerenityOS/serenity
synced 2024-10-15 04:13:11 +00:00
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:
parent
8f4d2486dd
commit
f7025435b2
10
AK/Time.h
10
AK/Time.h
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue