libqi-api  2.1.4.13
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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.hpp>
14 
15 namespace qi
16 {
17 
18 
19  typedef boost::chrono::duration<int64_t, boost::nano> Duration;
20  typedef boost::chrono::duration<int64_t, boost::nano> NanoSeconds;
21  typedef boost::chrono::duration<int64_t, boost::micro> MicroSeconds;
22  typedef boost::chrono::duration<int64_t, boost::milli> MilliSeconds;
23  typedef boost::chrono::duration<int64_t> Seconds;
24  typedef boost::chrono::duration<int64_t, boost::ratio<60> > Minutes;
25  typedef boost::chrono::duration<int64_t, boost::ratio<3600> > Hours;
26 
27 
29  {
30  public:
31  typedef int64_t rep;
32  typedef boost::nano period; // clock counts nanoseconds
33  typedef boost::chrono::duration<rep, period> duration;
34  typedef boost::chrono::time_point<SteadyClock> time_point;
35  BOOST_STATIC_CONSTEXPR bool is_steady = boost::chrono::steady_clock::is_steady;
36 
37  public:
38  typedef boost::chrono::time_point<SteadyClock> SteadyClockTimePoint;
39 
40  enum Expect {
43  Expect_Sooner
44  };
45 
46 
47  // Returns a time_point representing the current value of the clock.
48  static SteadyClockTimePoint now();
49 
50  // Convert the time point to a number of milliseconds on 32 bits.
51  // Since the 32 bits number overflows every 2^32 ms ~ 50 days,
52  // this is a lossy operation.
53  static uint32_t toUint32ms(const SteadyClockTimePoint &t) throw();
54  static int32_t toInt32ms(const SteadyClockTimePoint &t) throw();
55 
56  // Get a time point from a number of milliseconds on 32 bits.
57  //
58  // Since the 32 bits number overflows every ~50 days, an infinity of
59  // time points match a given 32 bits number (all modulo ~50 days).
60  // This function picks the result near the guess timepoint depending on
61  // the expect argument:
62  //
63  // if expect == LATER, result is expected to be later than guess:
64  //
65  // guess <= result < guess + period
66  //
67  // if expect == SOONER, result is expected to be sooner than guess:
68  //
69  // guess - period < result <= guess
70  //
71  // if expect == SOONER_OR_LATER, pick the nearest result:
72  //
73  // guess - period/2 < result <= guess + period/2
74  //
75  // where period == 2^32 ms ~ 50 days
76  static time_point fromUint32ms(uint32_t t_ms, SteadyClockTimePoint guess,
77  Expect expect=Expect_SoonerOrLater) throw();
78  static time_point fromInt32ms(int32_t t_ms, SteadyClockTimePoint guess,
79  Expect expect=Expect_SoonerOrLater) throw();
80  };
81 
83  {
84  public:
85  typedef int64_t rep;
86  typedef boost::nano period; // clock counts nanoseconds
87  typedef boost::chrono::duration<rep, period> duration;
88  typedef boost::chrono::time_point<WallClock> time_point;
89  BOOST_STATIC_CONSTEXPR bool is_steady = false;
90 
91  public:
92  typedef boost::chrono::time_point<WallClock> WallClockTimePoint;
93 
94  // Returns a time_point representing the current value of the clock.
95  static WallClockTimePoint now();
96 
97  // Converts a system clock time point to std::time_t
98  static std::time_t to_time_t(const WallClockTimePoint& t) throw();
99 
100  // Converts std::time_t to a system clock time point
101  static WallClockTimePoint from_time_t(const std::time_t &t) throw();
102  };
103 
104 
105 
108 
110  return SteadyClock::now();
111  }
112 
113  inline WallClockTimePoint wallClockNow() {
114  return WallClock::now();
115  }
116 
117 
118  // Blocks the execution of the current thread for at least d.
119  QI_API void sleepFor(const qi::Duration& d);
120  template <class Rep, class Period>
121  inline void sleepFor(const boost::chrono::duration<Rep, Period>& d);
122 
123  // Blocks the execution of the current thread until t has been
124  // reached.
125  //
126  // This is equivalent to sleep_for(t-steady_clock::now())
127  QI_API void sleepUntil(const SteadyClockTimePoint &t);
128  template <class Duration>
129  inline void sleepUntil(const boost::chrono::time_point<SteadyClock, Duration>& t);
130 
131  // Blocks the execution of the current thread until t has been
132  // reached.
133  // Adjustments of the clock are taken into account.
134  // Thus the duration of the block might, but might not, be less
135  // or more than t - system_clock::now()
136  QI_API void sleepUntil(const WallClockTimePoint& t);
137  template <class Duration>
138  inline void sleepUntil(const boost::chrono::time_point<WallClock, Duration>& t);
139 
140 }
141 
142 #ifdef __APPLE__
143  //export template instanciation for RTTI issues across libraries. (mostly for OSX)
144  template class QI_API boost::chrono::duration<int64_t, boost::nano>;
145  template class QI_API boost::chrono::duration<int64_t, boost::micro>;
146  template class QI_API boost::chrono::duration<int64_t, boost::milli>;
147  template class QI_API boost::chrono::duration<int64_t>;
148  template class QI_API boost::chrono::duration<int64_t, boost::ratio<60> >;
149  template class QI_API boost::chrono::duration<int64_t, boost::ratio<3600> >;
150  template class QI_API boost::chrono::time_point<qi::SteadyClock>;
151  template class QI_API boost::chrono::time_point<qi::WallClock>;
152 #endif
153 
154 #include <qi/clock.hxx>
155 
156 #endif // _QI_OS_HPP_
int32_t int32_t
Cross-platform signed integer of length 32 bits (4 bytes).
Definition: types.hpp:32
WallClockTimePoint wallClockNow()
Definition: clock.hpp:113
boost::chrono::duration< int64_t, boost::nano > Duration
Definition: clock.hpp:19
boost::chrono::duration< rep, period > duration
Definition: clock.hpp:87
boost::chrono::time_point< SteadyClock > SteadyClockTimePoint
Definition: clock.hpp:38
boost::chrono::duration< int64_t, boost::micro > MicroSeconds
Definition: clock.hpp:21
boost::chrono::duration< int64_t, boost::ratio< 60 > > Minutes
Definition: clock.hpp:24
boost::nano period
Definition: clock.hpp:86
WallClock::WallClockTimePoint WallClockTimePoint
Definition: clock.hpp:107
void sleepUntil(const SteadyClockTimePoint &t)
int64_t rep
Definition: clock.hpp:85
boost::chrono::time_point< WallClock > time_point
Definition: clock.hpp:88
int64_t int64_t
Cross-platform signed integer of length 64 bits (8 bytes).
Definition: types.hpp:33
SteadyClockTimePoint steadyClockNow()
Definition: clock.hpp:109
boost::chrono::duration< int64_t, boost::nano > NanoSeconds
Definition: clock.hpp:20
boost::chrono::duration< rep, period > duration
Definition: clock.hpp:33
boost::chrono::time_point< WallClock > WallClockTimePoint
Definition: clock.hpp:92
void sleepFor(const qi::Duration &d)
boost::nano period
Definition: clock.hpp:32
static WallClockTimePoint now()
boost::chrono::time_point< SteadyClock > time_point
Definition: clock.hpp:34
dll import/export and compiler message
uint32_t uint32_t
Cross-platform unsigned integer of length 32 bits (4 bytes).
Definition: types.hpp:37
static SteadyClockTimePoint now()
boost::chrono::duration< int64_t > Seconds
Definition: clock.hpp:23
int64_t rep
Definition: clock.hpp:31
boost::chrono::duration< int64_t, boost::milli > MilliSeconds
Definition: clock.hpp:22
SteadyClock::SteadyClockTimePoint SteadyClockTimePoint
Definition: clock.hpp:106
#define QI_API
Definition: api.hpp:24
boost::chrono::duration< int64_t, boost::ratio< 3600 > > Hours
Definition: clock.hpp:25