TGUI  0.10-beta
Duration.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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#if TGUI_HAS_WINDOW_BACKEND_SFML
34 #include <SFML/System/Time.hpp>
35#endif
36
38
39namespace tgui
40{
52 {
53 public:
54
58 TGUI_CONSTEXPR Duration() :
59 m_duration{std::chrono::nanoseconds::zero()}
60 {
61 }
62
63
67 template <typename Rep, typename Period>
68 TGUI_CONSTEXPR Duration(std::chrono::duration<Rep, Period> duration) :
69 m_duration{std::chrono::duration_cast<std::chrono::nanoseconds>(duration)}
70 {
71 }
72
73
78 TGUI_CONSTEXPR Duration(int milliseconds) :
79 Duration{std::chrono::milliseconds(milliseconds)}
80 {
81 }
82
83
84#if TGUI_HAS_WINDOW_BACKEND_SFML
88 Duration(sf::Time duration) :
89 m_duration{std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::microseconds(duration.asMicroseconds()))}
90 {
91 }
92#endif
93
97 TGUI_CONSTEXPR float asSeconds() const
98 {
99 return static_cast<float>(static_cast<double>(m_duration.count()) / 1000000000.0);
100 }
101
102
106 TGUI_CONSTEXPR operator std::chrono::nanoseconds() const
107 {
108 return m_duration;
109 }
110
111
115 template <typename Rep, typename Period>
116 TGUI_CONSTEXPR operator std::chrono::duration<Rep, Period>() const
117 {
118 return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(m_duration);
119 }
120
121#if TGUI_HAS_WINDOW_BACKEND_SFML
125 operator sf::Time() const
126 {
127 return sf::microseconds(m_duration.count() / 1000);
128 }
129#endif
130
132 private:
133
134 std::chrono::nanoseconds m_duration;
135 };
136
138
139 TGUI_CONSTEXPR bool operator==(const Duration& lhs, const Duration& rhs)
140 {
141 return std::chrono::nanoseconds(lhs) == std::chrono::nanoseconds(rhs);
142 }
143
144 TGUI_CONSTEXPR bool operator!=(const Duration& lhs, const Duration& rhs)
145 {
146 return std::chrono::nanoseconds(lhs) != std::chrono::nanoseconds(rhs);
147 }
148
149 TGUI_CONSTEXPR bool operator>(const Duration& lhs, const Duration& rhs)
150 {
151 return std::chrono::nanoseconds(lhs) > std::chrono::nanoseconds(rhs);
152 }
153
154 TGUI_CONSTEXPR bool operator>=(const Duration& lhs, const Duration& rhs)
155 {
156 return std::chrono::nanoseconds(lhs) >= std::chrono::nanoseconds(rhs);
157 }
158
159 TGUI_CONSTEXPR bool operator<(const Duration& lhs, const Duration& rhs)
160 {
161 return std::chrono::nanoseconds(lhs) < std::chrono::nanoseconds(rhs);
162 }
163
164 TGUI_CONSTEXPR bool operator<=(const Duration& lhs, const Duration& rhs)
165 {
166 return std::chrono::nanoseconds(lhs) <= std::chrono::nanoseconds(rhs);
167 }
168
170
171 TGUI_CONSTEXPR Duration operator+(const Duration& lhs, const Duration& rhs)
172 {
173 return {std::chrono::nanoseconds(lhs) + std::chrono::nanoseconds(rhs)};
174 }
175
176 TGUI_CONSTEXPR Duration operator-(const Duration& lhs, const Duration& rhs)
177 {
178 return {std::chrono::nanoseconds(lhs) - std::chrono::nanoseconds(rhs)};
179 }
180
181 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
182 TGUI_CONSTEXPR Duration operator*(const Duration& lhs, T rhs)
183 {
184 return {std::chrono::nanoseconds(lhs) * rhs};
185 }
186
187 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
188 TGUI_CONSTEXPR Duration operator*(T lhs, const Duration& rhs)
189 {
190 return {lhs * std::chrono::nanoseconds(rhs)};
191 }
192
193 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
194 TGUI_CONSTEXPR Duration operator/(const Duration& lhs, T rhs)
195 {
196 return {std::chrono::nanoseconds(lhs) / rhs};
197 }
198
199 TGUI_CONSTEXPR float operator/(const Duration& lhs, const Duration& rhs)
200 {
201 return lhs.asSeconds() / rhs.asSeconds();
202 }
203
204 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
205 TGUI_CONSTEXPR Duration operator%(const Duration& lhs, T rhs)
206 {
207 return {std::chrono::nanoseconds(lhs) % rhs};
208 }
209
210 TGUI_CONSTEXPR Duration operator%(const Duration& lhs, const Duration& rhs)
211 {
212 return {std::chrono::nanoseconds(lhs) % std::chrono::nanoseconds(rhs)};
213 }
214
216
217 TGUI_CONSTEXPR Duration& operator+=(Duration& lhs, const Duration& rhs)
218 {
219 return lhs = lhs + rhs;
220 }
221
222 TGUI_CONSTEXPR Duration& operator-=(Duration& lhs, const Duration& rhs)
223 {
224
225 return lhs = lhs - rhs;
226 }
227
228 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
229 TGUI_CONSTEXPR Duration& operator*=(Duration& lhs, T rhs)
230 {
231 return lhs = lhs * rhs;
232 }
233
234 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
235 TGUI_CONSTEXPR Duration& operator/=(Duration& lhs, T rhs)
236 {
237 return lhs = lhs / rhs;
238 }
239
240 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
241 TGUI_CONSTEXPR Duration& operator%=(Duration& lhs, T rhs)
242 {
243 return lhs = lhs % rhs;
244 }
245
246 TGUI_CONSTEXPR Duration& operator%=(Duration& lhs, const Duration& rhs)
247 {
248 return lhs = lhs % rhs;
249 }
250
252}
253
255
256#endif // TGUI_DURATION_HPP
257
258
Wrapper for durations.
Definition: Duration.hpp:52
TGUI_CONSTEXPR Duration()
Creates an zero-length duration.
Definition: Duration.hpp:58
TGUI_CONSTEXPR Duration(std::chrono::duration< Rep, Period > duration)
Creates the duration from any kind of std::chrono::duration.
Definition: Duration.hpp:68
TGUI_CONSTEXPR Duration(int milliseconds)
Creates the duration from a given amount of milliseconds.
Definition: Duration.hpp:78
TGUI_CONSTEXPR float asSeconds() const
Returns the duration in seconds.
Definition: Duration.hpp:97
Duration(sf::Time duration)
Creates the duration from an sf::Time instance.
Definition: Duration.hpp:88
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36