OSVR-Core
TimeValueChrono.h
Go to the documentation of this file.
1 
11 // Copyright 2016 Sensics, Inc.
12 //
13 // Licensed under the Apache License, Version 2.0 (the "License");
14 // you may not use this file except in compliance with the License.
15 // You may obtain a copy of the License at
16 //
17 // http://www.apache.org/licenses/LICENSE-2.0
18 //
19 // Unless required by applicable law or agreed to in writing, software
20 // distributed under the License is distributed on an "AS IS" BASIS,
21 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 // See the License for the specific language governing permissions and
23 // limitations under the License.
24 
25 #ifndef INCLUDED_TimeValueChrono_h_GUID_62662C09_CE8E_4394_87A9_26D2FC0D2920
26 #define INCLUDED_TimeValueChrono_h_GUID_62662C09_CE8E_4394_87A9_26D2FC0D2920
27 
28 // Internal Includes
29 // - none
30 
31 // Library/third-party includes
32 #include <osvr/Util/TimeValue.h>
33 
34 // Standard includes
35 #include <chrono>
36 #include <cstdint>
37 
38 namespace osvr {
39 namespace util {
40  namespace time {
41 #ifdef OSVR_DUMMY_DEFINE //_WIN32
42  using time_value_period =
46  std::ratio_multiply<std::ratio<100>, std::nano>::type;
47 #else
48  using time_value_period = std::micro;
50 #endif
51 
52  using time_value_scalar_rep = std::uint64_t;
53  using time_value_scalar_duration =
54  std::chrono::duration<time_value_scalar_rep, time_value_period>;
55  } // namespace time
56 } // namespace util
57 } // namespace osvr
58 
59 namespace std {
60 namespace chrono {
62 } // namespace chrono
63 } // namespace std
64 
65 namespace osvr {
66 namespace util {
67  namespace time {
69  public:
73  using period = time_value_period;
74  using duration = std::chrono::duration<rep, period>;
75  using clock = TimeValueClock;
76  using time_point = std::chrono::time_point<clock>;
77 
79  static const bool is_steady = true;
80 
81  static time_point now() {
82  return time_point(duration{osvr::util::time::getNow()});
83  }
84 
88  using scalar_duration = time_value_scalar_duration;
89  };
90 
93  getTimeValue(TimeValueClock::time_point const &tp) {
94  return tp.time_since_epoch().count();
95  }
96 
99  std::chrono::duration<OSVR_TimeValue_Seconds>;
102  std::chrono::duration<OSVR_TimeValue_Microseconds, std::micro>;
103  namespace detail {
106  template <typename Derived> class TimeValueChronoWrapperBase {
107  public:
108  using derived_type = Derived;
109 
110  OSVR_TimeValue_Seconds getSecondsPart() const {
111  return get().seconds;
112  }
113  time_value_seconds_duration getSecondsPartAsDuration() const {
114  return time_value_seconds_duration{getSecondsPart()};
115  }
116  OSVR_TimeValue_Microseconds getMicrosecondsPart() const {
117  return get().microseconds;
118  }
119 
121  getMicrosecondsPartAsDuration() const {
123  getMicrosecondsPart()};
124  }
125 
126  time_value_scalar_duration toScalarDuration() const {
127  using std::chrono::duration_cast;
128  return duration_cast<time_value_scalar_duration>(
129  getSecondsPartAsDuration()) +
130  duration_cast<time_value_scalar_duration>(
131  getMicrosecondsPartAsDuration());
132  }
133 
134  derived_type const &derived() const {
135  return *static_cast<derived_type const *>(this);
136  }
137  util::time::TimeValue const &get() const {
138  return derived().get();
139  }
140  };
141  } // namespace detail
142 
146  TimeValueChronoValueWrapper> {
147  public:
149  util::time::TimeValue const &tv)
150  : m_timeval(tv){};
151  util::time::TimeValue const &get() const { return m_timeval; }
152 
153  private:
154  util::time::TimeValue m_timeval;
155  };
156 
160  TimeValueChronoConstRefWrapper> {
161  public:
163  util::time::TimeValue const &tv)
164  : m_timeval(tv) {}
168  : m_timeval(other.m_timeval) {}
171  TimeValueChronoConstRefWrapper const &) = delete;
174  operator=(TimeValueChronoConstRefWrapper const &) = delete;
176  util::time::TimeValue const &get() const { return m_timeval; }
177 
178  private:
179  util::time::TimeValue const &m_timeval;
180  };
181 
182  namespace detail {
184  wrap_time_value(util::time::TimeValue const &tv) {
186  }
188  wrap_time_value(TimeValueClock::time_point const &tp) {
190  tp.time_since_epoch().count()};
191  }
192  } // namespace detail
193 
200  template <typename DerivedA, typename DerivedB>
201  inline time_value_scalar_duration
204  using namespace std::chrono;
205  auto diffSeconds = time_value_seconds_duration{a.getSecondsPart() -
206  b.getSecondsPart()};
207  auto diffUsec = time_value_microseconds_duration{
208  a.getMicrosecondsPart() - b.getMicrosecondsPart()};
209  return duration_cast<time_value_scalar_duration>(diffSeconds) +
210  duration_cast<time_value_scalar_duration>(diffUsec);
211  }
212 
214  template <typename Derived>
215  inline time_value_scalar_duration
217  util::time::TimeValue const &b) {
218  return a - detail::wrap_time_value(b);
219  }
221  template <typename Derived>
222  inline time_value_scalar_duration
225  return detail::wrap_time_value(a) - b;
226  }
227 
231  inline time_value_scalar_duration
232  operator-(TimeValueClock::time_point const &a,
233  TimeValueClock::time_point const &b) {
234  return detail::wrap_time_value(a) - detail::wrap_time_value(b);
235  }
236 
238  template <typename Derived>
239  inline time_value_scalar_duration
240  operator-(TimeValueClock::time_point const &a,
242  return detail::wrap_time_value(a) - b;
243  }
244 
246  template <typename Derived>
247  inline time_value_scalar_duration
249  TimeValueClock::time_point const &b) {
250  return a - detail::wrap_time_value(b);
251  }
252 
254  inline time_value_scalar_duration
255  operator-(TimeValueClock::time_point const &a,
256  util::time::TimeValue const &b) {
257  return detail::wrap_time_value(a) - detail::wrap_time_value(b);
258  }
259 
261  template <typename Derived>
262  inline time_value_scalar_duration
264  TimeValueClock::time_point const &b) {
265  return detail::wrap_time_value(a) - detail::wrap_time_value(b);
266  }
267 
269  inline TimeValueClock::time_point
271  return TimeValueClock::time_point{TimeValueClock::duration{tv}};
272  }
273 
277  return detail::wrap_time_value(tv).toScalarDuration();
278  }
279  } // namespace time
280 } // namespace util
281 } // namespace osvr
282 
283 #endif // INCLUDED_TimeValueChrono_h_GUID_62662C09_CE8E_4394_87A9_26D2FC0D2920
A simple argument-pack-based metaprogramming library, inspired by and based partially on https://eric...
Definition: Always.h:41
osvr::util::time::TimeValue getTimeValue(TimeValueClock::time_point const &tp)
Get the TimeValue inside of a TimeValueClock time_point.
Definition: TimeValueChrono.h:93
Definition: RunLoopManager.h:42
time_value_scalar_duration scalar_duration
When we want a short-ish duration (time difference), we can go to a scalar instead of the structure -...
Definition: TimeValueChrono.h:88
TimeValueClock::time_point as_time_point(time::TimeValue const &tv)
Treat/convert a TimeValue into a time point.
Definition: TimeValueChrono.h:270
void getNow(TimeValue &tv)
Set the given TimeValue to the current time.
Definition: TimeValue.h:51
The main namespace for all C++ elements of the framework, internal and external.
Definition: namespace_osvr.dox:3
Definition: TypeSafeIdHash.h:44
CRTP base class for things that wrap a OSVR_TimeValue for the purposes of some interop with std::chro...
Definition: TimeValueChrono.h:106
std::chrono::duration< OSVR_TimeValue_Microseconds, std::micro > time_value_microseconds_duration
Duration representation of the second element of the structure.
Definition: TimeValueChrono.h:102
Copy and store a TimeValue in a wrapper for std::chrono interop.
Definition: TimeValueChrono.h:144
int32_t OSVR_TimeValue_Microseconds
The signed integer type storing the microseconds in a struct OSVR_TimeValue.
Definition: TimeValueC.h:69
Store a reference to const to a TimeValue for std::chrono interop.
Definition: TimeValueChrono.h:158
::OSVR_TimeValue TimeValue
C++-friendly typedef for the OSVR_TimeValue structure.
Definition: TimeValue.h:48
TimeValueClock::scalar_duration to_duration(time::TimeValue const &tv)
Convert a TimeValue into a scalar duration.
Definition: TimeValueChrono.h:276
Definition: newuoa.h:1888
int64_t OSVR_TimeValue_Seconds
The signed integer type storing the seconds in a struct OSVR_TimeValue.
Definition: TimeValueC.h:66
Definition: TimeValueChrono.h:68
Definition: TimeValueChrono.h:60
TimeValueChronoConstRefWrapper(TimeValueChronoConstRefWrapper &&other)
movable
Definition: TimeValueChrono.h:166
time_value_scalar_duration operator-(detail::TimeValueChronoWrapperBase< DerivedA > const &a, detail::TimeValueChronoWrapperBase< DerivedB > const &b)
Subtract one wrapped TimeValue from another and get a std::duration back!
Definition: TimeValueChrono.h:202
Header providing a C++ wrapper around TimeValueC.h.
std::chrono::duration< OSVR_TimeValue_Seconds > time_value_seconds_duration
Duration representation of the first element in the structure.
Definition: TimeValueChrono.h:99
Standardized, portable parallel to struct timeval for representing both absolute times and time inter...
Definition: TimeValueC.h:81
std::micro time_value_period
Assumes the max representable resolution otherwise.
Definition: TimeValueChrono.h:49