chrono_time_traits.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // detail/chrono_time_traits.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef BOOST_ASIO_DETAIL_CHRONO_TIME_TRAITS_HPP
  11. #define BOOST_ASIO_DETAIL_CHRONO_TIME_TRAITS_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/detail/cstdint.hpp>
  16. #include <boost/asio/detail/push_options.hpp>
  17. namespace boost {
  18. namespace asio {
  19. namespace detail {
  20. // Adapts std::chrono clocks for use with a deadline timer.
  21. template <typename Clock, typename WaitTraits>
  22. struct chrono_time_traits
  23. {
  24. // The clock type.
  25. typedef Clock clock_type;
  26. // The duration type of the clock.
  27. typedef typename clock_type::duration duration_type;
  28. // The time point type of the clock.
  29. typedef typename clock_type::time_point time_type;
  30. // The period of the clock.
  31. typedef typename duration_type::period period_type;
  32. // Get the current time.
  33. static time_type now()
  34. {
  35. return clock_type::now();
  36. }
  37. // Add a duration to a time.
  38. static time_type add(const time_type& t, const duration_type& d)
  39. {
  40. const time_type epoch;
  41. if (t >= epoch)
  42. {
  43. if ((time_type::max)() - t < d)
  44. return (time_type::max)();
  45. }
  46. else // t < epoch
  47. {
  48. if (-(t - (time_type::min)()) > d)
  49. return (time_type::min)();
  50. }
  51. return t + d;
  52. }
  53. // Subtract one time from another.
  54. static duration_type subtract(const time_type& t1, const time_type& t2)
  55. {
  56. const time_type epoch;
  57. if (t1 >= epoch)
  58. {
  59. if (t2 >= epoch)
  60. {
  61. return t1 - t2;
  62. }
  63. else if (t2 == (time_type::min)())
  64. {
  65. return (duration_type::max)();
  66. }
  67. else if ((time_type::max)() - t1 < epoch - t2)
  68. {
  69. return (duration_type::max)();
  70. }
  71. else
  72. {
  73. return t1 - t2;
  74. }
  75. }
  76. else // t1 < epoch
  77. {
  78. if (t2 < epoch)
  79. {
  80. return t1 - t2;
  81. }
  82. else if (t1 == (time_type::min)())
  83. {
  84. return (duration_type::min)();
  85. }
  86. else if ((time_type::max)() - t2 < epoch - t1)
  87. {
  88. return (duration_type::min)();
  89. }
  90. else
  91. {
  92. return -(t2 - t1);
  93. }
  94. }
  95. }
  96. // Test whether one time is less than another.
  97. static bool less_than(const time_type& t1, const time_type& t2)
  98. {
  99. return t1 < t2;
  100. }
  101. // Implement just enough of the posix_time::time_duration interface to supply
  102. // what the timer_queue requires.
  103. class posix_time_duration
  104. {
  105. public:
  106. explicit posix_time_duration(const duration_type& d)
  107. : d_(d)
  108. {
  109. }
  110. int64_t ticks() const
  111. {
  112. return d_.count();
  113. }
  114. int64_t total_seconds() const
  115. {
  116. return duration_cast<1, 1>();
  117. }
  118. int64_t total_milliseconds() const
  119. {
  120. return duration_cast<1, 1000>();
  121. }
  122. int64_t total_microseconds() const
  123. {
  124. return duration_cast<1, 1000000>();
  125. }
  126. private:
  127. template <int64_t Num, int64_t Den>
  128. int64_t duration_cast() const
  129. {
  130. const int64_t num = period_type::num * Den;
  131. const int64_t den = period_type::den * Num;
  132. if (num == 1 && den == 1)
  133. return ticks();
  134. else if (num != 1 && den == 1)
  135. return ticks() * num;
  136. else if (num == 1 && period_type::den != 1)
  137. return ticks() / den;
  138. else
  139. return ticks() * num / den;
  140. }
  141. duration_type d_;
  142. };
  143. // Convert to POSIX duration type.
  144. static posix_time_duration to_posix_duration(const duration_type& d)
  145. {
  146. return posix_time_duration(WaitTraits::to_wait_duration(d));
  147. }
  148. };
  149. } // namespace detail
  150. } // namespace asio
  151. } // namespace boost
  152. #include <boost/asio/detail/pop_options.hpp>
  153. #endif // BOOST_ASIO_DETAIL_CHRONO_TIME_TRAITS_HPP