base_from_cancellation_state.hpp 4.1 KB

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