blob: 2b1558d9638dd00b85e96cb3e4bd35d390afc31f [file] [log] [blame]
Alexander Afanasyevf6468892014-01-29 01:04:14 -08001/* -*- 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/**
8 * This code is based on https://svn.boost.org/trac/boost/attachment/ticket/3504/MonotonicDeadlineTimer.h
9 */
10
11#ifndef NDN_UTIL_MONOTONIC_DEADLINE_TIMER_HPP
12#define NDN_UTIL_MONOTONIC_DEADLINE_TIMER_HPP
13
14#include <boost/asio.hpp>
15#include "time.hpp"
16
17namespace boost {
18namespace asio {
19
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070020template <>
21struct time_traits<ndn::time::steady_clock::TimePoint::clock>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080022{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070023 typedef ndn::time::steady_clock::TimePoint time_type;
24 typedef ndn::time::steady_clock::TimePoint::clock::duration duration_type;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080025
26 static time_type
27 now()
28 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070029 return ndn::time::steady_clock::now();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080030 }
31
32 static time_type
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070033 add(const time_type& time, const duration_type& duration)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080034 {
35 return time + duration;
36 }
37
38 static duration_type
39 subtract(const time_type& timeLhs, const time_type& timeRhs)
40 {
41 return timeLhs - timeRhs;
42 }
43
44 static bool
45 less_than(const time_type& timeLhs, const time_type& timeRhs)
46 {
47 return timeLhs < timeRhs;
48 }
49
50 static boost::posix_time::time_duration
51 to_posix_duration(const duration_type& duration)
52 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070053 return
54#ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
55 boost::posix_time::nanoseconds(
56 ndn::time::duration_cast<ndn::time::nanoseconds>(duration).count())
57#else
58 boost::posix_time::microseconds(
59 ndn::time::duration_cast<ndn::time::microseconds>(duration).count())
60#endif
61 ;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080062 }
63};
64
Alexander Afanasyevf6468892014-01-29 01:04:14 -080065} // namespace asio
66} // namespace boost
67
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070068namespace ndn {
69
70typedef boost::asio::basic_deadline_timer<time::steady_clock::TimePoint::clock> monotonic_deadline_timer;
71
72} // namespace ndn
73
Alexander Afanasyevf6468892014-01-29 01:04:14 -080074#endif // NDN_UTIL_MONOTONIC_DEADLINE_TIMER_HPP