mitteLib
Toggle main menu visibility
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
10
namespace
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
23
class
reduce_timeout
{
24
ms
_timeout
;
25
timer
_timer
;
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
39
namespace
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
46
ms
reduce_timeout::remaining
()
const
{
47
if
(*
this
) {
48
return
_timeout
-
_timer
.elapsed();
49
}
50
return
0s;
51
}
52
53
ms
reduce_timeout::elapsed
()
const
{
54
return
_timer
.elapsed();
55
}
56
57
reduce_timeout::operator bool()
const
{
58
return
_timer
.elapsed() <
_timeout
;
59
}
60
61
ms
timer::elapsed
()
const
{
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
mlab::reduce_timeout::_timer
timer _timer
Definition
time.hpp:25
mlab::reduce_timeout::_timeout
ms _timeout
Definition
time.hpp:24
mlab::reduce_timeout::elapsed
ms elapsed() const
Definition
time.hpp:53
mlab::reduce_timeout::remaining
ms remaining() const
Definition
time.hpp:46
mlab::reduce_timeout::reduce_timeout
reduce_timeout(ms timeout)
Definition
time.hpp:41
mlab::timer
Definition
time.hpp:14
mlab::timer::_timestamp
std::chrono::time_point< std::chrono::high_resolution_clock > _timestamp
Definition
time.hpp:15
mlab::timer::elapsed
ms elapsed() const
Definition
time.hpp:61
mlab::timer::timer
timer()
Definition
time.hpp:44
mlab
Definition
log.cpp:10
mlab::ms
std::chrono::milliseconds ms
Definition
time.hpp:11
mittelib
include
mlab
time.hpp
Generated by
1.17.0