chrono.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2015 Hamburg University of Applied Sciences (HAW)
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
25 #ifndef RIOT_CHRONO_HPP
26 #define RIOT_CHRONO_HPP
27 
28 #include <chrono>
29 #include <algorithm>
30 
31 #include "time.h"
32 #include "xtimer.h"
33 
34 namespace riot {
35 
36 namespace {
37 constexpr uint32_t microsecs_in_sec = 1000000;
38 } // namespace anaonymous
39 
44 class time_point {
46 
47  public:
51  inline time_point() : m_handle{0, 0} {}
55  explicit inline time_point(timex_t&& tp) : m_handle(tp) {}
59  constexpr time_point(const time_point& tp) = default;
63  constexpr time_point(time_point&& tp) = default;
64 
68  inline native_handle_type native_handle() const { return m_handle; }
69 
73  template <class Rep, class Period>
74  inline time_point& operator+=(const std::chrono::duration<Rep, Period>& d) {
75  auto s = std::chrono::duration_cast<std::chrono::seconds>(d);
76  auto m = (std::chrono::duration_cast<std::chrono::microseconds>(d) - s);
77  m_handle.seconds += s.count();
78  m_handle.microseconds += m.count();
79  adjust_overhead();
80  return *this;
81  }
82 
86  inline uint32_t seconds() const { return m_handle.seconds; }
87 
91  inline uint32_t microseconds() const { return m_handle.microseconds; }
92 
93  private:
94  timex_t m_handle;
95  void inline adjust_overhead() {
96  auto secs = m_handle.microseconds / microsecs_in_sec;
97  m_handle.seconds += secs;
98  m_handle.microseconds -= (secs * microsecs_in_sec);
99  }
100 };
101 
107 inline time_point now() {
108  timex_t tp;
109  xtimer_now_timex(&tp);
110  return time_point(std::move(tp));
111 }
112 
116 inline bool operator<(const time_point& lhs, const time_point& rhs) {
117  return lhs.seconds() < rhs.seconds()
118  || (lhs.seconds() == rhs.seconds() && lhs.microseconds()
119  < rhs.microseconds());
120 }
121 
125 inline bool operator>(const time_point& lhs, const time_point& rhs) {
126  return rhs < lhs;
127 }
128 
132 inline bool operator<=(const time_point& lhs, const time_point& rhs) {
133  return !(rhs < lhs);
134 }
135 
139 inline bool operator>=(const time_point& lhs, const time_point& rhs) {
140  return !(lhs < rhs);
141 }
142 
143 } // namespace riot
144 
145 #endif // RIOT_CHRONO_HPP
time_point(timex_t &&tp)
Create time point from timex_t struct.
Definition: chrono.hpp:55
Definition: chrono.hpp:34
A timex timestamp.
Definition: timex.h:79
time_point()
Creates a time point with seconds and microseconds set to 0.
Definition: chrono.hpp:51
bool operator>=(const time_point &lhs, const time_point &rhs)
Compare two timepoints.
Definition: chrono.hpp:139
bool operator<=(const time_point &lhs, const time_point &rhs)
Compares two timepoints.
Definition: chrono.hpp:132
time_point now()
Returns the current time saved in a time point.
Definition: chrono.hpp:107
void xtimer_now_timex(timex_t *out)
get the current system time into a timex_t
native_handle_type native_handle() const
Gives access to the native handle that stores the time information.
Definition: chrono.hpp:68
uint32_t seconds() const
Returns seconds member as uint32_t.
Definition: chrono.hpp:86
time_point & operator+=(const std::chrono::duration< Rep, Period > &d)
Add a standard chrono::duration to this time point.
Definition: chrono.hpp:74
A time point for timed wait, as clocks from the standard are not available on RIOT.
Definition: chrono.hpp:44
xtimer interface definitions
bool operator<(const time_point &lhs, const time_point &rhs)
Compares two timepoints.
Definition: chrono.hpp:116
uint32_t microseconds() const
Returns microseconds member as uint32_t.
Definition: chrono.hpp:91
uint32_t microseconds
number of microseconds
Definition: timex.h:81
uint32_t seconds
number of seconds
Definition: timex.h:80
bool operator>(const time_point &lhs, const time_point &rhs)
Compares two timepoints.
Definition: chrono.hpp:125