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>
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>
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 {
271  char buf[DurationBase<D>::StringBufSize];
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 {
281  char buf[TimeBase<T, D>::StringBufSize];
282  t.toString(buf);
283  s << buf;
284  return s;
285 }
286 
287 }
288 
289 #endif // UAVCAN_TIME_HPP_INCLUDED
T operator-(const D &r) const
Definition: time.hpp:152
d
bool operator<=(const D &r) const
Definition: time.hpp:57
bool operator==(const T &r) const
Definition: time.hpp:125
bool operator>(const T &r) const
Definition: time.hpp:129
bool isZero() const
Definition: time.hpp:123
T operator+(const D &r) const
Definition: time.hpp:133
bool isNegative() const
Definition: time.hpp:49
bool isPositive() const
Definition: time.hpp:48
bool operator!=(const D &r) const
Definition: time.hpp:53
D operator+(const D &r) const
Definition: time.hpp:60
static T fromUSec(uint64_t us)
Definition: time.hpp:112
uint64_t toMSec() const
Definition: time.hpp:121
bool operator!=(const T &r) const
Definition: time.hpp:126
bool isZero() const
Definition: time.hpp:50
UtcTime(const Timestamp &ts)
Definition: time.hpp:196
uint64_t usec_
Definition: time.hpp:97
D & operator-=(const D &r)
Definition: time.hpp:70
uint64_t toUSec() const
Definition: time.hpp:120
bool operator<(const T &r) const
Definition: time.hpp:128
int64_t toUSec() const
Definition: time.hpp:43
static T getMax()
Definition: time.hpp:110
static T fromMSec(uint64_t ms)
Definition: time.hpp:118
void toString(char buf[StringBufSize]) const
Prints time in seconds with microsecond resolution.
Definition: time.hpp:224
UtcTime & operator=(const Timestamp &ts)
Definition: time.hpp:201
bool operator==(const D &r) const
Definition: time.hpp:52
Implicitly convertible to/from uavcan.Timestamp.
Definition: time.hpp:191
bool operator>(const D &r) const
Definition: time.hpp:56
static D getInfinite()
Definition: time.hpp:33
bool operator>=(const D &r) const
Definition: time.hpp:58
UAVCAN_EXPORT const T & max(const T &a, const T &b)
Definition: templates.hpp:291
void toString(char buf[StringBufSize]) const
Prints time in seconds with microsecond resolution.
Definition: time.hpp:238
D operator-(const T &r) const
Definition: time.hpp:156
D operator-() const
Definition: time.hpp:63
int64_t toMSec() const
Definition: time.hpp:44
bool operator>=(const T &r) const
Definition: time.hpp:131
T & operator-=(const D &r)
Definition: time.hpp:166
static D fromUSec(int64_t us)
Definition: time.hpp:35
std::uint64_t uint64_t
Definition: std.hpp:27
D & operator*=(Scale scale)
Definition: time.hpp:80
static const unsigned StringBufSize
Definition: time.hpp:86
D operator*(Scale scale) const
Definition: time.hpp:77
std::int64_t int64_t
Definition: std.hpp:32
D getAbs() const
Definition: time.hpp:46
static D fromMSec(int64_t ms)
Definition: time.hpp:41
D & operator+=(const D &r)
Definition: time.hpp:65
T & operator+=(const D &r)
Definition: time.hpp:161
bool operator<(const D &r) const
Definition: time.hpp:55
int snprintf(char *out, std::size_t maxlen, const char *format,...)
Definition: std.hpp:73
D operator-(const D &r) const
Definition: time.hpp:61
bool operator<=(const T &r) const
Definition: time.hpp:130


uavcan_communicator
Author(s):
autogenerated on Wed Jan 11 2023 03:59:40