blob: 14983faaaf563330a80c2cbc53b4cd57d5d8feb1 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
|
/*
* Copyright (C) 2005-2020 Team Kodi
* https://kodi.tv
*
* SPDX-License-Identifier: GPL-2.0-or-later
* See LICENSE.md for more information.
*/
#pragma once
#ifdef __cplusplus
#include <chrono>
namespace kodi
{
namespace tools
{
//==============================================================================
/// @defgroup cpp_kodi_tools_CEndTime class CEndTime
/// @ingroup cpp_kodi_tools
/// @brief **Timeout check**\n
/// Class which makes it easy to check if a specified amount of time has passed.
///
/// This code uses the support of platform-independent chrono system introduced
/// with C++11.
///
///
/// ----------------------------------------------------------------------------
///
/// **Example:**
/// ~~~~~~~~~~~~~{.cpp}
/// #include <kodi/tools/EndTime.h>
///
/// class ATTRIBUTE_HIDDEN CExample
/// {
/// public:
/// CExample()
/// {
/// TimerCall();
/// }
///
/// void TimerCall()
/// {
/// fprintf(stderr, "Hello World\n");
/// CEndTime timer(1000);
///
/// while (timer.MillisLeft())
/// {
/// if (timer.IsTimePast())
/// {
/// fprintf(stderr, "We timed out!\n");
/// }
/// std::this_thread::sleep_for(std::chrono::milliseconds(10));
/// }
/// }
///
/// };
/// ~~~~~~~~~~~~~
///
///@{
class CEndTime
{
public:
//============================================================================
/// @ingroup cpp_kodi_tools_CEndTime
/// @brief Class constructor with no time to expiry set
///
inline CEndTime() = default;
//============================================================================
/// @ingroup cpp_kodi_tools_CEndTime
/// @brief Class constructor to set future time when timer has expired
///
/// @param[in] millisecondsIntoTheFuture the time in the future we cosider this timer as expired
///
inline explicit CEndTime(unsigned int millisecondsIntoTheFuture)
: m_startTime(std::chrono::system_clock::now().time_since_epoch()),
m_totalWaitTime(std::chrono::milliseconds(millisecondsIntoTheFuture))
{
}
//----------------------------------------------------------------------------
//============================================================================
/// @ingroup cpp_kodi_tools_CEndTime
/// @brief Set the time in the future we cosider this timer as expired
///
/// @param[in] millisecondsIntoTheFuture the time in the future we cosider this timer as expired
///
inline void Set(unsigned int millisecondsIntoTheFuture)
{
using namespace std::chrono;
m_startTime = system_clock::now().time_since_epoch();
m_totalWaitTime = milliseconds(millisecondsIntoTheFuture);
}
//----------------------------------------------------------------------------
//============================================================================
/// @ingroup cpp_kodi_tools_CEndTime
/// @brief Check if the expiry time has been reached
///
/// @return True if the expiry amount of time has past, false otherwise
///
inline bool IsTimePast() const
{
using namespace std::chrono;
// timer is infinite
if (m_totalWaitTime.count() == std::numeric_limits<unsigned int>::max())
return false;
if (m_totalWaitTime.count() == 0)
return true;
else
return (system_clock::now().time_since_epoch() - m_startTime) >= m_totalWaitTime;
}
//----------------------------------------------------------------------------
//============================================================================
/// @ingroup cpp_kodi_tools_CEndTime
/// @brief The amount of time left till this timer expires
///
/// @return 0 if the expiry amount of time has past, the numbe rof milliseconds remaining otherwise
///
inline unsigned int MillisLeft() const
{
using namespace std::chrono;
// timer is infinite
if (m_totalWaitTime.count() == std::numeric_limits<unsigned int>::max())
return std::numeric_limits<unsigned int>::max();
if (m_totalWaitTime.count() == 0)
return 0;
auto elapsed = system_clock::now().time_since_epoch() - m_startTime;
auto timeWaitedAlready = duration_cast<milliseconds>(elapsed).count();
if (timeWaitedAlready >= m_totalWaitTime.count())
return 0;
return static_cast<unsigned int>(m_totalWaitTime.count() - timeWaitedAlready);
}
//----------------------------------------------------------------------------
//============================================================================
/// @ingroup cpp_kodi_tools_CEndTime
/// @brief Consider this timer expired
///
inline void SetExpired()
{
using namespace std::chrono;
m_totalWaitTime = milliseconds(0);
}
//----------------------------------------------------------------------------
//============================================================================
/// @ingroup cpp_kodi_tools_CEndTime
/// @brief Set this timer as never expiring
///
inline void SetInfinite()
{
using namespace std::chrono;
m_totalWaitTime = milliseconds(std::numeric_limits<unsigned int>::max());
}
//----------------------------------------------------------------------------
//============================================================================
/// @ingroup cpp_kodi_tools_CEndTime
/// @brief Check if the timer has been set to infinite expiry
///
/// @return True if the expiry has been set as infinite, false otherwise
///
inline bool IsInfinite(void) const
{
return (m_totalWaitTime.count() == std::numeric_limits<unsigned int>::max());
}
//----------------------------------------------------------------------------
//============================================================================
/// @ingroup cpp_kodi_tools_CEndTime
/// @brief Get the initial timeout value this timer had
///
/// @return The initial expiry amount of time this timer had in milliseconds
///
inline unsigned int GetInitialTimeoutValue(void) const
{
auto value = std::chrono::duration_cast<std::chrono::milliseconds>(m_totalWaitTime);
return static_cast<unsigned int>(value.count());
}
//============================================================================
/// @ingroup cpp_kodi_tools_CEndTime
/// @brief Get the time this timer started
///
/// @return The time this timer started in milliseconds since epoch
///
inline uint64_t GetStartTime(void) const
{
auto value = std::chrono::duration_cast<std::chrono::milliseconds>(m_startTime);
return value.count();
}
//----------------------------------------------------------------------------
private:
std::chrono::system_clock::duration m_startTime;
std::chrono::system_clock::duration m_totalWaitTime;
};
} /* namespace tools */
} /* namespace kodi */
#endif /* __cplusplus */
|