userver
C++ Async Framework
Toggle main menu visibility
Loading...
Searching...
No Matches
deadline.hpp
Go to the documentation of this file.
1
#pragma once
2
5
6
#include <chrono>
7
#include <type_traits>
8
9
USERVER_NAMESPACE_BEGIN
10
11
namespace
engine
{
12
14
class
Deadline
final {
15
public
:
16
using
Clock = std::chrono::steady_clock;
17
using
TimePoint = Clock::time_point;
18
using
Duration = TimePoint::duration;
19
21
constexpr
Deadline
() =
default
;
22
24
constexpr
bool
IsReachable
() const noexcept {
return
value_ != kUnreachable; }
25
27
bool
IsReached
() const noexcept;
28
31
bool
IsSurelyReachedApprox
() const noexcept;
32
34
Duration
TimeLeft
() const noexcept;
35
39
Duration
TimeLeftApprox
() const noexcept;
40
44
constexpr TimePoint
GetTimePoint
() const noexcept {
return
value_; }
45
47
template
<
typename
Rep,
typename
Period>
48
static
Deadline
FromDuration
(
const
std::chrono::duration<Rep, Period>& duration)
noexcept
{
49
return
FromDuration
(
ToDurationSaturating
(duration));
50
}
51
52
static
Deadline
FromDuration
(
const
Duration& duration)
noexcept
{
53
if
(duration < Duration::zero()) {
54
return
Deadline::Passed
();
55
}
56
return
Deadline
{SumWithSaturation(Clock::now(), duration)};
57
}
58
64
template
<
typename
Clock,
typename
Duration>
65
static
Deadline
FromTimePoint
(
const
std::chrono::time_point<Clock, Duration>& time_point)
noexcept
{
66
return
FromDuration
(time_point - Clock::now());
67
}
68
71
constexpr
static
Deadline
FromTimePoint
(
const
TimePoint& time_point)
noexcept
{
return
Deadline
(time_point); }
73
75
constexpr
static
Deadline
Passed
() noexcept {
return
Deadline
{kPassed}; }
76
77
constexpr
bool
operator==(
const
Deadline
& r)
const
noexcept
{
return
value_ == r.value_; }
78
79
constexpr
bool
operator<(
const
Deadline
& r)
const
noexcept
{
80
if
(!
IsReachable
()) {
81
return
false
;
82
}
83
if
(!r.IsReachable()) {
84
return
true
;
85
}
86
return
value_ < r.value_;
87
}
88
90
template
<
typename
Rep,
typename
Period>
91
static
Duration
ToDurationSaturating
(
const
std::chrono::duration<Rep, Period>& from)
noexcept
{
92
using
FromDuration
= std::chrono::duration<Rep, Period>;
93
94
// Require resolution of 'FromDuration' higher than 'Duration',
95
// to safely cast 'Duration::max' value to 'FromDuration'
96
static_assert
(std::is_constructible_v<Duration, FromDuration>);
97
98
if
(std::chrono::duration_cast<FromDuration>(Duration::max()) < from) {
99
return
Duration::max();
100
}
101
102
if
constexpr
(std::is_signed_v<Rep>) {
103
if
(from < std::chrono::duration_cast<FromDuration>(Duration::min())) {
104
return
Duration::min();
105
}
106
}
107
108
return
std::chrono::duration_cast<Duration>(from);
109
}
110
111
private
:
112
constexpr
explicit
Deadline
(TimePoint value) noexcept : value_(value) {}
113
114
constexpr
static
TimePoint SumWithSaturation(
const
TimePoint& time_point, Duration duration)
noexcept
{
115
return
TimePoint::max() - time_point < duration ? TimePoint::max() : time_point + duration;
116
}
117
118
static
constexpr
TimePoint kUnreachable = TimePoint::max();
119
static
constexpr
TimePoint kPassed = TimePoint::min();
120
121
TimePoint value_{kUnreachable};
122
};
123
124
}
// namespace engine
125
126
USERVER_NAMESPACE_END
userver
engine
deadline.hpp
Generated on
for userver by
Doxygen
1.17.0