channel_traits.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // experimental/channel_traits.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_EXPERIMENTAL_CHANNEL_TRAITS_HPP
  11. #define ASIO_EXPERIMENTAL_CHANNEL_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/config.hpp"
  16. #include <deque>
  17. #include "asio/detail/type_traits.hpp"
  18. #include "asio/error.hpp"
  19. #include "asio/experimental/channel_error.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. namespace asio {
  22. namespace experimental {
  23. #if defined(GENERATING_DOCUMENTATION)
  24. template <typename... Signatures>
  25. struct channel_traits
  26. {
  27. /// Rebind the traits to a new set of signatures.
  28. /**
  29. * This nested structure must have a single nested type @c other that
  30. * aliases a traits type with the specified set of signatures.
  31. */
  32. template <typename... NewSignatures>
  33. struct rebind
  34. {
  35. typedef user_defined other;
  36. };
  37. /// Determine the container for the specified elements.
  38. /**
  39. * This nested structure must have a single nested type @c other that
  40. * aliases a container type for the specified element type.
  41. */
  42. template <typename Element>
  43. struct container
  44. {
  45. typedef user_defined type;
  46. };
  47. /// The signature of a channel cancellation notification.
  48. typedef void receive_cancelled_signature(...);
  49. /// Invoke the specified handler with a cancellation notification.
  50. template <typename F>
  51. static void invoke_receive_cancelled(F f);
  52. /// The signature of a channel closed notification.
  53. typedef void receive_closed_signature(...);
  54. /// Invoke the specified handler with a closed notification.
  55. template <typename F>
  56. static void invoke_receive_closed(F f);
  57. };
  58. #else // defined(GENERATING_DOCUMENTATION)
  59. /// Traits used for customising channel behaviour.
  60. template <typename... Signatures>
  61. struct channel_traits
  62. {
  63. template <typename... NewSignatures>
  64. struct rebind
  65. {
  66. typedef channel_traits<NewSignatures...> other;
  67. };
  68. };
  69. template <typename R, typename... Args, typename... Signatures>
  70. struct channel_traits<R(asio::error_code, Args...), Signatures...>
  71. {
  72. template <typename... NewSignatures>
  73. struct rebind
  74. {
  75. typedef channel_traits<NewSignatures...> other;
  76. };
  77. template <typename Element>
  78. struct container
  79. {
  80. typedef std::deque<Element> type;
  81. };
  82. typedef R receive_cancelled_signature(asio::error_code, Args...);
  83. template <typename F>
  84. static void invoke_receive_cancelled(F f)
  85. {
  86. const asio::error_code e = error::channel_cancelled;
  87. ASIO_MOVE_OR_LVALUE(F)(f)(e, typename decay<Args>::type()...);
  88. }
  89. typedef R receive_closed_signature(asio::error_code, Args...);
  90. template <typename F>
  91. static void invoke_receive_closed(F f)
  92. {
  93. const asio::error_code e = error::channel_closed;
  94. ASIO_MOVE_OR_LVALUE(F)(f)(e, typename decay<Args>::type()...);
  95. }
  96. };
  97. template <typename R, typename... Args, typename... Signatures>
  98. struct channel_traits<R(std::exception_ptr, Args...), Signatures...>
  99. {
  100. template <typename... NewSignatures>
  101. struct rebind
  102. {
  103. typedef channel_traits<NewSignatures...> other;
  104. };
  105. template <typename Element>
  106. struct container
  107. {
  108. typedef std::deque<Element> type;
  109. };
  110. typedef R receive_cancelled_signature(std::exception_ptr, Args...);
  111. template <typename F>
  112. static void invoke_receive_cancelled(F f)
  113. {
  114. const asio::error_code e = error::channel_cancelled;
  115. ASIO_MOVE_OR_LVALUE(F)(f)(
  116. std::make_exception_ptr(asio::system_error(e)),
  117. typename decay<Args>::type()...);
  118. }
  119. typedef R receive_closed_signature(std::exception_ptr, Args...);
  120. template <typename F>
  121. static void invoke_receive_closed(F f)
  122. {
  123. const asio::error_code e = error::channel_closed;
  124. ASIO_MOVE_OR_LVALUE(F)(f)(
  125. std::make_exception_ptr(asio::system_error(e)),
  126. typename decay<Args>::type()...);
  127. }
  128. };
  129. template <typename R>
  130. struct channel_traits<R()>
  131. {
  132. template <typename... NewSignatures>
  133. struct rebind
  134. {
  135. typedef channel_traits<NewSignatures...> other;
  136. };
  137. template <typename Element>
  138. struct container
  139. {
  140. typedef std::deque<Element> type;
  141. };
  142. typedef R receive_cancelled_signature(asio::error_code);
  143. template <typename F>
  144. static void invoke_receive_cancelled(F f)
  145. {
  146. const asio::error_code e = error::channel_cancelled;
  147. ASIO_MOVE_OR_LVALUE(F)(f)(e);
  148. }
  149. typedef R receive_closed_signature(asio::error_code);
  150. template <typename F>
  151. static void invoke_receive_closed(F f)
  152. {
  153. const asio::error_code e = error::channel_closed;
  154. ASIO_MOVE_OR_LVALUE(F)(f)(e);
  155. }
  156. };
  157. template <typename R, typename T>
  158. struct channel_traits<R(T)>
  159. {
  160. template <typename... NewSignatures>
  161. struct rebind
  162. {
  163. typedef channel_traits<NewSignatures...> other;
  164. };
  165. template <typename Element>
  166. struct container
  167. {
  168. typedef std::deque<Element> type;
  169. };
  170. typedef R receive_cancelled_signature(asio::error_code);
  171. template <typename F>
  172. static void invoke_receive_cancelled(F f)
  173. {
  174. const asio::error_code e = error::channel_cancelled;
  175. ASIO_MOVE_OR_LVALUE(F)(f)(e);
  176. }
  177. typedef R receive_closed_signature(asio::error_code);
  178. template <typename F>
  179. static void invoke_receive_closed(F f)
  180. {
  181. const asio::error_code e = error::channel_closed;
  182. ASIO_MOVE_OR_LVALUE(F)(f)(e);
  183. }
  184. };
  185. #endif // defined(GENERATING_DOCUMENTATION)
  186. } // namespace experimental
  187. } // namespace asio
  188. #include "asio/detail/pop_options.hpp"
  189. #endif // ASIO_EXPERIMENTAL_CHANNEL_TRAITS_HPP