OKVIS ROS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Duration.hpp
Go to the documentation of this file.
1 /*********************************************************************
2  * Software License Agreement (BSD License)
3  *
4  * Modified and adapted by
5  * Copyright (c) 2015, Autonomous Systems Lab / ETH Zurich
6  *
7  * Original Copyright (c) 2008, Willow Garage, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * * Redistributions of source code must retain the above copyright
15  * notice, this list of conditions and the following disclaimer.
16  * * Redistributions in binary form must reproduce the above
17  * copyright notice, this list of conditions and the following
18  * disclaimer in the documentation and/or other materials provided
19  * with the distribution.
20  * * Neither the name of Willow Garage, Inc. nor the names of its
21  * contributors may be used to endorse or promote products derived
22  * from this software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  *********************************************************************/
37 /*
38  * Modified: Stefan Leutenegger (s.leutenegger@imperial.ac.uk)
39  * Modified: Andreas Forster (an.forster@gmail.com)
40  */
41 
51 #ifndef INCLUDE_OKVIS_DURATION_HPP_
52 #define INCLUDE_OKVIS_DURATION_HPP_
53 
54 /*********************************************************************
55  ** Pragmas
56  *********************************************************************/
57 
58 #ifdef _MSC_VER
59 // Okvistime has some magic interface that doesn't directly include
60 // its implementation, this just disbales those warnings.
61 #pragma warning(disable: 4244)
62 #pragma warning(disable: 4661)
63 #endif
64 
65 #include <iostream>
66 #include <math.h>
67 #include <stdexcept>
68 #include <climits>
69 #include <stdint.h>
70 //#include "rostime_decl.h"
71 
73 namespace okvis {
74 void normalizeSecNSecSigned(int64_t& sec, int64_t& nsec);
75 void normalizeSecNSecSigned(int32_t& sec, int32_t& nsec);
76 
81 template<class T>
82 class DurationBase {
83  public:
84  int32_t sec, nsec;
86  : sec(0),
87  nsec(0) {
88  }
89  DurationBase(int32_t _sec, int32_t _nsec);
90  explicit DurationBase(double t) {
91  fromSec(t);
92  }
93  ;
95  }
96  T operator+(const T &rhs) const;
97  T operator-(const T &rhs) const;
98  T operator-() const;
99  T operator*(double scale) const;
100  T& operator+=(const T &rhs);
101  T& operator-=(const T &rhs);
102  T& operator*=(double scale);
103  bool operator==(const T &rhs) const;
104  inline bool operator!=(const T &rhs) const {
105  return !(*static_cast<const T*>(this) == rhs);
106  }
107  bool operator>(const T &rhs) const;
108  bool operator<(const T &rhs) const;
109  bool operator>=(const T &rhs) const;
110  bool operator<=(const T &rhs) const;
111  double toSec() const {
112  return (double) sec + 1e-9 * (double) nsec;
113  }
114  ;
115  int64_t toNSec() const {
116  return (int64_t) sec * 1000000000ll + (int64_t) nsec;
117  }
118  ;
119  T& fromSec(double t);
120  T& fromNSec(int64_t t);
121  bool isZero();
122 };
123 
124 class Rate;
125 
131 class Duration : public DurationBase<Duration> {
132  public:
134  : DurationBase<Duration>() {
135  }
136 
137  Duration(int32_t _sec, int32_t _nsec)
138  : DurationBase<Duration>(_sec, _nsec) {
139  }
140 
141  explicit Duration(double t) {
142  fromSec(t);
143  }
144  explicit Duration(const Rate&);
148  bool sleep() const;
149 };
150 
151 extern const Duration DURATION_MAX;
152 extern const Duration DURATION_MIN;
153 
159 class WallDuration : public DurationBase<WallDuration> {
160  public:
163  }
164 
165  WallDuration(int32_t _sec, int32_t _nsec)
166  : DurationBase<WallDuration>(_sec, _nsec) {
167  }
168 
169  explicit WallDuration(double t) {
170  fromSec(t);
171  }
172  explicit WallDuration(const Rate&);
176  bool sleep() const;
177 };
178 
179 std::ostream &operator <<(std::ostream &os, const Duration &rhs);
180 std::ostream &operator <<(std::ostream &os, const WallDuration &rhs);
181 
182 }
183 
184 #endif // INCLUDE_OKVIS_DURATION_HPP_
185 
DurationBase()
Definition: Duration.hpp:85
bool operator==(const T &rhs) const
Definition: Duration.hpp:169
bool isZero()
Definition: Duration.hpp:174
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep...
Definition: Time.cpp:366
DurationBase(double t)
Definition: Duration.hpp:90
Base class for Duration implementations. Provides storage, common functions and operator overloads...
Definition: Duration.hpp:82
int64_t toNSec() const
Definition: Duration.hpp:115
bool operator<(const T &rhs) const
Definition: Duration.hpp:133
Duration(int32_t _sec, int32_t _nsec)
Definition: Duration.hpp:137
bool operator<=(const T &rhs) const
Definition: Duration.hpp:151
const Duration DURATION_MAX
std::ostream & operator<<(std::ostream &os, const Duration &rhs)
Definition: Time.cpp:284
bool operator>(const T &rhs) const
Definition: Duration.hpp:142
Duration()
Definition: Duration.hpp:133
T & fromNSec(int64_t t)
Definition: Duration.hpp:85
bool sleep() const
sleep for the amount of time specified by this Duration. If a signal interrupts the sleep...
Definition: Time.cpp:319
bool operator>=(const T &rhs) const
Definition: Duration.hpp:160
~DurationBase()
Definition: Duration.hpp:94
T & operator-=(const T &rhs)
Definition: Duration.hpp:121
double toSec() const
Definition: Duration.hpp:111
bool operator!=(const T &rhs) const
Definition: Duration.hpp:104
Duration(double t)
Definition: Duration.hpp:141
Duration representation for use with the Time class.
Definition: Duration.hpp:131
WallDuration(int32_t _sec, int32_t _nsec)
Definition: Duration.hpp:165
T operator+(const T &rhs) const
Definition: Duration.hpp:95
void normalizeSecNSecSigned(int64_t &sec, int64_t &nsec)
Definition: Duration.cpp:55
int32_t nsec
Definition: Duration.hpp:84
Duration representation for use with the WallTime class.
Definition: Duration.hpp:159
T operator-() const
Definition: Duration.hpp:110
T & fromSec(double t)
Definition: Duration.hpp:69
T operator*(double scale) const
Definition: Duration.hpp:100
T & operator*=(double scale)
Definition: Duration.hpp:127
WallDuration(double t)
Definition: Duration.hpp:169
const Duration DURATION_MIN
int32_t sec
Definition: Duration.hpp:84
WallDuration()
Definition: Duration.hpp:161
T & operator+=(const T &rhs)
Definition: Duration.hpp:115