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);};
80  T operator+(const T &rhs) const;
81  T operator-(const T &rhs) const;
82  T operator-() const;
83  T operator*(double scale) const;
84  T& operator+=(const T &rhs);
85  T& operator-=(const T &rhs);
86  T& operator*=(double scale);
87  bool operator==(const T &rhs) const;
88  inline bool operator!=(const T &rhs) const { return !(*static_cast<const T*>(this) == rhs); }
89  bool operator>(const T &rhs) const;
90  bool operator<(const T &rhs) const;
91  bool operator>=(const T &rhs) const;
92  bool operator<=(const T &rhs) const;
93  double toSec() const { return static_cast<double>(sec) + 1e-9*static_cast<double>(nsec); };
94  int64_t toNSec() const {return static_cast<int64_t>(sec)*1000000000ll + static_cast<int64_t>(nsec); };
95  T& fromSec(double t);
96  T& fromNSec(int64_t t);
97  bool isZero() const;
98  boost::posix_time::time_duration toBoost() const;
99 };
100 
101 class Rate;
102 
108 class ROSTIME_DECL Duration : public DurationBase<Duration>
109 {
110 public:
113  { }
114 
115  Duration(int32_t _sec, int32_t _nsec)
116  : DurationBase<Duration>(_sec, _nsec)
117  {}
118 
119  explicit Duration(double t) { fromSec(t); }
120  explicit Duration(const Rate&);
125  bool sleep() const;
126 };
127 
128 extern ROSTIME_DECL const Duration DURATION_MAX;
129 extern ROSTIME_DECL const Duration DURATION_MIN;
130 
136 class ROSTIME_DECL WallDuration : public DurationBase<WallDuration>
137 {
138 public:
141  { }
142 
143  WallDuration(int32_t _sec, int32_t _nsec)
144  : DurationBase<WallDuration>(_sec, _nsec)
145  {}
146 
147  explicit WallDuration(double t) { fromSec(t); }
148  explicit WallDuration(const Rate&);
153  bool sleep() const;
154 };
155 
156 ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const Duration &rhs);
157 ROSTIME_DECL std::ostream &operator <<(std::ostream &os, const WallDuration &rhs);
158 
159 }
160 
161 #endif // ROS_DURATION_H
Duration(int32_t _sec, int32_t _nsec)
Definition: duration.h:115
ROSTIME_DECL std::ostream & operator<<(std::ostream &os, const WallDuration &rhs)
Definition: src/time.cpp:454
Definition: duration.h:56
DurationBase(double t)
Definition: duration.h:78
Duration(double t)
Definition: duration.h:119
Class to help run loops at a desired frequency.
Definition: rate.h:51
Duration representation for use with the WallTime class.
Definition: duration.h:136
bool operator!=(const T &rhs) const
Definition: duration.h:88
ROSTIME_DECL void normalizeSecNSecSigned(int32_t &sec, int32_t &nsec)
WallDuration(int32_t _sec, int32_t _nsec)
Definition: duration.h:143
Base class for Duration implementations. Provides storage, common functions and operator overloads...
Definition: duration.h:72
WallDuration(double t)
Definition: duration.h:147
double toSec() const
Definition: duration.h:93
Duration representation for use with the Time class.
Definition: duration.h:108
int64_t toNSec() const
Definition: duration.h:94
ROSTIME_DECL const Duration DURATION_MIN
ROSTIME_DECL const Duration DURATION_MAX
#define ROSTIME_DECL
Definition: rostime_decl.h:52


rostime
Author(s): Josh Faust
autogenerated on Mon Feb 28 2022 23:31:37