TGUI  0.9-dev
Duration.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2020 Bruno Van de Velde (vdv_b@tgui.eu)
5 //
6 // This software is provided 'as-is', without any express or implied warranty.
7 // In no event will the authors be held liable for any damages arising from the use of this software.
8 //
9 // Permission is granted to anyone to use this software for any purpose,
10 // including commercial applications, and to alter it and redistribute it freely,
11 // subject to the following restrictions:
12 //
13 // 1. The origin of this software must not be misrepresented;
14 // you must not claim that you wrote the original software.
15 // If you use this software in a product, an acknowledgment
16 // in the product documentation would be appreciated but is not required.
17 //
18 // 2. Altered source versions must be plainly marked as such,
19 // and must not be misrepresented as being the original software.
20 //
21 // 3. This notice may not be removed or altered from any source distribution.
22 //
24 
25 
26 #ifndef TGUI_DURATION_HPP
27 #define TGUI_DURATION_HPP
28 
29 #include <TGUI/Config.hpp>
30 #include <cstdint>
31 #include <chrono>
32 
33 #include <SFML/System/Time.hpp>
34 
36 
37 namespace tgui
38 {
48  class Duration
49  {
50  public:
51 
55  TGUI_CONSTEXPR Duration() :
56  m_duration{std::chrono::nanoseconds::zero()}
57  {
58  }
59 
60 
64  template <typename Rep, typename Period>
65  TGUI_CONSTEXPR Duration(std::chrono::duration<Rep, Period> duration) :
66  m_duration{std::chrono::duration_cast<std::chrono::nanoseconds>(duration)}
67  {
68  }
69 
70 
74  Duration(sf::Time duration) :
75  m_duration{std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::microseconds(duration.asMicroseconds()))}
76  {
77  }
78 
79 
83  TGUI_CONSTEXPR float asSeconds() const
84  {
85  return m_duration.count() / 1000000000.f;
86  }
87 
88 
92  TGUI_CONSTEXPR operator std::chrono::nanoseconds() const
93  {
94  return m_duration;
95  }
96 
97 
101  template <typename Rep, typename Period>
102  TGUI_CONSTEXPR operator std::chrono::duration<Rep, Period>() const
103  {
104  return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(m_duration);
105  }
106 
107 
111  operator sf::Time() const
112  {
113  return sf::microseconds(m_duration.count() / 1000);
114  }
115 
116 
118  private:
119 
120  std::chrono::nanoseconds m_duration;
121  };
122 
124 
125  TGUI_CONSTEXPR bool operator==(const Duration& lhs, const Duration& rhs)
126  {
127  return std::chrono::nanoseconds(lhs) == std::chrono::nanoseconds(rhs);
128  }
129 
130  TGUI_CONSTEXPR bool operator!=(const Duration& lhs, const Duration& rhs)
131  {
132  return std::chrono::nanoseconds(lhs) != std::chrono::nanoseconds(rhs);
133  }
134 
135  TGUI_CONSTEXPR bool operator>(const Duration& lhs, const Duration& rhs)
136  {
137  return std::chrono::nanoseconds(lhs) > std::chrono::nanoseconds(rhs);
138  }
139 
140  TGUI_CONSTEXPR bool operator>=(const Duration& lhs, const Duration& rhs)
141  {
142  return std::chrono::nanoseconds(lhs) >= std::chrono::nanoseconds(rhs);
143  }
144 
145  TGUI_CONSTEXPR bool operator<(const Duration& lhs, const Duration& rhs)
146  {
147  return std::chrono::nanoseconds(lhs) < std::chrono::nanoseconds(rhs);
148  }
149 
150  TGUI_CONSTEXPR bool operator<=(const Duration& lhs, const Duration& rhs)
151  {
152  return std::chrono::nanoseconds(lhs) <= std::chrono::nanoseconds(rhs);
153  }
154 
156 
157  TGUI_CONSTEXPR Duration operator+(const Duration& lhs, const Duration& rhs)
158  {
159  return {std::chrono::nanoseconds(lhs) + std::chrono::nanoseconds(rhs)};
160  }
161 
162  TGUI_CONSTEXPR Duration operator-(const Duration& lhs, const Duration& rhs)
163  {
164  return {std::chrono::nanoseconds(lhs) - std::chrono::nanoseconds(rhs)};
165  }
166 
167  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
168  TGUI_CONSTEXPR Duration operator*(const Duration& lhs, T rhs)
169  {
170  return {std::chrono::nanoseconds(lhs) * rhs};
171  }
172 
173  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
174  TGUI_CONSTEXPR Duration operator*(T lhs, const Duration& rhs)
175  {
176  return {lhs * std::chrono::nanoseconds(rhs)};
177  }
178 
179  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
180  TGUI_CONSTEXPR Duration operator/(const Duration& lhs, T rhs)
181  {
182  return {std::chrono::nanoseconds(lhs) / rhs};
183  }
184 
185  TGUI_CONSTEXPR float operator/(const Duration& lhs, const Duration& rhs)
186  {
187  return lhs.asSeconds() / rhs.asSeconds();
188  }
189 
190  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
191  TGUI_CONSTEXPR Duration operator%(const Duration& lhs, T rhs)
192  {
193  return {std::chrono::nanoseconds(lhs) % rhs};
194  }
195 
196  TGUI_CONSTEXPR Duration operator%(const Duration& lhs, const Duration& rhs)
197  {
198  return {std::chrono::nanoseconds(lhs) % std::chrono::nanoseconds(rhs)};
199  }
200 
202 
203  TGUI_CONSTEXPR Duration& operator+=(Duration& lhs, const Duration& rhs)
204  {
205  return lhs = lhs + rhs;
206  }
207 
208  TGUI_CONSTEXPR Duration& operator-=(Duration& lhs, const Duration& rhs)
209  {
210 
211  return lhs = lhs - rhs;
212  }
213 
214  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
215  TGUI_CONSTEXPR Duration& operator*=(Duration& lhs, T rhs)
216  {
217  return lhs = lhs * rhs;
218  }
219 
220  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
221  TGUI_CONSTEXPR Duration& operator/=(Duration& lhs, T rhs)
222  {
223  return lhs = lhs / rhs;
224  }
225 
226  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
227  TGUI_CONSTEXPR Duration& operator%=(Duration& lhs, T rhs)
228  {
229  return lhs = lhs % rhs;
230  }
231 
232  TGUI_CONSTEXPR Duration& operator%=(Duration& lhs, const Duration& rhs)
233  {
234  return lhs = lhs % rhs;
235  }
236 
238 }
239 
241 
242 #endif // TGUI_DURATION_HPP
243 
244 
TGUI_CONSTEXPR Duration()
Creates an zero-length duration.
Definition: Duration.hpp:55
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
TGUI_CONSTEXPR float asSeconds() const
Returns the duration in seconds.
Definition: Duration.hpp:83
Wrapper for durations.
Definition: Duration.hpp:48
Duration(sf::Time duration)
Creates the duration from an sf::Time instance.
Definition: Duration.hpp:74
TGUI_CONSTEXPR Duration(std::chrono::duration< Rep, Period > duration)
Creates the duration from any kind of std::chrono::duration.
Definition: Duration.hpp:65