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


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