Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 3 | * 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 Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 11 | */ |
| 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 | |
| 23 | namespace boost { |
| 24 | namespace asio { |
| 25 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 26 | template <> |
| 27 | struct time_traits<ndn::time::steady_clock::TimePoint::clock> |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 28 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 29 | typedef ndn::time::steady_clock::TimePoint time_type; |
| 30 | typedef ndn::time::steady_clock::TimePoint::clock::duration duration_type; |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 31 | |
| 32 | static time_type |
| 33 | now() |
| 34 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 35 | return ndn::time::steady_clock::now(); |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | static time_type |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 39 | add(const time_type& time, const duration_type& duration) |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 40 | { |
| 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 Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 59 | 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 Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 68 | } |
| 69 | }; |
| 70 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 71 | } // namespace asio |
| 72 | } // namespace boost |
| 73 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 74 | namespace ndn { |
| 75 | |
| 76 | typedef boost::asio::basic_deadline_timer<time::steady_clock::TimePoint::clock> monotonic_deadline_timer; |
| 77 | |
| 78 | } // namespace ndn |
| 79 | |
Alexander Afanasyev | f646889 | 2014-01-29 01:04:14 -0800 | [diff] [blame] | 80 | #endif // NDN_UTIL_MONOTONIC_DEADLINE_TIMER_HPP |