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:
113 friend constexpr Time seconds(float);
114 friend constexpr Time milliseconds(std::int32_t);
115 friend constexpr Time microseconds(std::int64_t);
116
118 // Member data
120 std::chrono::microseconds m_microseconds{};
121};
122
134[[nodiscard]] constexpr Time seconds(float amount);
135
147[[nodiscard]] constexpr Time milliseconds(std::int32_t amount);
148
160[[nodiscard]] constexpr Time microseconds(std::int64_t amount);
161
172[[nodiscard]] constexpr bool operator==(Time left, Time right);
173
184[[nodiscard]] constexpr bool operator!=(Time left, Time right);
185
196[[nodiscard]] constexpr bool operator<(Time left, Time right);
197
208[[nodiscard]] constexpr bool operator>(Time left, Time right);
209
220[[nodiscard]] constexpr bool operator<=(Time left, Time right);
221
232[[nodiscard]] constexpr bool operator>=(Time left, Time right);
233
243[[nodiscard]] constexpr Time operator-(Time right);
244
255[[nodiscard]] constexpr Time operator+(Time left, Time right);
256
267constexpr Time& operator+=(Time& left, Time right);
268
279[[nodiscard]] constexpr Time operator-(Time left, Time right);
280
291constexpr Time& operator-=(Time& left, Time right);
292
303[[nodiscard]] constexpr Time operator*(Time left, float right);
304
315[[nodiscard]] constexpr Time operator*(Time left, std::int64_t right);
316
327[[nodiscard]] constexpr Time operator*(float left, Time right);
328
339[[nodiscard]] constexpr Time operator*(std::int64_t left, Time right);
340
351constexpr Time& operator*=(Time& left, float right);
352
363constexpr Time& operator*=(Time& left, std::int64_t right);
364
375[[nodiscard]] constexpr Time operator/(Time left, float right);
376
387[[nodiscard]] constexpr Time operator/(Time left, std::int64_t right);
388
399constexpr Time& operator/=(Time& left, float right);
400
411constexpr Time& operator/=(Time& left, std::int64_t right);
412
423[[nodiscard]] constexpr float operator/(Time left, Time right);
424
435[[nodiscard]] constexpr Time operator%(Time left, Time right);
436
447constexpr Time& operator%=(Time& left, Time right);
448
449} // namespace sf
450
451#include <SFML/System/Time.inl>
452
453
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.
friend constexpr Time microseconds(std::int64_t)
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.
friend constexpr Time seconds(float)
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.
friend constexpr Time milliseconds(std::int32_t)
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.