diff options
Diffstat (limited to 'compat')
-rw-r--r-- | compat/timer.cpp | 13 | ||||
-rw-r--r-- | compat/timer.hpp | 36 |
2 files changed, 19 insertions, 30 deletions
diff --git a/compat/timer.cpp b/compat/timer.cpp index 162ba16f..ef139d74 100644 --- a/compat/timer.cpp +++ b/compat/timer.cpp @@ -11,6 +11,8 @@ #include "timer.hpp" #include <cmath> +using time_type = Timer::time_type; + Timer::Timer() { start(); @@ -23,21 +25,21 @@ void Timer::start() // nanoseconds -long long Timer::elapsed_nsecs() const +Timer::time_type Timer::elapsed_nsecs() const { timespec cur{}; gettime(&cur); return conv_nsecs(cur); } -long long Timer::conv_nsecs(const struct timespec& cur) const +Timer::time_type Timer::conv_nsecs(const struct timespec& cur) const { return (cur.tv_sec - state.tv_sec) * 1000000000LL + (cur.tv_nsec - state.tv_nsec); } // microseconds -double Timer::elapsed_usecs() const +time_type Timer::elapsed_usecs() const { timespec cur{}; gettime(&cur); @@ -47,12 +49,12 @@ double Timer::elapsed_usecs() const // milliseconds -double Timer::elapsed_ms() const +time_type Timer::elapsed_ms() const { return elapsed_usecs() / 1000.; } -double Timer::elapsed_seconds() const +Timer::time_type Timer::elapsed_seconds() const { return double(elapsed_nsecs() * 1e-9L); } @@ -77,7 +79,6 @@ static void otr_clock_gettime(timespec* ts) static const LARGE_INTEGER freq = otr_get_clock_frequency(); LARGE_INTEGER d; - (void) QueryPerformanceCounter(&d); using ll = long long; diff --git a/compat/timer.hpp b/compat/timer.hpp index 3ceda093..b8b4ae59 100644 --- a/compat/timer.hpp +++ b/compat/timer.hpp @@ -12,16 +12,12 @@ #include "time.hpp" #include <ctime> +#include <type_traits> -class OTR_COMPAT_EXPORT Timer final +struct OTR_COMPAT_EXPORT Timer final { - struct timespec state; - long long conv_nsecs(const struct timespec& cur) const; - - static void gettime(struct timespec* state); + using time_type = time_t; - using ns = time_units::ns; -public: Timer(); void start(); @@ -32,21 +28,13 @@ public: return time_cast<t>(ns(elapsed_nsecs())); } - template<typename t> - bool is_elapsed(t&& time_value) - { - using namespace time_units; - - if (unlikely(elapsed<ns>() >= time_value)) - { - start(); - return true; - } - return false; - } - - long long elapsed_nsecs() const; - double elapsed_usecs() const; - double elapsed_ms() const; - double elapsed_seconds() const; + time_type elapsed_nsecs() const; + time_type elapsed_usecs() const; + time_type elapsed_ms() const; + time_type elapsed_seconds() const; +private: + struct timespec state; + static void gettime(struct timespec* state); + time_type conv_nsecs(const struct timespec& cur) const; + using ns = time_units::ns; }; |