aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorphunkyfish <phunkyfish@gmail.com>2020-10-04 22:06:18 +0100
committerphunkyfish <phunkyfish@gmail.com>2020-10-05 08:38:03 +0100
commita72506202681298487da8a90c9621b0091697304 (patch)
treeb67cdb548107be95a83e1ea78fb25291dfc0d4f7
parent6238042c32e88c135b74656e6942d5fb3aa319be (diff)
[addons] add CEndTime class to addon tools cpp
-rw-r--r--xbmc/addons/kodi-dev-kit/include/kodi/tools/CMakeLists.txt1
-rw-r--r--xbmc/addons/kodi-dev-kit/include/kodi/tools/EndTime.h215
-rw-r--r--xbmc/addons/kodi-dev-kit/include/kodi/versions.h2
3 files changed, 217 insertions, 1 deletions
diff --git a/xbmc/addons/kodi-dev-kit/include/kodi/tools/CMakeLists.txt b/xbmc/addons/kodi-dev-kit/include/kodi/tools/CMakeLists.txt
index 517ea9308e..16b83cb159 100644
--- a/xbmc/addons/kodi-dev-kit/include/kodi/tools/CMakeLists.txt
+++ b/xbmc/addons/kodi-dev-kit/include/kodi/tools/CMakeLists.txt
@@ -1,4 +1,5 @@
set(HEADERS DllHelper.h
+ EndTime.h
StringUtils.h
Thread.h
Timer.h)
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 0000000000..ac87cb9d86
--- /dev/null
+++ b/xbmc/addons/kodi-dev-kit/include/kodi/tools/EndTime.h
@@ -0,0 +1,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;
+
+ unsigned int timeWaitedAlready = duration_cast<milliseconds>(elapsed).count();
+
+ if (timeWaitedAlready >= m_totalWaitTime.count())
+ return 0;
+
+ return 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 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 */
diff --git a/xbmc/addons/kodi-dev-kit/include/kodi/versions.h b/xbmc/addons/kodi-dev-kit/include/kodi/versions.h
index 0908363884..9ffafab9c6 100644
--- a/xbmc/addons/kodi-dev-kit/include/kodi/versions.h
+++ b/xbmc/addons/kodi-dev-kit/include/kodi/versions.h
@@ -73,7 +73,7 @@
#define ADDON_GLOBAL_VERSION_NETWORK_DEPENDS "Network.h" \
"c-api/network.h"
-#define ADDON_GLOBAL_VERSION_TOOLS "1.0.2"
+#define ADDON_GLOBAL_VERSION_TOOLS "1.0.3"
#define ADDON_GLOBAL_VERSION_TOOLS_MIN "1.0.0"
#define ADDON_GLOBAL_VERSION_TOOLS_XML_ID "kodi.binary.global.tools"
#define ADDON_GLOBAL_VERSION_TOOLS_DEPENDS "tools/DllHelper.h" \