blob: 4147d51dd8eedf8f70d0d101b55077d1ed8b4bb2 [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyevf6468892014-01-29 01:04:14 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
Alexander Afanasyevf6468892014-01-29 01:04:14 -080011 */
12
13/**
14 * This code is based on https://svn.boost.org/trac/boost/attachment/ticket/3504/MonotonicDeadlineTimer.h
15 */
16
17#ifndef NDN_UTIL_MONOTONIC_DEADLINE_TIMER_HPP
18#define NDN_UTIL_MONOTONIC_DEADLINE_TIMER_HPP
19
20#include <boost/asio.hpp>
21#include "time.hpp"
22
23namespace boost {
24namespace asio {
25
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070026template <>
27struct time_traits<ndn::time::steady_clock::TimePoint::clock>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080028{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070029 typedef ndn::time::steady_clock::TimePoint time_type;
30 typedef ndn::time::steady_clock::TimePoint::clock::duration duration_type;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080031
32 static time_type
33 now()
34 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070035 return ndn::time::steady_clock::now();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080036 }
37
38 static time_type
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070039 add(const time_type& time, const duration_type& duration)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080040 {
41 return time + duration;
42 }
43
44 static duration_type
45 subtract(const time_type& timeLhs, const time_type& timeRhs)
46 {
47 return timeLhs - timeRhs;
48 }
49
50 static bool
51 less_than(const time_type& timeLhs, const time_type& timeRhs)
52 {
53 return timeLhs < timeRhs;
54 }
55
56 static boost::posix_time::time_duration
57 to_posix_duration(const duration_type& duration)
58 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070059 return
60#ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
61 boost::posix_time::nanoseconds(
62 ndn::time::duration_cast<ndn::time::nanoseconds>(duration).count())
63#else
64 boost::posix_time::microseconds(
65 ndn::time::duration_cast<ndn::time::microseconds>(duration).count())
66#endif
67 ;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080068 }
69};
70
Alexander Afanasyevf6468892014-01-29 01:04:14 -080071} // namespace asio
72} // namespace boost
73
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070074namespace ndn {
75
76typedef boost::asio::basic_deadline_timer<time::steady_clock::TimePoint::clock> monotonic_deadline_timer;
77
78} // namespace ndn
79
Alexander Afanasyevf6468892014-01-29 01:04:14 -080080#endif // NDN_UTIL_MONOTONIC_DEADLINE_TIMER_HPP