time.h revision e64f180233e64c40b56993cfea3696c5b4b16395
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
24class Seconds {
25 public:
26  /**
27   * Construct a Seconds time duration given a value.
28   */
29  constexpr explicit Seconds(uint64_t seconds);
30
31  /**
32   * Converts the underlying seconds to a raw uint64_t representation of
33   * nanoseconds. Handles overflyw by returning UINT64_MAX.
34   *
35   * @return the value of seconds converted to nanoseconds
36   */
37  constexpr uint64_t toRawNanoseconds() const;
38
39 private:
40  uint64_t mSeconds;
41};
42
43/**
44 * Represents a duration of time in milliseconds.
45 */
46class Milliseconds {
47 public:
48  /**
49   * Construct a Milliseconds time duration given a value.
50   */
51  constexpr explicit Milliseconds(uint64_t milliseconds);
52
53  /**
54   * Converts the underlying milliseconds to a raw uint64_t representation of
55   * nanoseconds. Handles overflow by returning UINT64_MAX.
56   *
57   * @return the value of milliseconds converted to nanoseconds
58   */
59  constexpr uint64_t toRawNanoseconds() const;
60
61 private:
62  //! Store the time duration.
63  uint64_t mMilliseconds;
64};
65
66/**
67 * Represents a duration of time in nanoseconds.
68 */
69class Nanoseconds {
70 public:
71  /**
72   * Constructs a Nanoseconds time duration given a value.
73   */
74  constexpr explicit Nanoseconds(uint64_t nanoseconds);
75
76  /**
77   * Converts the underlying nanoseconds to a raw uint64_t representation of
78   * nanoseconds.
79   *
80   * @return the value of nanoseconds
81   */
82  constexpr uint64_t toRawNanoseconds() const;
83
84 private:
85  uint64_t mNanoseconds;
86};
87
88/**
89 * Add seconds to nanoseconds.
90 *
91 * @param the seconds duration
92 * @param the nanoseconds duration
93 * @return the added time quantity expressed in nanoseconds
94 */
95constexpr Nanoseconds operator+(const Seconds& seconds,
96                                const Nanoseconds& nanoseconds);
97
98/**
99 * Subtract two nanosecond durations.
100 *
101 * @param the first nanoseconds duration
102 * @param the second nanoseconds duration
103 * @return the difference between the two durations
104 */
105constexpr Nanoseconds operator-(const Nanoseconds& nanos_a,
106                                const Nanoseconds& nanos_b);
107
108}  // namespace chre
109
110#include "time_impl.h"
111
112#endif // CHRE_UTIL_TIME_H_
113