TGUI  1.4.0
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#ifndef TGUI_DURATION_HPP
26#define TGUI_DURATION_HPP
27
28#include <TGUI/Config.hpp>
29
30#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
31 #include <cstdint>
32 #include <chrono>
33 #include <type_traits>
34#endif
35
36#if TGUI_HAS_WINDOW_BACKEND_SFML
37 #include <SFML/System/Time.hpp>
38#endif
39
41
42TGUI_MODULE_EXPORT namespace tgui
43{
54 class TGUI_API Duration
55 {
56 public:
57
61 constexpr Duration() :
62 m_duration{std::chrono::nanoseconds::zero()}
63 {
64 }
65
69 template <typename Rep, typename Period>
70 constexpr Duration(std::chrono::duration<Rep, Period> duration) :
71 m_duration{std::chrono::duration_cast<std::chrono::nanoseconds>(duration)}
72 {
73 }
74
79 constexpr Duration(int milliseconds) :
80 Duration{std::chrono::milliseconds(milliseconds)}
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_NODISCARD constexpr float asSeconds() const
98 {
99 return static_cast<float>(static_cast<double>(m_duration.count()) / 1000000000.0);
100 }
101
105 constexpr operator std::chrono::nanoseconds() const
106 {
107 return m_duration;
108 }
109
113 template <typename Rep, typename Period>
114 constexpr operator std::chrono::duration<Rep, Period>() const
115 {
116 return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(m_duration);
117 }
118
119#if TGUI_HAS_WINDOW_BACKEND_SFML
123 operator sf::Time() const
124 {
125 return sf::microseconds(m_duration.count() / 1000);
126 }
127#endif
128
130 private:
131
132 std::chrono::nanoseconds m_duration;
133 };
134
136
137 TGUI_NODISCARD constexpr bool operator==(const Duration& lhs, const Duration& rhs)
138 {
139 return std::chrono::nanoseconds(lhs) == std::chrono::nanoseconds(rhs);
140 }
141
142 TGUI_NODISCARD constexpr bool operator!=(const Duration& lhs, const Duration& rhs)
143 {
144 return std::chrono::nanoseconds(lhs) != std::chrono::nanoseconds(rhs);
145 }
146
147 TGUI_NODISCARD constexpr bool operator>(const Duration& lhs, const Duration& rhs)
148 {
149 return std::chrono::nanoseconds(lhs) > std::chrono::nanoseconds(rhs);
150 }
151
152 TGUI_NODISCARD constexpr bool operator>=(const Duration& lhs, const Duration& rhs)
153 {
154 return std::chrono::nanoseconds(lhs) >= std::chrono::nanoseconds(rhs);
155 }
156
157 TGUI_NODISCARD constexpr bool operator<(const Duration& lhs, const Duration& rhs)
158 {
159 return std::chrono::nanoseconds(lhs) < std::chrono::nanoseconds(rhs);
160 }
161
162 TGUI_NODISCARD constexpr bool operator<=(const Duration& lhs, const Duration& rhs)
163 {
164 return std::chrono::nanoseconds(lhs) <= std::chrono::nanoseconds(rhs);
165 }
166
168
169 TGUI_NODISCARD constexpr Duration operator+(const Duration& lhs, const Duration& rhs)
170 {
171 return {std::chrono::nanoseconds(lhs) + std::chrono::nanoseconds(rhs)};
172 }
173
174 TGUI_NODISCARD constexpr Duration operator-(const Duration& lhs, const Duration& rhs)
175 {
176 return {std::chrono::nanoseconds(lhs) - std::chrono::nanoseconds(rhs)};
177 }
178
179 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
180 TGUI_NODISCARD constexpr Duration operator*(const Duration& lhs, T rhs)
181 {
182 return {std::chrono::nanoseconds(lhs) * 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*(T lhs, const Duration& rhs)
187 {
188 return {lhs * std::chrono::nanoseconds(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/(const Duration& lhs, T rhs)
193 {
194 return {std::chrono::nanoseconds(lhs) / rhs};
195 }
196
197 TGUI_NODISCARD constexpr float operator/(const Duration& lhs, const Duration& rhs)
198 {
199 return lhs.asSeconds() / rhs.asSeconds();
200 }
201
202 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
203 TGUI_NODISCARD constexpr Duration operator%(const Duration& lhs, T rhs)
204 {
205 return {std::chrono::nanoseconds(lhs) % rhs};
206 }
207
208 TGUI_NODISCARD constexpr Duration operator%(const Duration& lhs, const Duration& rhs)
209 {
210 return {std::chrono::nanoseconds(lhs) % std::chrono::nanoseconds(rhs)};
211 }
212
214
215 constexpr Duration& operator+=(Duration& lhs, const Duration& rhs)
216 {
217 return lhs = lhs + rhs;
218 }
219
220 constexpr Duration& operator-=(Duration& lhs, const Duration& rhs)
221 {
222
223 return lhs = lhs - rhs;
224 }
225
226 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
227 constexpr Duration& operator*=(Duration& lhs, T rhs)
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 constexpr Duration& operator%=(Duration& lhs, const Duration& rhs)
245 {
246 return lhs = lhs % rhs;
247 }
248
250}
251
253
254#endif // TGUI_DURATION_HPP
255
Wrapper for durations.
Definition Duration.hpp:55
constexpr Duration()
Creates an zero-length duration.
Definition Duration.hpp:61
constexpr Duration(int milliseconds)
Creates the duration from a given amount of milliseconds.
Definition Duration.hpp:79
Duration(sf::Time duration)
Creates the duration from an sf::Time instance.
Definition Duration.hpp:88
TGUI_NODISCARD constexpr float asSeconds() const
Returns the duration in seconds.
Definition Duration.hpp:97
constexpr Duration(std::chrono::duration< Rep, Period > duration)
Creates the duration from any kind of std::chrono::duration.
Definition Duration.hpp:70
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38