1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
|
/*
* Copyright (C) 2016 Apple Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <optional>
#include <wtf/FastMalloc.h>
#include <wtf/MathExtras.h>
namespace WTF {
class ApproximateTime;
class ContinuousApproximateTime;
class ContinuousTime;
class MonotonicTime;
class PrintStream;
class TextStream;
class TimeWithDynamicClockType;
class WallTime;
class Seconds final {
WTF_MAKE_FAST_ALLOCATED;
public:
constexpr Seconds() { }
explicit constexpr Seconds(double value)
: m_value(value)
{
}
constexpr double value() const { return m_value; }
constexpr double minutes() const { return m_value / 60; }
constexpr double seconds() const { return m_value; }
constexpr double milliseconds() const { return seconds() * 1000; }
constexpr double microseconds() const { return milliseconds() * 1000; }
constexpr double nanoseconds() const { return microseconds() * 1000; }
// Keep in mind that Seconds is held in double. If the value is not in range of 53bit integer, the result may not be precise.
template<typename T> T minutesAs() const { static_assert(std::is_integral<T>::value); return clampToAccepting64<T>(minutes()); }
template<typename T> T secondsAs() const { static_assert(std::is_integral<T>::value); return clampToAccepting64<T>(seconds()); }
template<typename T> T millisecondsAs() const { static_assert(std::is_integral<T>::value); return clampToAccepting64<T>(milliseconds()); }
template<typename T> T microsecondsAs() const { static_assert(std::is_integral<T>::value); return clampToAccepting64<T>(microseconds()); }
template<typename T> T nanosecondsAs() const { static_assert(std::is_integral<T>::value); return clampToAccepting64<T>(nanoseconds()); }
static constexpr Seconds fromMinutes(double minutes)
{
return Seconds(minutes * 60);
}
static constexpr Seconds fromHours(double hours)
{
return Seconds(hours * 3600);
}
static constexpr Seconds fromMilliseconds(double milliseconds)
{
return Seconds(milliseconds / 1000);
}
static constexpr Seconds fromMicroseconds(double microseconds)
{
return fromMilliseconds(microseconds / 1000);
}
static constexpr Seconds fromNanoseconds(double nanoseconds)
{
return fromMicroseconds(nanoseconds / 1000);
}
static constexpr Seconds infinity()
{
return Seconds(std::numeric_limits<double>::infinity());
}
static constexpr Seconds nan()
{
return Seconds(std::numeric_limits<double>::quiet_NaN());
}
static constexpr Seconds highTimePrecision()
{
return Seconds::fromMicroseconds(20);
}
bool isNaN() const { return std::isnan(m_value); }
bool isInfinity() const { return std::isinf(m_value); }
bool isFinite() const { return std::isfinite(m_value); }
explicit constexpr operator bool() const { return !!m_value; }
constexpr Seconds operator+(Seconds other) const
{
return Seconds(value() + other.value());
}
constexpr Seconds operator-(Seconds other) const
{
return Seconds(value() - other.value());
}
constexpr Seconds operator-() const
{
return Seconds(-value());
}
// It makes sense to consider scaling a duration, like, "I want to wait 5 times as long as
// last time!".
constexpr Seconds operator*(double scalar) const
{
return Seconds(value() * scalar);
}
constexpr Seconds operator/(double scalar) const
{
return Seconds(value() / scalar);
}
// It's reasonable to think about ratios between Seconds.
constexpr double operator/(Seconds other) const
{
return value() / other.value();
}
Seconds operator%(double scalar) const
{
return Seconds(fmod(value(), scalar));
}
// This solves for r, where:
//
// floor(this / other) + r / other = this / other
//
// Therefore, if this is Seconds then r is Seconds.
Seconds operator%(Seconds other) const
{
return Seconds(fmod(value(), other.value()));
}
Seconds& operator+=(Seconds other)
{
return *this = *this + other;
}
Seconds& operator-=(Seconds other)
{
return *this = *this - other;
}
Seconds& operator*=(double scalar)
{
return *this = *this * scalar;
}
Seconds& operator/=(double scalar)
{
return *this = *this / scalar;
}
Seconds& operator%=(double scalar)
{
return *this = *this % scalar;
}
Seconds& operator%=(Seconds other)
{
return *this = *this % other;
}
WTF_EXPORT_PRIVATE WallTime operator+(WallTime) const;
WTF_EXPORT_PRIVATE MonotonicTime operator+(MonotonicTime) const;
WTF_EXPORT_PRIVATE ApproximateTime operator+(ApproximateTime) const;
WTF_EXPORT_PRIVATE ContinuousTime operator+(ContinuousTime) const;
WTF_EXPORT_PRIVATE ContinuousApproximateTime operator+(ContinuousApproximateTime) const;
WTF_EXPORT_PRIVATE TimeWithDynamicClockType operator+(const TimeWithDynamicClockType&) const;
WTF_EXPORT_PRIVATE WallTime operator-(WallTime) const;
WTF_EXPORT_PRIVATE MonotonicTime operator-(MonotonicTime) const;
WTF_EXPORT_PRIVATE ApproximateTime operator-(ApproximateTime) const;
WTF_EXPORT_PRIVATE ContinuousTime operator-(ContinuousTime) const;
WTF_EXPORT_PRIVATE ContinuousApproximateTime operator-(ContinuousApproximateTime) const;
WTF_EXPORT_PRIVATE TimeWithDynamicClockType operator-(const TimeWithDynamicClockType&) const;
friend constexpr bool operator==(Seconds, Seconds) = default;
constexpr bool operator<(Seconds other) const
{
return m_value < other.m_value;
}
constexpr bool operator>(Seconds other) const
{
return m_value > other.m_value;
}
constexpr bool operator<=(Seconds other) const
{
return m_value <= other.m_value;
}
constexpr bool operator>=(Seconds other) const
{
return m_value >= other.m_value;
}
WTF_EXPORT_PRIVATE void dump(PrintStream&) const;
Seconds isolatedCopy() const
{
return *this;
}
constexpr Seconds reduceTimeResolution(Seconds resolution)
{
double reduced = std::floor(seconds() / resolution.seconds()) * resolution.seconds();
return Seconds(reduced);
}
struct MarkableTraits;
private:
double m_value { 0 };
};
WTF_EXPORT_PRIVATE void sleep(Seconds);
struct Seconds::MarkableTraits {
static bool isEmptyValue(Seconds seconds)
{
return seconds.isNaN();
}
static constexpr Seconds emptyValue()
{
return Seconds::nan();
}
};
inline namespace seconds_literals {
constexpr Seconds operator""_min(long double minutes)
{
return Seconds::fromMinutes(minutes);
}
constexpr Seconds operator""_h(long double hours)
{
return Seconds::fromHours(hours);
}
constexpr Seconds operator""_s(long double seconds)
{
return Seconds(seconds);
}
constexpr Seconds operator""_ms(long double milliseconds)
{
return Seconds::fromMilliseconds(milliseconds);
}
constexpr Seconds operator""_us(long double microseconds)
{
return Seconds::fromMicroseconds(microseconds);
}
constexpr Seconds operator""_ns(long double nanoseconds)
{
return Seconds::fromNanoseconds(nanoseconds);
}
constexpr Seconds operator""_min(unsigned long long minutes)
{
return Seconds::fromMinutes(minutes);
}
constexpr Seconds operator""_h(unsigned long long hours)
{
return Seconds::fromHours(hours);
}
constexpr Seconds operator""_s(unsigned long long seconds)
{
return Seconds(seconds);
}
constexpr Seconds operator""_ms(unsigned long long milliseconds)
{
return Seconds::fromMilliseconds(milliseconds);
}
constexpr Seconds operator""_us(unsigned long long microseconds)
{
return Seconds::fromMicroseconds(microseconds);
}
constexpr Seconds operator""_ns(unsigned long long nanoseconds)
{
return Seconds::fromNanoseconds(nanoseconds);
}
} // inline seconds_literals
inline Seconds operator*(double scalar, Seconds seconds)
{
return Seconds(scalar * seconds.value());
}
inline Seconds operator/(double scalar, Seconds seconds)
{
return Seconds(scalar / seconds.value());
}
WTF_EXPORT_PRIVATE TextStream& operator<<(TextStream&, Seconds);
} // namespace WTF
using WTF::sleep;
using namespace WTF::seconds_literals;
using WTF::Seconds;
|