time.h
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Copyright (c) 2010, Willow Garage, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * * Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  * * Redistributions in binary form must reproduce the above
14  * copyright notice, this list of conditions and the following
15  * disclaimer in the documentation and/or other materials provided
16  * with the distribution.
17  * * Neither the name of Willow Garage, Inc. nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  * POSSIBILITY OF SUCH DAMAGE.
33  *********************************************************************/
34 
35 #ifndef ROS_TIME_H_INCLUDED
36 #define ROS_TIME_H_INCLUDED
37 
38 /*********************************************************************
39  ** Pragmas
40  *********************************************************************/
41 
42 #ifdef _MSC_VER
43  // Rostime has some magic interface that doesn't directly include
44  // its implementation, this just disables those warnings.
45  #pragma warning(disable: 4244)
46  #pragma warning(disable: 4661)
47 #endif
48 
49 /*********************************************************************
50  ** Headers
51  *********************************************************************/
52 #include "../../../cpp_common/include/ros/platform.h"
53 #include <iostream>
54 #include <cmath>
55 #include "../../../cpp_common/include/ros/exception.h"
56 #include "duration.h"
57 //#include <boost/math/special_functions/round.hpp>
58 #include "rostime_decl.h"
59 
60 /*********************************************************************
61  ** Cross Platform Headers
62  *********************************************************************/
63 
64 #ifdef WIN32
65  #include <sys/timeb.h>
66 #else
67  #include <sys/time.h>
68 #endif
69 
70 #include "math.h"
71 
72 //namespace boost {
73 // namespace posix_time {
74 // class ptime;
75 // class time_duration;
76 // }
77 //}
78 
79 namespace rs2rosinternal
80 {
81 
82  /*********************************************************************
83  ** Exceptions
84  *********************************************************************/
85 
90  {
91  public:
93  : Exception("Cannot use rs2rosinternal::Time::now() before the first NodeHandle has been created or rs2rosinternal::start() has been called. "
94  "If this is a standalone app or test that just uses rs2rosinternal::Time and does not communicate over ROS, you may also call rs2rosinternal::Time::init()")
95  {}
96  };
97 
104  {
105  public:
107  : Exception("This windows platform does not "
108  "support the high-performance timing api.")
109  {}
110  };
111 
112  /*********************************************************************
113  ** Functions
114  *********************************************************************/
115 
119 
120  /*********************************************************************
121  ** Time Classes
122  *********************************************************************/
123 
128  template<class T, class D>
129  class TimeBase
130  {
131  public:
132  uint32_t sec, nsec;
133 
134  TimeBase() : sec(0), nsec(0) { }
135  TimeBase(uint32_t _sec, uint32_t _nsec) : sec(_sec), nsec(_nsec)
136  {
137  normalizeSecNSec(sec, nsec);
138  }
139  explicit TimeBase(double t) { fromSec(t); }
141  D operator-(const T &rhs) const;
142  T operator+(const D &rhs) const;
143  T operator-(const D &rhs) const;
144  T& operator+=(const D &rhs);
145  T& operator-=(const D &rhs);
146  bool operator==(const T &rhs) const;
147  inline bool operator!=(const T &rhs) const { return !(*static_cast<const T*>(this) == rhs); }
148  bool operator>(const T &rhs) const;
149  bool operator<(const T &rhs) const;
150  bool operator>=(const T &rhs) const;
151  bool operator<=(const T &rhs) const;
152 
153  double toSec() const { return (double)sec + 1e-9*(double)nsec; };
154  T& fromSec(double t) {
155  sec = static_cast<uint32_t>(floor(t));
156  nsec = static_cast<uint32_t>(round((t-sec) * 1e9));
157  // avoid rounding errors
158  sec += static_cast<uint32_t>((nsec / 1000000000ul));
159  nsec %= 1000000000ul;
160  return *static_cast<T*>(this);
161  }
162 
163  uint64_t toNSec() const {return (uint64_t)sec*1000000000ull + (uint64_t)nsec; }
164  T& fromNSec(uint64_t t);
165 
166  inline bool isZero() const { return sec == 0 && nsec == 0; }
167  inline bool is_zero() const { return isZero(); }
168  // boost::posix_time::ptime toBoost() const;
169 
170  };
171 
177  class ROSTIME_DECL Time : public TimeBase<Time, Duration>
178  {
179  public:
181  : TimeBase<Time, Duration>()
182  {}
183 
184  Time(uint32_t _sec, uint32_t _nsec)
185  : TimeBase<Time, Duration>(_sec, _nsec)
186  {}
187 
188  explicit Time(double t) { fromSec(t); }
189 
194  static Time now();
199  static bool sleepUntil(const Time& end);
200 
201  static void init();
202  static void shutdown();
203  static void setNow(const Time& new_now);
204  static bool useSystemTime();
205  static bool isSimTime();
206  static bool isSystemTime();
207 
211  static bool isValid();
215  static bool waitForValid();
219  static bool waitForValid(const rs2rosinternal::WallDuration& timeout);
220 
221  /*static Time fromBoost(const boost::posix_time::ptime& t);
222  static Time fromBoost(const boost::posix_time::time_duration& d);*/
223  };
224 
225  extern ROSTIME_DECL const Time TIME_MAX;
226  extern ROSTIME_DECL const Time TIME_MIN;
227 
233  class ROSTIME_DECL WallTime : public TimeBase<WallTime, WallDuration>
234  {
235  public:
238  {}
239 
241  : TimeBase<WallTime, WallDuration>(_sec, _nsec)
242  {}
243 
244  explicit WallTime(double t) { fromSec(t); }
245 
249  static WallTime now();
250 
255  static bool sleepUntil(const WallTime& end);
256 
257  static bool isSystemTime() { return true; }
258  };
259 
260  ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const Time &rhs);
261  ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const WallTime &rhs);
262 }
263 
264 #endif // ROS_TIME_H
265 
GLuint GLuint end
Time(double t)
Definition: time.h:188
ROSTIME_DECL void normalizeSecNSec(uint64_t &sec, uint64_t &nsec)
Definition: time.cpp:467
TimeBase(double t)
Definition: time.h:139
bool operator==(const plane &lhs, const plane &rhs)
Definition: rendering.h:134
T & fromSec(double t)
Definition: time.h:154
ROSTIME_DECL const Time TIME_MAX
Thrown if the ros subsystem hasn&#39;t been initialised before use.
Definition: time.h:89
Base class for Time implementations. Provides storage, common functions and operator overloads...
Definition: time.h:129
bool operator>(failed, failed)
e
Definition: rmse.py:177
Thrown if windoze high perf. timestamping is unavailable.
Definition: time.h:103
WallTime(double t)
Definition: time.h:244
GLdouble t
Time(uint32_t _sec, uint32_t _nsec)
Definition: time.h:184
bool isZero() const
Definition: time.h:166
Duration representation for use with the Time class.
Definition: duration.h:108
ImVec4 operator+(const ImVec4 &c, float v)
Duration representation for use with the WallTime class.
Definition: duration.h:136
Base class for all exceptions thrown by ROS.
Definition: exception.h:39
unsigned int uint32_t
Definition: stdint.h:80
ROSTIME_DECL void normalizeSecNSecUnsigned(int64_t &sec, int64_t &nsec)
Definition: time.cpp:490
float3 operator-(const float3 &a, const float3 &b)
Definition: rendering.h:178
bool operator!=(const T &rhs) const
Definition: time.h:147
bool operator<(failed, failed)
Time representation. Always wall-clock time.
Definition: time.h:233
bool is_zero() const
Definition: time.h:167
Time representation. May either represent wall clock time or ROS clock time.
Definition: time.h:177
unsigned __int64 uint64_t
Definition: stdint.h:90
void init(void)
Definition: boing.c:180
uint64_t toNSec() const
Definition: time.h:163
auto operator+=(std::string &lhs, StringRef const &sr) -> std::string &
std::ostream & operator<<(std::ostream &os, const Duration &rhs)
Definition: time.cpp:340
TimeBase(uint32_t _sec, uint32_t _nsec)
Definition: time.h:135
double toSec() const
Definition: time.h:153
ROSTIME_DECL const Time TIME_MIN
GLbitfield GLuint64 timeout
signed __int64 int64_t
Definition: stdint.h:89
bool operator>=(failed, failed)
static bool isSystemTime()
Definition: time.h:257
#define D(...)
Definition: usbhost.c:33
WallTime(uint32_t _sec, uint32_t _nsec)
Definition: time.h:240
bool operator<=(failed, failed)
#define ROSTIME_DECL
Definition: rostime_decl.h:52


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:50:11