X-Git-Url: https://git.saurik.com/apple/system_cmds.git/blobdiff_plain/1a7e3f61d38d679bba59130891c2031b5a0092b6..bd6521f0fc816ab056bc71376f9706a69b3b52c1:/CPPUtil/UtilAbsTime.hpp diff --git a/CPPUtil/UtilAbsTime.hpp b/CPPUtil/UtilAbsTime.hpp new file mode 100644 index 0000000..8fe56b0 --- /dev/null +++ b/CPPUtil/UtilAbsTime.hpp @@ -0,0 +1,62 @@ +// +// UtilAbsTime.hpp +// CPPUtil +// +// Created by James McIlree on 4/14/13. +// Copyright (c) 2013 Apple. All rights reserved. +// + +#ifndef __CPPUtil__UtilAbsTime__ +#define __CPPUtil__UtilAbsTime__ + +class NanoTime; + +class AbsTime { + protected: + uint64_t _time; + + public: + // Minimum and Maximum possible values + static const AbsTime BEGINNING_OF_TIME; + static const AbsTime END_OF_TIME; + + static AbsTime now(); + + AbsTime() : _time(0ULL) {} + explicit AbsTime(uint64_t t) : _time(t) {} + + bool operator==(const AbsTime& rhs) const { return this->_time == rhs._time; } + bool operator!=(const AbsTime &rhs) const { return !(*this == rhs); } + + bool operator<(const AbsTime& rhs) const { return this->_time < rhs._time; } + bool operator<=(const AbsTime& rhs) const { return this->_time <= rhs._time; } + bool operator>(const AbsTime& rhs) const { return this->_time > rhs._time; } + bool operator>=(const AbsTime& rhs) const { return this->_time >= rhs._time; } + + // We do not want to be able to mutate AbsTime(s) + // without type enforcement, but it is useful to be able + // to say "if (time == 0) {}", so we have value based + // operators for comparison + bool operator==(uint64_t value) const { return this->_time == value; } + bool operator!=(uint64_t value) const { return !(*this == value); } + + bool operator<(uint64_t value) const { return this->_time < value; } + bool operator<=(uint64_t value) const { return this->_time <= value; } + bool operator>(uint64_t value) const { return this->_time > value; } + bool operator>=(uint64_t value) const { return this->_time >= value; } + + AbsTime operator+(const AbsTime& rhs) const { return AbsTime(_time + rhs._time); } + AbsTime operator-(const AbsTime& rhs) const { return AbsTime(_time - rhs._time); } + AbsTime operator*(const AbsTime& rhs) const { return AbsTime(_time * rhs._time); } + AbsTime operator/(const AbsTime& rhs) const { return AbsTime(_time / rhs._time); } + + AbsTime& operator+=(const AbsTime& rhs) { _time += rhs._time; return *this; } + + NanoTime nano_time() const; // NOTE! Uses system mach_timebase_info, potentially expensive conversion costs. + NanoTime nano_time(mach_timebase_info_data_t timebase_info) const; + + uint64_t value() const { return _time; } + double double_value() const { return (double)_time; } +}; + +#endif /* defined(__CPPUtil__UtilAbsTime__) */