2022-08-13 12:33:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
|
|
|
#include <windows.h>
|
|
|
|
#else
|
|
|
|
#include <chrono>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
namespace orange
|
|
|
|
{
|
|
|
|
#if defined(_WIN32)
|
|
|
|
struct Time
|
|
|
|
{
|
|
|
|
static constexpr bool is_steady = true;
|
|
|
|
|
|
|
|
using rep = int64_t;
|
|
|
|
using period = std::nano;
|
|
|
|
using duration = std::chrono::nanoseconds;
|
|
|
|
using time_point = std::chrono::time_point<Time>;
|
|
|
|
|
|
|
|
static inline time_point now() noexcept
|
|
|
|
{
|
|
|
|
// Keep the frequency static, this doesn't change at all.
|
2022-08-25 07:27:11 +01:00
|
|
|
static const int64_t freq = get_frequency();
|
|
|
|
const int64_t counter = get_counter();
|
2022-08-13 12:33:38 +01:00
|
|
|
|
|
|
|
const int64_t whole = (counter / freq) * period::den;
|
|
|
|
const int64_t part = (counter % freq) * period::den / freq;
|
|
|
|
|
|
|
|
return time_point(duration(whole + part));
|
|
|
|
}
|
|
|
|
|
2022-08-25 07:27:11 +01:00
|
|
|
static inline int64_t get_frequency() noexcept
|
2022-08-13 12:33:38 +01:00
|
|
|
{
|
|
|
|
LARGE_INTEGER freq;
|
|
|
|
QueryPerformanceFrequency(&freq);
|
|
|
|
|
|
|
|
return freq.QuadPart;
|
|
|
|
}
|
|
|
|
|
2022-08-25 07:27:11 +01:00
|
|
|
static inline int64_t get_counter() noexcept
|
2022-08-13 12:33:38 +01:00
|
|
|
{
|
|
|
|
LARGE_INTEGER count;
|
|
|
|
QueryPerformanceCounter(&count);
|
|
|
|
|
|
|
|
return count.QuadPart;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
using Time = std::chrono::high_resolution_clock;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using TimePoint = Time::time_point;
|
|
|
|
|
|
|
|
using Seconds = std::chrono::duration<float>;
|
|
|
|
}
|