Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 1 | /* -*- 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 | |
| 17 | namespace boost { |
| 18 | namespace asio { |
| 19 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 20 | template <> |
| 21 | struct time_traits<ndn::time::steady_clock::TimePoint::clock> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 22 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 23 | typedef ndn::time::steady_clock::TimePoint time_type; |
| 24 | typedef ndn::time::steady_clock::TimePoint::clock::duration duration_type; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 25 | |
| 26 | static time_type |
| 27 | now() |
| 28 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 29 | return ndn::time::steady_clock::now(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 30 | } |
| 31 | |
| 32 | static time_type |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 33 | add(const time_type& time, const duration_type& duration) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 34 | { |
| 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 Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 53 | 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 Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 62 | } |
| 63 | }; |
| 64 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 65 | } // namespace asio |
| 66 | } // namespace boost |
| 67 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame^] | 68 | namespace ndn { |
| 69 | |
| 70 | typedef boost::asio::basic_deadline_timer<time::steady_clock::TimePoint::clock> monotonic_deadline_timer; |
| 71 | |
| 72 | } // namespace ndn |
| 73 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 74 | #endif // NDN_UTIL_MONOTONIC_DEADLINE_TIMER_HPP |