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