libqi-api  2.4.3.28
 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  typedef boost::chrono::duration<int64_t, boost::nano> Duration;
19  typedef boost::chrono::duration<int64_t, boost::nano> NanoSeconds;
20  typedef boost::chrono::duration<int64_t, boost::micro> MicroSeconds;
21  typedef boost::chrono::duration<int64_t, boost::milli> MilliSeconds;
22  typedef boost::chrono::duration<int64_t> Seconds;
23  typedef boost::chrono::duration<int64_t, boost::ratio<60> > Minutes;
24  typedef boost::chrono::duration<int64_t, boost::ratio<3600> > Hours;
25 
35  {
36  public:
37  typedef int64_t rep;
38  typedef boost::nano period;
39  typedef boost::chrono::duration<rep, period> duration;
40 
45  typedef boost::chrono::time_point<SteadyClock> time_point;
46 
51  BOOST_STATIC_CONSTEXPR bool is_steady = boost::chrono::steady_clock::is_steady;
52 
53  public:
56 
58  enum Expect {
61  Expect_Sooner
62  };
63 
65  static time_point now();
66  };
67 
68 
76  class QI_API Clock
77  {
78  public:
79  typedef int64_t rep;
80  typedef boost::nano period;
81  typedef boost::chrono::duration<rep, period> duration;
82 
87  typedef boost::chrono::time_point<Clock> time_point;
88 
93  BOOST_STATIC_CONSTEXPR bool is_steady = boost::chrono::steady_clock::is_steady;
94 
95  public:
97  enum Expect {
100  Expect_Sooner
101  };
102 
104  static time_point now();
105 
114  static uint32_t toUint32ms(const time_point &t) throw();
123  static int32_t toInt32ms(const time_point &t) throw();
124 
144  static time_point fromUint32ms(uint32_t t_ms, time_point guess,
145  Expect expect=Expect_SoonerOrLater) throw();
147  static time_point fromInt32ms(int32_t t_ms, time_point guess,
148  Expect expect=Expect_SoonerOrLater) throw();
149  };
150 
159  {
160  public:
161  typedef int64_t rep;
162  typedef boost::nano period;
163  typedef boost::chrono::duration<rep, period> duration;
164 
169  typedef boost::chrono::time_point<SystemClock> time_point;
170 
173  BOOST_STATIC_CONSTEXPR bool is_steady = false;
174 
175  public:
176 
178  typedef time_point WallClockTimePoint;
179 
181  static time_point now();
182 
188  static std::time_t to_time_t(const time_point& t) throw();
189 
190  // Converts std::time_t to a system clock time point
196  static time_point from_time_t(const std::time_t &t) throw();
197  };
198 
200  typedef SystemClock WallClock;
201 
202 
208 
212  return SteadyClock::now();
213  }
214 
218  return SystemClock::now();
219  }
220 
223  QI_API void sleepFor(const qi::Duration& d);
224  template <class Rep, class Period>
225  inline void sleepFor(const boost::chrono::duration<Rep, Period>& d);
227 
232  QI_API void sleepUntil(const SteadyClockTimePoint &t);
233  template <class Duration>
234  inline void sleepUntil(const boost::chrono::time_point<SteadyClock, Duration>& t);
236 
239  QI_API void sleepUntil(const ClockTimePoint &t);
240  template <class Duration>
241  inline void sleepUntil(const boost::chrono::time_point<Clock, Duration>& t);
243 
250  QI_API void sleepUntil(const SystemClockTimePoint& t);
251  template <class Duration>
252  inline void sleepUntil(const boost::chrono::time_point<SystemClock, Duration>& t);
254 
263  QI_API std::string toISO8601String(const SystemClockTimePoint &t);
265 
268  template <class DurationTo, class TimePointFrom>
269  inline DurationTo durationSince(const TimePointFrom& t);
271 }
272 
273 # ifdef __APPLE__
274  // export template instanciation for RTTI issues across libraries. (mostly for OSX)
275  template class QI_API boost::chrono::duration<int64_t, boost::nano>;
276  template class QI_API boost::chrono::duration<int64_t, boost::micro>;
277  template class QI_API boost::chrono::duration<int64_t, boost::milli>;
278  template class QI_API boost::chrono::duration<int64_t>;
279  template class QI_API boost::chrono::duration<int64_t, boost::ratio<60> >;
280  template class QI_API boost::chrono::duration<int64_t, boost::ratio<3600> >;
281  template class QI_API boost::chrono::time_point<qi::SteadyClock>;
282  template class QI_API boost::chrono::time_point<qi::SystemClock>;
283 # endif
284 
285 # include <qi/detail/clock.hxx>
286 
287 #endif // _QI_OS_HPP_
int64_t rep
The representation type of the duration and time_point.
Definition: clock.hpp:79
SystemClock::time_point SystemClockTimePoint
System clock time point.
Definition: clock.hpp:205
DurationTo durationSince(const TimePointFrom &t)
}@
Definition: clock.hxx:40
boost::nano period
The tick period of the clock in nanoseconds.
Definition: clock.hpp:38
boost::chrono::duration< rep, period > duration
The duration type of the clock.
Definition: clock.hpp:81
#define QI_API
Definition: api.hpp:34
int64_t rep
The representation type of the duration and time_point.
Definition: clock.hpp:37
Result is expected to be later than user-provided reference.
Definition: clock.hpp:99
boost::chrono::duration< int64_t, boost::nano > Duration
Convenience typedefs.
Definition: clock.hpp:18
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:158
boost::chrono::time_point< Clock > time_point
Definition: clock.hpp:87
Expect
Enum expected argument.
Definition: clock.hpp:97
dll import/export and compiler message
Result is expected to be later than user-provided reference.
Definition: clock.hpp:60
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...
Clock::time_point ClockTimePoint
qi::Clock time point.
Definition: clock.hpp:204
boost::nano period
The tick period of the clock in nanoseconds.
Definition: clock.hpp:162
boost::chrono::duration< rep, period > duration
The duration type of the clock.
Definition: clock.hpp:163
QI_API_DEPRECATED typedef SystemClock WallClock
Definition: clock.hpp:200
The SteadyClock class represents a monotonic clock. <includename>qi/clock.hpp</includename> ...
Definition: clock.hpp:34
QI_API_DEPRECATED SystemClockTimePoint wallClockNow()
Returns a time_point representing the current value of the clock.
Definition: clock.hpp:217
QI_API_DEPRECATED SteadyClockTimePoint steadyClockNow()
Returns a time_point representing the current value of the clock.
Definition: clock.hpp:211
boost::chrono::duration< int64_t, boost::ratio< 60 > > Minutes
Definition: clock.hpp:23
Pick the nearest result to user-provided reference.
Definition: clock.hpp:98
boost::nano period
The tick period of the clock in nanoseconds.
Definition: clock.hpp:80
QI_API_DEPRECATED typedef time_point WallClockTimePoint
Definition: clock.hpp:178
boost::chrono::duration< int64_t, boost::milli > MilliSeconds
Definition: clock.hpp:21
int32_t int32_t
Cross-platform signed integer of length 32 bits (4 bytes).
Definition: types.hpp:75
boost::chrono::duration< int64_t, boost::nano > NanoSeconds
Definition: clock.hpp:19
int64_t rep
The representation type of the duration and time_point.
Definition: clock.hpp:161
QI_API_DEPRECATED typedef SystemClockTimePoint WallClockTimePoint
System clock time point.
Definition: clock.hpp:207
Expect
Enum expected argument.
Definition: clock.hpp:58
boost::chrono::duration< int64_t, boost::micro > MicroSeconds
Definition: clock.hpp:20
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:55
void sleepUntil(const SteadyClockTimePoint &t)
Blocks the execution of the current thread until t has been reached.
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:39
boost::chrono::duration< int64_t > Seconds
Definition: clock.hpp:22
boost::chrono::time_point< SystemClock > time_point
Definition: clock.hpp:169
boost::chrono::time_point< SteadyClock > time_point
Definition: clock.hpp:45
boost::chrono::duration< int64_t, boost::ratio< 3600 > > Hours
Definition: clock.hpp:24
SteadyClock::time_point SteadyClockTimePoint
Steady clock time point.
Definition: clock.hpp:203
The Clock class represents a system-wide clock, best suitable for timestamping events.
Definition: clock.hpp:76
int64_t int64_t
Cross-platform signed integer of length 64 bits (8 bytes).
Definition: types.hpp:76
uint32_t uint32_t
Cross-platform unsigned integer of length 32 bits (4 bytes).
Definition: types.hpp:80
boost::chrono::duration< rep, period > duration
The duration type of the clock.
Definition: clock.hpp:39
Pick the nearest result to user-provided reference.
Definition: clock.hpp:59