TGUI  1.3-dev
Loading...
Searching...
No Matches
Duration.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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
31#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
32 #include <cstdint>
33 #include <chrono>
34 #include <type_traits>
35#endif
36
37#if TGUI_HAS_WINDOW_BACKEND_SFML
38 #include <SFML/System/Time.hpp>
39#endif
40
42
43TGUI_MODULE_EXPORT namespace tgui
44{
56 {
57 public:
58
62 constexpr Duration() :
63 m_duration{std::chrono::nanoseconds::zero()}
64 {
65 }
66
67
71 template <typename Rep, typename Period>
72 constexpr Duration(std::chrono::duration<Rep, Period> duration) :
73 m_duration{std::chrono::duration_cast<std::chrono::nanoseconds>(duration)}
74 {
75 }
76
77
82 constexpr Duration(int milliseconds) :
83 Duration{std::chrono::milliseconds(milliseconds)}
84 {
85 }
86
87
88#if TGUI_HAS_WINDOW_BACKEND_SFML
92 Duration(sf::Time duration) :
93 m_duration{std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::microseconds(duration.asMicroseconds()))}
94 {
95 }
96#endif
97
101 TGUI_NODISCARD constexpr float asSeconds() const
102 {
103 return static_cast<float>(static_cast<double>(m_duration.count()) / 1000000000.0);
104 }
105
106
110 constexpr operator std::chrono::nanoseconds() const
111 {
112 return m_duration;
113 }
114
115
119 template <typename Rep, typename Period>
120 constexpr operator std::chrono::duration<Rep, Period>() const
121 {
122 return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(m_duration);
123 }
124
125#if TGUI_HAS_WINDOW_BACKEND_SFML
129 operator sf::Time() const
130 {
131 return sf::microseconds(m_duration.count() / 1000);
132 }
133#endif
134
136 private:
137
138 std::chrono::nanoseconds m_duration;
139 };
140
142
143 TGUI_NODISCARD constexpr bool operator==(const Duration& lhs, const Duration& rhs)
144 {
145 return std::chrono::nanoseconds(lhs) == std::chrono::nanoseconds(rhs);
146 }
147
148 TGUI_NODISCARD constexpr bool operator!=(const Duration& lhs, const Duration& rhs)
149 {
150 return std::chrono::nanoseconds(lhs) != std::chrono::nanoseconds(rhs);
151 }
152
153 TGUI_NODISCARD constexpr bool operator>(const Duration& lhs, const Duration& rhs)
154 {
155 return std::chrono::nanoseconds(lhs) > std::chrono::nanoseconds(rhs);
156 }
157
158 TGUI_NODISCARD constexpr bool operator>=(const Duration& lhs, const Duration& rhs)
159 {
160 return std::chrono::nanoseconds(lhs) >= std::chrono::nanoseconds(rhs);
161 }
162
163 TGUI_NODISCARD constexpr bool operator<(const Duration& lhs, const Duration& rhs)
164 {
165 return std::chrono::nanoseconds(lhs) < std::chrono::nanoseconds(rhs);
166 }
167
168 TGUI_NODISCARD constexpr bool operator<=(const Duration& lhs, const Duration& rhs)
169 {
170 return std::chrono::nanoseconds(lhs) <= std::chrono::nanoseconds(rhs);
171 }
172
174
175 TGUI_NODISCARD constexpr Duration operator+(const Duration& lhs, const Duration& rhs)
176 {
177 return {std::chrono::nanoseconds(lhs) + std::chrono::nanoseconds(rhs)};
178 }
179
180 TGUI_NODISCARD constexpr Duration operator-(const Duration& lhs, const Duration& rhs)
181 {
182 return {std::chrono::nanoseconds(lhs) - std::chrono::nanoseconds(rhs)};
183 }
184
185 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
186 TGUI_NODISCARD constexpr Duration operator*(const Duration& lhs, T rhs)
187 {
188 return {std::chrono::nanoseconds(lhs) * rhs};
189 }
190
191 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
192 TGUI_NODISCARD constexpr Duration operator*(T lhs, const Duration& rhs)
193 {
194 return {lhs * std::chrono::nanoseconds(rhs)};
195 }
196
197 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
198 TGUI_NODISCARD constexpr Duration operator/(const Duration& lhs, T rhs)
199 {
200 return {std::chrono::nanoseconds(lhs) / rhs};
201 }
202
203 TGUI_NODISCARD constexpr float operator/(const Duration& lhs, const Duration& rhs)
204 {
205 return lhs.asSeconds() / rhs.asSeconds();
206 }
207
208 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
209 TGUI_NODISCARD constexpr Duration operator%(const Duration& lhs, T rhs)
210 {
211 return {std::chrono::nanoseconds(lhs) % rhs};
212 }
213
214 TGUI_NODISCARD constexpr Duration operator%(const Duration& lhs, const Duration& rhs)
215 {
216 return {std::chrono::nanoseconds(lhs) % std::chrono::nanoseconds(rhs)};
217 }
218
220
221 constexpr Duration& operator+=(Duration& lhs, const Duration& rhs)
222 {
223 return lhs = lhs + rhs;
224 }
225
226 constexpr Duration& operator-=(Duration& lhs, const Duration& rhs)
227 {
228
229 return lhs = lhs - rhs;
230 }
231
232 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
233 constexpr Duration& operator*=(Duration& lhs, T rhs)
234 {
235 return lhs = lhs * rhs;
236 }
237
238 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
239 constexpr Duration& operator/=(Duration& lhs, T rhs)
240 {
241 return lhs = lhs / rhs;
242 }
243
244 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
245 constexpr Duration& operator%=(Duration& lhs, T rhs)
246 {
247 return lhs = lhs % rhs;
248 }
249
250 constexpr Duration& operator%=(Duration& lhs, const Duration& rhs)
251 {
252 return lhs = lhs % rhs;
253 }
254
256}
257
259
260#endif // TGUI_DURATION_HPP
261
262
Wrapper for durations.
Definition Duration.hpp:56
constexpr Duration()
Creates an zero-length duration.
Definition Duration.hpp:62
constexpr Duration(int milliseconds)
Creates the duration from a given amount of milliseconds.
Definition Duration.hpp:82
Duration(sf::Time duration)
Creates the duration from an sf::Time instance.
Definition Duration.hpp:92
TGUI_NODISCARD constexpr float asSeconds() const
Returns the duration in seconds.
Definition Duration.hpp:101
constexpr Duration(std::chrono::duration< Rep, Period > duration)
Creates the duration from any kind of std::chrono::duration.
Definition Duration.hpp:72
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39