25#ifndef TGUI_DURATION_HPP
26#define TGUI_DURATION_HPP
28#include <TGUI/Config.hpp>
30#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
33 #include <type_traits>
36#if TGUI_HAS_WINDOW_BACKEND_SFML
37 #include <SFML/System/Time.hpp>
42TGUI_MODULE_EXPORT
namespace tgui
62 m_duration{std::chrono::nanoseconds::zero()}
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)}
80 Duration{std::chrono::milliseconds(milliseconds)}
84#if TGUI_HAS_WINDOW_BACKEND_SFML
89 m_duration{std::chrono::duration_cast<std::chrono::nanoseconds>(std::chrono::microseconds(duration.asMicroseconds()))}
99 return static_cast<float>(
static_cast<double>(m_duration.count()) / 1000000000.0);
105 constexpr operator std::chrono::nanoseconds()
const
113 template <
typename Rep,
typename Period>
114 constexpr operator std::chrono::duration<Rep, Period>()
const
116 return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(m_duration);
119#if TGUI_HAS_WINDOW_BACKEND_SFML
123 operator sf::Time()
const
125 return sf::microseconds(m_duration.count() / 1000);
132 std::chrono::nanoseconds m_duration;
137 TGUI_NODISCARD
constexpr bool operator==(
const Duration& lhs,
const Duration& rhs)
139 return std::chrono::nanoseconds(lhs) == std::chrono::nanoseconds(rhs);
142 TGUI_NODISCARD
constexpr bool operator!=(
const Duration& lhs,
const Duration& rhs)
144 return std::chrono::nanoseconds(lhs) != std::chrono::nanoseconds(rhs);
147 TGUI_NODISCARD
constexpr bool operator>(
const Duration& lhs,
const Duration& rhs)
149 return std::chrono::nanoseconds(lhs) > std::chrono::nanoseconds(rhs);
152 TGUI_NODISCARD
constexpr bool operator>=(
const Duration& lhs,
const Duration& rhs)
154 return std::chrono::nanoseconds(lhs) >= std::chrono::nanoseconds(rhs);
157 TGUI_NODISCARD
constexpr bool operator<(
const Duration& lhs,
const Duration& rhs)
159 return std::chrono::nanoseconds(lhs) < std::chrono::nanoseconds(rhs);
162 TGUI_NODISCARD
constexpr bool operator<=(
const Duration& lhs,
const Duration& rhs)
164 return std::chrono::nanoseconds(lhs) <= std::chrono::nanoseconds(rhs);
169 TGUI_NODISCARD
constexpr Duration operator+(
const Duration& lhs,
const Duration& rhs)
171 return {std::chrono::nanoseconds(lhs) + std::chrono::nanoseconds(rhs)};
174 TGUI_NODISCARD
constexpr Duration operator-(
const Duration& lhs,
const Duration& rhs)
176 return {std::chrono::nanoseconds(lhs) - std::chrono::nanoseconds(rhs)};
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)
182 return {std::chrono::nanoseconds(lhs) * rhs};
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)
188 return {lhs * std::chrono::nanoseconds(rhs)};
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)
194 return {std::chrono::nanoseconds(lhs) / rhs};
197 TGUI_NODISCARD
constexpr float operator/(
const Duration& lhs,
const Duration& rhs)
199 return lhs.
asSeconds() / rhs.asSeconds();
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)
205 return {std::chrono::nanoseconds(lhs) % rhs};
208 TGUI_NODISCARD
constexpr Duration operator%(
const Duration& lhs,
const Duration& rhs)
210 return {std::chrono::nanoseconds(lhs) % std::chrono::nanoseconds(rhs)};
215 constexpr Duration& operator+=(Duration& lhs,
const Duration& rhs)
217 return lhs = lhs + rhs;
220 constexpr Duration& operator-=(Duration& lhs,
const Duration& rhs)
222 return lhs = lhs - rhs;
225 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
226 constexpr Duration& operator*=(Duration& lhs, T rhs)
228 return lhs = lhs * rhs;
231 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
232 constexpr Duration& operator/=(Duration& lhs, T rhs)
234 return lhs = lhs / rhs;
237 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
238 constexpr Duration& operator%=(Duration& lhs, T rhs)
240 return lhs = lhs % rhs;
243 constexpr Duration& operator%=(Duration& lhs,
const Duration& rhs)
245 return lhs = lhs % rhs;
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
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