redirect_error.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // impl/redirect_error.hpp
  2. // ~~~~~~~~~~~~~~~~~~~~~~~
  3. //
  4. // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  7. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. #ifndef BOOST_ASIO_IMPL_REDIRECT_ERROR_HPP
  10. #define BOOST_ASIO_IMPL_REDIRECT_ERROR_HPP
  11. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  12. # pragma once
  13. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  14. #include <boost/asio/detail/config.hpp>
  15. #include <boost/asio/associated_executor.hpp>
  16. #include <boost/asio/associated_allocator.hpp>
  17. #include <boost/asio/async_result.hpp>
  18. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  19. #include <boost/asio/detail/handler_cont_helpers.hpp>
  20. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  21. #include <boost/asio/detail/type_traits.hpp>
  22. #include <boost/asio/detail/variadic_templates.hpp>
  23. #include <boost/system/system_error.hpp>
  24. #include <boost/asio/detail/push_options.hpp>
  25. namespace boost {
  26. namespace asio {
  27. namespace detail {
  28. // Class to adapt a redirect_error_t as a completion handler.
  29. template <typename Handler>
  30. class redirect_error_handler
  31. {
  32. public:
  33. typedef void result_type;
  34. template <typename CompletionToken>
  35. redirect_error_handler(redirect_error_t<CompletionToken> e)
  36. : ec_(e.ec_),
  37. handler_(BOOST_ASIO_MOVE_CAST(CompletionToken)(e.token_))
  38. {
  39. }
  40. template <typename RedirectedHandler>
  41. redirect_error_handler(boost::system::error_code& ec,
  42. BOOST_ASIO_MOVE_ARG(RedirectedHandler) h)
  43. : ec_(ec),
  44. handler_(BOOST_ASIO_MOVE_CAST(RedirectedHandler)(h))
  45. {
  46. }
  47. void operator()()
  48. {
  49. handler_();
  50. }
  51. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  52. template <typename Arg, typename... Args>
  53. typename enable_if<
  54. !is_same<typename decay<Arg>::type, boost::system::error_code>::value
  55. >::type
  56. operator()(BOOST_ASIO_MOVE_ARG(Arg) arg, BOOST_ASIO_MOVE_ARG(Args)... args)
  57. {
  58. handler_(BOOST_ASIO_MOVE_CAST(Arg)(arg),
  59. BOOST_ASIO_MOVE_CAST(Args)(args)...);
  60. }
  61. template <typename... Args>
  62. void operator()(const boost::system::error_code& ec,
  63. BOOST_ASIO_MOVE_ARG(Args)... args)
  64. {
  65. ec_ = ec;
  66. handler_(BOOST_ASIO_MOVE_CAST(Args)(args)...);
  67. }
  68. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  69. template <typename Arg>
  70. typename enable_if<
  71. !is_same<typename decay<Arg>::type, boost::system::error_code>::value
  72. >::type
  73. operator()(BOOST_ASIO_MOVE_ARG(Arg) arg)
  74. {
  75. handler_(BOOST_ASIO_MOVE_CAST(Arg)(arg));
  76. }
  77. void operator()(const boost::system::error_code& ec)
  78. {
  79. ec_ = ec;
  80. handler_();
  81. }
  82. #define BOOST_ASIO_PRIVATE_REDIRECT_ERROR_DEF(n) \
  83. template <typename Arg, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  84. typename enable_if< \
  85. !is_same<typename decay<Arg>::type, boost::system::error_code>::value \
  86. >::type \
  87. operator()(BOOST_ASIO_MOVE_ARG(Arg) arg, BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  88. { \
  89. handler_(BOOST_ASIO_MOVE_CAST(Arg)(arg), \
  90. BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
  91. } \
  92. \
  93. template <BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  94. void operator()(const boost::system::error_code& ec, \
  95. BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  96. { \
  97. ec_ = ec; \
  98. handler_(BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
  99. } \
  100. /**/
  101. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_REDIRECT_ERROR_DEF)
  102. #undef BOOST_ASIO_PRIVATE_REDIRECT_ERROR_DEF
  103. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  104. //private:
  105. boost::system::error_code& ec_;
  106. Handler handler_;
  107. };
  108. template <typename Handler>
  109. inline void* asio_handler_allocate(std::size_t size,
  110. redirect_error_handler<Handler>* this_handler)
  111. {
  112. return boost_asio_handler_alloc_helpers::allocate(
  113. size, this_handler->handler_);
  114. }
  115. template <typename Handler>
  116. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  117. redirect_error_handler<Handler>* this_handler)
  118. {
  119. boost_asio_handler_alloc_helpers::deallocate(
  120. pointer, size, this_handler->handler_);
  121. }
  122. template <typename Handler>
  123. inline bool asio_handler_is_continuation(
  124. redirect_error_handler<Handler>* this_handler)
  125. {
  126. return boost_asio_handler_cont_helpers::is_continuation(
  127. this_handler->handler_);
  128. }
  129. template <typename Function, typename Handler>
  130. inline void asio_handler_invoke(Function& function,
  131. redirect_error_handler<Handler>* this_handler)
  132. {
  133. boost_asio_handler_invoke_helpers::invoke(
  134. function, this_handler->handler_);
  135. }
  136. template <typename Function, typename Handler>
  137. inline void asio_handler_invoke(const Function& function,
  138. redirect_error_handler<Handler>* this_handler)
  139. {
  140. boost_asio_handler_invoke_helpers::invoke(
  141. function, this_handler->handler_);
  142. }
  143. template <typename Signature>
  144. struct redirect_error_signature
  145. {
  146. typedef Signature type;
  147. };
  148. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  149. template <typename R, typename... Args>
  150. struct redirect_error_signature<R(boost::system::error_code, Args...)>
  151. {
  152. typedef R type(Args...);
  153. };
  154. template <typename R, typename... Args>
  155. struct redirect_error_signature<R(const boost::system::error_code&, Args...)>
  156. {
  157. typedef R type(Args...);
  158. };
  159. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  160. template <typename R>
  161. struct redirect_error_signature<R(boost::system::error_code)>
  162. {
  163. typedef R type();
  164. };
  165. template <typename R>
  166. struct redirect_error_signature<R(const boost::system::error_code&)>
  167. {
  168. typedef R type();
  169. };
  170. #define BOOST_ASIO_PRIVATE_REDIRECT_ERROR_DEF(n) \
  171. template <typename R, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  172. struct redirect_error_signature< \
  173. R(boost::system::error_code, BOOST_ASIO_VARIADIC_TARGS(n))> \
  174. { \
  175. typedef R type(BOOST_ASIO_VARIADIC_TARGS(n)); \
  176. }; \
  177. \
  178. template <typename R, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  179. struct redirect_error_signature< \
  180. R(const boost::system::error_code&, BOOST_ASIO_VARIADIC_TARGS(n))> \
  181. { \
  182. typedef R type(BOOST_ASIO_VARIADIC_TARGS(n)); \
  183. }; \
  184. /**/
  185. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_REDIRECT_ERROR_DEF)
  186. #undef BOOST_ASIO_PRIVATE_REDIRECT_ERROR_DEF
  187. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  188. } // namespace detail
  189. #if !defined(GENERATING_DOCUMENTATION)
  190. template <typename CompletionToken, typename Signature>
  191. struct async_result<redirect_error_t<CompletionToken>, Signature>
  192. {
  193. typedef typename async_result<CompletionToken,
  194. typename detail::redirect_error_signature<Signature>::type>
  195. ::return_type return_type;
  196. template <typename Initiation>
  197. struct init_wrapper
  198. {
  199. template <typename Init>
  200. init_wrapper(boost::system::error_code& ec, BOOST_ASIO_MOVE_ARG(Init) init)
  201. : ec_(ec),
  202. initiation_(BOOST_ASIO_MOVE_CAST(Init)(init))
  203. {
  204. }
  205. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  206. template <typename Handler, typename... Args>
  207. void operator()(
  208. BOOST_ASIO_MOVE_ARG(Handler) handler,
  209. BOOST_ASIO_MOVE_ARG(Args)... args)
  210. {
  211. BOOST_ASIO_MOVE_CAST(Initiation)(initiation_)(
  212. detail::redirect_error_handler<
  213. typename decay<Handler>::type>(
  214. ec_, BOOST_ASIO_MOVE_CAST(Handler)(handler)),
  215. BOOST_ASIO_MOVE_CAST(Args)(args)...);
  216. }
  217. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  218. template <typename Handler>
  219. void operator()(
  220. BOOST_ASIO_MOVE_ARG(Handler) handler)
  221. {
  222. BOOST_ASIO_MOVE_CAST(Initiation)(initiation_)(
  223. detail::redirect_error_handler<
  224. typename decay<Handler>::type>(
  225. ec_, BOOST_ASIO_MOVE_CAST(Handler)(handler)));
  226. }
  227. #define BOOST_ASIO_PRIVATE_INIT_WRAPPER_DEF(n) \
  228. template <typename Handler, BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  229. void operator()( \
  230. BOOST_ASIO_MOVE_ARG(Handler) handler, \
  231. BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  232. { \
  233. BOOST_ASIO_MOVE_CAST(Initiation)(initiation_)( \
  234. detail::redirect_error_handler< \
  235. typename decay<Handler>::type>( \
  236. ec_, BOOST_ASIO_MOVE_CAST(Handler)(handler)), \
  237. BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
  238. } \
  239. /**/
  240. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_INIT_WRAPPER_DEF)
  241. #undef BOOST_ASIO_PRIVATE_INIT_WRAPPER_DEF
  242. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  243. boost::system::error_code& ec_;
  244. Initiation initiation_;
  245. };
  246. #if defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  247. template <typename Initiation, typename RawCompletionToken, typename... Args>
  248. static return_type initiate(
  249. BOOST_ASIO_MOVE_ARG(Initiation) initiation,
  250. BOOST_ASIO_MOVE_ARG(RawCompletionToken) token,
  251. BOOST_ASIO_MOVE_ARG(Args)... args)
  252. {
  253. return async_initiate<CompletionToken,
  254. typename detail::redirect_error_signature<Signature>::type>(
  255. init_wrapper<typename decay<Initiation>::type>(
  256. token.ec_, BOOST_ASIO_MOVE_CAST(Initiation)(initiation)),
  257. token.token_, BOOST_ASIO_MOVE_CAST(Args)(args)...);
  258. }
  259. #else // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  260. template <typename Initiation, typename RawCompletionToken>
  261. static return_type initiate(
  262. BOOST_ASIO_MOVE_ARG(Initiation) initiation,
  263. BOOST_ASIO_MOVE_ARG(RawCompletionToken) token)
  264. {
  265. return async_initiate<CompletionToken,
  266. typename detail::redirect_error_signature<Signature>::type>(
  267. init_wrapper<typename decay<Initiation>::type>(
  268. token.ec_, BOOST_ASIO_MOVE_CAST(Initiation)(initiation)),
  269. token.token_);
  270. }
  271. #define BOOST_ASIO_PRIVATE_INITIATE_DEF(n) \
  272. template <typename Initiation, typename RawCompletionToken, \
  273. BOOST_ASIO_VARIADIC_TPARAMS(n)> \
  274. static return_type initiate( \
  275. BOOST_ASIO_MOVE_ARG(Initiation) initiation, \
  276. BOOST_ASIO_MOVE_ARG(RawCompletionToken) token, \
  277. BOOST_ASIO_VARIADIC_MOVE_PARAMS(n)) \
  278. { \
  279. return async_initiate<CompletionToken, \
  280. typename detail::redirect_error_signature<Signature>::type>( \
  281. init_wrapper<typename decay<Initiation>::type>( \
  282. token.ec_, BOOST_ASIO_MOVE_CAST(Initiation)(initiation)), \
  283. token.token_, BOOST_ASIO_VARIADIC_MOVE_ARGS(n)); \
  284. } \
  285. /**/
  286. BOOST_ASIO_VARIADIC_GENERATE(BOOST_ASIO_PRIVATE_INITIATE_DEF)
  287. #undef BOOST_ASIO_PRIVATE_INITIATE_DEF
  288. #endif // defined(BOOST_ASIO_HAS_VARIADIC_TEMPLATES)
  289. };
  290. template <typename Handler, typename Executor>
  291. struct associated_executor<detail::redirect_error_handler<Handler>, Executor>
  292. {
  293. typedef typename associated_executor<Handler, Executor>::type type;
  294. static type get(
  295. const detail::redirect_error_handler<Handler>& h,
  296. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  297. {
  298. return associated_executor<Handler, Executor>::get(h.handler_, ex);
  299. }
  300. };
  301. template <typename Handler, typename Allocator>
  302. struct associated_allocator<detail::redirect_error_handler<Handler>, Allocator>
  303. {
  304. typedef typename associated_allocator<Handler, Allocator>::type type;
  305. static type get(
  306. const detail::redirect_error_handler<Handler>& h,
  307. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  308. {
  309. return associated_allocator<Handler, Allocator>::get(h.handler_, a);
  310. }
  311. };
  312. #endif // !defined(GENERATING_DOCUMENTATION)
  313. } // namespace asio
  314. } // namespace boost
  315. #include <boost/asio/detail/pop_options.hpp>
  316. #endif // BOOST_ASIO_IMPL_REDIRECT_ERROR_HPP