associated_cancellation_slot.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // associated_cancellation_slot.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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_ASSOCIATED_CANCELLATION_SLOT_HPP
  11. #define ASIO_ASSOCIATED_CANCELLATION_SLOT_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/associator.hpp"
  17. #include "asio/cancellation_signal.hpp"
  18. #include "asio/detail/type_traits.hpp"
  19. #include "asio/detail/push_options.hpp"
  20. namespace asio {
  21. template <typename T, typename CancellationSlot>
  22. struct associated_cancellation_slot;
  23. namespace detail {
  24. template <typename T, typename = void>
  25. struct has_cancellation_slot_type : false_type
  26. {
  27. };
  28. template <typename T>
  29. struct has_cancellation_slot_type<T,
  30. typename void_type<typename T::cancellation_slot_type>::type>
  31. : true_type
  32. {
  33. };
  34. template <typename T, typename S, typename = void, typename = void>
  35. struct associated_cancellation_slot_impl
  36. {
  37. typedef void asio_associated_cancellation_slot_is_unspecialised;
  38. typedef S type;
  39. static type get(const T&, const S& s = S()) ASIO_NOEXCEPT
  40. {
  41. return s;
  42. }
  43. };
  44. template <typename T, typename S>
  45. struct associated_cancellation_slot_impl<T, S,
  46. typename void_type<typename T::cancellation_slot_type>::type>
  47. {
  48. typedef typename T::cancellation_slot_type type;
  49. static type get(const T& t, const S& = S()) ASIO_NOEXCEPT
  50. {
  51. return t.get_cancellation_slot();
  52. }
  53. };
  54. template <typename T, typename S>
  55. struct associated_cancellation_slot_impl<T, S,
  56. typename enable_if<
  57. !has_cancellation_slot_type<T>::value
  58. >::type,
  59. typename void_type<
  60. typename associator<associated_cancellation_slot, T, S>::type
  61. >::type> : associator<associated_cancellation_slot, T, S>
  62. {
  63. };
  64. } // namespace detail
  65. /// Traits type used to obtain the cancellation_slot associated with an object.
  66. /**
  67. * A program may specialise this traits type if the @c T template parameter in
  68. * the specialisation is a user-defined type. The template parameter @c
  69. * CancellationSlot shall be a type meeting the CancellationSlot requirements.
  70. *
  71. * Specialisations shall meet the following requirements, where @c t is a const
  72. * reference to an object of type @c T, and @c s is an object of type @c
  73. * CancellationSlot.
  74. *
  75. * @li Provide a nested typedef @c type that identifies a type meeting the
  76. * CancellationSlot requirements.
  77. *
  78. * @li Provide a noexcept static member function named @c get, callable as @c
  79. * get(t) and with return type @c type.
  80. *
  81. * @li Provide a noexcept static member function named @c get, callable as @c
  82. * get(t,s) and with return type @c type.
  83. */
  84. template <typename T, typename CancellationSlot = cancellation_slot>
  85. struct associated_cancellation_slot
  86. #if !defined(GENERATING_DOCUMENTATION)
  87. : detail::associated_cancellation_slot_impl<T, CancellationSlot>
  88. #endif // !defined(GENERATING_DOCUMENTATION)
  89. {
  90. #if defined(GENERATING_DOCUMENTATION)
  91. /// If @c T has a nested type @c cancellation_slot_type,
  92. /// <tt>T::cancellation_slot_type</tt>. Otherwise
  93. /// @c CancellationSlot.
  94. typedef see_below type;
  95. /// If @c T has a nested type @c cancellation_slot_type, returns
  96. /// <tt>t.get_cancellation_slot()</tt>. Otherwise returns @c s.
  97. static type get(const T& t,
  98. const CancellationSlot& s = CancellationSlot()) ASIO_NOEXCEPT;
  99. #endif // defined(GENERATING_DOCUMENTATION)
  100. };
  101. /// Helper function to obtain an object's associated cancellation_slot.
  102. /**
  103. * @returns <tt>associated_cancellation_slot<T>::get(t)</tt>
  104. */
  105. template <typename T>
  106. ASIO_NODISCARD inline typename associated_cancellation_slot<T>::type
  107. get_associated_cancellation_slot(const T& t) ASIO_NOEXCEPT
  108. {
  109. return associated_cancellation_slot<T>::get(t);
  110. }
  111. /// Helper function to obtain an object's associated cancellation_slot.
  112. /**
  113. * @returns <tt>associated_cancellation_slot<T,
  114. * CancellationSlot>::get(t, st)</tt>
  115. */
  116. template <typename T, typename CancellationSlot>
  117. ASIO_NODISCARD inline
  118. typename associated_cancellation_slot<T, CancellationSlot>::type
  119. get_associated_cancellation_slot(const T& t,
  120. const CancellationSlot& st) ASIO_NOEXCEPT
  121. {
  122. return associated_cancellation_slot<T, CancellationSlot>::get(t, st);
  123. }
  124. #if defined(ASIO_HAS_ALIAS_TEMPLATES)
  125. template <typename T, typename CancellationSlot = cancellation_slot>
  126. using associated_cancellation_slot_t =
  127. typename associated_cancellation_slot<T, CancellationSlot>::type;
  128. #endif // defined(ASIO_HAS_ALIAS_TEMPLATES)
  129. namespace detail {
  130. template <typename T, typename S, typename = void>
  131. struct associated_cancellation_slot_forwarding_base
  132. {
  133. };
  134. template <typename T, typename S>
  135. struct associated_cancellation_slot_forwarding_base<T, S,
  136. typename enable_if<
  137. is_same<
  138. typename associated_cancellation_slot<T,
  139. S>::asio_associated_cancellation_slot_is_unspecialised,
  140. void
  141. >::value
  142. >::type>
  143. {
  144. typedef void asio_associated_cancellation_slot_is_unspecialised;
  145. };
  146. } // namespace detail
  147. } // namespace asio
  148. #include "asio/detail/pop_options.hpp"
  149. #endif // ASIO_ASSOCIATED_CANCELLATION_SLOT_HPP