xbmc
SystemClock.h
1 /*
2  * Copyright (C) 2005-2018 Team Kodi
3  * This file is part of Kodi - https://kodi.tv
4  *
5  * SPDX-License-Identifier: GPL-2.0-or-later
6  * See LICENSES/README.md for more information.
7  */
8 
9 #pragma once
10 
11 #include "utils/log.h"
12 
13 #include <chrono>
14 #include <limits>
15 #include <thread>
16 
17 namespace XbmcThreads
18 {
19 
20 template<typename>
21 struct is_chrono_duration : std::false_type
22 {
23 };
24 
25 template<typename Rep, typename Period>
26 struct is_chrono_duration<std::chrono::duration<Rep, Period>> : std::true_type
27 {
28 };
29 
30 template<typename T = std::chrono::milliseconds, bool = is_chrono_duration<T>::value>
31 class EndTime;
32 
33 template<typename T>
34 class EndTime<T, true>
35 {
36 public:
37  explicit EndTime(const T duration) { Set(duration); }
38 
39  EndTime() = default;
40  EndTime(const EndTime& right) = delete;
41  ~EndTime() = default;
42 
43  static constexpr T Max() { return m_max; }
44 
45  void Set(const T duration)
46  {
47  m_startTime = std::chrono::steady_clock::now();
48 
49  if (duration > m_max)
50  {
51  m_totalWaitTime = m_max;
52  CLog::Log(LOGWARNING, "duration ({}) greater than max ({}) - duration will be truncated!",
53  duration.count(), m_max.count());
54  }
55  else
56  {
57  m_totalWaitTime = duration;
58  }
59  }
60 
61  bool IsTimePast() const
62  {
63  const auto now = std::chrono::steady_clock::now();
64 
65  return ((now - m_startTime) >= m_totalWaitTime);
66  }
67 
68  T GetTimeLeft() const
69  {
70  const auto now = std::chrono::steady_clock::now();
71 
72  const auto left = ((m_startTime + m_totalWaitTime) - now);
73 
74  if (left < T::zero())
75  return T::zero();
76 
77  return std::chrono::duration_cast<T>(left);
78  }
79 
80  void SetExpired() { m_totalWaitTime = T::zero(); }
81 
82  void SetInfinite() { m_totalWaitTime = m_max; }
83 
84  T GetInitialTimeoutValue() const { return m_totalWaitTime; }
85 
86  std::chrono::steady_clock::time_point GetStartTime() const { return m_startTime; }
87 
88 private:
89  std::chrono::steady_clock::time_point m_startTime;
90  T m_totalWaitTime = T::zero();
91 
92  static constexpr T m_max =
93  std::chrono::duration_cast<T>(std::chrono::steady_clock::duration::max());
94 };
95 
96 } // namespace XbmcThreads
Definition: SystemClock.h:31
Definition: SystemClock.h:21
Definition: RecursiveMutex.cpp:11