duration.h
Go to the documentation of this file.
00001 /*********************************************************************
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
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   // DurationBase template member function implementation
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     // (morgan: why doesn't win32 provide trunc? argh. hacked this together
00059     // without much thought. need to test this conversion.
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()
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


rostime
Author(s): Josh Faust
autogenerated on Fri Jan 3 2014 11:49:59