Embedded Multicore Building Blocks V1.0.0
duration.h
1 /*
2  * Copyright (c) 2014-2017, Siemens AG. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
18  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  * POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #ifndef EMBB_BASE_DURATION_H_
28 #define EMBB_BASE_DURATION_H_
29 
30 #include <embb/base/c/duration.h>
31 #include <embb/base/exceptions.h>
32 #include <cassert>
33 
34 namespace embb {
35 namespace base {
36 
56 template<typename Tick>
57 class Duration {
58  public:
63  static const Duration<Tick>& Zero();
64 
72  static const Duration<Tick>& Max();
73 
81  static const Duration<Tick>& Min();
82 
86  Duration();
87 
91  explicit Duration(
92  unsigned long long ticks
94  );
95 
99  Duration(
100  const Duration<Tick>& to_copy
102  );
103 
110  const Duration<Tick>& to_assign
112  );
113 
118  unsigned long long Count() const;
119 
126  const Duration<Tick>& rhs
128  );
129 
130  private:
137  Duration(
138  const embb_duration_t& duration
140  );
141 
145  embb_duration_t rep_;
146 
150  friend class Time;
151 
155  friend class ConditionVariable;
156 };
157 
165 template<typename Tick>
167  const Duration<Tick>& lhs,
169  const Duration<Tick>& rhs
171  ) {
172  return embb_duration_compare(&lhs, &rhs) == 0;
173 }
174 
182 template<typename Tick>
184  const Duration<Tick>& lhs,
186  const Duration<Tick>& rhs
188  ) {
189  return embb_duration_compare(&lhs, &rhs) != 0;
190 }
191 
199 template<typename Tick>
201  const Duration<Tick>& lhs,
203  const Duration<Tick>& rhs
205  ) {
206  return embb_duration_compare(&lhs, &rhs) == -1;
207 }
208 
216 template<typename Tick>
218  const Duration<Tick>& lhs,
220  const Duration<Tick>& rhs
222  ) {
223  return embb_duration_compare(&lhs, &rhs) == 1;
224 }
225 
233 template<typename Tick>
235  const Duration<Tick>& lhs,
237  const Duration<Tick>& rhs
239  ) {
240  return embb_duration_compare(&lhs, &rhs) < 1;
241 }
242 
250 template<typename Tick>
252  const Duration<Tick>& lhs,
254  const Duration<Tick>& rhs
256  ) {
257  return embb_duration_compare(&lhs, &rhs) > -1;
258 }
259 
267 template<typename Tick>
269  const Duration<Tick>& lhs,
271  const Duration<Tick>& rhs
273  ) {
274  return Duration<Tick>(lhs.Count() + rhs.Count());
275 }
276 
277 namespace internal {
278 
282 class Tick {
283  public:
288  static void CheckExceptions(
289  int status,
291  const char* msg
293  );
294 };
295 
302 class Seconds : public Tick {
303  public:
309  static int Set(
310  embb_duration_t& duration,
312  unsigned long long ticks
314  );
315 
319  static void SetAndCheck(
320  embb_duration_t& duration,
322  unsigned long long ticks
324  );
325 
331  static unsigned long long Get(
332  const embb_duration_t& duration
334  );
335 
341  static unsigned long long Min();
342 
348  static unsigned long long Max();
349 };
350 
357 class Milliseconds : public Tick {
358  public:
365  static int Set(
366  embb_duration_t& duration,
368  unsigned long long ticks
370  );
371 
375  static void SetAndCheck(
376  embb_duration_t& duration,
378  unsigned long long ticks
380  );
381 
387  static unsigned long long Get(
388  const embb_duration_t& duration
390  );
391 
398  static unsigned long long Min();
399 
406  static unsigned long long Max();
407 };
408 
415 class Microseconds : public Tick {
416  public:
423  static int Set(
424  embb_duration_t& duration,
426  unsigned long long ticks
428  );
429 
433  static void SetAndCheck(
434  embb_duration_t& duration,
436  unsigned long long ticks
438  );
439 
445  static unsigned long long Get(
446  const embb_duration_t& duration
448  );
449 
456  static unsigned long long Min();
457 
464  static unsigned long long Max();
465 };
466 
473 class Nanoseconds : public Tick {
474  public:
481  static int Set(
482  embb_duration_t& duration,
484  unsigned long long ticks
486  );
487 
491  static void SetAndCheck(
492  embb_duration_t& duration,
494  unsigned long long ticks
496  );
497 
503  static unsigned long long Get(
504  const embb_duration_t& duration
506  );
507 
514  static unsigned long long Min();
515 
522  static unsigned long long Max();
523 };
524 
525 } // namespace internal
526 
551 
552 } // namespace base
553 } // namespace embb
554 
555 #include <embb/base/internal/duration-inl.h>
556 
557 #endif // EMBB_BASE_DURATION_H_
Definition: lock_free_mpmc_queue.h:40
Duration()
Constructs a duration of length zero.
Duration< Tick > & operator+=(const Duration< Tick > &rhs)
Assignment by addition of another duration with same tick type.
bool operator<(const Duration< Tick > &lhs, const Duration< Tick > &rhs)
Compares two durations (less than)
Definition: duration.h:200
Duration< internal::Microseconds > DurationMicroseconds
Duration with microseconds tick.
Definition: duration.h:544
int embb_duration_compare(const embb_duration_t *lhs, const embb_duration_t *rhs)
Compares two durations.
Duration< internal::Seconds > DurationSeconds
Duration with seconds tick.
Definition: duration.h:532
static const Duration< Tick > & Min()
Returns duration with minimum ticks representable by implementation.
static const Duration< Tick > & Zero()
Returns duration of length zero.
static const Duration< Tick > & Max()
Returns duration with maximum ticks representable by implementation.
Represents a relative time duration for a given tick type.
Definition: duration.h:57
unsigned long long Count() const
Returns the number of ticks of the duration.
Duration< internal::Milliseconds > DurationMilliseconds
Duration with milliseconds tick.
Definition: duration.h:538
bool operator!=(const Duration< Tick > &lhs, const Duration< Tick > &rhs)
Compares two durations (inequality).
Definition: duration.h:183
Represents a condition variable for thread synchronization.
Definition: condition_variable.h:56
Duration< Tick > operator+(const Duration< Tick > &lhs, const Duration< Tick > &rhs)
Adds two durations.
Definition: duration.h:268
bool operator>=(const Duration< Tick > &lhs, const Duration< Tick > &rhs)
Compares two durations (greater than or equal to)
Definition: duration.h:251
bool operator>(const Duration< Tick > &lhs, const Duration< Tick > &rhs)
Compares two durations (greater than)
Definition: duration.h:217
opaque_type embb_duration_t
Opaque type representing a relative time duration.
Definition: duration.h:52
Duration< internal::Nanoseconds > DurationNanoseconds
Duration with nanoseconds tick.
Definition: duration.h:550
bool operator<=(const Duration< Tick > &lhs, const Duration< Tick > &rhs)
Compares two durations (less than or equal to)
Definition: duration.h:234
Duration< Tick > & operator=(const Duration< Tick > &to_assign)
Assigns an existing duration.
Represents an absolute time point.
Definition: time.h:41
bool operator==(const Duration< Tick > &lhs, const Duration< Tick > &rhs)
Compares two durations (equality).
Definition: duration.h:166