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 
53 #include <ros/platform.h>
54 #include <iostream>
55 #include <cmath>
56 #include <ros/exception.h>
57 #include "duration.h"
58 #include <boost/math/special_functions/round.hpp>
59 #include "rostime_decl.h"
60 
61 /*********************************************************************
62  ** Cross Platform Headers
63  *********************************************************************/
64 
65 #ifdef WIN32
66  #include <sys/timeb.h>
67 #else
68  #include <sys/time.h>
69 #endif
70 
71 namespace boost {
72  namespace posix_time {
73  class ptime;
74  class time_duration;
75  }
76 }
77 
78 namespace ros
79 {
80 
81  /*********************************************************************
82  ** Exceptions
83  *********************************************************************/
84 
89  {
90  public:
92  : Exception("Cannot use ros::Time::now() before the first NodeHandle has been created or ros::start() has been called. "
93  "If this is a standalone app or test that just uses ros::Time and does not communicate over ROS, you may also call ros::Time::init()")
94  {}
95  };
96 
103  {
104  public:
106  : Exception("This windows platform does not "
107  "support the high-performance timing api.")
108  {}
109  };
110 
111  /*********************************************************************
112  ** Functions
113  *********************************************************************/
114 
115  ROSTIME_DECL void normalizeSecNSec(uint64_t& sec, uint64_t& nsec);
116  ROSTIME_DECL void normalizeSecNSec(uint32_t& sec, uint32_t& nsec);
117  ROSTIME_DECL void normalizeSecNSecUnsigned(int64_t& sec, int64_t& nsec);
118  ROSTIME_DECL void ros_walltime(uint32_t& sec, uint32_t& nsec);
119  ROSTIME_DECL void ros_steadytime(uint32_t& sec, uint32_t& nsec);
120 
121  /*********************************************************************
122  ** Time Classes
123  *********************************************************************/
124 
129  template<class T, class D>
130  class TimeBase
131  {
132  public:
133  uint32_t sec, nsec;
134 
135  TimeBase() : sec(0), nsec(0) { }
136  TimeBase(uint32_t _sec, uint32_t _nsec) : sec(_sec), nsec(_nsec)
137  {
138  normalizeSecNSec(sec, nsec);
139  }
140  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  int64_t sec64 = (int64_t)floor(t);
156  if (sec64 < 0 || sec64 > UINT_MAX)
157  throw std::runtime_error("Time is out of dual 32-bit range");
158  sec = (uint32_t)sec64;
159  nsec = (uint32_t)boost::math::round((t-sec) * 1e9);
160  // avoid rounding errors
161  sec += (nsec / 1000000000ul);
162  nsec %= 1000000000ul;
163  return *static_cast<T*>(this);
164  }
165 
166  uint64_t toNSec() const {return (uint64_t)sec*1000000000ull + (uint64_t)nsec; }
167  T& fromNSec(uint64_t t);
168 
169  inline bool isZero() const { return sec == 0 && nsec == 0; }
170  inline bool is_zero() const { return isZero(); }
171  boost::posix_time::ptime toBoost() const;
172 
173  };
174 
180  class ROSTIME_DECL Time : public TimeBase<Time, Duration>
181  {
182  public:
184  : TimeBase<Time, Duration>()
185  {}
186 
187  Time(uint32_t _sec, uint32_t _nsec)
188  : TimeBase<Time, Duration>(_sec, _nsec)
189  {}
190 
191  explicit Time(double t) { fromSec(t); }
192 
197  static Time now();
202  static bool sleepUntil(const Time& end);
203 
204  static void init();
205  static void shutdown();
206  static void setNow(const Time& new_now);
207  static bool useSystemTime();
208  static bool isSimTime();
209  static bool isSystemTime();
210 
214  static bool isValid();
218  static bool waitForValid();
222  static bool waitForValid(const ros::WallDuration& timeout);
223 
224  static Time fromBoost(const boost::posix_time::ptime& t);
225  static Time fromBoost(const boost::posix_time::time_duration& d);
226  };
227 
228  extern ROSTIME_DECL const Time TIME_MAX;
229  extern ROSTIME_DECL const Time TIME_MIN;
230 
236  class ROSTIME_DECL WallTime : public TimeBase<WallTime, WallDuration>
237  {
238  public:
241  {}
242 
243  WallTime(uint32_t _sec, uint32_t _nsec)
244  : TimeBase<WallTime, WallDuration>(_sec, _nsec)
245  {}
246 
247  explicit WallTime(double t) { fromSec(t); }
248 
252  static WallTime now();
253 
258  static bool sleepUntil(const WallTime& end);
259 
260  static bool isSystemTime() { return true; }
261  };
262 
270  class ROSTIME_DECL SteadyTime : public TimeBase<SteadyTime, WallDuration>
271  {
272  public:
275  {}
276 
277  SteadyTime(uint32_t _sec, uint32_t _nsec)
278  : TimeBase<SteadyTime, WallDuration>(_sec, _nsec)
279  {}
280 
281  explicit SteadyTime(double t) { fromSec(t); }
282 
286  static SteadyTime now();
287 
292  static bool sleepUntil(const SteadyTime& end);
293 
294  static bool isSystemTime() { return true; }
295  };
296 
297  ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const Time &rhs);
298  ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const WallTime &rhs);
299  ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const SteadyTime &rhs);
300 }
301 
302 #endif // ROS_TIME_H
303 
ROSTIME_DECL std::ostream & operator<<(std::ostream &os, const SteadyTime &rhs)
Definition: src/time.cpp:508
Definition: duration.h:56
TimeBase()
Definition: time.h:135
static bool isSystemTime()
Definition: time.h:294
ROSTIME_DECL void ros_steadytime(uint32_t &sec, uint32_t &nsec)
Definition: src/time.cpp:177
Time representation. May either represent wall clock time or ROS clock time.
Definition: time.h:180
uint32_t sec
Definition: time.h:133
bool is_zero() const
Definition: time.h:170
WallTime(uint32_t _sec, uint32_t _nsec)
Definition: time.h:243
ROSTIME_DECL void normalizeSecNSecUnsigned(int64_t &sec, int64_t &nsec)
Definition: src/time.cpp:573
bool operator!=(const T &rhs) const
Definition: time.h:147
SteadyTime(uint32_t _sec, uint32_t _nsec)
Definition: time.h:277
Time(double t)
Definition: time.h:191
TimeBase(uint32_t _sec, uint32_t _nsec)
Definition: time.h:136
WallTime(double t)
Definition: time.h:247
Duration representation for use with the WallTime class.
Definition: duration.h:136
Thrown if the ros subsystem hasn&#39;t been initialised before use.
Definition: time.h:88
ROSTIME_DECL void ros_walltime(uint32_t &sec, uint32_t &nsec)
Definition: src/time.cpp:98
Time(uint32_t _sec, uint32_t _nsec)
Definition: time.h:187
Base class for Time implementations. Provides storage, common functions and operator overloads...
Definition: time.h:130
Thrown if windoze high perf. timestamping is unavailable.
Definition: time.h:102
SteadyTime(double t)
Definition: time.h:281
Time representation. Always wall-clock time.
Definition: time.h:236
TimeBase(double t)
Definition: time.h:140
Time()
Definition: time.h:183
ROSTIME_DECL void normalizeSecNSec(uint32_t &sec, uint32_t &nsec)
Definition: src/time.cpp:562
uint64_t toNSec() const
Definition: time.h:166
ROSTIME_DECL const Time TIME_MAX
Time representation. Always steady-clock time.
Definition: time.h:270
static bool isSystemTime()
Definition: time.h:260
ROSTIME_DECL const Time TIME_MIN
Duration representation for use with the Time class.
Definition: duration.h:108
T & fromSec(double t)
Definition: time.h:154
WallTime()
Definition: time.h:239
double toSec() const
Definition: time.h:153
bool isZero() const
Definition: time.h:169
#define ROSTIME_DECL
Definition: rostime_decl.h:52


rostime
Author(s): Josh Faust
autogenerated on Sat Jun 9 2018 02:21:15