blob: 5efd082ab7228da48e58fd5fc70806bae24f858c [file] [log] [blame]
Junxiao Shi61c5ef32014-01-24 20:59:30 -07001/* -*- 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
Alexander Afanasyev18bbf812014-01-29 01:40:23 -080012namespace nfd {
Junxiao Shi61c5ef32014-01-24 20:59:30 -070013namespace time {
14
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080015class monotonic_clock;
16
Junxiao Shi61c5ef32014-01-24 20:59:30 -070017/** \class Duration
18 * \brief represents a time interval
19 * Time unit is nanosecond.
20 */
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080021class Duration
22{
23public:
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
57private:
58 int64_t m_value;
59};
Junxiao Shi61c5ef32014-01-24 20:59:30 -070060
61/** \class Point
62 * \brief represents a point in time
63 * This uses monotonic clock.
64 */
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080065class Point
66{
67public:
68 Point()
69 : m_value(0)
70 {
71 }
Junxiao Shi61c5ef32014-01-24 20:59:30 -070072
Alexander Afanasyev920af2f2014-01-25 22:56:11 -080073 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
107private:
108 int64_t m_value;
109};
110
111/**
112 * \brief Get current time
113 * \return{ the current time in monotonic clock }
114 */
Junxiao Shi61c5ef32014-01-24 20:59:30 -0700115Point
116now();
117
Alexander Afanasyev920af2f2014-01-25 22:56:11 -0800118/**
119 * \brief Get time::Duration for the specified number of seconds
120 */
121template<class T>
122inline Duration
123seconds(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 */
131template<class T>
132inline Duration
133milliseconds(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 */
141template<class T>
142inline Duration
143microseconds(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 */
151inline Duration
152nanoseconds(int64_t value)
153{
154 return Duration(value);
155}
156
157
Junxiao Shi61c5ef32014-01-24 20:59:30 -0700158} // namespace time
Alexander Afanasyev18bbf812014-01-29 01:40:23 -0800159} // namespace nfd
Junxiao Shi61c5ef32014-01-24 20:59:30 -0700160
161#endif // NFD_CORE_TIME_H