async_ops.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. //
  2. // async_ops.hpp
  3. // ~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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 ARCHETYPES_ASYNC_OPS_HPP
  11. #define ARCHETYPES_ASYNC_OPS_HPP
  12. #include <boost/asio/associated_allocator.hpp>
  13. #include <boost/asio/associated_executor.hpp>
  14. #include <boost/asio/async_result.hpp>
  15. #include <boost/asio/error.hpp>
  16. #if defined(BOOST_ASIO_HAS_BOOST_BIND)
  17. # include <boost/bind.hpp>
  18. #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
  19. # include <functional>
  20. #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
  21. namespace archetypes {
  22. #if defined(BOOST_ASIO_HAS_BOOST_BIND)
  23. namespace bindns = boost;
  24. #else // defined(BOOST_ASIO_HAS_BOOST_BIND)
  25. namespace bindns = std;
  26. #endif // defined(BOOST_ASIO_HAS_BOOST_BIND)
  27. template <typename CompletionToken>
  28. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void())
  29. async_op_0(BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  30. {
  31. typedef typename boost::asio::async_completion<CompletionToken,
  32. void()>::completion_handler_type handler_type;
  33. boost::asio::async_completion<CompletionToken,
  34. void()> completion(token);
  35. typename boost::asio::associated_allocator<handler_type>::type a
  36. = boost::asio::get_associated_allocator(completion.completion_handler);
  37. typename boost::asio::associated_executor<handler_type>::type ex
  38. = boost::asio::get_associated_executor(completion.completion_handler);
  39. ex.post(BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler), a);
  40. return completion.result.get();
  41. }
  42. template <typename CompletionToken>
  43. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void(boost::system::error_code))
  44. async_op_ec_0(bool ok, BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  45. {
  46. typedef typename boost::asio::async_completion<CompletionToken,
  47. void(boost::system::error_code)>::completion_handler_type handler_type;
  48. boost::asio::async_completion<CompletionToken,
  49. void(boost::system::error_code)> completion(token);
  50. typename boost::asio::associated_allocator<handler_type>::type a
  51. = boost::asio::get_associated_allocator(completion.completion_handler);
  52. typename boost::asio::associated_executor<handler_type>::type ex
  53. = boost::asio::get_associated_executor(completion.completion_handler);
  54. if (ok)
  55. {
  56. ex.post(
  57. bindns::bind(
  58. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  59. boost::system::error_code()), a);
  60. }
  61. else
  62. {
  63. ex.post(
  64. bindns::bind(
  65. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  66. boost::system::error_code(boost::asio::error::operation_aborted)), a);
  67. }
  68. return completion.result.get();
  69. }
  70. template <typename CompletionToken>
  71. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void(std::exception_ptr))
  72. async_op_ex_0(bool ok, BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  73. {
  74. typedef typename boost::asio::async_completion<CompletionToken,
  75. void(std::exception_ptr)>::completion_handler_type handler_type;
  76. boost::asio::async_completion<CompletionToken,
  77. void(std::exception_ptr)> completion(token);
  78. typename boost::asio::associated_allocator<handler_type>::type a
  79. = boost::asio::get_associated_allocator(completion.completion_handler);
  80. typename boost::asio::associated_executor<handler_type>::type ex
  81. = boost::asio::get_associated_executor(completion.completion_handler);
  82. if (ok)
  83. {
  84. ex.post(
  85. bindns::bind(
  86. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  87. std::exception_ptr()), a);
  88. }
  89. else
  90. {
  91. ex.post(
  92. bindns::bind(
  93. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  94. std::make_exception_ptr(std::runtime_error("blah"))), a);
  95. }
  96. return completion.result.get();
  97. }
  98. template <typename CompletionToken>
  99. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void(int))
  100. async_op_1(BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  101. {
  102. typedef typename boost::asio::async_completion<CompletionToken,
  103. void(int)>::completion_handler_type handler_type;
  104. boost::asio::async_completion<CompletionToken,
  105. void(int)> completion(token);
  106. typename boost::asio::associated_allocator<handler_type>::type a
  107. = boost::asio::get_associated_allocator(completion.completion_handler);
  108. typename boost::asio::associated_executor<handler_type>::type ex
  109. = boost::asio::get_associated_executor(completion.completion_handler);
  110. ex.post(
  111. bindns::bind(
  112. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  113. 42), a);
  114. return completion.result.get();
  115. }
  116. template <typename CompletionToken>
  117. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken,
  118. void(boost::system::error_code, int))
  119. async_op_ec_1(bool ok, BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  120. {
  121. typedef typename boost::asio::async_completion<CompletionToken,
  122. void(boost::system::error_code, int)>::completion_handler_type
  123. handler_type;
  124. boost::asio::async_completion<CompletionToken,
  125. void(boost::system::error_code, int)> completion(token);
  126. typename boost::asio::associated_allocator<handler_type>::type a
  127. = boost::asio::get_associated_allocator(completion.completion_handler);
  128. typename boost::asio::associated_executor<handler_type>::type ex
  129. = boost::asio::get_associated_executor(completion.completion_handler);
  130. if (ok)
  131. {
  132. ex.post(
  133. bindns::bind(
  134. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  135. boost::system::error_code(), 42), a);
  136. }
  137. else
  138. {
  139. ex.post(
  140. bindns::bind(
  141. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  142. boost::system::error_code(boost::asio::error::operation_aborted),
  143. 0), a);
  144. }
  145. return completion.result.get();
  146. }
  147. template <typename CompletionToken>
  148. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void(std::exception_ptr, int))
  149. async_op_ex_1(bool ok, BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  150. {
  151. typedef typename boost::asio::async_completion<CompletionToken,
  152. void(std::exception_ptr, int)>::completion_handler_type
  153. handler_type;
  154. boost::asio::async_completion<CompletionToken,
  155. void(std::exception_ptr, int)> completion(token);
  156. typename boost::asio::associated_allocator<handler_type>::type a
  157. = boost::asio::get_associated_allocator(completion.completion_handler);
  158. typename boost::asio::associated_executor<handler_type>::type ex
  159. = boost::asio::get_associated_executor(completion.completion_handler);
  160. if (ok)
  161. {
  162. ex.post(
  163. bindns::bind(
  164. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  165. std::exception_ptr(), 42), a);
  166. }
  167. else
  168. {
  169. ex.post(
  170. bindns::bind(
  171. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  172. std::make_exception_ptr(std::runtime_error("blah")), 0), a);
  173. }
  174. return completion.result.get();
  175. }
  176. template <typename CompletionToken>
  177. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void(int, double))
  178. async_op_2(BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  179. {
  180. typedef typename boost::asio::async_completion<CompletionToken,
  181. void(int, double)>::completion_handler_type handler_type;
  182. boost::asio::async_completion<CompletionToken,
  183. void(int, double)> completion(token);
  184. typename boost::asio::associated_allocator<handler_type>::type a
  185. = boost::asio::get_associated_allocator(completion.completion_handler);
  186. typename boost::asio::associated_executor<handler_type>::type ex
  187. = boost::asio::get_associated_executor(completion.completion_handler);
  188. ex.post(
  189. bindns::bind(
  190. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  191. 42, 2.0), a);
  192. return completion.result.get();
  193. }
  194. template <typename CompletionToken>
  195. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken,
  196. void(boost::system::error_code, int, double))
  197. async_op_ec_2(bool ok, BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  198. {
  199. typedef typename boost::asio::async_completion<CompletionToken,
  200. void(boost::system::error_code, int, double)>::completion_handler_type
  201. handler_type;
  202. boost::asio::async_completion<CompletionToken,
  203. void(boost::system::error_code, int, double)> completion(token);
  204. typename boost::asio::associated_allocator<handler_type>::type a
  205. = boost::asio::get_associated_allocator(completion.completion_handler);
  206. typename boost::asio::associated_executor<handler_type>::type ex
  207. = boost::asio::get_associated_executor(completion.completion_handler);
  208. if (ok)
  209. {
  210. ex.post(
  211. bindns::bind(
  212. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  213. boost::system::error_code(), 42, 2.0), a);
  214. }
  215. else
  216. {
  217. ex.post(
  218. bindns::bind(
  219. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  220. boost::system::error_code(boost::asio::error::operation_aborted),
  221. 0, 0.0), a);
  222. }
  223. return completion.result.get();
  224. }
  225. template <typename CompletionToken>
  226. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken,
  227. void(std::exception_ptr, int, double))
  228. async_op_ex_2(bool ok, BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  229. {
  230. typedef typename boost::asio::async_completion<CompletionToken,
  231. void(std::exception_ptr, int, double)>::completion_handler_type
  232. handler_type;
  233. boost::asio::async_completion<CompletionToken,
  234. void(std::exception_ptr, int, double)> completion(token);
  235. typename boost::asio::associated_allocator<handler_type>::type a
  236. = boost::asio::get_associated_allocator(completion.completion_handler);
  237. typename boost::asio::associated_executor<handler_type>::type ex
  238. = boost::asio::get_associated_executor(completion.completion_handler);
  239. if (ok)
  240. {
  241. ex.post(
  242. bindns::bind(
  243. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  244. std::exception_ptr(), 42, 2.0), a);
  245. }
  246. else
  247. {
  248. ex.post(
  249. bindns::bind(
  250. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  251. std::make_exception_ptr(std::runtime_error("blah")), 0, 0.0), a);
  252. }
  253. return completion.result.get();
  254. }
  255. template <typename CompletionToken>
  256. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken, void(int, double, char))
  257. async_op_3(BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  258. {
  259. typedef typename boost::asio::async_completion<CompletionToken,
  260. void(int, double, char)>::completion_handler_type handler_type;
  261. boost::asio::async_completion<CompletionToken,
  262. void(int, double, char)> completion(token);
  263. typename boost::asio::associated_allocator<handler_type>::type a
  264. = boost::asio::get_associated_allocator(completion.completion_handler);
  265. typename boost::asio::associated_executor<handler_type>::type ex
  266. = boost::asio::get_associated_executor(completion.completion_handler);
  267. ex.post(
  268. bindns::bind(
  269. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  270. 42, 2.0, 'a'), a);
  271. return completion.result.get();
  272. }
  273. template <typename CompletionToken>
  274. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken,
  275. void(boost::system::error_code, int, double, char))
  276. async_op_ec_3(bool ok, BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  277. {
  278. typedef typename boost::asio::async_completion<CompletionToken,
  279. void(boost::system::error_code, int, double, char)>::completion_handler_type
  280. handler_type;
  281. boost::asio::async_completion<CompletionToken,
  282. void(boost::system::error_code, int, double, char)> completion(token);
  283. typename boost::asio::associated_allocator<handler_type>::type a
  284. = boost::asio::get_associated_allocator(completion.completion_handler);
  285. typename boost::asio::associated_executor<handler_type>::type ex
  286. = boost::asio::get_associated_executor(completion.completion_handler);
  287. if (ok)
  288. {
  289. ex.post(
  290. bindns::bind(
  291. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  292. boost::system::error_code(), 42, 2.0, 'a'), a);
  293. }
  294. else
  295. {
  296. ex.post(
  297. bindns::bind(
  298. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  299. boost::system::error_code(boost::asio::error::operation_aborted),
  300. 0, 0.0, 'z'), a);
  301. }
  302. return completion.result.get();
  303. }
  304. template <typename CompletionToken>
  305. BOOST_ASIO_INITFN_RESULT_TYPE(CompletionToken,
  306. void(std::exception_ptr, int, double, char))
  307. async_op_ex_3(bool ok, BOOST_ASIO_MOVE_ARG(CompletionToken) token)
  308. {
  309. typedef typename boost::asio::async_completion<CompletionToken,
  310. void(std::exception_ptr, int, double, char)>::completion_handler_type
  311. handler_type;
  312. boost::asio::async_completion<CompletionToken,
  313. void(std::exception_ptr, int, double, char)> completion(token);
  314. typename boost::asio::associated_allocator<handler_type>::type a
  315. = boost::asio::get_associated_allocator(completion.completion_handler);
  316. typename boost::asio::associated_executor<handler_type>::type ex
  317. = boost::asio::get_associated_executor(completion.completion_handler);
  318. if (ok)
  319. {
  320. ex.post(
  321. bindns::bind(
  322. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  323. std::exception_ptr(), 42, 2.0, 'a'), a);
  324. }
  325. else
  326. {
  327. ex.post(
  328. bindns::bind(
  329. BOOST_ASIO_MOVE_CAST(handler_type)(completion.completion_handler),
  330. std::make_exception_ptr(std::runtime_error("blah")),
  331. 0, 0.0, 'z'), a);
  332. }
  333. return completion.result.get();
  334. }
  335. } // namespace archetypes
  336. #endif // ARCHETYPES_ASYNC_OPS_HPP