base_from_cancellation_state.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. //
  2. // detail/base_from_cancellation_state.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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_BASE_FROM_CANCELLATION_STATE_HPP
  11. #define ASIO_DETAIL_BASE_FROM_CANCELLATION_STATE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/associated_cancellation_slot.hpp"
  17. #include "asio/cancellation_state.hpp"
  18. #include "asio/detail/type_traits.hpp"
  19. #include "asio/detail/push_options.hpp"
  20. namespace asio {
  21. namespace detail {
  22. template <typename Handler, typename = void>
  23. class base_from_cancellation_state
  24. {
  25. public:
  26. typedef cancellation_slot cancellation_slot_type;
  27. cancellation_slot_type get_cancellation_slot() const noexcept
  28. {
  29. return cancellation_state_.slot();
  30. }
  31. cancellation_state get_cancellation_state() const noexcept
  32. {
  33. return cancellation_state_;
  34. }
  35. protected:
  36. explicit base_from_cancellation_state(const Handler& handler)
  37. : cancellation_state_(
  38. asio::get_associated_cancellation_slot(handler))
  39. {
  40. }
  41. template <typename Filter>
  42. base_from_cancellation_state(const Handler& handler, Filter filter)
  43. : cancellation_state_(
  44. asio::get_associated_cancellation_slot(handler), filter, filter)
  45. {
  46. }
  47. template <typename InFilter, typename OutFilter>
  48. base_from_cancellation_state(const Handler& handler,
  49. InFilter&& in_filter,
  50. OutFilter&& out_filter)
  51. : cancellation_state_(
  52. asio::get_associated_cancellation_slot(handler),
  53. static_cast<InFilter&&>(in_filter),
  54. static_cast<OutFilter&&>(out_filter))
  55. {
  56. }
  57. void reset_cancellation_state(const Handler& handler)
  58. {
  59. cancellation_state_ = cancellation_state(
  60. asio::get_associated_cancellation_slot(handler));
  61. }
  62. template <typename Filter>
  63. void reset_cancellation_state(const Handler& handler, Filter filter)
  64. {
  65. cancellation_state_ = cancellation_state(
  66. asio::get_associated_cancellation_slot(handler), filter, filter);
  67. }
  68. template <typename InFilter, typename OutFilter>
  69. void reset_cancellation_state(const Handler& handler,
  70. InFilter&& in_filter,
  71. OutFilter&& out_filter)
  72. {
  73. cancellation_state_ = cancellation_state(
  74. asio::get_associated_cancellation_slot(handler),
  75. static_cast<InFilter&&>(in_filter),
  76. static_cast<OutFilter&&>(out_filter));
  77. }
  78. cancellation_type_t cancelled() const noexcept
  79. {
  80. return cancellation_state_.cancelled();
  81. }
  82. private:
  83. cancellation_state cancellation_state_;
  84. };
  85. template <typename Handler>
  86. class base_from_cancellation_state<Handler,
  87. enable_if_t<
  88. is_same<
  89. typename associated_cancellation_slot<
  90. Handler, cancellation_slot
  91. >::asio_associated_cancellation_slot_is_unspecialised,
  92. void
  93. >::value
  94. >
  95. >
  96. {
  97. public:
  98. cancellation_state get_cancellation_state() const noexcept
  99. {
  100. return cancellation_state();
  101. }
  102. protected:
  103. explicit base_from_cancellation_state(const Handler&)
  104. {
  105. }
  106. template <typename Filter>
  107. base_from_cancellation_state(const Handler&, Filter)
  108. {
  109. }
  110. template <typename InFilter, typename OutFilter>
  111. base_from_cancellation_state(const Handler&,
  112. InFilter&&,
  113. OutFilter&&)
  114. {
  115. }
  116. void reset_cancellation_state(const Handler&)
  117. {
  118. }
  119. template <typename Filter>
  120. void reset_cancellation_state(const Handler&, Filter)
  121. {
  122. }
  123. template <typename InFilter, typename OutFilter>
  124. void reset_cancellation_state(const Handler&,
  125. InFilter&&,
  126. OutFilter&&)
  127. {
  128. }
  129. constexpr cancellation_type_t cancelled() const noexcept
  130. {
  131. return cancellation_type::none;
  132. }
  133. };
  134. } // namespace detail
  135. } // namespace asio
  136. #include "asio/detail/pop_options.hpp"
  137. #endif // ASIO_DETAIL_BASE_FROM_CANCELLATION_STATE_HPP