5 // Created by James McIlree on 4/14/13.
6 // Copyright (c) 2013 Apple. All rights reserved.
9 #ifndef __CPPUtil__UtilNanoTime__
10 #define __CPPUtil__UtilNanoTime__
17 NanoTime() : _time(0ULL) {}
18 NanoTime(uint64_t t) : _time(t) {}
20 bool operator==(const NanoTime& rhs) const { return this->_time == rhs._time; }
21 bool operator!=(const NanoTime &rhs) const { return !(*this == rhs); }
23 bool operator<(const NanoTime& rhs) const { return this->_time < rhs._time; }
24 bool operator<=(const NanoTime& rhs) const { return this->_time <= rhs._time; }
25 bool operator>(const NanoTime& rhs) const { return this->_time > rhs._time; }
26 bool operator>=(const NanoTime& rhs) const { return this->_time >= rhs._time; }
28 // We do not want to be able to mutate NanoTime(s)
29 // without type enforcement, but it is useful to be able
30 // to say "if (time == 0) {}", so we have value based
31 // operators for comparison
32 bool operator==(uint64_t value) const { return this->_time == value; }
33 bool operator!=(uint64_t value) const { return !(*this == value); }
35 bool operator<(uint64_t value) const { return this->_time < value; }
36 bool operator<=(uint64_t value) const { return this->_time <= value; }
37 bool operator>(uint64_t value) const { return this->_time > value; }
38 bool operator>=(uint64_t value) const { return this->_time >= value; }
40 NanoTime operator+(const NanoTime& rhs) const { return NanoTime(_time + rhs._time); }
41 NanoTime operator-(const NanoTime& rhs) const { return NanoTime(_time - rhs._time); }
42 NanoTime operator*(const NanoTime& rhs) const { return NanoTime(_time * rhs._time); }
43 NanoTime operator/(const NanoTime& rhs) const { return NanoTime(_time / rhs._time); }
45 NanoTime& operator+=(const NanoTime& rhs) { _time += rhs._time; return *this; }
47 AbsTime abs_time() const; // NOTE! Uses system mach_timebase_info, potentially expensive conversion costs.
48 AbsTime abs_time(mach_timebase_info_data_t timebase_info) const {
49 return AbsTime(_time * timebase_info.denom / timebase_info.numer);
52 uint64_t value() const { return _time; }
53 double double_value() const { return (double)_time; }
57 #endif /* defined(__CPPUtil__UtilNanoTime__) */