浏览代码

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.
Andrew Kaster 2 年之前
父节点
当前提交
f7025435b2
共有 1 个文件被更改,包括 10 次插入0 次删除
  1. 10 0
      AK/Time.h

+ 10 - 0
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;
     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
 #if USING_AK_GLOBALLY