25#ifndef TGUI_DURATION_HPP
26#define TGUI_DURATION_HPP
28#include <TGUI/Config.hpp>
34#if TGUI_HAS_WINDOW_BACKEND_SFML
35 #include <SFML/System/Time.hpp>
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)}
78 Duration{std::chrono::milliseconds(milliseconds)}
82#if TGUI_HAS_WINDOW_BACKEND_SFML
87 m_duration{std::chrono::duration_cast<std::chrono::
nanoseconds>(std::chrono::microseconds(duration.asMicroseconds()))}
97 return static_cast<float>(
static_cast<double>(m_duration.count()) / 1000000000.0);
103 constexpr operator std::chrono::nanoseconds()
const
111 template <
typename Rep,
typename Period>
112 constexpr operator std::chrono::duration<Rep, Period>()
const
114 return std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(m_duration);
117#if TGUI_HAS_WINDOW_BACKEND_SFML
121 operator sf::Time()
const
123 return sf::microseconds(m_duration.count() / 1000);
130 std::chrono::nanoseconds m_duration;
135 TGUI_NODISCARD
constexpr bool operator==(
const Duration& lhs,
const Duration& rhs)
137 return std::chrono::nanoseconds(lhs) == std::chrono::nanoseconds(rhs);
140 TGUI_NODISCARD
constexpr bool operator!=(
const Duration& lhs,
const Duration& rhs)
142 return std::chrono::nanoseconds(lhs) != std::chrono::nanoseconds(rhs);
145 TGUI_NODISCARD
constexpr bool operator>(
const Duration& lhs,
const Duration& rhs)
147 return std::chrono::nanoseconds(lhs) > std::chrono::nanoseconds(rhs);
150 TGUI_NODISCARD
constexpr bool operator>=(
const Duration& lhs,
const Duration& rhs)
152 return std::chrono::nanoseconds(lhs) >= std::chrono::nanoseconds(rhs);
155 TGUI_NODISCARD
constexpr bool operator<(
const Duration& lhs,
const Duration& rhs)
157 return std::chrono::nanoseconds(lhs) < std::chrono::nanoseconds(rhs);
160 TGUI_NODISCARD
constexpr bool operator<=(
const Duration& lhs,
const Duration& rhs)
162 return std::chrono::nanoseconds(lhs) <= std::chrono::nanoseconds(rhs);
169 return {std::chrono::nanoseconds(lhs) + std::chrono::nanoseconds(rhs)};
174 return {std::chrono::nanoseconds(lhs) - std::chrono::nanoseconds(rhs)};
177 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
180 return {std::chrono::nanoseconds(lhs) * rhs};
183 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
186 return {lhs * std::chrono::nanoseconds(rhs)};
189 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
192 return {std::chrono::nanoseconds(lhs) / rhs};
195 TGUI_NODISCARD
constexpr float operator/(
const Duration& lhs,
const Duration& rhs)
197 return lhs.asSeconds() / rhs.asSeconds();
200 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
203 return {std::chrono::nanoseconds(lhs) % rhs};
208 return {std::chrono::nanoseconds(lhs) % std::chrono::nanoseconds(rhs)};
215 return lhs = lhs + rhs;
220 return lhs = lhs - rhs;
223 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
226 return lhs = lhs * rhs;
229 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
232 return lhs = lhs / rhs;
235 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
238 return lhs = lhs % rhs;
243 return lhs = lhs % rhs;
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