Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034 #ifndef ROSTIME_IMPL_DURATION_H_INCLUDED
00035 #define ROSTIME_IMPL_DURATION_H_INCLUDED
00036
00037 #include <ros/duration.h>
00038 #include <ros/rate.h>
00039 #include <boost/date_time/posix_time/posix_time_types.hpp>
00040
00041 namespace ros {
00042
00043
00044
00045 template<class T>
00046 DurationBase<T>::DurationBase(int32_t _sec, int32_t _nsec)
00047 : sec(_sec), nsec(_nsec)
00048 {
00049 normalizeSecNSecSigned(sec, nsec);
00050 }
00051
00052 template<class T>
00053 T& DurationBase<T>::fromSec(double d)
00054 {
00055 #ifdef HAVE_TRUNC
00056 sec = (int32_t)trunc(d);
00057 #else
00058
00059
00060 if (d >= 0.0)
00061 sec = (int32_t)floor(d);
00062 else
00063 sec = (int32_t)floor(d) + 1;
00064 #endif
00065 nsec = (int32_t)((d - (double)sec)*1000000000);
00066 return *static_cast<T*>(this);
00067 }
00068
00069 template<class T>
00070 T& DurationBase<T>::fromNSec(int64_t t)
00071 {
00072 sec = (int32_t)(t / 1000000000);
00073 nsec = (int32_t)(t % 1000000000);
00074
00075 normalizeSecNSecSigned(sec, nsec);
00076
00077 return *static_cast<T*>(this);
00078 }
00079
00080 template<class T>
00081 T DurationBase<T>::operator+(const T &rhs) const
00082 {
00083 return T(sec + rhs.sec, nsec + rhs.nsec);
00084 }
00085
00086 template<class T>
00087 T DurationBase<T>::operator*(double scale) const
00088 {
00089 return T(toSec() * scale);
00090 }
00091
00092 template<class T>
00093 T DurationBase<T>::operator-(const T &rhs) const
00094 {
00095 return T(sec - rhs.sec, nsec - rhs.nsec);
00096 }
00097
00098 template<class T>
00099 T DurationBase<T>::operator-() const
00100 {
00101 return T(-sec , -nsec);
00102 }
00103
00104 template<class T>
00105 T& DurationBase<T>::operator+=(const T &rhs)
00106 {
00107 *this = *this + rhs;
00108 return *static_cast<T*>(this);
00109 }
00110
00111 template<class T>
00112 T& DurationBase<T>::operator-=(const T &rhs)
00113 {
00114 *this += (-rhs);
00115 return *static_cast<T*>(this);
00116 }
00117
00118 template<class T>
00119 T& DurationBase<T>::operator*=(double scale)
00120 {
00121 fromSec(toSec() * scale);
00122 return *static_cast<T*>(this);
00123 }
00124
00125 template<class T>
00126 bool DurationBase<T>::operator<(const T &rhs) const
00127 {
00128 if (sec < rhs.sec)
00129 return true;
00130 else if (sec == rhs.sec && nsec < rhs.nsec)
00131 return true;
00132 return false;
00133 }
00134
00135 template<class T>
00136 bool DurationBase<T>::operator>(const T &rhs) const
00137 {
00138 if (sec > rhs.sec)
00139 return true;
00140 else if (sec == rhs.sec && nsec > rhs.nsec)
00141 return true;
00142 return false;
00143 }
00144
00145 template<class T>
00146 bool DurationBase<T>::operator<=(const T &rhs) const
00147 {
00148 if (sec < rhs.sec)
00149 return true;
00150 else if (sec == rhs.sec && nsec <= rhs.nsec)
00151 return true;
00152 return false;
00153 }
00154
00155 template<class T>
00156 bool DurationBase<T>::operator>=(const T &rhs) const
00157 {
00158 if (sec > rhs.sec)
00159 return true;
00160 else if (sec == rhs.sec && nsec >= rhs.nsec)
00161 return true;
00162 return false;
00163 }
00164
00165 template<class T>
00166 bool DurationBase<T>::operator==(const T &rhs) const
00167 {
00168 return sec == rhs.sec && nsec == rhs.nsec;
00169 }
00170
00171 template<class T>
00172 bool DurationBase<T>::isZero() const
00173 {
00174 return sec == 0 && nsec == 0;
00175 }
00176
00177 template <class T>
00178 boost::posix_time::time_duration
00179 DurationBase<T>::toBoost() const
00180 {
00181 namespace bt = boost::posix_time;
00182 #if defined(BOOST_DATE_TIME_HAS_NANOSECONDS)
00183 return bt::seconds(sec) + bt::nanoseconds(nsec);
00184 #else
00185 return bt::seconds(sec) + bt::microseconds(nsec/1000.0);
00186 #endif
00187 }
00188
00189 inline Duration::Duration(const Rate& r)
00190 {
00191 fromSec(r.expectedCycleTime().toSec());
00192 }
00193
00194 inline WallDuration::WallDuration(const Rate& r)
00195 {
00196 fromSec(r.expectedCycleTime().toSec());
00197 }
00198 }
00199 #endif