duration.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, 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_DURATION_H
36 #define ROS_DURATION_H
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 disbales those warnings.
45  #pragma warning(disable: 4244)
46  #pragma warning(disable: 4661)
47 #endif
48 
49 #include <iostream>
50 #include <math.h>
51 #include <stdexcept>
52 #include <climits>
53 #include <stdint.h>
54 #include "rostime_decl.h"
55 
56 namespace boost {
57  namespace posix_time {
58  class time_duration;
59  }
60 }
61 
62 namespace ros
63 {
64 ROSTIME_DECL void normalizeSecNSecSigned(int64_t& sec, int64_t& nsec);
65 ROSTIME_DECL void normalizeSecNSecSigned(int32_t& sec, int32_t& nsec);
66 
71 template <class T>
73 {
74 public:
75  int32_t sec, nsec;
76  DurationBase() : sec(0), nsec(0) { }
77  DurationBase(int32_t _sec, int32_t _nsec);
78  explicit DurationBase(double t){fromSec(t);};
79  T operator+(const T &rhs) const;
80  T operator-(const T &rhs) const;
81  T operator-() const;
82  T operator*(double scale) const;
83  T& operator+=(const T &rhs);
84  T& operator-=(const T &rhs);
85  T& operator*=(double scale);
86  bool operator==(const T &rhs) const;
87  inline bool operator!=(const T &rhs) const { return !(*static_cast<const T*>(this) == rhs); }
88  bool operator>(const T &rhs) const;
89  bool operator<(const T &rhs) const;
90  bool operator>=(const T &rhs) const;
91  bool operator<=(const T &rhs) const;
92  double toSec() const { return static_cast<double>(sec) + 1e-9*static_cast<double>(nsec); };
93  int64_t toNSec() const {return static_cast<int64_t>(sec)*1000000000ll + static_cast<int64_t>(nsec); };
94  T& fromSec(double t);
95  T& fromNSec(int64_t t);
96  bool isZero() const;
97  boost::posix_time::time_duration toBoost() const;
98  static const T MIN;
99  static const T MAX;
100  static const T ZERO;
101  static const T DAY;
102  static const T HOUR;
103  static const T MINUTE;
104  static const T SECOND;
105  static const T MILLISECOND;
106  static const T MICROSECOND;
107  static const T NANOSECOND;
108 };
109 
110 class Rate;
111 
117 class ROSTIME_DECL Duration : public DurationBase<Duration>
118 {
119 public:
122  { }
123 
124  Duration(int32_t _sec, int32_t _nsec)
125  : DurationBase<Duration>(_sec, _nsec)
126  {}
127 
128  explicit Duration(double t) { fromSec(t); }
129  explicit Duration(const Rate&);
134  bool sleep() const;
135 };
136 
137 extern ROSTIME_DECL const Duration DURATION_MAX;
138 extern ROSTIME_DECL const Duration DURATION_MIN;
149 
155 class ROSTIME_DECL WallDuration : public DurationBase<WallDuration>
156 {
157 public:
160  { }
161 
162  WallDuration(int32_t _sec, int32_t _nsec)
163  : DurationBase<WallDuration>(_sec, _nsec)
164  {}
165 
166  explicit WallDuration(double t) { fromSec(t); }
167  explicit WallDuration(const Rate&);
172  bool sleep() const;
173 };
174 
185 
186 ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const Duration &rhs);
187 ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const WallDuration &rhs);
188 
189 }
190 
191 #endif // ROS_DURATION_H
ros::DurationBase::HOUR
static const T HOUR
One hour duration.
Definition: duration.h:102
ros::DurationBase::operator-
T operator-() const
Definition: impl/duration.h:143
ros::WallDuration::WallDuration
WallDuration(int32_t _sec, int32_t _nsec)
Definition: duration.h:162
ros::operator<<
ROSTIME_DECL std::ostream & operator<<(std::ostream &os, const Duration &rhs)
Definition: src/time.cpp:359
ros::DurationBase::operator*=
T & operator*=(double scale)
Definition: impl/duration.h:164
ros::DurationBase::DurationBase
DurationBase(double t)
Definition: duration.h:78
ros::DurationBase::fromSec
T & fromSec(double t)
Definition: impl/duration.h:89
ros::DURATION_MIN
const ROSTIME_DECL Duration DURATION_MIN
ros
ros::Duration::Duration
Duration()
Definition: duration.h:120
ros::DurationBase::DAY
static const T DAY
One day duration.
Definition: duration.h:101
ros::WallDuration::WallDuration
WallDuration()
Definition: duration.h:158
ros::DurationBase::nsec
int32_t nsec
Definition: duration.h:75
ros::Duration::Duration
Duration(int32_t _sec, int32_t _nsec)
Definition: duration.h:124
ros::DurationBase::operator!=
bool operator!=(const T &rhs) const
Definition: duration.h:87
boost
Definition: duration.h:56
ros::DURATION_MAX
const ROSTIME_DECL Duration DURATION_MAX
ros::DurationBase::operator*
T operator*(double scale) const
Definition: impl/duration.h:130
ros::DurationBase::operator+=
T & operator+=(const T &rhs)
Definition: impl/duration.h:150
ros::DurationBase::MICROSECOND
static const T MICROSECOND
One microsecond duration.
Definition: duration.h:106
ros::DurationBase::MILLISECOND
static const T MILLISECOND
One millisecond duration.
Definition: duration.h:105
ros::DurationBase::MIN
static const T MIN
Minimum representable duration (negative)
Definition: duration.h:98
ros::DurationBase::toBoost
boost::posix_time::time_duration toBoost() const
Definition: impl/duration.h:224
ros::DurationBase::ZERO
static const T ZERO
Zero duration.
Definition: duration.h:100
ros::Duration::Duration
Duration(double t)
Definition: duration.h:128
ros::DurationBase::operator<=
bool operator<=(const T &rhs) const
Definition: impl/duration.h:191
ros::DurationBase
Base class for Duration implementations. Provides storage, common functions and operator overloads....
Definition: duration.h:72
ros::WallDuration::WallDuration
WallDuration(double t)
Definition: duration.h:166
ros::DurationBase::SECOND
static const T SECOND
One second duration.
Definition: duration.h:104
ros::DurationBase::operator>=
bool operator>=(const T &rhs) const
Definition: impl/duration.h:201
ros::DurationBase::NANOSECOND
static const T NANOSECOND
One nanosecond duration.
Definition: duration.h:107
ros::DurationBase::operator==
bool operator==(const T &rhs) const
Definition: impl/duration.h:211
ros::DurationBase::MAX
static const T MAX
Maximum representable duration.
Definition: duration.h:99
ros::DurationBase::toNSec
int64_t toNSec() const
Definition: duration.h:93
ros::DurationBase::DurationBase
DurationBase()
Definition: duration.h:76
ros::DurationBase::operator>
bool operator>(const T &rhs) const
Definition: impl/duration.h:181
ros::normalizeSecNSecSigned
ROSTIME_DECL void normalizeSecNSecSigned(int64_t &sec, int64_t &nsec)
Definition: src/duration.cpp:79
ros::DurationBase::MINUTE
static const T MINUTE
One minute duration.
Definition: duration.h:103
ros::Rate
Class to help run loops at a desired frequency.
Definition: rate.h:86
ros::DurationBase::toSec
double toSec() const
Definition: duration.h:92
ros::DurationBase::operator<
bool operator<(const T &rhs) const
Definition: impl/duration.h:171
ros::WallDuration
Duration representation for use with the WallTime class.
Definition: duration.h:155
ROSTIME_DECL
#define ROSTIME_DECL
Definition: rostime_decl.h:52
ros::Duration
Duration representation for use with the Time class.
Definition: duration.h:117
ros::DurationBase::sec
int32_t sec
Definition: duration.h:75
ros::DurationBase::isZero
bool isZero() const
Definition: impl/duration.h:217
ros::DurationBase::operator-=
T & operator-=(const T &rhs)
Definition: impl/duration.h:157
ros::DurationBase::operator+
T operator+(const T &rhs) const
Definition: impl/duration.h:123
rostime_decl.h
ros::DurationBase::fromNSec
T & fromNSec(int64_t t)
Definition: impl/duration.h:109


rostime
Author(s): Josh Faust, Dirk Thomas
autogenerated on Sat Jun 17 2023 02:32:37