From a77c1282d047a1c0f64ca09c62596f0868fe5b1f Mon Sep 17 00:00:00 2001 From: Nadav Har'El <nyh@cloudius-systems.com> Date: Wed, 15 Jan 2014 09:49:04 +0200 Subject: [PATCH] osv/clock.hh: Add std::chrono::duration literals In <drivers/clock.hh> we had support for time literals looking like 100_ns, 100_us, 100_ms, 100_s. These simply translated to an integer number of nanoseconds. Added to <osv/clock.hh> similar literals, but using strongly-typed std::chrono::duration, so that 100_ms translates to std::chrono::milliseconds(100), and this is, as usual, automatically scaled by the compiler to other units when other units (e.g., nanoseconds) are required. Because the literals of <drivers/clock.hh> and <osv/clock.hh> have the same name, to use the latter you need to explicitly enable them in a scope using the statement using namespace osv::clock::literals; One the entire codebase is converted to use <osv/clock.hh> and std::chrono, the old literals from <drivers/clock.hh> should be removed. Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com> Signed-off-by: Pekka Enberg <penberg@cloudius-systems.com> --- include/osv/clock.hh | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/include/osv/clock.hh b/include/osv/clock.hh index 6defff6cd..c5390f6c2 100644 --- a/include/osv/clock.hh +++ b/include/osv/clock.hh @@ -42,8 +42,43 @@ public: return time_point(duration(::clock::get()->uptime())); } }; + +/** + * Convenient literals for specifying std::chrono::duration's. + * + * C++14 will support std::chrono::duration literals, used as in the following + * example: + * @code + * using namespace std::literals::chrono_literals; + * auto d = 100ns; + * @endcode + * Because these are not yet available to us, we implement a similar feature + * but using different names: + * @code + * using namespace osv::clock::literals; + * auto d = 100_ns; + * @endcode + */ +namespace literals { +/** Nanoseconds */ +constexpr std::chrono::nanoseconds operator "" _ns(unsigned long long c) { + return std::chrono::nanoseconds(c); +} +/** Microseconds */ +constexpr std::chrono::microseconds operator "" _us(unsigned long long c) { + return std::chrono::microseconds(c); +} +/** Milliseconds */ +constexpr std::chrono::milliseconds operator "" _ms(unsigned long long c) { + return std::chrono::milliseconds(c); +} +/** Seconds */ +constexpr std::chrono::seconds operator "" _s(unsigned long long c) { + return std::chrono::seconds(c); } } +} +} #endif /* OSV_CLOCK_HH_ */ -- GitLab