time.h revision c1893d235b674f8a859a9e53aad406b5100c725d
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   * Default constructs a milliseconds time duration to zero.
56   */
57  constexpr Milliseconds();
58
59  /**
60   * Construct a Milliseconds time duration given a value.
61   */
62  constexpr explicit Milliseconds(uint64_t milliseconds);
63
64  /**
65   * Constructs a Microseconds time duration given nanoseconds.
66   */
67  constexpr Milliseconds(Nanoseconds nanoseconds);
68
69  /**
70   * Converts the underlying milliseconds to a raw uint64_t representation of
71   * nanoseconds. Handles overflow by returning UINT64_MAX.
72   *
73   * @return the value of milliseconds converted to nanoseconds
74   */
75  constexpr uint64_t toRawNanoseconds() const;
76
77  /**
78   * Obtains the number of Milliseconds stroed by this time duration.
79   *
80   * @return the value of milliseconds.
81   */
82  constexpr uint64_t getMilliseconds() const;
83
84  /**
85   * Performs an equality comparison to another Milliseconds value.
86   *
87   * @return Returns true if this milliseconds object is equal to another.
88   */
89  constexpr bool operator==(const Milliseconds& millis) const;
90
91 private:
92  //! Store the time duration.
93  uint64_t mMilliseconds;
94};
95
96/**
97 * Represents a duration of time in microseconds.
98 */
99class Microseconds {
100 public:
101  /**
102   * Construct a Microseconds time duration given a value.
103   */
104  constexpr explicit Microseconds(uint64_t microseconds);
105
106  /**
107   * Constructs a Microseconds time duration given nanoseconds.
108   */
109  constexpr Microseconds(Nanoseconds nanoseconds);
110
111  /**
112   * Converts the underlying microseconds to a raw uint64_t representation of
113   * nanoseconds. Handles overflow by returning UINT64_MAX.
114   *
115   * @return the value of microseconds converted to nanoseconds.
116   */
117  constexpr uint64_t toRawNanoseconds() const;
118
119  /*
120   * Obtains the number of Microseconds stroed by this time duration.
121   *
122   * @return the value of microseconds.
123   */
124  constexpr uint64_t getMicroseconds() const;
125
126 private:
127  //! Store the time duration.
128  uint64_t mMicroseconds;
129};
130
131/**
132 * Represents a duration of time in nanoseconds.
133 */
134class Nanoseconds {
135 public:
136  /**
137   * Default constructs a Nanoseconds time duration to zero.
138   */
139  constexpr Nanoseconds();
140
141  /**
142   * Constructs a Nanoseconds time duration given a value.
143   */
144  constexpr explicit Nanoseconds(uint64_t nanoseconds);
145
146  /**
147   * Converts a seconds value to nanoseconds.
148   */
149  constexpr Nanoseconds(Seconds seconds);
150
151  /**
152   * Converts a milliseconds value to nanoseconds.
153   */
154  constexpr Nanoseconds(Milliseconds milliseconds);
155
156  /**
157   * Constructs a Nanoseconds time duration given microseconds.
158   */
159  constexpr Nanoseconds(Microseconds microseconds);
160
161  /**
162   * Converts the underlying nanoseconds to a raw uint64_t representation of
163   * nanoseconds.
164   *
165   * @return the value of nanoseconds
166   */
167  constexpr uint64_t toRawNanoseconds() const;
168
169  /**
170   * Performs an equality comparison to another Nanoseconds value.
171   *
172   * @return Returns true if this nanoseconds object is equal to another.
173   */
174  constexpr bool operator==(const Nanoseconds& nanos) const;
175
176  /**
177   * Performs an inequality comparison to another Nanoseconds value.
178   *
179   * @return Returns true if this nanoseconds object is not equal to another.
180   */
181  constexpr bool operator!=(const Nanoseconds& nanos) const;
182
183 private:
184  uint64_t mNanoseconds;
185};
186
187/**
188 * Add seconds to nanoseconds.
189 *
190 * @param seconds the seconds duration
191 * @param nanoseconds the nanoseconds duration
192 * @return the added time quantity expressed in nanoseconds
193 */
194constexpr Nanoseconds operator+(const Seconds& seconds,
195                                const Nanoseconds& nanoseconds);
196
197/**
198 * Add nanoseconds to nanoseconds.
199 *
200 * @param nanos_a The first nanoseconds duration
201 * @param nanos_b The second nanoseconds duration
202 * @return The added time quantity expressed in nanoseconds
203 */
204constexpr Nanoseconds operator+(const Nanoseconds& nanos_a,
205                                const Nanoseconds& nanos_b);
206
207/**
208 * Subtract two nanosecond durations.
209 *
210 * @param nanos_a the first nanoseconds duration
211 * @param nanos_b the second nanoseconds duration
212 * @return the difference between the two durations
213 */
214constexpr Nanoseconds operator-(const Nanoseconds& nanos_a,
215                                const Nanoseconds& nanos_b);
216
217/**
218 * Performs a greater than or equal to comparison on two nanoseconds values.
219 *
220 * @param nanos_a the first nanoseconds duration
221 * @param nanos_b the second nanoseconds duration
222 * @return Whether nanos_a is greater than or equal to nanos_b.
223 */
224constexpr bool operator>=(const Nanoseconds& nanos_a,
225                          const Nanoseconds& nanos_b);
226
227/**
228 * Performs a less than comparison on two nanoseconds values.
229 *
230 * @param nanos_a the first nanoseconds duration
231 * @param nanos_b the second nanoseconds duration
232 * @return Whether nanos_a is less than nanos_b.
233 */
234constexpr bool operator<(const Nanoseconds& nanos_a,
235                         const Nanoseconds& nanos_b);
236
237/**
238 * Performs a greater than comparison on two nanoseconds values.
239 *
240 * @param nanos_a the first nanoseconds duration
241 * @param nanos_b the second nanoseconds duration
242 * @return Whether nanos_a is less than nanos_b.
243 */
244constexpr bool operator>(const Nanoseconds& nanos_a,
245                         const Nanoseconds& nanos_b);
246
247}  // namespace chre
248
249#include "time_impl.h"
250
251#endif // CHRE_UTIL_TIME_H_
252