blob: 002912cc9f291568fbddfe797dc24de78d21c7a8 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Junxiao Shi5bcab562017-07-16 17:19:14 +00002/*
3 * Copyright (c) 2013-2017 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
Junxiao Shi5bcab562017-07-16 17:19:14 +000026#ifndef NDN_UTIL_DETAIL_MONOTONIC_DEADLINE_TIMER_HPP
27#define NDN_UTIL_DETAIL_MONOTONIC_DEADLINE_TIMER_HPP
Alexander Afanasyevf6468892014-01-29 01:04:14 -080028
Junxiao Shi5bcab562017-07-16 17:19:14 +000029#include "../time.hpp"
Davide Pesavento537dc3a2016-02-18 19:35:26 +010030#include <boost/asio/basic_deadline_timer.hpp>
Junxiao Shi5bcab562017-07-16 17:19:14 +000031#include <boost/asio/io_service.hpp>
Davide Pesavento537dc3a2016-02-18 19:35:26 +010032
Alexander Afanasyevf6468892014-01-29 01:04:14 -080033namespace boost {
34namespace asio {
35
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080036template<>
37struct time_traits<ndn::time::steady_clock>
Alexander Afanasyevf6468892014-01-29 01:04:14 -080038{
Junxiao Shi5bcab562017-07-16 17:19:14 +000039 using time_type = ndn::time::steady_clock::TimePoint;
40 using duration_type = ndn::time::steady_clock::Duration;
Alexander Afanasyevf6468892014-01-29 01:04:14 -080041
42 static time_type
43 now()
44 {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070045 return ndn::time::steady_clock::now();
Alexander Afanasyevf6468892014-01-29 01:04:14 -080046 }
47
48 static time_type
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070049 add(const time_type& time, const duration_type& duration)
Alexander Afanasyevf6468892014-01-29 01:04:14 -080050 {
51 return time + duration;
52 }
53
54 static duration_type
55 subtract(const time_type& timeLhs, const time_type& timeRhs)
56 {
57 return timeLhs - timeRhs;
58 }
59
60 static bool
61 less_than(const time_type& timeLhs, const time_type& timeRhs)
62 {
63 return timeLhs < timeRhs;
64 }
65
66 static boost::posix_time::time_duration
67 to_posix_duration(const duration_type& duration)
68 {
Alexander Afanasyev85b17b82014-11-10 16:22:05 -080069 return ndn::time::steady_clock::to_posix_duration(duration);
Alexander Afanasyevf6468892014-01-29 01:04:14 -080070 }
71};
72
Alexander Afanasyevf6468892014-01-29 01:04:14 -080073} // namespace asio
74} // namespace boost
75
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070076namespace ndn {
Junxiao Shi5bcab562017-07-16 17:19:14 +000077namespace util {
78namespace detail {
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070079
Junxiao Shi5bcab562017-07-16 17:19:14 +000080class MonotonicDeadlineTimer : public boost::asio::basic_deadline_timer<time::steady_clock>
81{
82public:
83 MonotonicDeadlineTimer(boost::asio::io_service& ioService)
84 : boost::asio::basic_deadline_timer<time::steady_clock>(ioService)
85 {
86 }
87};
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070088
Junxiao Shi5bcab562017-07-16 17:19:14 +000089} // namespace detail
90} // namespace util
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070091} // namespace ndn
92
Junxiao Shi5bcab562017-07-16 17:19:14 +000093#endif // NDN_UTIL_DETAIL_MONOTONIC_DEADLINE_TIMER_HPP