Junxiao Shi | 61c5ef3 | 2014-01-24 20:59:30 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #ifndef NFD_CORE_TIME_H |
| 8 | #define NFD_CORE_TIME_H |
| 9 | |
| 10 | #include "common.hpp" |
| 11 | |
| 12 | namespace ndn { |
| 13 | namespace time { |
| 14 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame^] | 15 | class monotonic_clock; |
| 16 | |
Junxiao Shi | 61c5ef3 | 2014-01-24 20:59:30 -0700 | [diff] [blame] | 17 | /** \class Duration |
| 18 | * \brief represents a time interval |
| 19 | * Time unit is nanosecond. |
| 20 | */ |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame^] | 21 | class Duration |
| 22 | { |
| 23 | public: |
| 24 | Duration() |
| 25 | : m_value(0) |
| 26 | { |
| 27 | } |
| 28 | |
| 29 | explicit |
| 30 | Duration(int64_t value) |
| 31 | : m_value(value) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | operator int64_t&() |
| 36 | { |
| 37 | return m_value; |
| 38 | } |
| 39 | |
| 40 | operator const int64_t&() const |
| 41 | { |
| 42 | return m_value; |
| 43 | } |
| 44 | |
| 45 | Duration |
| 46 | operator+(const Duration& other) const |
| 47 | { |
| 48 | return Duration(this->m_value + other.m_value); |
| 49 | } |
| 50 | |
| 51 | Duration |
| 52 | operator-(const Duration& other) const |
| 53 | { |
| 54 | return Duration(this->m_value - other.m_value); |
| 55 | } |
| 56 | |
| 57 | private: |
| 58 | int64_t m_value; |
| 59 | }; |
Junxiao Shi | 61c5ef3 | 2014-01-24 20:59:30 -0700 | [diff] [blame] | 60 | |
| 61 | /** \class Point |
| 62 | * \brief represents a point in time |
| 63 | * This uses monotonic clock. |
| 64 | */ |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame^] | 65 | class Point |
| 66 | { |
| 67 | public: |
| 68 | Point() |
| 69 | : m_value(0) |
| 70 | { |
| 71 | } |
Junxiao Shi | 61c5ef3 | 2014-01-24 20:59:30 -0700 | [diff] [blame] | 72 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame^] | 73 | explicit |
| 74 | Point(int64_t value) |
| 75 | : m_value(value) |
| 76 | { |
| 77 | } |
| 78 | |
| 79 | operator int64_t&() |
| 80 | { |
| 81 | return m_value; |
| 82 | } |
| 83 | |
| 84 | operator const int64_t&() const |
| 85 | { |
| 86 | return m_value; |
| 87 | } |
| 88 | |
| 89 | Point |
| 90 | operator+(const Duration& other) const |
| 91 | { |
| 92 | return Point(this->m_value + static_cast<int64_t>(other)); |
| 93 | } |
| 94 | |
| 95 | Duration |
| 96 | operator-(const Point& other) const |
| 97 | { |
| 98 | return Duration(this->m_value - other.m_value); |
| 99 | } |
| 100 | |
| 101 | Point |
| 102 | operator-(const Duration& other) const |
| 103 | { |
| 104 | return Point(this->m_value - static_cast<int64_t>(other)); |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | int64_t m_value; |
| 109 | }; |
| 110 | |
| 111 | /** |
| 112 | * \brief Get current time |
| 113 | * \return{ the current time in monotonic clock } |
| 114 | */ |
Junxiao Shi | 61c5ef3 | 2014-01-24 20:59:30 -0700 | [diff] [blame] | 115 | Point |
| 116 | now(); |
| 117 | |
Alexander Afanasyev | 920af2f | 2014-01-25 22:56:11 -0800 | [diff] [blame^] | 118 | /** |
| 119 | * \brief Get time::Duration for the specified number of seconds |
| 120 | */ |
| 121 | template<class T> |
| 122 | inline Duration |
| 123 | seconds(T value) |
| 124 | { |
| 125 | return Duration(value * static_cast<int64_t>(1000000000)); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * \brief Get time::Duration for the specified number of milliseconds |
| 130 | */ |
| 131 | template<class T> |
| 132 | inline Duration |
| 133 | milliseconds(T value) |
| 134 | { |
| 135 | return Duration(value * static_cast<int64_t>(1000000)); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * \brief Get time::Duration for the specified number of microseconds |
| 140 | */ |
| 141 | template<class T> |
| 142 | inline Duration |
| 143 | microseconds(T value) |
| 144 | { |
| 145 | return Duration(value * static_cast<int64_t>(1000)); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * \brief Get time::Duration for the specified number of nanoseconds |
| 150 | */ |
| 151 | inline Duration |
| 152 | nanoseconds(int64_t value) |
| 153 | { |
| 154 | return Duration(value); |
| 155 | } |
| 156 | |
| 157 | |
Junxiao Shi | 61c5ef3 | 2014-01-24 20:59:30 -0700 | [diff] [blame] | 158 | } // namespace time |
| 159 | } // namespace ndn |
| 160 | |
| 161 | #endif // NFD_CORE_TIME_H |