OKVIS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Time.hpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Modified and adapted by
5  * Copyright (c) 2015, Autonomous Systems Lab / ETH Zurich
6  *
7  * Original Copyright (c) 2010, Willow Garage, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of Willow Garage, Inc. nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *********************************************************************/
37 /*
38  * Modified: Stefan Leutenegger (s.leutenegger@imperial.ac.uk)
39  * Modified: Andreas Forster (an.forster@gmail.com)
40  */
41 
50 #ifndef INCLUDE_OKVIS_TIME_HPP_
51 #define INCLUDE_OKVIS_TIME_HPP_
52 
53 /*********************************************************************
54  ** Pragmas
55  *********************************************************************/
56 
57 #ifdef _MSC_VER
58 // Okvistime has some magic interface that doesn't directly include
59 // its implementation, this just disables those warnings.
60 #pragma warning(disable: 4244)
61 #pragma warning(disable: 4661)
62 #endif
63 
64 #ifdef _WIN32
65 #include <windows.h>
66 #endif
67 
68 /*********************************************************************
69  ** Headers
70  *********************************************************************/
71 
72 //#include <ros/platform.h>
73 #include <iostream>
74 #include <cmath>
75 //#include <ros/exception.h>
76 #include "Duration.hpp"
77 //#include "rostime_decl.h"
78 
79 /*********************************************************************
80  ** Cross Platform Headers
81  *********************************************************************/
82 
83 #ifdef WIN32
84 #include <sys/timeb.h>
85 #else
86 #include <sys/time.h>
87 #endif
88 
90 namespace okvis {
91 
92 /*********************************************************************
93  ** Exceptions
94  *********************************************************************/
95 
101 class NoHighPerformanceTimersException : std::runtime_error {
102  public:
104  : std::runtime_error("This windows platform does not "
105  "support the high-performance timing api.") {
106  }
107 };
108 
109 /*********************************************************************
110  ** Functions
111  *********************************************************************/
112 
113 void normalizeSecNSec(uint64_t& sec, uint64_t& nsec);
114 void normalizeSecNSec(uint32_t& sec, uint32_t& nsec);
115 void normalizeSecNSecUnsigned(int64_t& sec, int64_t& nsec);
116 
117 /*********************************************************************
118  ** Time Classes
119  *********************************************************************/
120 
125 template<class T, class D>
126 class TimeBase {
127  public:
128  uint32_t sec, nsec;
129 
131  : sec(0),
132  nsec(0) {
133  }
134  TimeBase(uint32_t _sec, uint32_t _nsec)
135  : sec(_sec),
136  nsec(_nsec) {
138  }
139  explicit TimeBase(double t) {
140  fromSec(t);
141  }
143  }
144  D operator-(const T &rhs) const;
145  T operator+(const D &rhs) const;
146  T operator-(const D &rhs) const;
147  T& operator+=(const D &rhs);
148  T& operator-=(const D &rhs);
149  bool operator==(const T &rhs) const;
150  inline bool operator!=(const T &rhs) const {
151  return !(*static_cast<const T*>(this) == rhs);
152  }
153  bool operator>(const T &rhs) const;
154  bool operator<(const T &rhs) const;
155  bool operator>=(const T &rhs) const;
156  bool operator<=(const T &rhs) const;
157 
158  double toSec() const {
159  return (double) sec + 1e-9 * (double) nsec;
160  }
161  ;
162  T& fromSec(double t) {
163  sec = (uint32_t) floor(t);
164  nsec = (uint32_t) std::round((t - sec) * 1e9);
165  return *static_cast<T*>(this);
166  }
167 
168  uint64_t toNSec() const {
169  return (uint64_t) sec * 1000000000ull + (uint64_t) nsec;
170  }
171  T& fromNSec(uint64_t t);
172 
173  inline bool isZero() const {
174  return sec == 0 && nsec == 0;
175  }
176  inline bool is_zero() const {
177  return isZero();
178  }
179 
180 };
181 
187 class Time : public TimeBase<Time, Duration> {
188  public:
190  : TimeBase<Time, Duration>() {
191  }
192 
193  Time(uint32_t _sec, uint32_t _nsec)
194  : TimeBase<Time, Duration>(_sec, _nsec) {
195  }
196 
197  explicit Time(double t) {
198  fromSec(t);
199  }
200 
204  static Time now();
208  static bool sleepUntil(const Time& end);
209 
210  static void init();
211  static void shutdown();
212  static void setNow(const Time& new_now);
213  static bool useSystemTime();
214  static bool isSimTime();
215  static bool isSystemTime();
216 
220  static bool isValid();
224  static bool waitForValid();
228  static bool waitForValid(const okvis::WallDuration& timeout);
229 };
230 
231 extern const Time TIME_MAX;
232 extern const Time TIME_MIN;
233 
239 class WallTime : public TimeBase<WallTime, WallDuration> {
240  public:
243  }
244 
245  WallTime(uint32_t _sec, uint32_t _nsec)
246  : TimeBase<WallTime, WallDuration>(_sec, _nsec) {
247  }
248 
249  explicit WallTime(double t) {
250  fromSec(t);
251  }
252 
256  static WallTime now();
257 
261  static bool sleepUntil(const WallTime& end);
262 
263  static bool isSystemTime() {
264  return true;
265  }
266 };
267 
268 std::ostream &operator <<(std::ostream &os, const Time &rhs);
269 std::ostream &operator <<(std::ostream &os, const WallTime &rhs);
270 
271 }
272 
273 #endif // INCLUDE_OKVIS_TIME_HPP_
274 
WallTime(double t)
Definition: Time.hpp:249
bool is_zero() const
Definition: Time.hpp:176
Time representation. Always wall-clock time.
Definition: Time.hpp:239
void normalizeSecNSecUnsigned(int64_t &sec, int64_t &nsec)
Definition: Time.cpp:391
TimeBase()
Definition: Time.hpp:130
Thrown if windoze high perf. timestamping is unavailable.
Definition: Time.hpp:101
static void init()
Definition: Time.cpp:249
static Time now()
Retrieve the current time. Returns the current wall clock time.
Definition: Time.cpp:237
static bool isSimTime()
Definition: Time.cpp:229
void normalizeSecNSec(uint64_t &sec, uint64_t &nsec)
Definition: Time.cpp:370
uint32_t sec
Definition: Time.hpp:128
T & fromNSec(uint64_t t)
Definition: Time.hpp:77
bool isZero() const
Definition: Time.hpp:173
Base class for Time implementations. Provides storage, common functions and operator overloads...
Definition: Time.hpp:126
Header file for the DurationBase, Duration and WallDuration class.
static bool isSystemTime()
Definition: Time.hpp:263
std::ostream & operator<<(std::ostream &os, const Duration &rhs)
Definition: Time.cpp:284
Time()
Definition: Time.hpp:189
Time(uint32_t _sec, uint32_t _nsec)
Definition: Time.hpp:193
NoHighPerformanceTimersException()
Definition: Time.hpp:103
bool operator>=(const T &rhs) const
Definition: Time.hpp:154
static bool waitForValid()
Wait for time to become valid.
Definition: Time.cpp:259
const Time TIME_MAX
T & operator+=(const D &rhs)
Definition: Time.hpp:110
static void setNow(const Time &new_now)
Definition: Time.cpp:245
uint64_t toNSec() const
Definition: Time.hpp:168
Time(double t)
Definition: Time.hpp:197
bool operator<(const T &rhs) const
Definition: Time.hpp:127
T & fromSec(double t)
Definition: Time.hpp:162
bool operator==(const T &rhs) const
Definition: Time.hpp:122
bool operator<=(const T &rhs) const
Definition: Time.hpp:145
Duration representation for use with the Time class.
Definition: Duration.hpp:131
static bool useSystemTime()
Definition: Time.cpp:225
~TimeBase()
Definition: Time.hpp:142
Duration representation for use with the WallTime class.
Definition: Duration.hpp:159
static bool isValid()
Returns whether or not the current time is valid. Time is valid if it is non-zero.
Definition: Time.cpp:255
WallTime(uint32_t _sec, uint32_t _nsec)
Definition: Time.hpp:245
T operator+(const D &rhs) const
Definition: Time.hpp:98
Time representation. May either represent wall clock time or ROS clock time.
Definition: Time.hpp:187
const Time TIME_MIN
bool operator!=(const T &rhs) const
Definition: Time.hpp:150
TimeBase(uint32_t _sec, uint32_t _nsec)
Definition: Time.hpp:134
static WallTime now()
Returns the current wall clock time.
Definition: Time.cpp:354
uint32_t nsec
Definition: Time.hpp:128
WallTime()
Definition: Time.hpp:241
static bool isSystemTime()
Definition: Time.cpp:233
static bool sleepUntil(const Time &end)
Sleep until a specific time has been reached.
Definition: Time.cpp:289
D operator-(const T &rhs) const
Definition: Time.hpp:87
bool operator>(const T &rhs) const
Definition: Time.hpp:136
TimeBase(double t)
Definition: Time.hpp:139
static void shutdown()
Definition: Time.cpp:252
T & operator-=(const D &rhs)
Definition: Time.hpp:116
double toSec() const
Definition: Time.hpp:158
static bool sleepUntil(const WallTime &end)
Sleep until a specific time has been reached.
Definition: Time.cpp:310