ILLIXR: Illinois Extended Reality Testbed
relative_clock.hpp
1 #pragma once
2 
3 #include "phonebook.hpp"
4 
5 #include <cassert>
6 #include <chrono>
7 #include <ratio>
8 
9 namespace ILLIXR {
10 
22 using _clock_rep = long;
23 using _clock_period = std::nano;
24 using _clock_duration = std::chrono::duration<_clock_rep, _clock_period>;
25 
26 class time_point {
27 public:
28  using duration = _clock_duration;
29 
30  time_point() = default;
31 
32  constexpr explicit time_point(const duration& time_since_epoch)
33  : _m_time_since_epoch{time_since_epoch} { }
34 
35  [[nodiscard]] duration time_since_epoch() const {
36  return _m_time_since_epoch;
37  }
38 
39  time_point& operator+=(const duration& d) {
40  this->_m_time_since_epoch += d;
41  return *this;
42  }
43 
44  time_point& operator-=(const duration& d) {
45  this->_m_time_since_epoch -= d;
46  return *this;
47  }
48 
49 private:
50  duration _m_time_since_epoch;
51 };
52 
53 inline time_point::duration operator-(const time_point& lhs, const time_point& rhs) {
54  return lhs.time_since_epoch() - rhs.time_since_epoch();
55 }
56 
57 inline time_point operator+(const time_point& pt, const time_point::duration& d) {
58  return time_point(pt.time_since_epoch() + d);
59 }
60 
61 inline time_point operator+(const time_point::duration& d, const time_point& pt) {
62  return time_point(pt.time_since_epoch() + d);
63 }
64 
65 inline bool operator<(const time_point& lhs, const time_point& rhs) {
66  return lhs.time_since_epoch() < rhs.time_since_epoch();
67 }
68 
69 inline bool operator>(const time_point& lhs, const time_point& rhs) {
70  return lhs.time_since_epoch() > rhs.time_since_epoch();
71 }
72 
73 inline bool operator<=(const time_point& lhs, const time_point& rhs) {
74  return lhs.time_since_epoch() <= rhs.time_since_epoch();
75 }
76 
77 inline bool operator>=(const time_point& lhs, const time_point& rhs) {
78  return lhs.time_since_epoch() >= rhs.time_since_epoch();
79 }
80 
81 inline bool operator==(const time_point& lhs, const time_point& rhs) {
82  return lhs.time_since_epoch() == rhs.time_since_epoch();
83 }
84 
85 inline bool operator!=(const time_point& lhs, const time_point& rhs) {
86  return lhs.time_since_epoch() != rhs.time_since_epoch();
87 }
88 
103 public:
104  using rep = _clock_rep;
105  using period = _clock_period;
106  using duration = _clock_duration;
107  // using time_point = time_point;
108  static constexpr bool is_steady = true;
109  static_assert(std::chrono::steady_clock::is_steady);
110 
111  [[nodiscard]] time_point now() const {
112  assert(this->is_started() && "Can't call now() before this clock has been start()ed.");
113  return time_point{std::chrono::steady_clock::now() - _m_start};
114  }
115 
116  int64_t absolute_ns(time_point relative) {
117  return std::chrono::nanoseconds{_m_start.time_since_epoch()}.count() +
118  std::chrono::nanoseconds{relative.time_since_epoch()}.count();
119  }
120 
124  void start() {
125  _m_start = std::chrono::steady_clock::now();
126  }
127 
131  [[nodiscard]] bool is_started() const {
132  return _m_start > std::chrono::steady_clock::time_point{};
133  }
134 
138  [[nodiscard]] time_point start_time() const {
139  return time_point{_m_start.time_since_epoch()};
140  }
141 
142 private:
143  std::chrono::steady_clock::time_point _m_start;
144 };
145 
146 using duration = RelativeClock::duration;
147 
148 template<typename unit = std::ratio<1>>
149 double duration2double(duration dur) {
150  return std::chrono::duration<double, unit>{dur}.count();
151 }
152 
153 constexpr duration freq2period(double fps) {
154  return duration{static_cast<size_t>(std::chrono::nanoseconds{std::chrono::seconds{1}}.count() / fps)};
155 }
156 
157 } // namespace ILLIXR
Relative clock for all of ILLIXR.
Definition: relative_clock.hpp:102
bool is_started() const
Check if the clock is started.
Definition: relative_clock.hpp:131
void start()
Starts the clock. All times are relative to this point.
Definition: relative_clock.hpp:124
time_point start_time() const
Get the start time of the clock.
Definition: relative_clock.hpp:138
A 'service' that can be registered in the phonebook.
Definition: phonebook.hpp:86
RAC_ERRNO_MSG.
Definition: data_format.hpp:15
long _clock_rep
Definition: relative_clock.hpp:22