Loading...
Searching...
No Matches
Time.hpp
Go to the documentation of this file.
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2024 Laurent Gomila (laurent@sfml-dev.org)
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#pragma once
26
28// Headers
30#include <chrono>
31
32#include <cstdint>
33
34
35namespace sf
36{
41class Time
42{
43public:
50 constexpr Time() = default;
51
56 template <typename Rep, typename Period>
57 constexpr Time(const std::chrono::duration<Rep, Period>& duration);
58
67 [[nodiscard]] constexpr float asSeconds() const;
68
77 [[nodiscard]] constexpr std::int32_t asMilliseconds() const;
78
87 [[nodiscard]] constexpr std::int64_t asMicroseconds() const;
88
95 [[nodiscard]] constexpr std::chrono::microseconds toDuration() const;
96
103 template <typename Rep, typename Period>
104 constexpr operator std::chrono::duration<Rep, Period>() const;
105
107 // Static member data
109 // NOLINTNEXTLINE(readability-identifier-naming)
110 static const Time Zero;
111
112private:
114 // Member data
116 std::chrono::microseconds m_microseconds{};
117};
118
130[[nodiscard]] constexpr Time seconds(float amount);
131
143[[nodiscard]] constexpr Time milliseconds(std::int32_t amount);
144
156[[nodiscard]] constexpr Time microseconds(std::int64_t amount);
157
168[[nodiscard]] constexpr bool operator==(Time left, Time right);
169
180[[nodiscard]] constexpr bool operator!=(Time left, Time right);
181
192[[nodiscard]] constexpr bool operator<(Time left, Time right);
193
204[[nodiscard]] constexpr bool operator>(Time left, Time right);
205
216[[nodiscard]] constexpr bool operator<=(Time left, Time right);
217
228[[nodiscard]] constexpr bool operator>=(Time left, Time right);
229
239[[nodiscard]] constexpr Time operator-(Time right);
240
251[[nodiscard]] constexpr Time operator+(Time left, Time right);
252
263constexpr Time& operator+=(Time& left, Time right);
264
275[[nodiscard]] constexpr Time operator-(Time left, Time right);
276
287constexpr Time& operator-=(Time& left, Time right);
288
299[[nodiscard]] constexpr Time operator*(Time left, float right);
300
311[[nodiscard]] constexpr Time operator*(Time left, std::int64_t right);
312
323[[nodiscard]] constexpr Time operator*(float left, Time right);
324
335[[nodiscard]] constexpr Time operator*(std::int64_t left, Time right);
336
347constexpr Time& operator*=(Time& left, float right);
348
359constexpr Time& operator*=(Time& left, std::int64_t right);
360
371[[nodiscard]] constexpr Time operator/(Time left, float right);
372
383[[nodiscard]] constexpr Time operator/(Time left, std::int64_t right);
384
395constexpr Time& operator/=(Time& left, float right);
396
407constexpr Time& operator/=(Time& left, std::int64_t right);
408
419[[nodiscard]] constexpr float operator/(Time left, Time right);
420
431[[nodiscard]] constexpr Time operator%(Time left, Time right);
432
443constexpr Time& operator%=(Time& left, Time right);
444
445} // namespace sf
446
447#include <SFML/System/Time.inl>
448
449
Represents a time value.
Definition Time.hpp:42
constexpr Time operator-(Time right)
Overload of unary operator- to negate a time value.
constexpr float asSeconds() const
Return the time value as a number of seconds.
constexpr Time operator%(Time left, Time right)
Overload of binary operator% to compute remainder of a time value.
constexpr Time & operator*=(Time &left, std::int64_t right)
Overload of binary operator*= to scale/assign a time value.
constexpr Time microseconds(std::int64_t amount)
Construct a time value from a number of microseconds.
constexpr Time(const std::chrono::duration< Rep, Period > &duration)
Construct from std::chrono::duration
constexpr Time operator-(Time left, Time right)
Overload of binary operator- to subtract two time values.
constexpr Time operator*(Time left, std::int64_t right)
Overload of binary operator* to scale a time value.
constexpr Time & operator%=(Time &left, Time right)
Overload of binary operator%= to compute/assign remainder of a time value.
constexpr Time seconds(float amount)
Construct a time value from a number of seconds.
constexpr Time operator*(std::int64_t left, Time right)
Overload of binary operator* to scale a time value.
constexpr float operator/(Time left, Time right)
Overload of binary operator/ to compute the ratio of two time values.
constexpr bool operator!=(Time left, Time right)
Overload of operator!= to compare two time values.
constexpr std::int64_t asMicroseconds() const
Return the time value as a number of microseconds.
constexpr std::chrono::microseconds toDuration() const
Return the time value as a std::chrono::duration
constexpr Time operator/(Time left, std::int64_t right)
Overload of binary operator/ to scale a time value.
static const Time Zero
Predefined "zero" time value.
Definition Time.hpp:110
constexpr bool operator>(Time left, Time right)
Overload of operator> to compare two time values.
constexpr std::int32_t asMilliseconds() const
Return the time value as a number of milliseconds.
constexpr Time operator+(Time left, Time right)
Overload of binary operator+ to add two time values.
constexpr Time operator*(Time left, float right)
Overload of binary operator* to scale a time value.
constexpr bool operator<(Time left, Time right)
Overload of operator< to compare two time values.
constexpr Time & operator-=(Time &left, Time right)
Overload of binary operator-= to subtract/assign two time values.
constexpr bool operator<=(Time left, Time right)
Overload of operator<= to compare two time values.
constexpr Time & operator*=(Time &left, float right)
Overload of binary operator*= to scale/assign a time value.
constexpr Time()=default
Default constructor.
constexpr Time milliseconds(std::int32_t amount)
Construct a time value from a number of milliseconds.
constexpr bool operator==(Time left, Time right)
Overload of operator== to compare two time values.
constexpr Time operator*(float left, Time right)
Overload of binary operator* to scale a time value.
constexpr Time operator/(Time left, float right)
Overload of binary operator/ to scale a time value.
constexpr bool operator>=(Time left, Time right)
Overload of operator>= to compare two time values.
constexpr Time & operator/=(Time &left, std::int64_t right)
Overload of binary operator/= to scale/assign a time value.
constexpr Time & operator/=(Time &left, float right)
Overload of binary operator/= to scale/assign a time value.
constexpr Time & operator+=(Time &left, Time right)
Overload of binary operator+= to add/assign two time values.