libqi-api  2.8.7.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
clock.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 Aldebaran Robotics. All rights reserved.
3  * Use of this source code is governed by a BSD-style license that can be
4  * found in the COPYING file.
5  */
6 
7 #pragma once
8 #ifndef _QI_CLOCK_HPP_
9 # define _QI_CLOCK_HPP_
10 
11 # include <qi/api.hpp>
12 # include <qi/types.hpp>
13 # include <boost/chrono/chrono.hpp>
14 
15 namespace qi
16 {
18 
19  template<class Rep, class Ratio>
20  using DurationType = boost::chrono::duration<Rep, Ratio>;
21 
22  template<class ClockType>
23  using TimePoint = boost::chrono::time_point<ClockType>;
24 
31 
33 
43  {
44  public:
45  using rep = Duration::rep;
46  using period = Duration::period;
47  using duration = Duration;
48 
54 
59  BOOST_STATIC_CONSTEXPR bool is_steady = boost::chrono::steady_clock::is_steady;
60 
61  public:
64 
66  enum Expect {
69  Expect_Sooner
70  };
71 
73  static time_point now();
74  };
75 
76 
85  class QI_API Clock
86  {
87  public:
88  using rep = Duration::rep;
89  using period = Duration::period;
90  using duration = Duration;
91 
96  using time_point = boost::chrono::time_point<Clock>;
97 
102  BOOST_STATIC_CONSTEXPR bool is_steady = boost::chrono::steady_clock::is_steady;
103 
104  public:
106  enum Expect {
109  Expect_Sooner
110  };
111 
113  static time_point now();
114 
123  static uint32_t toUint32ms(const time_point &t) throw();
132  static int32_t toInt32ms(const time_point &t) throw();
133 
153  static time_point fromUint32ms(uint32_t t_ms, time_point guess,
154  Expect expect=Expect_SoonerOrLater) throw();
156  static time_point fromInt32ms(int32_t t_ms, time_point guess,
157  Expect expect=Expect_SoonerOrLater) throw();
158  };
159 
168  {
169  public:
170  using rep = Duration::rep;
171  using period = Duration::period;
172  using duration = Duration;
173 
178  using time_point = boost::chrono::time_point<SystemClock>;
179 
182  BOOST_STATIC_CONSTEXPR bool is_steady = false;
183 
184  public:
185 
188 
190  static time_point now();
191 
197  static std::time_t to_time_t(const time_point& t) throw();
198 
199  // Converts std::time_t to a system clock time point
205  static time_point from_time_t(const std::time_t &t) throw();
206  };
207 
209  typedef SystemClock WallClock;
210 
214 
217 
221  return SteadyClock::now();
222  }
223 
227  return SystemClock::now();
228  }
229 
232  QI_API void sleepFor(const qi::Duration& d);
233  template <class Rep, class Period>
234  inline void sleepFor(const DurationType<Rep, Period>& d);
236 
241  QI_API void sleepUntil(const SteadyClockTimePoint &t);
242  template <class Duration>
243  inline void sleepUntil(const boost::chrono::time_point<SteadyClock, Duration>& t);
245 
248  QI_API void sleepUntil(const ClockTimePoint &t);
249  template <class Duration>
250  inline void sleepUntil(const boost::chrono::time_point<Clock, Duration>& t);
252 
259  QI_API void sleepUntil(const SystemClockTimePoint& t);
260  template <class Duration>
261  inline void sleepUntil(const boost::chrono::time_point<SystemClock, Duration>& t);
263 
272  QI_API std::string toISO8601String(const SystemClockTimePoint &t);
274 
277  template <class DurationTo, class TimePointFrom>
278  inline DurationTo durationSince(const TimePointFrom& t);
280 
281  template <class R, class P>
282  inline std::string to_string(const DurationType<R, P> &d);
283 
284  template <class C, class D>
285  inline std::string to_string(const boost::chrono::time_point<C, D> &t);
286 }
287 
288 # ifdef __APPLE__
289  // export template instanciation for RTTI issues across libraries. (mostly for OSX)
290  template class QI_API boost::chrono::duration<int64_t, boost::nano>;
291  template class QI_API boost::chrono::duration<int64_t, boost::micro>;
292  template class QI_API boost::chrono::duration<int64_t, boost::milli>;
293  template class QI_API boost::chrono::duration<int64_t>;
294  template class QI_API boost::chrono::duration<int64_t, boost::ratio<60> >;
295  template class QI_API boost::chrono::duration<int64_t, boost::ratio<3600> >;
296  template class QI_API boost::chrono::time_point<qi::SteadyClock>;
297  template class QI_API boost::chrono::time_point<qi::SystemClock>;
298 # endif
299 
300 # include <qi/detail/clock.hxx>
301 
302 #endif // _QI_OS_HPP_
DurationTo durationSince(const TimePointFrom &t)
}@
Definition: clock.hxx:41
DurationType< int64_t, boost::micro > MicroSeconds
Definition: clock.hpp:26
Duration::rep rep
The representation type of the duration and time_point.
Definition: clock.hpp:88
#define QI_API
Definition: api.hpp:33
uint32_t uint32_t
Definition: types.hpp:65
Result is expected to be later than user-provided reference.
Definition: clock.hpp:108
Duration::period period
The tick period of the clock in nanoseconds.
Definition: clock.hpp:89
SteadyClock::time_point SteadyClockTimePoint
Steady clock time point.
Definition: clock.hpp:211
Duration duration
The duration type of the clock.
Definition: clock.hpp:172
void sleepFor(const qi::Duration &d)
The SystemClock class represents the system-wide real time wall clock. It may not be monotonic: on mo...
Definition: clock.hpp:167
TimePoint< SteadyClock > time_point
Definition: clock.hpp:53
Duration duration
The duration type of the clock.
Definition: clock.hpp:47
Expect
Enum expected argument.
Definition: clock.hpp:106
DurationType< int64_t, boost::ratio< 3600 >> Hours
Definition: clock.hpp:30
std::string to_string(const DurationType< R, P > &d)
Duration::rep rep
The representation type of the duration and time_point.
Definition: clock.hpp:45
DurationType< int64_t, boost::ratio< 60 >> Minutes
Definition: clock.hpp:29
dll import/export and compiler message
Result is expected to be later than user-provided reference.
Definition: clock.hpp:68
std::string toISO8601String(const SystemClockTimePoint &t)
Return the date and time as a string in ISO 8601 format. The time is given up to millisecond precisio...
Duration duration
The duration type of the clock.
Definition: clock.hpp:90
NanoSeconds Duration
Definition: clock.hpp:32
DurationType< int64_t, boost::nano > NanoSeconds
Definition: clock.hpp:25
QI_API_DEPRECATED typedef SystemClock WallClock
Definition: clock.hpp:209
SystemClock::time_point SystemClockTimePoint
System clock time point.
Definition: clock.hpp:213
The SteadyClock class represents a monotonic clock. <includename>qi/clock.hpp</includename> ...
Definition: clock.hpp:42
DurationType< int64_t, boost::milli > MilliSeconds
Definition: clock.hpp:27
QI_API_DEPRECATED SystemClockTimePoint wallClockNow()
Returns a time_point representing the current value of the clock.
Definition: clock.hpp:226
QI_API_DEPRECATED SteadyClockTimePoint steadyClockNow()
Returns a time_point representing the current value of the clock.
Definition: clock.hpp:220
Duration::rep rep
The representation type of the duration and time_point.
Definition: clock.hpp:170
Pick the nearest result to user-provided reference.
Definition: clock.hpp:107
int32_t int32_t
Definition: types.hpp:60
QI_API_DEPRECATED typedef time_point WallClockTimePoint
Definition: clock.hpp:187
Duration::period period
The tick period of the clock in nanoseconds.
Definition: clock.hpp:171
Duration::period period
The tick period of the clock in nanoseconds.
Definition: clock.hpp:46
Clock::time_point ClockTimePoint
qi::Clock time point.
Definition: clock.hpp:212
QI_API_DEPRECATED typedef SystemClockTimePoint WallClockTimePoint
System clock time point.
Definition: clock.hpp:216
Expect
Enum expected argument.
Definition: clock.hpp:66
static time_point now()
Returns a time_point representing the current value of the clock.
QI_API_DEPRECATED typedef time_point SteadyClockTimePoint
Definition: clock.hpp:63
void sleepUntil(const SteadyClockTimePoint &t)
Blocks the execution of the current thread until t has been reached.
boost::chrono::time_point< Clock > time_point
Definition: clock.hpp:96
DurationType< int64_t, boost::ratio< 1 >> Seconds
Definition: clock.hpp:28
boost::chrono::time_point< SystemClock > time_point
Definition: clock.hpp:178
boost::chrono::time_point< ClockType > TimePoint
Definition: clock.hpp:23
static time_point now()
Returns a time_point representing the current value of the clock.
#define QI_API_DEPRECATED
Compiler flags to mark a function as deprecated. It will generate a compiler warning.
Definition: macro.hpp:41
boost::chrono::duration< Rep, Ratio > DurationType
Convenience typedefs.
Definition: clock.hpp:20
The Clock class represents a system-wide clock, best suitable for timestamping events. Typically monotonic and unaffected by the system clock adjustment, altough this is not guaranteed.
Definition: clock.hpp:85
Pick the nearest result to user-provided reference.
Definition: clock.hpp:67