channel_traits.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // experimental/channel_traits.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_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>
  70. struct channel_traits<R(asio::error_code)>
  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);
  83. template <typename F>
  84. static void invoke_receive_cancelled(F f)
  85. {
  86. const asio::error_code e = error::channel_cancelled;
  87. static_cast<F&&>(f)(e);
  88. }
  89. typedef R receive_closed_signature(asio::error_code);
  90. template <typename F>
  91. static void invoke_receive_closed(F f)
  92. {
  93. const asio::error_code e = error::channel_closed;
  94. static_cast<F&&>(f)(e);
  95. }
  96. };
  97. template <typename R, typename... Args, typename... Signatures>
  98. struct channel_traits<R(asio::error_code, 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(asio::error_code, Args...);
  111. template <typename F>
  112. static void invoke_receive_cancelled(F f)
  113. {
  114. const asio::error_code e = error::channel_cancelled;
  115. static_cast<F&&>(f)(e, decay_t<Args>()...);
  116. }
  117. typedef R receive_closed_signature(asio::error_code, Args...);
  118. template <typename F>
  119. static void invoke_receive_closed(F f)
  120. {
  121. const asio::error_code e = error::channel_closed;
  122. static_cast<F&&>(f)(e, decay_t<Args>()...);
  123. }
  124. };
  125. template <typename R>
  126. struct channel_traits<R(std::exception_ptr)>
  127. {
  128. template <typename... NewSignatures>
  129. struct rebind
  130. {
  131. typedef channel_traits<NewSignatures...> other;
  132. };
  133. template <typename Element>
  134. struct container
  135. {
  136. typedef std::deque<Element> type;
  137. };
  138. typedef R receive_cancelled_signature(std::exception_ptr);
  139. template <typename F>
  140. static void invoke_receive_cancelled(F f)
  141. {
  142. const asio::error_code e = error::channel_cancelled;
  143. static_cast<F&&>(f)(
  144. std::make_exception_ptr(asio::system_error(e)));
  145. }
  146. typedef R receive_closed_signature(std::exception_ptr);
  147. template <typename F>
  148. static void invoke_receive_closed(F f)
  149. {
  150. const asio::error_code e = error::channel_closed;
  151. static_cast<F&&>(f)(
  152. std::make_exception_ptr(asio::system_error(e)));
  153. }
  154. };
  155. template <typename R, typename... Args, typename... Signatures>
  156. struct channel_traits<R(std::exception_ptr, Args...), Signatures...>
  157. {
  158. template <typename... NewSignatures>
  159. struct rebind
  160. {
  161. typedef channel_traits<NewSignatures...> other;
  162. };
  163. template <typename Element>
  164. struct container
  165. {
  166. typedef std::deque<Element> type;
  167. };
  168. typedef R receive_cancelled_signature(std::exception_ptr, Args...);
  169. template <typename F>
  170. static void invoke_receive_cancelled(F f)
  171. {
  172. const asio::error_code e = error::channel_cancelled;
  173. static_cast<F&&>(f)(
  174. std::make_exception_ptr(asio::system_error(e)),
  175. decay_t<Args>()...);
  176. }
  177. typedef R receive_closed_signature(std::exception_ptr, Args...);
  178. template <typename F>
  179. static void invoke_receive_closed(F f)
  180. {
  181. const asio::error_code e = error::channel_closed;
  182. static_cast<F&&>(f)(
  183. std::make_exception_ptr(asio::system_error(e)),
  184. decay_t<Args>()...);
  185. }
  186. };
  187. template <typename R>
  188. struct channel_traits<R()>
  189. {
  190. template <typename... NewSignatures>
  191. struct rebind
  192. {
  193. typedef channel_traits<NewSignatures...> other;
  194. };
  195. template <typename Element>
  196. struct container
  197. {
  198. typedef std::deque<Element> type;
  199. };
  200. typedef R receive_cancelled_signature(asio::error_code);
  201. template <typename F>
  202. static void invoke_receive_cancelled(F f)
  203. {
  204. const asio::error_code e = error::channel_cancelled;
  205. static_cast<F&&>(f)(e);
  206. }
  207. typedef R receive_closed_signature(asio::error_code);
  208. template <typename F>
  209. static void invoke_receive_closed(F f)
  210. {
  211. const asio::error_code e = error::channel_closed;
  212. static_cast<F&&>(f)(e);
  213. }
  214. };
  215. template <typename R, typename T>
  216. struct channel_traits<R(T)>
  217. {
  218. template <typename... NewSignatures>
  219. struct rebind
  220. {
  221. typedef channel_traits<NewSignatures...> other;
  222. };
  223. template <typename Element>
  224. struct container
  225. {
  226. typedef std::deque<Element> type;
  227. };
  228. typedef R receive_cancelled_signature(asio::error_code);
  229. template <typename F>
  230. static void invoke_receive_cancelled(F f)
  231. {
  232. const asio::error_code e = error::channel_cancelled;
  233. static_cast<F&&>(f)(e);
  234. }
  235. typedef R receive_closed_signature(asio::error_code);
  236. template <typename F>
  237. static void invoke_receive_closed(F f)
  238. {
  239. const asio::error_code e = error::channel_closed;
  240. static_cast<F&&>(f)(e);
  241. }
  242. };
  243. #endif // defined(GENERATING_DOCUMENTATION)
  244. } // namespace experimental
  245. } // namespace asio
  246. #include "asio/detail/pop_options.hpp"
  247. #endif // ASIO_EXPERIMENTAL_CHANNEL_TRAITS_HPP