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