mitteLib
Loading...
Searching...
No Matches
time.hpp
Go to the documentation of this file.
1//
2// Created by spak on 3/7/21.
3//
4
5#ifndef MLAB_TIME_HPP
6#define MLAB_TIME_HPP
7
8#include <chrono>
9
10namespace mlab {
11 using ms = std::chrono::milliseconds;
12 using namespace std::chrono_literals;
13
14 class timer {
15 std::chrono::time_point<std::chrono::high_resolution_clock> _timestamp;
16
17 public:
18 inline timer();
19
20 [[nodiscard]] inline ms elapsed() const;
21 };
22
26
27 public:
28 inline explicit reduce_timeout(ms timeout);
29
30 [[nodiscard]] inline ms remaining() const;
31
32 [[nodiscard]] inline ms elapsed() const;
33
34 inline explicit operator bool() const;
35 };
36
37}// namespace mlab
38
39namespace mlab {
40
41 reduce_timeout::reduce_timeout(ms timeout) : _timeout{timeout},
42 _timer{} {}
43
44 timer::timer() : _timestamp{std::chrono::high_resolution_clock::now()} {}
45
47 if (*this) {
48 return _timeout - _timer.elapsed();
49 }
50 return 0s;
51 }
52
54 return _timer.elapsed();
55 }
56
57 reduce_timeout::operator bool() const {
58 return _timer.elapsed() < _timeout;
59 }
60
62 const auto elapsed = std::chrono::high_resolution_clock::now() - _timestamp;
63 return std::chrono::duration_cast<ms>(elapsed);
64 }
65
66}// namespace mlab
67
68#endif//MLAB_TIME_HPP
Definition time.hpp:23
timer _timer
Definition time.hpp:25
ms _timeout
Definition time.hpp:24
ms elapsed() const
Definition time.hpp:53
ms remaining() const
Definition time.hpp:46
reduce_timeout(ms timeout)
Definition time.hpp:41
Definition time.hpp:14
std::chrono::time_point< std::chrono::high_resolution_clock > _timestamp
Definition time.hpp:15
ms elapsed() const
Definition time.hpp:61
timer()
Definition time.hpp:44
Definition log.cpp:8
std::chrono::milliseconds ms
Definition time.hpp:11