chrono_time_traits.hpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. //
  2. // detail/chrono_time_traits.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2018 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. // Helper template to compute the greatest common divisor.
  21. template <int64_t v1, int64_t v2>
  22. struct gcd { enum { value = gcd<v2, v1 % v2>::value }; };
  23. template <int64_t v1>
  24. struct gcd<v1, 0> { enum { value = v1 }; };
  25. // Adapts std::chrono clocks for use with a deadline timer.
  26. template <typename Clock, typename WaitTraits>
  27. struct chrono_time_traits
  28. {
  29. // The clock type.
  30. typedef Clock clock_type;
  31. // The duration type of the clock.
  32. typedef typename clock_type::duration duration_type;
  33. // The time point type of the clock.
  34. typedef typename clock_type::time_point time_type;
  35. // The period of the clock.
  36. typedef typename duration_type::period period_type;
  37. // Get the current time.
  38. static time_type now()
  39. {
  40. return clock_type::now();
  41. }
  42. // Add a duration to a time.
  43. static time_type add(const time_type& t, const duration_type& d)
  44. {
  45. const time_type epoch;
  46. if (t >= epoch)
  47. {
  48. if ((time_type::max)() - t < d)
  49. return (time_type::max)();
  50. }
  51. else // t < epoch
  52. {
  53. if (-(t - (time_type::min)()) > d)
  54. return (time_type::min)();
  55. }
  56. return t + d;
  57. }
  58. // Subtract one time from another.
  59. static duration_type subtract(const time_type& t1, const time_type& t2)
  60. {
  61. const time_type epoch;
  62. if (t1 >= epoch)
  63. {
  64. if (t2 >= epoch)
  65. {
  66. return t1 - t2;
  67. }
  68. else if (t2 == (time_type::min)())
  69. {
  70. return (duration_type::max)();
  71. }
  72. else if ((time_type::max)() - t1 < epoch - t2)
  73. {
  74. return (duration_type::max)();
  75. }
  76. else
  77. {
  78. return t1 - t2;
  79. }
  80. }
  81. else // t1 < epoch
  82. {
  83. if (t2 < epoch)
  84. {
  85. return t1 - t2;
  86. }
  87. else if (t1 == (time_type::min)())
  88. {
  89. return (duration_type::min)();
  90. }
  91. else if ((time_type::max)() - t2 < epoch - t1)
  92. {
  93. return (duration_type::min)();
  94. }
  95. else
  96. {
  97. return -(t2 - t1);
  98. }
  99. }
  100. }
  101. // Test whether one time is less than another.
  102. static bool less_than(const time_type& t1, const time_type& t2)
  103. {
  104. return t1 < t2;
  105. }
  106. // Implement just enough of the posix_time::time_duration interface to supply
  107. // what the timer_queue requires.
  108. class posix_time_duration
  109. {
  110. public:
  111. explicit posix_time_duration(const duration_type& d)
  112. : d_(d)
  113. {
  114. }
  115. int64_t ticks() const
  116. {
  117. return d_.count();
  118. }
  119. int64_t total_seconds() const
  120. {
  121. return duration_cast<1, 1>();
  122. }
  123. int64_t total_milliseconds() const
  124. {
  125. return duration_cast<1, 1000>();
  126. }
  127. int64_t total_microseconds() const
  128. {
  129. return duration_cast<1, 1000000>();
  130. }
  131. private:
  132. template <int64_t Num, int64_t Den>
  133. int64_t duration_cast() const
  134. {
  135. const int64_t num1 = period_type::num / gcd<period_type::num, Num>::value;
  136. const int64_t num2 = Num / gcd<period_type::num, Num>::value;
  137. const int64_t den1 = period_type::den / gcd<period_type::den, Den>::value;
  138. const int64_t den2 = Den / gcd<period_type::den, Den>::value;
  139. const int64_t num = num1 * den2;
  140. const int64_t den = num2 * den1;
  141. if (num == 1 && den == 1)
  142. return ticks();
  143. else if (num != 1 && den == 1)
  144. return ticks() * num;
  145. else if (num == 1 && period_type::den != 1)
  146. return ticks() / den;
  147. else
  148. return ticks() * num / den;
  149. }
  150. duration_type d_;
  151. };
  152. // Convert to POSIX duration type.
  153. static posix_time_duration to_posix_duration(const duration_type& d)
  154. {
  155. return posix_time_duration(WaitTraits::to_wait_duration(d));
  156. }
  157. };
  158. } // namespace detail
  159. } // namespace asio
  160. } // namespace boost
  161. #include <boost/asio/detail/pop_options.hpp>
  162. #endif // BOOST_ASIO_DETAIL_CHRONO_TIME_TRAITS_HPP