time.h revision 58d03812b7cce3a90c39cd30bbff6531cc3e6e30
1/* 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17#ifndef CHRE_UTIL_TIME_H_ 18#define CHRE_UTIL_TIME_H_ 19 20#include <cstdint> 21 22namespace chre { 23 24// Forward declare classes for unit-conversion constructors. 25class Milliseconds; 26class Microseconds; 27class Nanoseconds; 28 29 30class Seconds { 31 public: 32 /** 33 * Construct a Seconds time duration given a value. 34 */ 35 constexpr explicit Seconds(uint64_t seconds); 36 37 /** 38 * Converts the underlying seconds to a raw uint64_t representation of 39 * nanoseconds. Handles overflyw by returning UINT64_MAX. 40 * 41 * @return the value of seconds converted to nanoseconds 42 */ 43 constexpr uint64_t toRawNanoseconds() const; 44 45 private: 46 uint64_t mSeconds; 47}; 48 49/** 50 * Represents a duration of time in milliseconds. 51 */ 52class Milliseconds { 53 public: 54 /** 55 * Construct a Milliseconds time duration given a value. 56 */ 57 constexpr explicit Milliseconds(uint64_t milliseconds); 58 59 /** 60 * Constructs a Microseconds time duration given nanoseconds. 61 */ 62 constexpr Milliseconds(Nanoseconds nanoseconds); 63 64 /** 65 * Converts the underlying milliseconds to a raw uint64_t representation of 66 * nanoseconds. Handles overflow by returning UINT64_MAX. 67 * 68 * @return the value of milliseconds converted to nanoseconds 69 */ 70 constexpr uint64_t toRawNanoseconds() const; 71 72 /** 73 * Obtains the number of Milliseconds stroed by this time duration. 74 * 75 * @return the value of milliseconds. 76 */ 77 constexpr uint64_t getMilliseconds() const; 78 79 private: 80 //! Store the time duration. 81 uint64_t mMilliseconds; 82}; 83 84/** 85 * Represents a duration of time in microseconds. 86 */ 87class Microseconds { 88 public: 89 /** 90 * Construct a Microseconds time duration given a value. 91 */ 92 constexpr explicit Microseconds(uint64_t microseconds); 93 94 /** 95 * Constructs a Microseconds time duration given nanoseconds. 96 */ 97 constexpr Microseconds(Nanoseconds nanoseconds); 98 99 /** 100 * Converts the underlying microseconds to a raw uint64_t representation of 101 * nanoseconds. Handles overflow by returning UINT64_MAX. 102 * 103 * @return the value of microseconds converted to nanoseconds. 104 */ 105 constexpr uint64_t toRawNanoseconds() const; 106 107 /* 108 * Obtains the number of Microseconds stroed by this time duration. 109 * 110 * @return the value of microseconds. 111 */ 112 constexpr uint64_t getMicroseconds() const; 113 114 private: 115 //! Store the time duration. 116 uint64_t mMicroseconds; 117}; 118 119/** 120 * Represents a duration of time in nanoseconds. 121 */ 122class Nanoseconds { 123 public: 124 /** 125 * Default constructs a Nanoseconds time duration to zero. 126 */ 127 constexpr Nanoseconds(); 128 129 /** 130 * Constructs a Nanoseconds time duration given a value. 131 */ 132 constexpr explicit Nanoseconds(uint64_t nanoseconds); 133 134 /** 135 * Converts a seconds value to nanoseconds. 136 */ 137 constexpr Nanoseconds(Seconds seconds); 138 139 /** 140 * Converts a milliseconds value to nanoseconds. 141 */ 142 constexpr Nanoseconds(Milliseconds milliseconds); 143 144 /** 145 * Constructs a Nanoseconds time duration given microseconds. 146 */ 147 constexpr Nanoseconds(Microseconds microseconds); 148 149 /** 150 * Converts the underlying nanoseconds to a raw uint64_t representation of 151 * nanoseconds. 152 * 153 * @return the value of nanoseconds 154 */ 155 constexpr uint64_t toRawNanoseconds() const; 156 157 /** 158 * Performs an equality comparison to another Nanoseconds value. 159 * 160 * @return Returns true if this nanoseconds object is equal to another. 161 */ 162 constexpr bool operator==(const Nanoseconds& nanos) const; 163 164 /** 165 * Performs an inequality comparison to another Nanoseconds value. 166 * 167 * @return Returns true if this nanoseconds object is not equal to another. 168 */ 169 constexpr bool operator!=(const Nanoseconds& nanos) const; 170 171 private: 172 uint64_t mNanoseconds; 173}; 174 175/** 176 * Add seconds to nanoseconds. 177 * 178 * @param seconds the seconds duration 179 * @param nanoseconds the nanoseconds duration 180 * @return the added time quantity expressed in nanoseconds 181 */ 182constexpr Nanoseconds operator+(const Seconds& seconds, 183 const Nanoseconds& nanoseconds); 184 185/** 186 * Add nanoseconds to nanoseconds. 187 * 188 * @param nanos_a The first nanoseconds duration 189 * @param nanos_b The second nanoseconds duration 190 * @return The added time quantity expressed in nanoseconds 191 */ 192constexpr Nanoseconds operator+(const Nanoseconds& nanos_a, 193 const Nanoseconds& nanos_b); 194 195/** 196 * Subtract two nanosecond durations. 197 * 198 * @param nanos_a the first nanoseconds duration 199 * @param nanos_b the second nanoseconds duration 200 * @return the difference between the two durations 201 */ 202constexpr Nanoseconds operator-(const Nanoseconds& nanos_a, 203 const Nanoseconds& nanos_b); 204 205/** 206 * Performs a greater than or equal to comparison on two nanoseconds values. 207 * 208 * @param nanos_a the first nanoseconds duration 209 * @param nanos_b the second nanoseconds duration 210 * @return Whether nanos_a is greater than or equal to nanos_b. 211 */ 212constexpr bool operator>=(const Nanoseconds& nanos_a, 213 const Nanoseconds& nanos_b); 214 215/** 216 * Performs a less than comparison on two nanoseconds values. 217 * 218 * @param nanos_a the first nanoseconds duration 219 * @param nanos_b the second nanoseconds duration 220 * @return Whether nanos_a is less than nanos_b. 221 */ 222constexpr bool operator<(const Nanoseconds& nanos_a, 223 const Nanoseconds& nanos_b); 224 225/** 226 * Performs a greater than comparison on two nanoseconds values. 227 * 228 * @param nanos_a the first nanoseconds duration 229 * @param nanos_b the second nanoseconds duration 230 * @return Whether nanos_a is less than nanos_b. 231 */ 232constexpr bool operator>(const Nanoseconds& nanos_a, 233 const Nanoseconds& nanos_b); 234 235} // namespace chre 236 237#include "time_impl.h" 238 239#endif // CHRE_UTIL_TIME_H_ 240