blob: 3328a86b713031e5683d449a3a47f69da5c25b81 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyevf6468892014-01-29 01:04:14 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevf6468892014-01-29 01:04:14 -080020 */
21
22/**
23 * This code is based on https://svn.boost.org/trac/boost/attachment/ticket/3504/MonotonicDeadlineTimer.h
24 */
25
26#ifndef NDN_UTIL_MONOTONIC_DEADLINE_TIMER_HPP
27#define NDN_UTIL_MONOTONIC_DEADLINE_TIMER_HPP
28
29#include <boost/asio.hpp>
30#include "time.hpp"
31
32namespace boost {
33namespace asio {
34
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070035template <>
36struct time_traits<ndn::time::steady_clock::TimePoint::clock>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080037{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070038 typedef ndn::time::steady_clock::TimePoint time_type;
39 typedef ndn::time::steady_clock::TimePoint::clock::duration duration_type;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080040
41 static time_type
42 now()
43 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070044 return ndn::time::steady_clock::now();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080045 }
46
47 static time_type
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070048 add(const time_type& time, const duration_type& duration)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080049 {
50 return time + duration;
51 }
52
53 static duration_type
54 subtract(const time_type& timeLhs, const time_type& timeRhs)
55 {
56 return timeLhs - timeRhs;
57 }
58
59 static bool
60 less_than(const time_type& timeLhs, const time_type& timeRhs)
61 {
62 return timeLhs < timeRhs;
63 }
64
65 static boost::posix_time::time_duration
66 to_posix_duration(const duration_type& duration)
67 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070068 return
69#ifdef BOOST_DATE_TIME_HAS_NANOSECONDS
70 boost::posix_time::nanoseconds(
71 ndn::time::duration_cast<ndn::time::nanoseconds>(duration).count())
72#else
73 boost::posix_time::microseconds(
74 ndn::time::duration_cast<ndn::time::microseconds>(duration).count())
75#endif
76 ;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080077 }
78};
79
Alexander Afanasyevf6468892014-01-29 01:04:14 -080080} // namespace asio
81} // namespace boost
82
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070083namespace ndn {
84
85typedef boost::asio::basic_deadline_timer<time::steady_clock::TimePoint::clock> monotonic_deadline_timer;
86
87} // namespace ndn
88
Alexander Afanasyevf6468892014-01-29 01:04:14 -080089#endif // NDN_UTIL_MONOTONIC_DEADLINE_TIMER_HPP