26#ifndef TGUI_DURATION_HPP
27#define TGUI_DURATION_HPP
29#include <TGUI/Config.hpp>
33#if TGUI_HAS_BACKEND_SFML
34 #include <SFML/System/Time.hpp>
67 template <
typename Rep,
typename Period>
68 TGUI_CONSTEXPR
Duration(std::chrono::duration<Rep, Period> duration) :
69 m_duration{std::chrono::duration_cast<std::chrono::
nanoseconds>(duration)}
79 Duration{std::chrono::milliseconds(milliseconds)}
84#if TGUI_HAS_BACKEND_SFML
89 m_duration{std::chrono::duration_cast<std::chrono::
nanoseconds>(std::chrono::microseconds(duration.asMicroseconds()))}
99 return m_duration.count() / 1000000000.f;
106 TGUI_CONSTEXPR
operator std::chrono::nanoseconds()
const
115 template <
typename Rep,
typename Period>
116 TGUI_CONSTEXPR
operator std::chrono::duration<Rep, Period>()
const
118 return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(m_duration);
121#if TGUI_HAS_BACKEND_SFML
125 operator sf::Time()
const
127 return sf::microseconds(m_duration.count() / 1000);
134 std::chrono::nanoseconds m_duration;
139 TGUI_CONSTEXPR
bool operator==(
const Duration& lhs,
const Duration& rhs)
141 return std::chrono::nanoseconds(lhs) == std::chrono::nanoseconds(rhs);
144 TGUI_CONSTEXPR
bool operator!=(
const Duration& lhs,
const Duration& rhs)
146 return std::chrono::nanoseconds(lhs) != std::chrono::nanoseconds(rhs);
149 TGUI_CONSTEXPR
bool operator>(
const Duration& lhs,
const Duration& rhs)
151 return std::chrono::nanoseconds(lhs) > std::chrono::nanoseconds(rhs);
154 TGUI_CONSTEXPR
bool operator>=(
const Duration& lhs,
const Duration& rhs)
156 return std::chrono::nanoseconds(lhs) >= std::chrono::nanoseconds(rhs);
159 TGUI_CONSTEXPR
bool operator<(
const Duration& lhs,
const Duration& rhs)
161 return std::chrono::nanoseconds(lhs) < std::chrono::nanoseconds(rhs);
164 TGUI_CONSTEXPR
bool operator<=(
const Duration& lhs,
const Duration& rhs)
166 return std::chrono::nanoseconds(lhs) <= std::chrono::nanoseconds(rhs);
171 TGUI_CONSTEXPR Duration operator+(
const Duration& lhs,
const Duration& rhs)
173 return {std::chrono::nanoseconds(lhs) + std::chrono::nanoseconds(rhs)};
176 TGUI_CONSTEXPR Duration operator-(
const Duration& lhs,
const Duration& rhs)
178 return {std::chrono::nanoseconds(lhs) - std::chrono::nanoseconds(rhs)};
181 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
182 TGUI_CONSTEXPR Duration operator*(
const Duration& lhs, T rhs)
184 return {std::chrono::nanoseconds(lhs) * rhs};
187 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
188 TGUI_CONSTEXPR Duration operator*(T lhs,
const Duration& rhs)
190 return {lhs * std::chrono::nanoseconds(rhs)};
193 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
194 TGUI_CONSTEXPR Duration operator/(
const Duration& lhs, T rhs)
196 return {std::chrono::nanoseconds(lhs) / rhs};
199 TGUI_CONSTEXPR
float operator/(
const Duration& lhs,
const Duration& rhs)
201 return lhs.
asSeconds() / rhs.asSeconds();
204 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
205 TGUI_CONSTEXPR Duration operator%(
const Duration& lhs, T rhs)
207 return {std::chrono::nanoseconds(lhs) % rhs};
210 TGUI_CONSTEXPR Duration operator%(
const Duration& lhs,
const Duration& rhs)
212 return {std::chrono::nanoseconds(lhs) % std::chrono::nanoseconds(rhs)};
217 TGUI_CONSTEXPR Duration& operator+=(Duration& lhs,
const Duration& rhs)
219 return lhs = lhs + rhs;
222 TGUI_CONSTEXPR Duration& operator-=(Duration& lhs,
const Duration& rhs)
225 return lhs = lhs - rhs;
228 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
229 TGUI_CONSTEXPR Duration& operator*=(Duration& lhs, T rhs)
231 return lhs = lhs * rhs;
234 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
235 TGUI_CONSTEXPR Duration& operator/=(Duration& lhs, T rhs)
237 return lhs = lhs / rhs;
240 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
241 TGUI_CONSTEXPR Duration& operator%=(Duration& lhs, T rhs)
243 return lhs = lhs % rhs;
246 TGUI_CONSTEXPR Duration& operator%=(Duration& lhs,
const Duration& rhs)
248 return lhs = lhs % rhs;
Wrapper for durations.
Definition Duration.hpp:52
TGUI_CONSTEXPR Duration()
Creates an zero-length duration.
Definition Duration.hpp:58
TGUI_CONSTEXPR Duration(std::chrono::duration< Rep, Period > duration)
Creates the duration from any kind of std::chrono::duration.
Definition Duration.hpp:68
TGUI_CONSTEXPR Duration(int milliseconds)
Creates the duration from a given amount of milliseconds.
Definition Duration.hpp:78
TGUI_CONSTEXPR float asSeconds() const
Returns the duration in seconds.
Definition Duration.hpp:97
TGUI_CONSTEXPR operator std::chrono::nanoseconds() const
Convert the duration to std::chrono::nanoseconds.
Definition Duration.hpp:106
Duration(sf::Time duration)
Creates the duration from an sf::Time instance.
Definition Duration.hpp:88
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36