time.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #ifndef UAVCAN_TIME_HPP_INCLUDED
6 #define UAVCAN_TIME_HPP_INCLUDED
7 
8 #include <cstdio>
9 #include <uavcan/std.hpp>
10 #include <uavcan/build_config.hpp>
12 #include <uavcan/Timestamp.hpp>
13 
14 
15 namespace uavcan
16 {
17 
18 template <typename D>
20 {
22 
23 protected:
25 
27  : usec_(0)
28  {
29  StaticAssert<(sizeof(D) == 8)>::check();
30  }
31 
32 public:
34 
35  static D fromUSec(int64_t us)
36  {
37  D d;
38  d.usec_ = us;
39  return d;
40  }
41  static D fromMSec(int64_t ms) { return fromUSec(ms * 1000); }
42 
43  int64_t toUSec() const { return usec_; }
44  int64_t toMSec() const { return usec_ / 1000; }
45 
46  D getAbs() const { return D::fromUSec((usec_ < 0) ? (-usec_) : usec_); }
47 
48  bool isPositive() const { return usec_ > 0; }
49  bool isNegative() const { return usec_ < 0; }
50  bool isZero() const { return usec_ == 0; }
51 
52  bool operator==(const D& r) const { return usec_ == r.usec_; }
53  bool operator!=(const D& r) const { return !operator==(r); }
54 
55  bool operator<(const D& r) const { return usec_ < r.usec_; }
56  bool operator>(const D& r) const { return usec_ > r.usec_; }
57  bool operator<=(const D& r) const { return usec_ <= r.usec_; }
58  bool operator>=(const D& r) const { return usec_ >= r.usec_; }
59 
60  D operator+(const D& r) const { return fromUSec(usec_ + r.usec_); } // TODO: overflow check
61  D operator-(const D& r) const { return fromUSec(usec_ - r.usec_); } // ditto
62 
63  D operator-() const { return fromUSec(-usec_); }
64 
65  D& operator+=(const D& r)
66  {
67  *this = *this + r;
68  return *static_cast<D*>(this);
69  }
70  D& operator-=(const D& r)
71  {
72  *this = *this - r;
73  return *static_cast<D*>(this);
74  }
75 
76  template <typename Scale>
77  D operator*(Scale scale) const { return fromUSec(usec_ * scale); }
78 
79  template <typename Scale>
80  D& operator*=(Scale scale)
81  {
82  *this = *this * scale;
83  return *static_cast<D*>(this);
84  }
85 
86  static const unsigned StringBufSize = 32;
87  void toString(char buf[StringBufSize]) const;
88 #if UAVCAN_TOSTRING
89  std::string toString() const;
90 #endif
91 };
92 
93 
94 template <typename T, typename D>
95 class TimeBase
96 {
98 
99 protected:
100  ~TimeBase() { }
101 
103  : usec_(0)
104  {
105  StaticAssert<(sizeof(T) == 8)>::check();
106  StaticAssert<(sizeof(D) == 8)>::check();
107  }
108 
109 public:
110  static T getMax() { return fromUSec(NumericTraits<uint64_t>::max()); }
111 
112  static T fromUSec(uint64_t us)
113  {
114  T d;
115  d.usec_ = us;
116  return d;
117  }
118  static T fromMSec(uint64_t ms) { return fromUSec(ms * 1000); }
119 
120  uint64_t toUSec() const { return usec_; }
121  uint64_t toMSec() const { return usec_ / 1000; }
122 
123  bool isZero() const { return usec_ == 0; }
124 
125  bool operator==(const T& r) const { return usec_ == r.usec_; }
126  bool operator!=(const T& r) const { return !operator==(r); }
127 
128  bool operator<(const T& r) const { return usec_ < r.usec_; }
129  bool operator>(const T& r) const { return usec_ > r.usec_; }
130  bool operator<=(const T& r) const { return usec_ <= r.usec_; }
131  bool operator>=(const T& r) const { return usec_ >= r.usec_; }
132 
133  T operator+(const D& r) const
134  {
135  if (r.isNegative())
136  {
137  if (uint64_t(r.getAbs().toUSec()) > usec_)
138  {
139  return fromUSec(0);
140  }
141  }
142  else
143  {
144  if (uint64_t(int64_t(usec_) + r.toUSec()) < usec_)
145  {
147  }
148  }
149  return fromUSec(uint64_t(int64_t(usec_) + r.toUSec()));
150  }
151 
152  T operator-(const D& r) const
153  {
154  return *static_cast<const T*>(this) + (-r);
155  }
156  D operator-(const T& r) const
157  {
158  return D::fromUSec((usec_ > r.usec_) ? int64_t(usec_ - r.usec_) : -int64_t(r.usec_ - usec_));
159  }
160 
161  T& operator+=(const D& r)
162  {
163  *this = *this + r;
164  return *static_cast<T*>(this);
165  }
166  T& operator-=(const D& r)
167  {
168  *this = *this - r;
169  return *static_cast<T*>(this);
170  }
171 
172  static const unsigned StringBufSize = 32;
173  void toString(char buf[StringBufSize]) const;
174 #if UAVCAN_TOSTRING
175  std::string toString() const;
176 #endif
177 };
178 
179 /*
180  * Monotonic
181  */
182 class UAVCAN_EXPORT MonotonicDuration : public DurationBase<MonotonicDuration> { };
183 
184 class UAVCAN_EXPORT MonotonicTime : public TimeBase<MonotonicTime, MonotonicDuration> { };
185 
186 /*
187  * UTC
188  */
189 class UAVCAN_EXPORT UtcDuration : public DurationBase<UtcDuration> { };
190 
191 class UAVCAN_EXPORT UtcTime : public TimeBase<UtcTime, UtcDuration>
192 {
193 public:
194  UtcTime() { }
195 
196  UtcTime(const Timestamp& ts) // Implicit
197  {
198  operator=(ts);
199  }
200 
201  UtcTime& operator=(const Timestamp& ts)
202  {
203  *this = fromUSec(ts.usec);
204  return *this;
205  }
206 
207  operator Timestamp() const
208  {
209  Timestamp ts;
210  ts.usec = toUSec();
211  return ts;
212  }
213 };
214 
215 // ----------------------------------------------------------------------------
216 
217 template <typename D>
218 const unsigned DurationBase<D>::StringBufSize;
219 
220 template <typename T, typename D>
221 const unsigned TimeBase<T, D>::StringBufSize;
222 
223 template <typename D>
224 void DurationBase<D>::toString(char buf[StringBufSize]) const
225 {
226  char* ptr = buf;
227  if (isNegative())
228  {
229  *ptr++ = '-';
230  }
231  (void)snprintf(ptr, StringBufSize - 1, "%llu.%06lu",
232  static_cast<unsigned long long>(getAbs().toUSec() / 1000000L),
233  static_cast<unsigned long>(getAbs().toUSec() % 1000000L));
234 }
235 
236 
237 template <typename T, typename D>
238 void TimeBase<T, D>::toString(char buf[StringBufSize]) const
239 {
240  (void)snprintf(buf, StringBufSize, "%llu.%06lu",
241  static_cast<unsigned long long>(toUSec() / 1000000UL),
242  static_cast<unsigned long>(toUSec() % 1000000UL));
243 }
244 
245 
246 #if UAVCAN_TOSTRING
247 
248 template <typename D>
249 std::string DurationBase<D>::toString() const
250 {
251  char buf[StringBufSize];
252  toString(buf);
253  return std::string(buf);
254 }
255 
256 template <typename T, typename D>
257 std::string TimeBase<T, D>::toString() const
258 {
259  char buf[StringBufSize];
260  toString(buf);
261  return std::string(buf);
262 }
263 
264 #endif
265 
266 
267 template <typename Stream, typename D>
269 Stream& operator<<(Stream& s, const DurationBase<D>& d)
270 {
272  d.toString(buf);
273  s << buf;
274  return s;
275 }
276 
277 template <typename Stream, typename T, typename D>
279 Stream& operator<<(Stream& s, const TimeBase<T, D>& t)
280 {
282  t.toString(buf);
283  s << buf;
284  return s;
285 }
286 
287 }
288 
289 #endif // UAVCAN_TIME_HPP_INCLUDED
uavcan::TimeBase::operator>=
bool operator>=(const T &r) const
Definition: time.hpp:131
uavcan::DurationBase::usec_
int64_t usec_
Definition: time.hpp:21
uavcan::DurationBase::~DurationBase
~DurationBase()
Definition: time.hpp:24
uavcan::TimeBase::operator-
T operator-(const D &r) const
Definition: time.hpp:152
uavcan::DurationBase::toMSec
int64_t toMSec() const
Definition: time.hpp:44
uavcan::uint64_t
std::uint64_t uint64_t
Definition: std.hpp:27
templates.hpp
uavcan::DurationBase::operator*
D operator*(Scale scale) const
Definition: time.hpp:77
uavcan::TimeBase::fromMSec
static T fromMSec(uint64_t ms)
Definition: time.hpp:118
uavcan::DurationBase
Definition: time.hpp:19
uavcan::TimeBase::operator>
bool operator>(const T &r) const
Definition: time.hpp:129
uavcan::UtcTime::UtcTime
UtcTime()
Definition: time.hpp:194
uavcan::UtcTime
Implicitly convertible to/from uavcan.Timestamp.
Definition: time.hpp:191
uavcan::UtcDuration
Definition: time.hpp:189
uavcan::TimeBase::~TimeBase
~TimeBase()
Definition: time.hpp:100
uavcan::operator<<
OStream & operator<<(OStream &s, long long x)
Definition: ostream.hpp:40
uavcan::DurationBase::fromMSec
static D fromMSec(int64_t ms)
Definition: time.hpp:41
uavcan::DurationBase::StringBufSize
static const unsigned StringBufSize
Definition: time.hpp:86
uavcan::DurationBase::getAbs
D getAbs() const
Definition: time.hpp:46
uavcan::UtcTime::operator=
UtcTime & operator=(const Timestamp &ts)
Definition: time.hpp:201
uavcan::StaticAssert
struct UAVCAN_EXPORT StaticAssert
Definition: templates.hpp:29
uavcan::TimeBase::operator!=
bool operator!=(const T &r) const
Definition: time.hpp:126
uavcan::int64_t
std::int64_t int64_t
Definition: std.hpp:32
std.hpp
uavcan::MonotonicDuration
Definition: time.hpp:182
uavcan::DurationBase::operator*=
D & operator*=(Scale scale)
Definition: time.hpp:80
uavcan::TimeBase::operator==
bool operator==(const T &r) const
Definition: time.hpp:125
uavcan::TimeBase::TimeBase
TimeBase()
Definition: time.hpp:102
uavcan::DurationBase::operator+=
D & operator+=(const D &r)
Definition: time.hpp:65
uavcan::DurationBase::operator-
D operator-(const D &r) const
Definition: time.hpp:61
uavcan::TimeBase::fromUSec
static T fromUSec(uint64_t us)
Definition: time.hpp:112
uavcan::DurationBase::isNegative
bool isNegative() const
Definition: time.hpp:49
uavcan::snprintf
int snprintf(char *out, std::size_t maxlen, const char *format,...)
Definition: std.hpp:73
uavcan::DurationBase::operator<
bool operator<(const D &r) const
Definition: time.hpp:55
uavcan::TimeBase::getMax
static T getMax()
Definition: time.hpp:110
uavcan::DurationBase::DurationBase
DurationBase()
Definition: time.hpp:26
uavcan::max
const UAVCAN_EXPORT T & max(const T &a, const T &b)
Definition: templates.hpp:291
uavcan::DurationBase::operator!=
bool operator!=(const D &r) const
Definition: time.hpp:53
uavcan::DurationBase::isPositive
bool isPositive() const
Definition: time.hpp:48
d
d
toString
static std::string toString(long x)
Definition: multiset.cpp:16
UAVCAN_EXPORT
#define UAVCAN_EXPORT
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:108
uavcan::TimeBase::isZero
bool isZero() const
Definition: time.hpp:123
uavcan::TimeBase::toMSec
uint64_t toMSec() const
Definition: time.hpp:121
uavcan::DurationBase::operator<=
bool operator<=(const D &r) const
Definition: time.hpp:57
uavcan::DurationBase::operator+
D operator+(const D &r) const
Definition: time.hpp:60
build_config.hpp
uavcan::UtcTime::UtcTime
UtcTime(const Timestamp &ts)
Definition: time.hpp:196
uavcan::DurationBase::toString
void toString(char buf[StringBufSize]) const
Prints time in seconds with microsecond resolution.
Definition: time.hpp:224
uavcan::TimeBase::StringBufSize
static const unsigned StringBufSize
Definition: time.hpp:172
pyuavcan_v0.dsdl.signature.s
s
Definition: signature.py:73
uavcan::TimeBase::operator<
bool operator<(const T &r) const
Definition: time.hpp:128
uavcan::TimeBase::operator-
D operator-(const T &r) const
Definition: time.hpp:156
uavcan::DurationBase::operator-=
D & operator-=(const D &r)
Definition: time.hpp:70
uavcan::DurationBase::operator==
bool operator==(const D &r) const
Definition: time.hpp:52
uavcan::TimeBase::operator<=
bool operator<=(const T &r) const
Definition: time.hpp:130
uavcan::DurationBase::getInfinite
static D getInfinite()
Definition: time.hpp:33
uavcan::MonotonicTime
Definition: time.hpp:184
uavcan::DurationBase::operator>
bool operator>(const D &r) const
Definition: time.hpp:56
uavcan::TimeBase
Definition: time.hpp:95
uavcan
Definition: libuavcan/libuavcan/include/uavcan/build_config.hpp:204
uavcan::DurationBase::isZero
bool isZero() const
Definition: time.hpp:50
uavcan::TimeBase::usec_
uint64_t usec_
Definition: time.hpp:97
uavcan::TimeBase::toString
void toString(char buf[StringBufSize]) const
Prints time in seconds with microsecond resolution.
Definition: time.hpp:238
uavcan::DurationBase::operator-
D operator-() const
Definition: time.hpp:63
uavcan::TimeBase::operator+=
T & operator+=(const D &r)
Definition: time.hpp:161
uavcan::TimeBase::toUSec
uint64_t toUSec() const
Definition: time.hpp:120
uavcan::DurationBase::toUSec
int64_t toUSec() const
Definition: time.hpp:43
uavcan::DurationBase::fromUSec
static D fromUSec(int64_t us)
Definition: time.hpp:35
uavcan::TimeBase::operator+
T operator+(const D &r) const
Definition: time.hpp:133
uavcan::DurationBase::operator>=
bool operator>=(const D &r) const
Definition: time.hpp:58
uavcan::TimeBase::operator-=
T & operator-=(const D &r)
Definition: time.hpp:166


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:03