Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
4.3KB

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