15 #ifndef EASTL_CHRONO_H
16 #define EASTL_CHRONO_H
18 #if defined(EA_PRAGMA_ONCE_SUPPORTED)
22 #include <EASTL/internal/config.h>
23 #include <EASTL/type_traits.h>
24 #include <EASTL/numeric_limits.h>
25 #include <EASTL/ratio.h>
29 #if defined EA_PLATFORM_MICROSOFT
30 EA_DISABLE_ALL_VC_WARNINGS()
32 #ifndef WIN32_LEAN_AND_MEAN
33 #define WIN32_LEAN_AND_MEAN
48 EA_RESTORE_ALL_VC_WARNINGS()
51 #if defined(EA_PLATFORM_MICROSOFT) && !defined(EA_PLATFORM_MINGW)
53 #elif defined(EA_PLATFORM_SONY)
54 #include <Dinkum/threads/xtimec.h>
56 #elif defined(EA_PLATFORM_APPLE)
57 #include <mach/mach_time.h>
58 #elif defined(EA_PLATFORM_POSIX) || defined(EA_PLATFORM_MINGW) || defined(EA_PLATFORM_ANDROID)
60 #if defined(EA_PLATFORM_MINGW)
61 #include <pthread_time.h>
64 #if (defined(CLOCK_REALTIME) || defined(CLOCK_MONOTONIC))
91 EASTL_FORCE_INLINE
static EA_CONSTEXPR Rep zero() {
return Rep(0); }
100 template <
typename Rep,
typename Period = ratio<1>>
129 template <
class Period1,
class Period2>
146 template <
typename FromDuration,
148 typename CommonPeriod =
149 typename ratio_divide<typename FromDuration::period, typename ToDuration::period>::type,
151 typename FromDuration::rep,
152 intmax_t>::type>::type,
153 bool = CommonPeriod::num == 1,
154 bool = CommonPeriod::den == 1>
157 template <
typename FromDuration,
typename ToDuration,
typename CommonPeriod,
typename CommonRep>
160 inline static ToDuration DoCast(
const FromDuration& fd)
162 return ToDuration(
static_cast<typename ToDuration::rep
>(fd.count()));
166 template <
typename FromDuration,
typename ToDuration,
typename CommonPeriod,
typename CommonRep>
169 inline static ToDuration DoCast(
const FromDuration& d)
171 return ToDuration(
static_cast<typename ToDuration::rep
>(
static_cast<CommonRep
>(d.count()) *
172 static_cast<CommonRep
>(CommonPeriod::num)));
176 template <
typename FromDuration,
typename ToDuration,
typename CommonPeriod,
typename CommonRep>
179 inline static ToDuration DoCast(
const FromDuration& d)
181 return ToDuration(
static_cast<typename ToDuration::rep
>(
static_cast<CommonRep
>(d.count()) /
182 static_cast<CommonRep
>(CommonPeriod::den)));
186 template <
typename FromDuration,
typename ToDuration,
typename CommonPeriod,
typename CommonRep>
189 inline static ToDuration DoCast(
const FromDuration& d)
191 return ToDuration(
static_cast<typename ToDuration::rep
>(
static_cast<CommonRep
>(d.count()) *
192 static_cast<CommonRep
>(CommonPeriod::num) /
193 static_cast<CommonRep
>(CommonPeriod::den)));
202 template <
typename ToDuration,
typename Rep,
typename Period>
214 template <
class Rep,
class Period>
221 typedef Period period;
224 #if defined(EA_COMPILER_NO_DEFAULTED_FUNCTIONS)
229 : mRep(Rep(other.mRep)) {}
232 { mRep = other.mRep;
return *
this; }
243 template <
class Rep2>
244 inline EA_CONSTEXPR
explicit duration(
249 : mRep(
static_cast<Rep
>(rep2)) {}
252 template <
class Rep2,
class Period2>
255 (eastl::ratio_divide<Period2, Period>::type::den == 1 &&
258 : mRep(duration_cast<duration>(d2).count()) {}
263 EA_CONSTEXPR Rep count()
const {
return mRep; }
275 EA_CONSTEXPR
inline duration operator+()
const {
return *
this; }
283 inline duration& operator++() { ++mRep;
return *
this; }
284 inline duration& operator--() { --mRep;
return *
this; }
285 inline duration& operator+=(
const duration& d) { mRep += d.count();
return *
this; }
286 inline duration& operator-=(
const duration& d) { mRep -= d.count();
return *
this; }
287 inline duration& operator*=(
const Rep& rhs) { mRep *= rhs;
return *
this; }
288 inline duration& operator/=(
const Rep& rhs) { mRep /= rhs;
return *
this; }
289 inline duration& operator%=(
const Rep& rhs) { mRep %= rhs;
return *
this; }
290 inline duration& operator%=(
const duration& d) { mRep %= d.count();
return *
this; }
297 template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
302 return common_duration_t(common_duration_t(lhs).
count() + common_duration_t(rhs).
count());
305 template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
307 operator-(
const duration<Rep1, Period1>& lhs,
const duration<Rep2, Period2>& rhs)
310 return common_duration_t(common_duration_t(lhs).
count() - common_duration_t(rhs).
count());
313 template <
typename Rep1,
typename Period1,
typename Rep2>
314 duration<typename eastl::common_type<Rep1, Rep2>::type, Period1> EASTL_FORCE_INLINE
315 operator*(
const duration<Rep1, Period1>& lhs,
const Rep2& rhs)
317 typedef typename duration<eastl::common_type<Rep1, Rep2>, Period1>::type common_duration_t;
318 return common_duration_t(common_duration_t(lhs).
count() * rhs);
321 template <
typename Rep1,
typename Rep2,
typename Period2>
322 duration<typename eastl::common_type<Rep1, Rep2>::type, Period2> EASTL_FORCE_INLINE
323 operator*(
const Rep1& lhs,
const duration<Rep2, Period2>& rhs)
325 typedef duration<typename eastl::common_type<Rep1, Rep2>::type, Period2> common_duration_t;
326 return common_duration_t(lhs * common_duration_t(rhs).
count());
329 template <
typename Rep1,
typename Period1,
typename Rep2>
330 duration<typename eastl::common_type<Rep1, Rep2>::type, Period1> EASTL_FORCE_INLINE
331 operator/(
const duration<Rep1, Period1>& lhs,
const Rep2& rhs)
333 typedef duration<typename eastl::common_type<Rep1, Rep2>::type, Period1> common_duration_t;
334 return common_duration_t(common_duration_t(lhs).
count() / rhs);
337 template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
339 operator/(
const duration<Rep1, Period1>& lhs,
const duration<Rep2, Period2>& rhs)
342 return common_duration_t(common_duration_t(lhs).
count() / common_duration_t(rhs).
count());
345 template <
typename Rep1,
typename Period1,
typename Rep2>
346 duration<typename eastl::common_type<Rep1, Rep2>::type, Period1> EASTL_FORCE_INLINE
347 operator%(
const duration<Rep1, Period1>& lhs,
const Rep2& rhs)
349 typedef duration<typename eastl::common_type<Rep1, Rep2>::type, Period1> common_duration_t;
350 return common_duration_t(common_duration_t(lhs).
count() % rhs);
353 template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
355 operator%(
const duration<Rep1, Period1>& lhs,
const duration<Rep2, Period2>& rhs)
358 return common_duration_t(common_duration_t(lhs).
count() % common_duration_t(rhs).
count());
365 template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
366 EASTL_FORCE_INLINE
bool operator==(
const duration<Rep1, Period1>& lhs,
367 const duration<Rep2, Period2>& rhs)
370 return common_duration_t(lhs).count() == common_duration_t(rhs).count();
373 template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
374 EASTL_FORCE_INLINE
bool operator<(
const duration<Rep1, Period1>& lhs,
375 const duration<Rep2, Period2>& rhs)
378 return common_duration_t(lhs).count() < common_duration_t(rhs).count();
381 template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
382 EASTL_FORCE_INLINE
bool operator!=(
const duration<Rep1, Period1>& lhs,
383 const duration<Rep2, Period2>& rhs)
385 return !(lhs == rhs);
388 template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
389 EASTL_FORCE_INLINE
bool operator<=(
const duration<Rep1, Period1>& lhs,
390 const duration<Rep2, Period2>& rhs)
395 template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
396 EASTL_FORCE_INLINE
bool operator>(
const duration<Rep1, Period1>& lhs,
397 const duration<Rep2, Period2>& rhs)
402 template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
403 EASTL_FORCE_INLINE
bool operator>=(
const duration<Rep1, Period1>& lhs,
404 const duration<Rep2, Period2>& rhs)
413 typedef duration<long long, nano> nanoseconds;
414 typedef duration<long long, micro> microseconds;
415 typedef duration<long long, milli> milliseconds;
416 typedef duration<long long> seconds;
417 typedef duration<int, ratio<60>> minutes;
418 typedef duration<int, ratio<3600>> hours;
424 template <
typename Clock,
typename Duration =
typename Clock::duration>
431 typedef Duration duration;
432 typedef typename Duration::rep rep;
433 typedef typename Duration::period period;
435 inline EA_CONSTEXPR
time_point() : mDuration(Duration::zero()) {}
436 EA_CONSTEXPR
explicit time_point(
const Duration& other) : mDuration(other) {}
438 template <
typename Duration2>
442 : mDuration(t.time_since_epoch()) {}
444 EA_CONSTEXPR Duration time_since_epoch()
const {
return mDuration; }
446 time_point& operator+=(
const Duration& d) { mDuration += d;
return *
this; }
447 time_point& operator-=(
const Duration& d) { mDuration -= d;
return *
this; }
457 template <
class Clock,
class Duration1,
class Rep2,
class Period2>
462 return common_timepoint_t(lhs.time_since_epoch() + rhs);
465 template <
class Rep1,
class Period1,
class Clock,
class Duration2>
466 inline EA_CONSTEXPR time_point<Clock, typename eastl::common_type<Duration2, duration<Rep1, Period1>>::type>
467 operator+(
const duration<Rep1, Period1>& lhs,
const time_point<Clock, Duration2>& rhs)
469 typedef time_point<Clock, typename eastl::common_type<Duration2, duration<Rep1, Period1>>::type> common_timepoint_t;
470 return common_timepoint_t(lhs + rhs.time_since_epoch());
473 template <
class Clock,
class Duration1,
class Rep2,
class Period2>
474 inline EA_CONSTEXPR time_point<Clock, typename eastl::common_type<Duration1, duration<Rep2, Period2>>::type>
475 operator-(
const time_point<Clock, Duration1>& lhs,
const duration<Rep2, Period2>& rhs)
477 typedef time_point<Clock, typename eastl::common_type<Duration1, duration<Rep2, Period2>>::type> common_timepoint_t;
478 return common_timepoint_t(lhs.time_since_epoch() - rhs);
481 template <
class Clock,
class Duration1,
class Duration2>
483 const time_point<Clock, Duration1>& lhs,
484 const time_point<Clock, Duration2>& rhs)
486 return lhs.time_since_epoch() - rhs.time_since_epoch();
489 template <
class Clock,
class Duration1,
class Duration2>
490 inline EA_CONSTEXPR
bool operator==(
const time_point<Clock, Duration1>& lhs,
491 const time_point<Clock, Duration2>& rhs)
493 return lhs.time_since_epoch() == rhs.time_since_epoch();
496 template <
class Clock,
class Duration1,
class Duration2>
497 inline EA_CONSTEXPR
bool operator!=(
const time_point<Clock, Duration1>& lhs,
498 const time_point<Clock, Duration2>& rhs)
500 return !(lhs == rhs);
503 template <
class Clock,
class Duration1,
class Duration2>
504 inline EA_CONSTEXPR
bool operator<(
const time_point<Clock, Duration1>& lhs,
const time_point<Clock, Duration2>& rhs)
506 return lhs.time_since_epoch() < rhs.time_since_epoch();
509 template <
class Clock,
class Duration1,
class Duration2>
510 inline EA_CONSTEXPR
bool operator<=(
const time_point<Clock, Duration1>& lhs,
511 const time_point<Clock, Duration2>& rhs)
516 template <
class Clock,
class Duration1,
class Duration2>
517 inline EA_CONSTEXPR
bool operator>(
const time_point<Clock, Duration1>& lhs,
const time_point<Clock, Duration2>& rhs)
522 template <
class Clock,
class Duration1,
class Duration2>
523 inline EA_CONSTEXPR
bool operator>=(
const time_point<Clock, Duration1>& lhs,
524 const time_point<Clock, Duration2>& rhs)
533 template <
typename ToDuration,
typename Clock,
typename Duration>
534 EA_CONSTEXPR time_point<Clock, ToDuration> time_point_cast(
535 const time_point<Clock, Duration>& t,
536 typename eastl::enable_if<Internal::IsDuration<ToDuration>::value>::type** = 0)
538 return time_point<Clock, ToDuration>(duration_cast<ToDuration>(t.time_since_epoch()));
548 #if defined(EA_PLATFORM_MICROSOFT) && !defined(EA_PLATFORM_MINGW)
549 #define EASTL_NS_PER_TICK 1
550 #elif defined EA_PLATFORM_SONY
551 #define EASTL_NS_PER_TICK _XTIME_NSECS_PER_TICK
552 #elif defined EA_PLATFORM_POSIX
553 #define EASTL_NS_PER_TICK _XTIME_NSECS_PER_TICK
555 #define EASTL_NS_PER_TICK 100
558 #if defined(EA_PLATFORM_POSIX)
559 typedef chrono::nanoseconds::period SystemClock_Period;
560 typedef chrono::nanoseconds::period SteadyClock_Period;
562 typedef eastl::ratio_multiply<eastl::ratio<EASTL_NS_PER_TICK, 1>, nano>::type SystemClock_Period;
563 typedef eastl::ratio_multiply<eastl::ratio<EASTL_NS_PER_TICK, 1>, nano>::type SteadyClock_Period;
570 inline uint64_t GetTicks()
572 #if defined EA_PLATFORM_MICROSOFT
573 auto queryFrequency = []
575 LARGE_INTEGER frequency;
576 QueryPerformanceFrequency(&frequency);
577 return double(1000000000.0L / frequency.QuadPart);
580 auto queryCounter = []
582 LARGE_INTEGER counter;
583 QueryPerformanceCounter(&counter);
584 return counter.QuadPart;
587 EA_DISABLE_VC_WARNING(4640)
588 static auto frequency = queryFrequency();
589 EA_RESTORE_VC_WARNING()
590 return uint64_t(frequency * queryCounter());
591 #elif defined EA_PLATFORM_SONY
592 return sceKernelGetProcessTimeCounter();
593 #elif defined(EA_PLATFORM_APPLE)
594 return mach_absolute_time();
595 #elif defined(EA_PLATFORM_POSIX)
596 #if (defined(CLOCK_REALTIME) || defined(CLOCK_MONOTONIC))
598 int result = clock_gettime(CLOCK_MONOTONIC, &ts);
600 if (result == -1 && errno == EINVAL)
601 result = clock_gettime(CLOCK_REALTIME, &ts);
603 const uint64_t nNanoseconds = (uint64_t)ts.tv_nsec + ((uint64_t)ts.tv_sec * UINT64_C(1000000000));
607 gettimeofday(&tv, NULL);
608 const uint64_t nMicroseconds = (uint64_t)tv.tv_usec + ((uint64_t)tv.tv_sec * 1000000);
609 return nMicroseconds;
612 #error "chrono not implemented for platform"
624 typedef long long rep;
625 typedef Internal::SystemClock_Period period;
630 EA_CONSTEXPR_OR_CONST
static bool is_steady =
false;
646 typedef long long rep;
647 typedef Internal::SteadyClock_Period period;
652 EA_CONSTEXPR_OR_CONST
static bool is_steady =
true;
674 template <
typename Rep1,
typename Period1,
typename Rep2,
typename Period2>
685 template <
typename Clock,
typename Duration1,
typename Duration2>
695 #if EASTL_USER_LITERALS_ENABLED && EASTL_INLINE_NAMESPACES_ENABLED
696 EA_DISABLE_VC_WARNING(4455)
697 inline namespace literals
699 inline namespace chrono_literals
705 EA_CONSTEXPR chrono::minutes
operator"" min(
unsigned long long m) {
return chrono::minutes(m); }
706 EA_CONSTEXPR chrono::seconds
operator"" s(
unsigned long long s) {
return chrono::seconds(s); }
707 EA_CONSTEXPR chrono::milliseconds
operator"" ms(
unsigned long long ms) {
return chrono::milliseconds(ms); }
708 EA_CONSTEXPR chrono::microseconds
operator"" us(
unsigned long long us) {
return chrono::microseconds(us); }
709 EA_CONSTEXPR chrono::nanoseconds
operator"" ns(
unsigned long long ns) {
return chrono::nanoseconds(ns); }
714 EA_CONSTEXPR chrono::duration<long double, ratio<3600, 1>>
operator"" h(
long double h)
715 {
return chrono::duration<long double, ratio<3600, 1>>(h); }
716 EA_CONSTEXPR chrono::duration<long double, ratio<60, 1>>
operator"" min(
long double m)
717 {
return chrono::duration<long double, ratio<60, 1>>(m); }
718 EA_CONSTEXPR chrono::duration<long double>
operator"" s(
long double s)
719 {
return chrono::duration<long double>(s); }
720 EA_CONSTEXPR chrono::duration<float, milli>
operator"" ms(
long double ms)
721 {
return chrono::duration<long double, milli>(ms); }
722 EA_CONSTEXPR chrono::duration<float, micro>
operator"" us(
long double us)
723 {
return chrono::duration<long double, micro>(us); }
724 EA_CONSTEXPR chrono::duration<float, nano>
operator"" ns(
long double ns)
725 {
return chrono::duration<long double, nano>(ns); }
729 EA_RESTORE_VC_WARNING()
735 #if EASTL_USER_LITERALS_ENABLED && EASTL_INLINE_NAMESPACES_ENABLED
738 using namespace eastl::literals::chrono_literals;
Definition: numeric_limits.h:267
EA Standard Template Library.
Definition: algorithm.h:288
eastl::iterator_traits< InputIterator >::difference_type count(InputIterator first, InputIterator last, const T &value)
Definition: algorithm.h:1347
T min(std::initializer_list< T > ilist)
Definition: algorithm.h:635
Definition: type_compound.h:663
Definition: type_compound.h:615
Definition: type_traits.h:442
Definition: type_traits.h:263
Definition: type_compound.h:328
Definition: type_fundamental.h:183