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