channel_traits.hpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // experimental/channel_traits.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_EXPERIMENTAL_CHANNEL_TRAITS_HPP
  11. #define BOOST_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 <boost/asio/detail/config.hpp>
  16. #include <deque>
  17. #include <boost/asio/detail/type_traits.hpp>
  18. #include <boost/asio/error.hpp>
  19. #include <boost/asio/experimental/channel_error.hpp>
  20. #include <boost/asio/detail/push_options.hpp>
  21. namespace boost {
  22. namespace asio {
  23. namespace experimental {
  24. #if defined(GENERATING_DOCUMENTATION)
  25. template <typename... Signatures>
  26. struct channel_traits
  27. {
  28. /// Rebind the traits to a new set of signatures.
  29. /**
  30. * This nested structure must have a single nested type @c other that
  31. * aliases a traits type with the specified set of signatures.
  32. */
  33. template <typename... NewSignatures>
  34. struct rebind
  35. {
  36. typedef user_defined other;
  37. };
  38. /// Determine the container for the specified elements.
  39. /**
  40. * This nested structure must have a single nested type @c other that
  41. * aliases a container type for the specified element type.
  42. */
  43. template <typename Element>
  44. struct container
  45. {
  46. typedef user_defined type;
  47. };
  48. /// The signature of a channel cancellation notification.
  49. typedef void receive_cancelled_signature(...);
  50. /// Invoke the specified handler with a cancellation notification.
  51. template <typename F>
  52. static void invoke_receive_cancelled(F f);
  53. /// The signature of a channel closed notification.
  54. typedef void receive_closed_signature(...);
  55. /// Invoke the specified handler with a closed notification.
  56. template <typename F>
  57. static void invoke_receive_closed(F f);
  58. };
  59. #else // defined(GENERATING_DOCUMENTATION)
  60. /// Traits used for customising channel behaviour.
  61. template <typename... Signatures>
  62. struct channel_traits
  63. {
  64. template <typename... NewSignatures>
  65. struct rebind
  66. {
  67. typedef channel_traits<NewSignatures...> other;
  68. };
  69. };
  70. template <typename R>
  71. struct channel_traits<R(boost::system::error_code)>
  72. {
  73. template <typename... NewSignatures>
  74. struct rebind
  75. {
  76. typedef channel_traits<NewSignatures...> other;
  77. };
  78. template <typename Element>
  79. struct container
  80. {
  81. typedef std::deque<Element> type;
  82. };
  83. typedef R receive_cancelled_signature(boost::system::error_code);
  84. template <typename F>
  85. static void invoke_receive_cancelled(F f)
  86. {
  87. const boost::system::error_code e = error::channel_cancelled;
  88. static_cast<F&&>(f)(e);
  89. }
  90. typedef R receive_closed_signature(boost::system::error_code);
  91. template <typename F>
  92. static void invoke_receive_closed(F f)
  93. {
  94. const boost::system::error_code e = error::channel_closed;
  95. static_cast<F&&>(f)(e);
  96. }
  97. };
  98. template <typename R, typename... Args, typename... Signatures>
  99. struct channel_traits<R(boost::system::error_code, Args...), Signatures...>
  100. {
  101. template <typename... NewSignatures>
  102. struct rebind
  103. {
  104. typedef channel_traits<NewSignatures...> other;
  105. };
  106. template <typename Element>
  107. struct container
  108. {
  109. typedef std::deque<Element> type;
  110. };
  111. typedef R receive_cancelled_signature(boost::system::error_code, Args...);
  112. template <typename F>
  113. static void invoke_receive_cancelled(F f)
  114. {
  115. const boost::system::error_code e = error::channel_cancelled;
  116. static_cast<F&&>(f)(e, decay_t<Args>()...);
  117. }
  118. typedef R receive_closed_signature(boost::system::error_code, Args...);
  119. template <typename F>
  120. static void invoke_receive_closed(F f)
  121. {
  122. const boost::system::error_code e = error::channel_closed;
  123. static_cast<F&&>(f)(e, decay_t<Args>()...);
  124. }
  125. };
  126. template <typename R>
  127. struct channel_traits<R(std::exception_ptr)>
  128. {
  129. template <typename... NewSignatures>
  130. struct rebind
  131. {
  132. typedef channel_traits<NewSignatures...> other;
  133. };
  134. template <typename Element>
  135. struct container
  136. {
  137. typedef std::deque<Element> type;
  138. };
  139. typedef R receive_cancelled_signature(std::exception_ptr);
  140. template <typename F>
  141. static void invoke_receive_cancelled(F f)
  142. {
  143. const boost::system::error_code e = error::channel_cancelled;
  144. static_cast<F&&>(f)(
  145. std::make_exception_ptr(boost::system::system_error(e)));
  146. }
  147. typedef R receive_closed_signature(std::exception_ptr);
  148. template <typename F>
  149. static void invoke_receive_closed(F f)
  150. {
  151. const boost::system::error_code e = error::channel_closed;
  152. static_cast<F&&>(f)(
  153. std::make_exception_ptr(boost::system::system_error(e)));
  154. }
  155. };
  156. template <typename R, typename... Args, typename... Signatures>
  157. struct channel_traits<R(std::exception_ptr, Args...), Signatures...>
  158. {
  159. template <typename... NewSignatures>
  160. struct rebind
  161. {
  162. typedef channel_traits<NewSignatures...> other;
  163. };
  164. template <typename Element>
  165. struct container
  166. {
  167. typedef std::deque<Element> type;
  168. };
  169. typedef R receive_cancelled_signature(std::exception_ptr, Args...);
  170. template <typename F>
  171. static void invoke_receive_cancelled(F f)
  172. {
  173. const boost::system::error_code e = error::channel_cancelled;
  174. static_cast<F&&>(f)(
  175. std::make_exception_ptr(boost::system::system_error(e)),
  176. decay_t<Args>()...);
  177. }
  178. typedef R receive_closed_signature(std::exception_ptr, Args...);
  179. template <typename F>
  180. static void invoke_receive_closed(F f)
  181. {
  182. const boost::system::error_code e = error::channel_closed;
  183. static_cast<F&&>(f)(
  184. std::make_exception_ptr(boost::system::system_error(e)),
  185. decay_t<Args>()...);
  186. }
  187. };
  188. template <typename R>
  189. struct channel_traits<R()>
  190. {
  191. template <typename... NewSignatures>
  192. struct rebind
  193. {
  194. typedef channel_traits<NewSignatures...> other;
  195. };
  196. template <typename Element>
  197. struct container
  198. {
  199. typedef std::deque<Element> type;
  200. };
  201. typedef R receive_cancelled_signature(boost::system::error_code);
  202. template <typename F>
  203. static void invoke_receive_cancelled(F f)
  204. {
  205. const boost::system::error_code e = error::channel_cancelled;
  206. static_cast<F&&>(f)(e);
  207. }
  208. typedef R receive_closed_signature(boost::system::error_code);
  209. template <typename F>
  210. static void invoke_receive_closed(F f)
  211. {
  212. const boost::system::error_code e = error::channel_closed;
  213. static_cast<F&&>(f)(e);
  214. }
  215. };
  216. template <typename R, typename T>
  217. struct channel_traits<R(T)>
  218. {
  219. template <typename... NewSignatures>
  220. struct rebind
  221. {
  222. typedef channel_traits<NewSignatures...> other;
  223. };
  224. template <typename Element>
  225. struct container
  226. {
  227. typedef std::deque<Element> type;
  228. };
  229. typedef R receive_cancelled_signature(boost::system::error_code);
  230. template <typename F>
  231. static void invoke_receive_cancelled(F f)
  232. {
  233. const boost::system::error_code e = error::channel_cancelled;
  234. static_cast<F&&>(f)(e);
  235. }
  236. typedef R receive_closed_signature(boost::system::error_code);
  237. template <typename F>
  238. static void invoke_receive_closed(F f)
  239. {
  240. const boost::system::error_code e = error::channel_closed;
  241. static_cast<F&&>(f)(e);
  242. }
  243. };
  244. #endif // defined(GENERATING_DOCUMENTATION)
  245. } // namespace experimental
  246. } // namespace asio
  247. } // namespace boost
  248. #include <boost/asio/detail/pop_options.hpp>
  249. #endif // BOOST_ASIO_EXPERIMENTAL_CHANNEL_TRAITS_HPP