time.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 
00035 #ifndef ROS_TIME_H_INCLUDED
00036 #define ROS_TIME_H_INCLUDED
00037 
00038 /*********************************************************************
00039  ** Pragmas
00040  *********************************************************************/
00041 
00042 #ifdef _MSC_VER
00043   // Rostime has some magic interface that doesn't directly include
00044   // its implementation, this just disables those warnings.
00045   #pragma warning(disable: 4244)
00046   #pragma warning(disable: 4661)
00047 #endif
00048 
00049 /*********************************************************************
00050  ** Headers
00051  *********************************************************************/
00052 
00053 #include <ros/platform.h>
00054 #include <iostream>
00055 #include <cmath>
00056 #include <ros/exception.h>
00057 #include "duration.h"
00058 #include <boost/math/special_functions/round.hpp>
00059 #include "rostime_decl.h"
00060 
00061 /*********************************************************************
00062  ** Cross Platform Headers
00063  *********************************************************************/
00064 
00065 #ifdef WIN32
00066   #include <sys/timeb.h>
00067 #else
00068   #include <sys/time.h>
00069 #endif
00070 
00071 namespace boost {
00072   namespace posix_time {
00073     class ptime;
00074     class time_duration;
00075   }
00076 }
00077 
00078 namespace ros
00079 {
00080 
00081   /*********************************************************************
00082    ** Exceptions
00083    *********************************************************************/
00084 
00088   class ROSTIME_DECL TimeNotInitializedException : public Exception
00089   {
00090   public:
00091     TimeNotInitializedException()
00092       : Exception("Cannot use ros::Time::now() before the first NodeHandle has been created or ros::start() has been called.  "
00093                   "If this is a standalone app or test that just uses ros::Time and does not communicate over ROS, you may also call ros::Time::init()")
00094     {}
00095   };
00096 
00102   class ROSTIME_DECL NoHighPerformanceTimersException : public Exception
00103   {
00104   public:
00105     NoHighPerformanceTimersException()
00106       : Exception("This windows platform does not "
00107                   "support the high-performance timing api.")
00108     {}
00109   };
00110 
00111   /*********************************************************************
00112    ** Functions
00113    *********************************************************************/
00114 
00115   ROSTIME_DECL void normalizeSecNSec(uint64_t& sec, uint64_t& nsec);
00116   ROSTIME_DECL void normalizeSecNSec(uint32_t& sec, uint32_t& nsec);
00117   ROSTIME_DECL void normalizeSecNSecUnsigned(int64_t& sec, int64_t& nsec);
00118 
00119   /*********************************************************************
00120    ** Time Classes
00121    *********************************************************************/
00122 
00127   template<class T, class D>
00128   class TimeBase
00129   {
00130   public:
00131     uint32_t sec, nsec;
00132 
00133     TimeBase() : sec(0), nsec(0) { }
00134     TimeBase(uint32_t _sec, uint32_t _nsec) : sec(_sec), nsec(_nsec)
00135     {
00136       normalizeSecNSec(sec, nsec);
00137     }
00138     explicit TimeBase(double t) { fromSec(t); }
00139     ~TimeBase() {}
00140     D operator-(const T &rhs) const;
00141     T operator+(const D &rhs) const;
00142     T operator-(const D &rhs) const;
00143     T& operator+=(const D &rhs);
00144     T& operator-=(const D &rhs);
00145     bool operator==(const T &rhs) const;
00146     inline bool operator!=(const T &rhs) const { return !(*static_cast<const T*>(this) == rhs); }
00147     bool operator>(const T &rhs) const;
00148     bool operator<(const T &rhs) const;
00149     bool operator>=(const T &rhs) const;
00150     bool operator<=(const T &rhs) const;
00151 
00152     double toSec()  const { return (double)sec + 1e-9*(double)nsec; };
00153     T& fromSec(double t) { sec = (uint32_t)floor(t); nsec = (uint32_t)boost::math::round((t-sec) * 1e9);  return *static_cast<T*>(this);}
00154 
00155     uint64_t toNSec() const {return (uint64_t)sec*1000000000ull + (uint64_t)nsec;  }
00156     T& fromNSec(uint64_t t);
00157 
00158     inline bool isZero() const { return sec == 0 && nsec == 0; }
00159     inline bool is_zero() const { return isZero(); }
00160     boost::posix_time::ptime toBoost() const;
00161 
00162   };
00163 
00169   class ROSTIME_DECL Time : public TimeBase<Time, Duration>
00170   {
00171   public:
00172     Time()
00173       : TimeBase<Time, Duration>()
00174     {}
00175 
00176     Time(uint32_t _sec, uint32_t _nsec)
00177       : TimeBase<Time, Duration>(_sec, _nsec)
00178     {}
00179 
00180     explicit Time(double t) { fromSec(t); }
00181 
00186     static Time now();
00190     static bool sleepUntil(const Time& end);
00191 
00192     static void init();
00193     static void shutdown();
00194     static void setNow(const Time& new_now);
00195     static bool useSystemTime();
00196     static bool isSimTime();
00197     static bool isSystemTime();
00198 
00202     static bool isValid();
00206     static bool waitForValid();
00210     static bool waitForValid(const ros::WallDuration& timeout);
00211 
00212     static Time fromBoost(const boost::posix_time::ptime& t);
00213     static Time fromBoost(const boost::posix_time::time_duration& d);
00214   };
00215 
00216   extern ROSTIME_DECL const Time TIME_MAX;
00217   extern ROSTIME_DECL const Time TIME_MIN;
00218 
00224   class ROSTIME_DECL WallTime : public TimeBase<WallTime, WallDuration>
00225   {
00226   public:
00227     WallTime()
00228       : TimeBase<WallTime, WallDuration>()
00229     {}
00230 
00231     WallTime(uint32_t _sec, uint32_t _nsec)
00232       : TimeBase<WallTime, WallDuration>(_sec, _nsec)
00233     {}
00234 
00235     explicit WallTime(double t) { fromSec(t); }
00236 
00240     static WallTime now();
00241 
00245     static bool sleepUntil(const WallTime& end);
00246 
00247     static bool isSystemTime() { return true; }
00248   };
00249 
00250   ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const Time &rhs);
00251   ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const WallTime &rhs);
00252 }
00253 
00254 #endif // ROS_TIME_H
00255 


rostime
Author(s): Josh Faust
autogenerated on Mon Oct 6 2014 07:00:39