IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
clock_types.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/types.hxx>
7
8namespace ice
9{
10
11 constexpr auto operator""_Ts(long double time) noexcept -> ice::Ts;
12 constexpr auto operator""_Ts(unsigned long long time) noexcept -> ice::Ts;
13 constexpr auto operator""_Tms(unsigned long long time) noexcept -> ice::Tms;
14 constexpr auto operator""_Tus(unsigned long long time) noexcept -> ice::Tus;
15 constexpr auto operator""_Tns(unsigned long long time) noexcept -> ice::Tns;
16
20 struct Ts
21 {
24
26
27 constexpr operator Tms() const noexcept;
28 constexpr operator Tus() const noexcept;
29 constexpr operator Tns() const noexcept;
30
31 static constexpr ValueType Constant_Precision = 1.0;
32 };
33
37 struct Tms
38 {
41
43
44 constexpr operator Ts() const noexcept;
45 constexpr operator Tus() const noexcept;
46 constexpr operator Tns() const noexcept;
47
48 static constexpr ValueType Constant_Precision = 1000;
49 };
50
55 struct Tus
56 {
59
61
62 constexpr operator Ts() const noexcept;
63 constexpr explicit operator Tms() const noexcept;
64 constexpr operator Tns() const noexcept;
65
66 static constexpr ValueType Constant_Precision = 1000'000;
67 };
68
72 struct Tns
73 {
76
78
79 constexpr operator Ts() const noexcept;
80 constexpr explicit operator Tms() const noexcept;
81 constexpr explicit operator Tus() const noexcept;
82
83 static constexpr ValueType Constant_Precision = 1000'000'000;
84 };
85
90
93
94
96
97
98 constexpr Ts::operator Tms() const noexcept { return { static_cast<Tms::ValueType>(value * Tms::Constant_Precision) }; }
99 constexpr Ts::operator Tus() const noexcept { return { static_cast<Tms::ValueType>(value * Tus::Constant_Precision) }; }
100 constexpr Ts::operator Tns() const noexcept { return { static_cast<Tms::ValueType>(value * Tns::Constant_Precision) }; }
101
102 constexpr Tms::operator Ts() const noexcept { return { static_cast<Ts::ValueType>((value * Ts::Constant_Precision) / Tms::Constant_Precision) }; }
103 constexpr Tms::operator Tus() const noexcept { return { static_cast<Tms::ValueType>((value * Tus::Constant_Precision) / Tms::Constant_Precision) }; }
104 constexpr Tms::operator Tns() const noexcept { return { static_cast<Tms::ValueType>((value * Tns::Constant_Precision) / Tms::Constant_Precision) }; }
105
106 constexpr Tus::operator Ts() const noexcept { return { static_cast<Ts::ValueType>((value * Ts::Constant_Precision) / Tus::Constant_Precision) }; }
107 constexpr Tus::operator Tms() const noexcept { return { static_cast<Tms::ValueType>((value * Tms::Constant_Precision) / Tus::Constant_Precision) }; }
108 constexpr Tus::operator Tns() const noexcept { return { static_cast<Tms::ValueType>((value * Tns::Constant_Precision) / Tus::Constant_Precision) }; }
109
110 constexpr Tns::operator Ts() const noexcept { return { static_cast<Ts::ValueType>((value * Ts::Constant_Precision) / Tns::Constant_Precision) }; }
111 constexpr Tns::operator Tms() const noexcept { return { static_cast<Tms::ValueType>((value * Tms::Constant_Precision) / Tns::Constant_Precision) }; }
112 constexpr Tns::operator Tus() const noexcept { return { static_cast<Tms::ValueType>((value * Tus::Constant_Precision) / Tns::Constant_Precision) }; }
113
114 template<typename T>
115 concept TimeType = std::is_same_v<T, Ts> || std::is_same_v<T, Tms> || std::is_same_v<T, Tus> || std::is_same_v<T, Tns>;
116
117 namespace detail
118 {
119
120 template<ice::TimeType T0, ice::TimeType T1>
122 {
123 using HighestPrecision = std::conditional_t<T0::Constant_Precision >= T1::Constant_Precision, T0, T1>;
124 using LowestPrecision = std::conditional_t<T0::Constant_Precision <= T1::Constant_Precision, T0, T1>;
125 };
126
127 template<ice::TimeType T0, ice::TimeType T1>
129
130 template<ice::TimeType T0, ice::TimeType T1>
132
133 } // namespace detail
134
135 constexpr auto operator<=>(ice::TimeType auto left, TimeType auto right) noexcept
136 {
137 using Type = ice::detail::TTHighestPrecisionType<decltype(left), decltype(right)>;
138 Type const left_op = left;
139 Type const right_op = right;
140 return left_op.value <=> right_op.value;
141 }
142
143 constexpr auto operator+(ice::TimeType auto left, TimeType auto right) noexcept
144 {
145 using Type = ice::detail::TTHighestPrecisionType<decltype(left), decltype(right)>;
146 Type const left_op = left;
147 Type const right_op = right;
148 return Type{ left_op.value + right_op.value };
149 }
150
151 constexpr auto operator-(ice::TimeType auto left, TimeType auto right) noexcept
152 {
153 using Type = ice::detail::TTHighestPrecisionType<decltype(left), decltype(right)>;
154 Type const left_op = left;
155 Type const right_op = right;
156 return Type{ left_op.value - right_op.value };
157 }
158
159 constexpr auto operator==(ice::TimeType auto left, TimeType auto right) noexcept
160 {
161 using Type = ice::detail::TTLowestPrecisionType<decltype(left), decltype(right)>;
162 Type const left_op = (Type) left;
163 Type const right_op = (Type) right;
164 return left_op.value == right_op.value;
165 }
166
167 constexpr auto operator""_Ts(long double time) noexcept -> ice::Ts
168 {
169 return { static_cast<ice::Ts::ValueType>(time) };
170 }
171
172 constexpr auto operator""_Ts(unsigned long long time) noexcept -> ice::Ts
173 {
174 return { static_cast<ice::Ts::ValueType>(time) };
175 }
176
177 constexpr auto operator""_Tms(unsigned long long time) noexcept -> ice::Tms
178 {
179 return { static_cast<ice::Tms::ValueType>(time) };
180 }
181
182 constexpr auto operator""_Tus(unsigned long long time) noexcept -> ice::Tus
183 {
184 return { static_cast<ice::Tus::ValueType>(time) };
185 }
186
187 constexpr auto operator""_Tns(unsigned long long time) noexcept -> ice::Tns
188 {
189 return { static_cast<ice::Tns::ValueType>(time) };
190 }
191
192
194 static constexpr Tns t1 = t0;
195
197 {
198
199 static_assert(1.0_Ts == 1_Ts);
200 // static_assert(0.5_Ts * 4_Ts == 2_Ts);
201
202 static_assert(1_Ts == 1000_Tms);
203 static_assert(1_Ts == 1000'000_Tus);
204 static_assert(1_Ts == 1000'000'000_Tns);
205 static_assert(0.001_Ts == 1_Tms);
206 static_assert(0.001_Ts == 1000_Tus);
207 static_assert(0.001_Ts == 1000'000_Tns);
208 static_assert(0.000'001_Ts == 1_Tus);
209 static_assert(0.000'001_Ts == 1000_Tns);
210 static_assert(0.000'000'001_Ts == 1_Tns);
211
212 static_assert(1_Ts == 1000_Tms);
213 static_assert(1_Ts == 1000'000_Tus);
214 static_assert(1_Ts == 1000'000'000_Tns);
215
216 static_assert(1_Tms == 0.001_Ts);
217 static_assert(1_Tms == 1000_Tus);
218 static_assert(1_Tms == 1000'000_Tns);
219
220 static_assert(1_Tus == 0.000'001_Ts);
221 static_assert(1_Tus == 0_Tms);
222 static_assert(1_Tus == 1000_Tns);
223
224 static_assert(1_Tns == 0.000'000'001_Ts);
225 static_assert(1_Tns == 0_Tms);
226 static_assert(1_Tns == 0_Tus);
227
228 static_assert(1_Tns + 1_Tns == 2_Tns);
229 static_assert(1_Tns + 1_Tus == 1001_Tns);
230 static_assert(1_Tns + 1_Tms == 1000'001_Tns);
231
232 static_assert(1_Ts + 1_Tms == 1001_Tms);
233 static_assert(1_Ts + 1_Tus == 1000'001_Tus);
234 static_assert(1_Ts + 1_Tns == 1000'000'001_Tns);
235
236 static_assert(1_Ts - 1_Tms == 999_Tms);
237 static_assert(1_Ts - 1_Tus == 999'999_Tus);
238 static_assert(1_Ts - 1_Tns == 999'999'999_Tns);
239
240 static_assert(2_Tms - 1_Tms == 1_Tms);
241 static_assert(2_Tms - 1_Tus == 1'999_Tus);
242 static_assert(2_Tms - 1_Tns == 1'999'999_Tns);
243
244 static_assert(1_Tns * 2 == 2_Tns);
245 static_assert(1_Tus * 2 == 2_Tus);
246 static_assert(1_Tms * 2 == 2_Tms);
247
248 static_assert(2_Tms * 2 - 1_Tms == 3_Tms);
249 static_assert(2_Tms * 2 - 1_Tus == 3'999_Tus);
250 static_assert(2_Tms * 2 - 1_Tns == 3'999'999_Tns);
251
252 } // namespace detail::ct_tests
253
254} // namespace ice
Definition clock_types.hxx:115
Definition clock_types.hxx:197
Definition hashmap_details.hxx:13
typename TimeTypeTraits< T0, T1 >::HighestPrecision TTHighestPrecisionType
Definition clock_types.hxx:128
typename TimeTypeTraits< T0, T1 >::LowestPrecision TTLowestPrecisionType
Definition clock_types.hxx:131
SPDX-License-Identifier: MIT.
Definition array.hxx:12
constexpr auto operator<=>(ice::TimeType auto left, TimeType auto right) noexcept
Definition clock_types.hxx:135
constexpr auto operator-(ice::TimeType auto left, TimeType auto right) noexcept
Definition clock_types.hxx:151
constexpr auto operator==(ice::TimeType auto left, TimeType auto right) noexcept
Definition clock_types.hxx:159
static constexpr ice::detail::TTLowestPrecisionType< Tms, Tus > t0
Definition clock_types.hxx:193
double f64
Definition types.hxx:17
std::int64_t i64
Definition types.hxx:22
static constexpr Tns t1
Definition clock_types.hxx:194
constexpr auto operator+(ice::TimeType auto left, TimeType auto right) noexcept
Definition clock_types.hxx:143
Represent the current systems clock frequency. Can be used to transform timestamps to time values.
Definition clock_types.hxx:92
ice::f64 value
Definition clock_types.hxx:92
Type tag to enable utility functions for strongly typed numeric values.
Definition strong_type_integral.hxx:11
Represents platform native timestamp with undefined representation.
Definition clock_types.hxx:89
ice::i64 value
Definition clock_types.hxx:89
Represents time interval of milliseconds.
Definition clock_types.hxx:38
ice::StrongNumeric TypeTag
Definition clock_types.hxx:39
static constexpr ValueType Constant_Precision
Definition clock_types.hxx:48
ValueType value
Definition clock_types.hxx:42
ice::i64 ValueType
Definition clock_types.hxx:40
Represents time interval of nanoseconds.
Definition clock_types.hxx:73
ice::StrongNumeric TypeTag
Definition clock_types.hxx:74
static constexpr ValueType Constant_Precision
Definition clock_types.hxx:83
ValueType value
Definition clock_types.hxx:77
ice::i64 ValueType
Definition clock_types.hxx:75
Represents time interval of seconds.
Definition clock_types.hxx:21
ValueType value
Definition clock_types.hxx:25
ice::StrongNumeric TypeTag
Definition clock_types.hxx:22
ice::f64 ValueType
Definition clock_types.hxx:23
static constexpr ValueType Constant_Precision
Definition clock_types.hxx:31
Represents time interval of microseconds.
Definition clock_types.hxx:56
static constexpr ValueType Constant_Precision
Definition clock_types.hxx:66
ice::StrongNumeric TypeTag
Definition clock_types.hxx:57
ValueType value
Definition clock_types.hxx:60
ice::i64 ValueType
Definition clock_types.hxx:58
Definition clock_types.hxx:122
std::conditional_t< T0::Constant_Precision<=T1::Constant_Precision, T0, T1 > LowestPrecision
Definition clock_types.hxx:124
std::conditional_t< T0::Constant_Precision >=T1::Constant_Precision, T0, T1 > HighestPrecision
Definition clock_types.hxx:123