TGUI 1.11
Loading...
Searching...
No Matches
Duration.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 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#ifndef TGUI_DURATION_HPP
26#define TGUI_DURATION_HPP
27
28#include <TGUI/Config.hpp>
29
30#include <cstdint>
31#include <chrono>
32#include <type_traits>
33
34#if TGUI_HAS_WINDOW_BACKEND_SFML
35 #include <SFML/System/Time.hpp>
36#endif
37
39
40namespace tgui
41{
52 class TGUI_API Duration
53 {
54 public:
55
59 constexpr Duration() :
60 m_duration{std::chrono::nanoseconds::zero()}
61 {
62 }
63
67 template <typename Rep, typename Period>
68 constexpr Duration(std::chrono::duration<Rep, Period> duration) :
69 m_duration{std::chrono::duration_cast<std::chrono::nanoseconds>(duration)}
70 {
71 }
72
77 constexpr Duration(int milliseconds) :
78 Duration{std::chrono::milliseconds(milliseconds)}
79 {
80 }
81
82#if TGUI_HAS_WINDOW_BACKEND_SFML
86 Duration(sf::Time duration) :
87 m_duration{std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::microseconds(duration.asMicroseconds()))}
88 {
89 }
90#endif
91
95 TGUI_NODISCARD constexpr float asSeconds() const
96 {
97 return static_cast<float>(static_cast<double>(m_duration.count()) / 1000000000.0);
98 }
99
103 constexpr operator std::chrono::nanoseconds() const
104 {
105 return m_duration;
106 }
107
111 template <typename Rep, typename Period>
112 constexpr operator std::chrono::duration<Rep, Period>() const
113 {
114 return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(m_duration);
115 }
116
117#if TGUI_HAS_WINDOW_BACKEND_SFML
121 operator sf::Time() const
122 {
123 return sf::microseconds(m_duration.count() / 1000);
124 }
125#endif
126
128 private:
129
130 std::chrono::nanoseconds m_duration;
131 };
132
134
135 TGUI_NODISCARD constexpr bool operator==(const Duration& lhs, const Duration& rhs)
136 {
137 return std::chrono::nanoseconds(lhs) == std::chrono::nanoseconds(rhs);
138 }
139
140 TGUI_NODISCARD constexpr bool operator!=(const Duration& lhs, const Duration& rhs)
141 {
142 return std::chrono::nanoseconds(lhs) != std::chrono::nanoseconds(rhs);
143 }
144
145 TGUI_NODISCARD constexpr bool operator>(const Duration& lhs, const Duration& rhs)
146 {
147 return std::chrono::nanoseconds(lhs) > std::chrono::nanoseconds(rhs);
148 }
149
150 TGUI_NODISCARD constexpr bool operator>=(const Duration& lhs, const Duration& rhs)
151 {
152 return std::chrono::nanoseconds(lhs) >= std::chrono::nanoseconds(rhs);
153 }
154
155 TGUI_NODISCARD constexpr bool operator<(const Duration& lhs, const Duration& rhs)
156 {
157 return std::chrono::nanoseconds(lhs) < std::chrono::nanoseconds(rhs);
158 }
159
160 TGUI_NODISCARD constexpr bool operator<=(const Duration& lhs, const Duration& rhs)
161 {
162 return std::chrono::nanoseconds(lhs) <= std::chrono::nanoseconds(rhs);
163 }
164
166
167 TGUI_NODISCARD constexpr Duration operator+(const Duration& lhs, const Duration& rhs)
168 {
169 return {std::chrono::nanoseconds(lhs) + std::chrono::nanoseconds(rhs)};
170 }
171
172 TGUI_NODISCARD constexpr Duration operator-(const Duration& lhs, const Duration& rhs)
173 {
174 return {std::chrono::nanoseconds(lhs) - std::chrono::nanoseconds(rhs)};
175 }
176
177 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
178 TGUI_NODISCARD constexpr Duration operator*(const Duration& lhs, T rhs)
179 {
180 return {std::chrono::nanoseconds(lhs) * rhs};
181 }
182
183 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
184 TGUI_NODISCARD constexpr Duration operator*(T lhs, const Duration& rhs)
185 {
186 return {lhs * std::chrono::nanoseconds(rhs)};
187 }
188
189 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
190 TGUI_NODISCARD constexpr Duration operator/(const Duration& lhs, T rhs)
191 {
192 return {std::chrono::nanoseconds(lhs) / rhs};
193 }
194
195 TGUI_NODISCARD constexpr float operator/(const Duration& lhs, const Duration& rhs)
196 {
197 return lhs.asSeconds() / rhs.asSeconds();
198 }
199
200 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
201 TGUI_NODISCARD constexpr Duration operator%(const Duration& lhs, T rhs)
202 {
203 return {std::chrono::nanoseconds(lhs) % rhs};
204 }
205
206 TGUI_NODISCARD constexpr Duration operator%(const Duration& lhs, const Duration& rhs)
207 {
208 return {std::chrono::nanoseconds(lhs) % std::chrono::nanoseconds(rhs)};
209 }
210
212
213 constexpr Duration& operator+=(Duration& lhs, const Duration& rhs)
214 {
215 return lhs = lhs + rhs;
216 }
217
218 constexpr Duration& operator-=(Duration& lhs, const Duration& rhs)
219 {
220 return lhs = lhs - rhs;
221 }
222
223 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
224 constexpr Duration& operator*=(Duration& lhs, T rhs)
225 {
226 return lhs = lhs * rhs;
227 }
228
229 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
230 constexpr Duration& operator/=(Duration& lhs, T rhs)
231 {
232 return lhs = lhs / rhs;
233 }
234
235 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
236 constexpr Duration& operator%=(Duration& lhs, T rhs)
237 {
238 return lhs = lhs % rhs;
239 }
240
241 constexpr Duration& operator%=(Duration& lhs, const Duration& rhs)
242 {
243 return lhs = lhs % rhs;
244 }
245
247}
248
250
251#endif // TGUI_DURATION_HPP
252
Wrapper for durations.
Definition Duration.hpp:53
constexpr Duration()
Creates an zero-length duration.
Definition Duration.hpp:59
constexpr Duration(int milliseconds)
Creates the duration from a given amount of milliseconds.
Definition Duration.hpp:77
constexpr operator std::chrono::nanoseconds() const
Convert the duration to std::chrono::nanoseconds.
Definition Duration.hpp:103
Duration(sf::Time duration)
Creates the duration from an sf::Time instance.
Definition Duration.hpp:86
constexpr float asSeconds() const
Returns the duration in seconds.
Definition Duration.hpp:95
constexpr Duration(std::chrono::duration< Rep, Period > duration)
Creates the duration from any kind of std::chrono::duration.
Definition Duration.hpp:68
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36