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


rostime
Author(s): Josh Faust
autogenerated on Sat Apr 6 2019 02:52:21