EtcPal  0.4.1
ETC Platform Abstraction Layer (EtcPal)
View other versions:
timer.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2022 ETC Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *******************************************************************************
16  * This file is a part of EtcPal. For more information, go to:
17  * https://github.com/ETCLabs/EtcPal
18  ******************************************************************************/
19 
22 
23 #ifndef ETCPAL_CPP_TIMER_H_
24 #define ETCPAL_CPP_TIMER_H_
25 
26 #include <algorithm>
27 #include <chrono>
28 #include <limits>
29 #include "etcpal/timer.h"
30 #include "etcpal/cpp/common.h"
31 
32 namespace etcpal
33 {
52 
102 {
103 public:
109  TimePoint() = default;
110  constexpr TimePoint(uint32_t ms);
111 
112  constexpr uint32_t value() const noexcept;
113 
114  ETCPAL_CONSTEXPR_14 TimePoint& operator+=(uint32_t duration) noexcept;
115  ETCPAL_CONSTEXPR_14 TimePoint& operator-=(uint32_t duration) noexcept;
116 
117  static TimePoint Now() noexcept;
118 
119 private:
120  uint32_t ms_{0};
121 };
122 
124 constexpr TimePoint::TimePoint(uint32_t ms) : ms_(ms)
125 {
126 }
127 
129 constexpr uint32_t TimePoint::value() const noexcept
130 {
131  return ms_;
132 }
133 
136 {
137  ms_ += duration;
138  return *this;
139 }
140 
143 {
144  ms_ -= duration;
145  return *this;
146 }
147 
149 inline TimePoint TimePoint::Now() noexcept
150 {
151  return etcpal_getms();
152 }
153 
174 class Timer
175 {
176 public:
178  Timer() = default;
179  // Note: this constructor is not explicit by design, to allow implicit conversions e.g.
180  // etcpal::Timer timer = etcpal_c_function_that_returns_timer();
181  constexpr Timer(const EtcPalTimer& c_timer) noexcept;
182  Timer& operator=(const EtcPalTimer& c_timer) noexcept;
183  explicit Timer(uint32_t interval) noexcept;
184  template <typename Rep, typename Period>
185  explicit Timer(const std::chrono::duration<Rep, Period>& interval) noexcept;
186 
187  constexpr const EtcPalTimer& get() const noexcept;
188  ETCPAL_CONSTEXPR_14 EtcPalTimer& get() noexcept;
189 
190  TimePoint GetStartTime() const noexcept;
191  uint32_t GetInterval() const noexcept;
192  uint32_t GetElapsed() const noexcept;
193  uint32_t GetRemaining() const noexcept;
194  bool IsExpired() const noexcept;
195 
196  void Start(uint32_t interval) noexcept;
197  template <typename Rep, typename Period>
198  void Start(const std::chrono::duration<Rep, Period>& interval) noexcept;
199  void Reset() noexcept;
200 
201 private:
202  EtcPalTimer timer_{};
203 };
204 
206 constexpr Timer::Timer(const EtcPalTimer& c_timer) noexcept : timer_(c_timer)
207 {
208 }
209 
211 inline Timer& Timer::operator=(const EtcPalTimer& c_timer) noexcept
212 {
213  timer_ = c_timer;
214  return *this;
215 }
216 
218 inline Timer::Timer(uint32_t interval) noexcept
219 {
220  Start(interval);
221 }
222 
226 template <typename Rep, typename Period>
227 Timer::Timer(const std::chrono::duration<Rep, Period>& interval) noexcept
228 {
229  Start(interval);
230 }
231 
233 constexpr const EtcPalTimer& Timer::get() const noexcept
234 {
235  return timer_;
236 }
237 
240 {
241  return timer_;
242 }
243 
245 inline TimePoint Timer::GetStartTime() const noexcept
246 {
247  return timer_.reset_time;
248 }
249 
251 inline uint32_t Timer::GetInterval() const noexcept
252 {
253  return timer_.interval;
254 }
255 
257 inline uint32_t Timer::GetElapsed() const noexcept
258 {
259  return etcpal_timer_elapsed(&timer_);
260 }
261 
263 inline uint32_t Timer::GetRemaining() const noexcept
264 {
265  return etcpal_timer_remaining(&timer_);
266 }
267 
269 inline bool Timer::IsExpired() const noexcept
270 {
271  return etcpal_timer_is_expired(&timer_);
272 }
273 
277 inline void Timer::Start(uint32_t interval) noexcept
278 {
279  etcpal_timer_start(&timer_, interval);
280 }
281 
285 template <typename Rep, typename Period>
286 void Timer::Start(const std::chrono::duration<Rep, Period>& interval) noexcept
287 {
288  uint32_t interval_clamped = static_cast<uint32_t>(
289  std::min(std::chrono::milliseconds(interval).count(),
290  static_cast<std::chrono::milliseconds::rep>(std::numeric_limits<uint32_t>::max())));
291  etcpal_timer_start(&timer_, interval_clamped);
292 }
293 
295 inline void Timer::Reset() noexcept
296 {
297  etcpal_timer_reset(&timer_);
298 }
299 
302 
306 constexpr int32_t operator-(const TimePoint& a, const TimePoint& b) noexcept
307 {
308  return static_cast<int32_t>(a.value() - b.value());
309 }
310 
311 constexpr bool operator==(const TimePoint& a, const TimePoint& b) noexcept
312 {
313  return (a.value() == b.value());
314 }
315 
316 constexpr bool operator!=(const TimePoint& a, const TimePoint& b) noexcept
317 {
318  return !(a == b);
319 }
320 
321 constexpr bool operator<(const TimePoint& a, const TimePoint& b) noexcept
322 {
323  return (a - b) < 0;
324 }
325 
326 constexpr bool operator>(const TimePoint& a, const TimePoint& b) noexcept
327 {
328  return b < a;
329 }
330 
331 constexpr bool operator<=(const TimePoint& a, const TimePoint& b) noexcept
332 {
333  return !(b < a);
334 }
335 
336 constexpr bool operator>=(const TimePoint& a, const TimePoint& b) noexcept
337 {
338  return !(a < b);
339 }
340 
343 
344 }; // namespace etcpal
345 
346 #endif // ETCPAL_CPP_TIMER_H_
Represents a point in time.
Definition: timer.h:102
ETCPAL_CONSTEXPR_14 TimePoint & operator-=(uint32_t duration) noexcept
Subtract a millisecond duration from a TimePoint.
Definition: timer.h:142
constexpr uint32_t value() const noexcept
Get the raw millisecond value from a TimePoint.
Definition: timer.h:129
ETCPAL_CONSTEXPR_14 TimePoint & operator+=(uint32_t duration) noexcept
Add a millisecond duration to a TimePoint.
Definition: timer.h:135
TimePoint()=default
Construct a TimePoint with a value of 0 by default.
static TimePoint Now() noexcept
Get a TimePoint representing the current time.
Definition: timer.h:149
A wrapper class for the EtcPal timer type.
Definition: timer.h:175
Timer()=default
Creates an expired timer with an interval of 0.
TimePoint GetStartTime() const noexcept
Get the time when this timer was started or reset.
Definition: timer.h:245
bool IsExpired() const noexcept
Whether the timer's interval is expired.
Definition: timer.h:269
Timer & operator=(const EtcPalTimer &c_timer) noexcept
Assign an instance of the C EtcPalTimer type to an instance of this class.
Definition: timer.h:211
void Reset() noexcept
Reset the timer while keeping the same interval.
Definition: timer.h:295
constexpr const EtcPalTimer & get() const noexcept
Get a const reference to the underlying C type.
Definition: timer.h:233
uint32_t GetElapsed() const noexcept
Get the time since the timer was reset.
Definition: timer.h:257
uint32_t GetInterval() const noexcept
Get the current interval being timed by the timer.
Definition: timer.h:251
void Start(uint32_t interval) noexcept
Start the timer with a new interval.
Definition: timer.h:277
uint32_t GetRemaining() const noexcept
Get the amount of time remaining in the timer's interval (returns 0 if the timer is expired).
Definition: timer.h:263
Common definitions used by EtcPal C++ wrappers.
#define ETCPAL_CONSTEXPR_14
Stand-in for "constexpr" on entities that can only be defined "constexpr" in C++14 or later.
Definition: common.h:53
#define ETCPAL_CONSTEXPR_14_OR_INLINE
Defined to "constexpr" in C++14 or later, "inline" earlier.
Definition: common.h:54
uint32_t etcpal_timer_elapsed(const EtcPalTimer *timer)
Get the time since a timer was reset.
Definition: timer.c:56
void etcpal_timer_start(EtcPalTimer *timer, uint32_t interval)
Start a timer.
Definition: timer.c:30
uint32_t etcpal_timer_remaining(const EtcPalTimer *timer)
Get the amount of time remaining in a timer.
Definition: timer.c:87
bool etcpal_timer_is_expired(const EtcPalTimer *timer)
Check to see if a timer is expired.
Definition: timer.c:73
uint32_t etcpal_getms(void)
Get a monotonically-increasing millisecond value.
void etcpal_timer_reset(EtcPalTimer *timer)
Reset a timer while keeping the same interval.
Definition: timer.c:43
A millisecond-resolution timer.
Definition: timer.h:102
uint32_t reset_time
The time at which this timer was reset.
Definition: timer.h:103
uint32_t interval
This timer's timeout interval.
Definition: timer.h:104