impl/duration.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 #ifndef ROSTIME_IMPL_DURATION_H_INCLUDED
35 #define ROSTIME_IMPL_DURATION_H_INCLUDED
36 
37 #include <cmath>
38 #include <limits>
39 
40 #include <ros/duration.h>
41 #include <ros/rate.h>
42 #include <boost/date_time/posix_time/posix_time_types.hpp>
43 #include <boost/math/special_functions/round.hpp>
44 
45 namespace ros {
46  //
47  // DurationBase template member function implementation
48  //
49  template<class T>
50  DurationBase<T>::DurationBase(int32_t _sec, int32_t _nsec)
51  : sec(_sec), nsec(_nsec)
52  {
53  normalizeSecNSecSigned(sec, nsec);
54  }
55 
56  template<class T>
57  T& DurationBase<T>::fromSec(double d)
58  {
59  if (!std::isfinite(d))
60  throw std::runtime_error("Duration has to be finite.");
61  constexpr double minInt64AsDouble = static_cast<double>(std::numeric_limits<int64_t>::min());
62  constexpr double maxInt64AsDouble = static_cast<double>(std::numeric_limits<int64_t>::max());
63  if (d <= minInt64AsDouble || d >= maxInt64AsDouble)
64  throw std::runtime_error("Duration is out of 64-bit integer range");
65  int64_t sec64 = static_cast<int64_t>(floor(d));
66  if (sec64 < std::numeric_limits<int32_t>::min() || sec64 > std::numeric_limits<int32_t>::max())
67  throw std::runtime_error("Duration is out of dual 32-bit range");
68  sec = static_cast<int32_t>(sec64);
69  nsec = static_cast<int32_t>(boost::math::round((d - sec) * 1e9));
70  int32_t rollover = nsec / 1000000000ul;
71  sec += rollover;
72  nsec %= 1000000000ul;
73  return *static_cast<T*>(this);
74  }
75 
76  template<class T>
77  T& DurationBase<T>::fromNSec(int64_t t)
78  {
79  int64_t sec64 = t / 1000000000LL;
80  if (sec64 < std::numeric_limits<int32_t>::min() || sec64 > std::numeric_limits<int32_t>::max())
81  throw std::runtime_error("Duration is out of dual 32-bit range");
82  sec = static_cast<int32_t>(sec64);
83  nsec = static_cast<int32_t>(t % 1000000000LL);
84 
85  normalizeSecNSecSigned(sec, nsec);
86 
87  return *static_cast<T*>(this);
88  }
89 
90  template<class T>
91  T DurationBase<T>::operator+(const T &rhs) const
92  {
93  T t;
94  return t.fromNSec(toNSec() + rhs.toNSec());
95  }
96 
97  template<class T>
98  T DurationBase<T>::operator*(double scale) const
99  {
100  return T(toSec() * scale);
101  }
102 
103  template<class T>
104  T DurationBase<T>::operator-(const T &rhs) const
105  {
106  T t;
107  return t.fromNSec(toNSec() - rhs.toNSec());
108  }
109 
110  template<class T>
112  {
113  T t;
114  return t.fromNSec(-toNSec());
115  }
116 
117  template<class T>
118  T& DurationBase<T>::operator+=(const T &rhs)
119  {
120  *this = *this + rhs;
121  return *static_cast<T*>(this);
122  }
123 
124  template<class T>
125  T& DurationBase<T>::operator-=(const T &rhs)
126  {
127  *this += (-rhs);
128  return *static_cast<T*>(this);
129  }
130 
131  template<class T>
132  T& DurationBase<T>::operator*=(double scale)
133  {
134  fromSec(toSec() * scale);
135  return *static_cast<T*>(this);
136  }
137 
138  template<class T>
139  bool DurationBase<T>::operator<(const T &rhs) const
140  {
141  if (sec < rhs.sec)
142  return true;
143  else if (sec == rhs.sec && nsec < rhs.nsec)
144  return true;
145  return false;
146  }
147 
148  template<class T>
149  bool DurationBase<T>::operator>(const T &rhs) const
150  {
151  if (sec > rhs.sec)
152  return true;
153  else if (sec == rhs.sec && nsec > rhs.nsec)
154  return true;
155  return false;
156  }
157 
158  template<class T>
159  bool DurationBase<T>::operator<=(const T &rhs) const
160  {
161  if (sec < rhs.sec)
162  return true;
163  else if (sec == rhs.sec && nsec <= rhs.nsec)
164  return true;
165  return false;
166  }
167 
168  template<class T>
169  bool DurationBase<T>::operator>=(const T &rhs) const
170  {
171  if (sec > rhs.sec)
172  return true;
173  else if (sec == rhs.sec && nsec >= rhs.nsec)
174  return true;
175  return false;
176  }
177 
178  template<class T>
179  bool DurationBase<T>::operator==(const T &rhs) const
180  {
181  return sec == rhs.sec && nsec == rhs.nsec;
182  }
183 
184  template<class T>
185  bool DurationBase<T>::isZero() const
186  {
187  return sec == 0 && nsec == 0;
188  }
189 
190  template <class T>
191  boost::posix_time::time_duration
193  {
194  namespace bt = boost::posix_time;
195 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
196  return bt::seconds(sec) + bt::nanoseconds(nsec);
197 #else
198  return bt::seconds(sec) + bt::microseconds(nsec/1000);
199 #endif
200  }
201 }
202 #endif
ros
rate.h
duration.h
ros::DurationBase
Base class for Duration implementations. Provides storage, common functions and operator overloads....
Definition: duration.h:72
ros::DurationBase::DurationBase
DurationBase()
Definition: duration.h:76
boost::posix_time
Definition: duration.h:91
ros::normalizeSecNSecSigned
ROSTIME_DECL void normalizeSecNSecSigned(int64_t &sec, int64_t &nsec)
Definition: src/duration.cpp:79
ros::DurationBase::sec
int32_t sec
Definition: duration.h:75
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