Time.h 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #pragma once
  2. #include "../C/Baselib_Timer.h"
  3. #include <chrono>
  4. #include <cmath>
  5. namespace baselib
  6. {
  7. BASELIB_CPP_INTERFACE
  8. {
  9. using timeout_ms = std::chrono::duration<uint32_t, std::milli>;
  10. using timeout_us = std::chrono::duration<uint64_t, std::micro>;
  11. struct high_precision_clock
  12. {
  13. using duration = std::chrono::duration<double, std::nano>;
  14. using time_point = std::chrono::time_point<high_precision_clock, duration>;
  15. using rep = duration::rep;
  16. using period = duration::period;
  17. static constexpr bool is_steady = true;
  18. static time_point now()
  19. {
  20. return time_point_from_ticks(now_in_ticks());
  21. }
  22. static Baselib_Timer_Ticks now_in_ticks()
  23. {
  24. return Baselib_Timer_GetHighPrecisionTimerTicks();
  25. }
  26. static duration duration_from_ticks(Baselib_Timer_Ticks ticks)
  27. {
  28. return duration(ticks * Baselib_Timer_TickToNanosecondsConversionFactor);
  29. }
  30. static Baselib_Timer_Ticks ticks_from_duration_roundup(duration d)
  31. {
  32. double ticks = d.count() / Baselib_Timer_TickToNanosecondsConversionFactor;
  33. return (Baselib_Timer_Ticks)std::ceil(ticks);
  34. }
  35. static time_point time_point_from_ticks(Baselib_Timer_Ticks ticks)
  36. {
  37. return time_point(duration_from_ticks(ticks));
  38. }
  39. };
  40. }
  41. }