duration.h
Go to the documentation of this file.
1 /*********************************************************************
2 * Software License Agreement (BSD License)
3 *
4 * Copyright (c) 2008, 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 
35 #ifndef ROS_DURATION_H
36 #define ROS_DURATION_H
37 
38 /*********************************************************************
39  ** Pragmas
40  *********************************************************************/
41 
42 #ifdef _MSC_VER
43  // Rostime has some magic interface that doesn't directly include
44  // its implementation, this just disbales those warnings.
45  #pragma warning(disable: 4244)
46  #pragma warning(disable: 4661)
47 #endif
48 
49 #include <iostream>
50 #include <math.h>
51 #include <stdexcept>
52 #include <climits>
53 #include <stdint.h>
54 #include "rostime_decl.h"
55 
56 namespace boost {
57  namespace posix_time {
58  class time_duration;
59  }
60 }
61 
62 namespace ros
63 {
64 ROSTIME_DECL void normalizeSecNSecSigned(int64_t& sec, int64_t& nsec);
65 ROSTIME_DECL void normalizeSecNSecSigned(int32_t& sec, int32_t& nsec);
66 
71 template <class T>
73 {
74 public:
75  int32_t sec, nsec;
76  DurationBase() : sec(0), nsec(0) { }
77  DurationBase(int32_t _sec, int32_t _nsec);
78  explicit DurationBase(double t){fromSec(t);};
79  T operator+(const T &rhs) const;
80  T operator-(const T &rhs) const;
81  T operator-() const;
82  T operator*(double scale) const;
83  T& operator+=(const T &rhs);
84  T& operator-=(const T &rhs);
85  T& operator*=(double scale);
86  bool operator==(const T &rhs) const;
87  inline bool operator!=(const T &rhs) const { return !(*static_cast<const T*>(this) == rhs); }
88  bool operator>(const T &rhs) const;
89  bool operator<(const T &rhs) const;
90  bool operator>=(const T &rhs) const;
91  bool operator<=(const T &rhs) const;
92  double toSec() const { return static_cast<double>(sec) + 1e-9*static_cast<double>(nsec); };
93  int64_t toNSec() const {return static_cast<int64_t>(sec)*1000000000ll + static_cast<int64_t>(nsec); };
94  T& fromSec(double t);
95  T& fromNSec(int64_t t);
96  bool isZero() const;
97  boost::posix_time::time_duration toBoost() const;
98 };
99 
100 class Rate;
101 
107 class ROSTIME_DECL Duration : public DurationBase<Duration>
108 {
109 public:
112  { }
113 
114  Duration(int32_t _sec, int32_t _nsec)
115  : DurationBase<Duration>(_sec, _nsec)
116  {}
117 
118  explicit Duration(double t) { fromSec(t); }
119  explicit Duration(const Rate&);
124  bool sleep() const;
125 };
126 
127 extern ROSTIME_DECL const Duration DURATION_MAX;
128 extern ROSTIME_DECL const Duration DURATION_MIN;
129 
135 class ROSTIME_DECL WallDuration : public DurationBase<WallDuration>
136 {
137 public:
140  { }
141 
142  WallDuration(int32_t _sec, int32_t _nsec)
143  : DurationBase<WallDuration>(_sec, _nsec)
144  {}
145 
146  explicit WallDuration(double t) { fromSec(t); }
147  explicit WallDuration(const Rate&);
152  bool sleep() const;
153 };
154 
155 ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const Duration &rhs);
156 ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const WallDuration &rhs);
157 
158 }
159 
160 #endif // ROS_DURATION_H
ros::DurationBase::operator-
T operator-() const
Definition: impl/duration.h:134
ros::WallDuration::WallDuration
WallDuration(int32_t _sec, int32_t _nsec)
Definition: duration.h:142
ros::operator<<
ROSTIME_DECL std::ostream & operator<<(std::ostream &os, const Duration &rhs)
Definition: src/time.cpp:322
ros::DurationBase::operator*=
T & operator*=(double scale)
Definition: impl/duration.h:155
ros::DurationBase::DurationBase
DurationBase(double t)
Definition: duration.h:78
ros::DurationBase::fromSec
T & fromSec(double t)
Definition: impl/duration.h:86
ros::DURATION_MIN
const ROSTIME_DECL Duration DURATION_MIN
ros
ros::Duration::Duration
Duration()
Definition: duration.h:110
ros::WallDuration::WallDuration
WallDuration()
Definition: duration.h:138
ros::DurationBase::nsec
int32_t nsec
Definition: duration.h:75
ros::Duration::Duration
Duration(int32_t _sec, int32_t _nsec)
Definition: duration.h:114
ros::DurationBase::operator!=
bool operator!=(const T &rhs) const
Definition: duration.h:87
boost
Definition: duration.h:56
ros::DURATION_MAX
const ROSTIME_DECL Duration DURATION_MAX
ros::DurationBase::operator*
T operator*(double scale) const
Definition: impl/duration.h:121
ros::DurationBase::operator+=
T & operator+=(const T &rhs)
Definition: impl/duration.h:141
ros::DurationBase::toBoost
boost::posix_time::time_duration toBoost() const
Definition: impl/duration.h:215
ros::Duration::Duration
Duration(double t)
Definition: duration.h:118
ros::DurationBase::operator<=
bool operator<=(const T &rhs) const
Definition: impl/duration.h:182
ros::DurationBase
Base class for Duration implementations. Provides storage, common functions and operator overloads....
Definition: duration.h:72
ros::WallDuration::WallDuration
WallDuration(double t)
Definition: duration.h:146
ros::DurationBase::operator>=
bool operator>=(const T &rhs) const
Definition: impl/duration.h:192
ros::DurationBase::operator==
bool operator==(const T &rhs) const
Definition: impl/duration.h:202
ros::DurationBase::toNSec
int64_t toNSec() const
Definition: duration.h:93
ros::DurationBase::DurationBase
DurationBase()
Definition: duration.h:76
ros::DurationBase::operator>
bool operator>(const T &rhs) const
Definition: impl/duration.h:172
ros::normalizeSecNSecSigned
ROSTIME_DECL void normalizeSecNSecSigned(int64_t &sec, int64_t &nsec)
Definition: src/duration.cpp:79
ros::Rate
Class to help run loops at a desired frequency.
Definition: rate.h:86
ros::DurationBase::toSec
double toSec() const
Definition: duration.h:92
ros::DurationBase::operator<
bool operator<(const T &rhs) const
Definition: impl/duration.h:162
ros::WallDuration
Duration representation for use with the WallTime class.
Definition: duration.h:135
ROSTIME_DECL
#define ROSTIME_DECL
Definition: rostime_decl.h:52
ros::Duration
Duration representation for use with the Time class.
Definition: duration.h:107
ros::DurationBase::sec
int32_t sec
Definition: duration.h:75
ros::DurationBase::isZero
bool isZero() const
Definition: impl/duration.h:208
ros::DurationBase::operator-=
T & operator-=(const T &rhs)
Definition: impl/duration.h:148
ros::DurationBase::operator+
T operator+(const T &rhs) const
Definition: impl/duration.h:114
rostime_decl.h
ros::DurationBase::fromNSec
T & fromNSec(int64_t t)
Definition: impl/duration.h:100


rostime
Author(s): Josh Faust
autogenerated on Tue Jun 2 2020 03:31:27