]> git.saurik.com Git - apple/system_cmds.git/blob - CPPUtil/UtilNanoTime.hpp
system_cmds-671.10.3.tar.gz
[apple/system_cmds.git] / CPPUtil / UtilNanoTime.hpp
1 //
2 // UtilNanoTime.hpp
3 // CPPUtil
4 //
5 // Created by James McIlree on 4/14/13.
6 // Copyright (c) 2013 Apple. All rights reserved.
7 //
8
9 #ifndef __CPPUtil__UtilNanoTime__
10 #define __CPPUtil__UtilNanoTime__
11
12 class NanoTime {
13 protected:
14 uint64_t _time;
15
16 public:
17 NanoTime() : _time(0ULL) {}
18 NanoTime(uint64_t t) : _time(t) {}
19
20 bool operator==(const NanoTime& rhs) const { return this->_time == rhs._time; }
21 bool operator!=(const NanoTime &rhs) const { return !(*this == rhs); }
22
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; }
27
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); }
34
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; }
39
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); }
44
45 NanoTime& operator+=(const NanoTime& rhs) { _time += rhs._time; return *this; }
46
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);
50 }
51
52 uint64_t value() const { return _time; }
53 double double_value() const { return (double)_time; }
54 };
55
56
57 #endif /* defined(__CPPUtil__UtilNanoTime__) */