write_at.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. //
  2. // impl/write_at.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2021 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_IMPL_WRITE_AT_HPP
  11. #define BOOST_ASIO_IMPL_WRITE_AT_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <boost/asio/associated_allocator.hpp>
  16. #include <boost/asio/associated_executor.hpp>
  17. #include <boost/asio/buffer.hpp>
  18. #include <boost/asio/completion_condition.hpp>
  19. #include <boost/asio/detail/array_fwd.hpp>
  20. #include <boost/asio/detail/base_from_completion_cond.hpp>
  21. #include <boost/asio/detail/bind_handler.hpp>
  22. #include <boost/asio/detail/consuming_buffers.hpp>
  23. #include <boost/asio/detail/dependent_type.hpp>
  24. #include <boost/asio/detail/handler_alloc_helpers.hpp>
  25. #include <boost/asio/detail/handler_cont_helpers.hpp>
  26. #include <boost/asio/detail/handler_invoke_helpers.hpp>
  27. #include <boost/asio/detail/handler_tracking.hpp>
  28. #include <boost/asio/detail/handler_type_requirements.hpp>
  29. #include <boost/asio/detail/non_const_lvalue.hpp>
  30. #include <boost/asio/detail/throw_error.hpp>
  31. #include <boost/asio/detail/push_options.hpp>
  32. namespace boost {
  33. namespace asio {
  34. namespace detail
  35. {
  36. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  37. typename ConstBufferIterator, typename CompletionCondition>
  38. std::size_t write_at_buffer_sequence(SyncRandomAccessWriteDevice& d,
  39. uint64_t offset, const ConstBufferSequence& buffers,
  40. const ConstBufferIterator&, CompletionCondition completion_condition,
  41. boost::system::error_code& ec)
  42. {
  43. ec = boost::system::error_code();
  44. boost::asio::detail::consuming_buffers<const_buffer,
  45. ConstBufferSequence, ConstBufferIterator> tmp(buffers);
  46. while (!tmp.empty())
  47. {
  48. if (std::size_t max_size = detail::adapt_completion_condition_result(
  49. completion_condition(ec, tmp.total_consumed())))
  50. {
  51. tmp.consume(d.write_some_at(offset + tmp.total_consumed(),
  52. tmp.prepare(max_size), ec));
  53. }
  54. else
  55. break;
  56. }
  57. return tmp.total_consumed();
  58. }
  59. } // namespace detail
  60. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  61. typename CompletionCondition>
  62. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  63. uint64_t offset, const ConstBufferSequence& buffers,
  64. CompletionCondition completion_condition, boost::system::error_code& ec)
  65. {
  66. return detail::write_at_buffer_sequence(d, offset, buffers,
  67. boost::asio::buffer_sequence_begin(buffers),
  68. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
  69. }
  70. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
  71. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  72. uint64_t offset, const ConstBufferSequence& buffers)
  73. {
  74. boost::system::error_code ec;
  75. std::size_t bytes_transferred = write_at(
  76. d, offset, buffers, transfer_all(), ec);
  77. boost::asio::detail::throw_error(ec, "write_at");
  78. return bytes_transferred;
  79. }
  80. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
  81. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  82. uint64_t offset, const ConstBufferSequence& buffers,
  83. boost::system::error_code& ec)
  84. {
  85. return write_at(d, offset, buffers, transfer_all(), ec);
  86. }
  87. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  88. typename CompletionCondition>
  89. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  90. uint64_t offset, const ConstBufferSequence& buffers,
  91. CompletionCondition completion_condition)
  92. {
  93. boost::system::error_code ec;
  94. std::size_t bytes_transferred = write_at(d, offset, buffers,
  95. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
  96. boost::asio::detail::throw_error(ec, "write_at");
  97. return bytes_transferred;
  98. }
  99. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  100. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  101. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  102. typename CompletionCondition>
  103. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  104. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  105. CompletionCondition completion_condition, boost::system::error_code& ec)
  106. {
  107. std::size_t bytes_transferred = write_at(d, offset, b.data(),
  108. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
  109. b.consume(bytes_transferred);
  110. return bytes_transferred;
  111. }
  112. template <typename SyncRandomAccessWriteDevice, typename Allocator>
  113. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  114. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b)
  115. {
  116. boost::system::error_code ec;
  117. std::size_t bytes_transferred = write_at(d, offset, b, transfer_all(), ec);
  118. boost::asio::detail::throw_error(ec, "write_at");
  119. return bytes_transferred;
  120. }
  121. template <typename SyncRandomAccessWriteDevice, typename Allocator>
  122. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  123. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  124. boost::system::error_code& ec)
  125. {
  126. return write_at(d, offset, b, transfer_all(), ec);
  127. }
  128. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  129. typename CompletionCondition>
  130. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  131. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  132. CompletionCondition completion_condition)
  133. {
  134. boost::system::error_code ec;
  135. std::size_t bytes_transferred = write_at(d, offset, b,
  136. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
  137. boost::asio::detail::throw_error(ec, "write_at");
  138. return bytes_transferred;
  139. }
  140. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  141. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  142. namespace detail
  143. {
  144. template <typename AsyncRandomAccessWriteDevice,
  145. typename ConstBufferSequence, typename ConstBufferIterator,
  146. typename CompletionCondition, typename WriteHandler>
  147. class write_at_op
  148. : detail::base_from_completion_cond<CompletionCondition>
  149. {
  150. public:
  151. write_at_op(AsyncRandomAccessWriteDevice& device,
  152. uint64_t offset, const ConstBufferSequence& buffers,
  153. CompletionCondition& completion_condition, WriteHandler& handler)
  154. : detail::base_from_completion_cond<
  155. CompletionCondition>(completion_condition),
  156. device_(device),
  157. offset_(offset),
  158. buffers_(buffers),
  159. start_(0),
  160. handler_(BOOST_ASIO_MOVE_CAST(WriteHandler)(handler))
  161. {
  162. }
  163. #if defined(BOOST_ASIO_HAS_MOVE)
  164. write_at_op(const write_at_op& other)
  165. : detail::base_from_completion_cond<CompletionCondition>(other),
  166. device_(other.device_),
  167. offset_(other.offset_),
  168. buffers_(other.buffers_),
  169. start_(other.start_),
  170. handler_(other.handler_)
  171. {
  172. }
  173. write_at_op(write_at_op&& other)
  174. : detail::base_from_completion_cond<CompletionCondition>(
  175. BOOST_ASIO_MOVE_CAST(detail::base_from_completion_cond<
  176. CompletionCondition>)(other)),
  177. device_(other.device_),
  178. offset_(other.offset_),
  179. buffers_(BOOST_ASIO_MOVE_CAST(buffers_type)(other.buffers_)),
  180. start_(other.start_),
  181. handler_(BOOST_ASIO_MOVE_CAST(WriteHandler)(other.handler_))
  182. {
  183. }
  184. #endif // defined(BOOST_ASIO_HAS_MOVE)
  185. void operator()(const boost::system::error_code& ec,
  186. std::size_t bytes_transferred, int start = 0)
  187. {
  188. std::size_t max_size;
  189. switch (start_ = start)
  190. {
  191. case 1:
  192. max_size = this->check_for_completion(ec, buffers_.total_consumed());
  193. do
  194. {
  195. {
  196. BOOST_ASIO_HANDLER_LOCATION((__FILE__, __LINE__, "async_write_at"));
  197. device_.async_write_some_at(
  198. offset_ + buffers_.total_consumed(), buffers_.prepare(max_size),
  199. BOOST_ASIO_MOVE_CAST(write_at_op)(*this));
  200. }
  201. return; default:
  202. buffers_.consume(bytes_transferred);
  203. if ((!ec && bytes_transferred == 0) || buffers_.empty())
  204. break;
  205. max_size = this->check_for_completion(ec, buffers_.total_consumed());
  206. } while (max_size > 0);
  207. handler_(ec, buffers_.total_consumed());
  208. }
  209. }
  210. //private:
  211. typedef boost::asio::detail::consuming_buffers<const_buffer,
  212. ConstBufferSequence, ConstBufferIterator> buffers_type;
  213. AsyncRandomAccessWriteDevice& device_;
  214. uint64_t offset_;
  215. buffers_type buffers_;
  216. int start_;
  217. WriteHandler handler_;
  218. };
  219. template <typename AsyncRandomAccessWriteDevice,
  220. typename ConstBufferSequence, typename ConstBufferIterator,
  221. typename CompletionCondition, typename WriteHandler>
  222. inline asio_handler_allocate_is_deprecated
  223. asio_handler_allocate(std::size_t size,
  224. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  225. ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
  226. {
  227. #if defined(BOOST_ASIO_NO_DEPRECATED)
  228. boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_);
  229. return asio_handler_allocate_is_no_longer_used();
  230. #else // defined(BOOST_ASIO_NO_DEPRECATED)
  231. return boost_asio_handler_alloc_helpers::allocate(
  232. size, this_handler->handler_);
  233. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  234. }
  235. template <typename AsyncRandomAccessWriteDevice,
  236. typename ConstBufferSequence, typename ConstBufferIterator,
  237. typename CompletionCondition, typename WriteHandler>
  238. inline asio_handler_deallocate_is_deprecated
  239. asio_handler_deallocate(void* pointer, std::size_t size,
  240. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  241. ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
  242. {
  243. boost_asio_handler_alloc_helpers::deallocate(
  244. pointer, size, this_handler->handler_);
  245. #if defined(BOOST_ASIO_NO_DEPRECATED)
  246. return asio_handler_deallocate_is_no_longer_used();
  247. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  248. }
  249. template <typename AsyncRandomAccessWriteDevice,
  250. typename ConstBufferSequence, typename ConstBufferIterator,
  251. typename CompletionCondition, typename WriteHandler>
  252. inline bool asio_handler_is_continuation(
  253. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  254. ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
  255. {
  256. return this_handler->start_ == 0 ? true
  257. : boost_asio_handler_cont_helpers::is_continuation(
  258. this_handler->handler_);
  259. }
  260. template <typename Function, typename AsyncRandomAccessWriteDevice,
  261. typename ConstBufferSequence, typename ConstBufferIterator,
  262. typename CompletionCondition, typename WriteHandler>
  263. inline asio_handler_invoke_is_deprecated
  264. asio_handler_invoke(Function& function,
  265. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  266. ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
  267. {
  268. boost_asio_handler_invoke_helpers::invoke(
  269. function, this_handler->handler_);
  270. #if defined(BOOST_ASIO_NO_DEPRECATED)
  271. return asio_handler_invoke_is_no_longer_used();
  272. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  273. }
  274. template <typename Function, typename AsyncRandomAccessWriteDevice,
  275. typename ConstBufferSequence, typename ConstBufferIterator,
  276. typename CompletionCondition, typename WriteHandler>
  277. inline asio_handler_invoke_is_deprecated
  278. asio_handler_invoke(const Function& function,
  279. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  280. ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
  281. {
  282. boost_asio_handler_invoke_helpers::invoke(
  283. function, this_handler->handler_);
  284. #if defined(BOOST_ASIO_NO_DEPRECATED)
  285. return asio_handler_invoke_is_no_longer_used();
  286. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  287. }
  288. template <typename AsyncRandomAccessWriteDevice,
  289. typename ConstBufferSequence, typename ConstBufferIterator,
  290. typename CompletionCondition, typename WriteHandler>
  291. inline void start_write_at_buffer_sequence_op(AsyncRandomAccessWriteDevice& d,
  292. uint64_t offset, const ConstBufferSequence& buffers,
  293. const ConstBufferIterator&, CompletionCondition& completion_condition,
  294. WriteHandler& handler)
  295. {
  296. detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  297. ConstBufferIterator, CompletionCondition, WriteHandler>(
  298. d, offset, buffers, completion_condition, handler)(
  299. boost::system::error_code(), 0, 1);
  300. }
  301. template <typename AsyncRandomAccessWriteDevice>
  302. class initiate_async_write_at_buffer_sequence
  303. {
  304. public:
  305. typedef typename AsyncRandomAccessWriteDevice::executor_type executor_type;
  306. explicit initiate_async_write_at_buffer_sequence(
  307. AsyncRandomAccessWriteDevice& device)
  308. : device_(device)
  309. {
  310. }
  311. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  312. {
  313. return device_.get_executor();
  314. }
  315. template <typename WriteHandler, typename ConstBufferSequence,
  316. typename CompletionCondition>
  317. void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  318. uint64_t offset, const ConstBufferSequence& buffers,
  319. BOOST_ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
  320. {
  321. // If you get an error on the following line it means that your handler
  322. // does not meet the documented type requirements for a WriteHandler.
  323. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  324. non_const_lvalue<WriteHandler> handler2(handler);
  325. non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
  326. start_write_at_buffer_sequence_op(device_, offset, buffers,
  327. boost::asio::buffer_sequence_begin(buffers),
  328. completion_cond2.value, handler2.value);
  329. }
  330. private:
  331. AsyncRandomAccessWriteDevice& device_;
  332. };
  333. } // namespace detail
  334. #if !defined(GENERATING_DOCUMENTATION)
  335. template <typename AsyncRandomAccessWriteDevice,
  336. typename ConstBufferSequence, typename ConstBufferIterator,
  337. typename CompletionCondition, typename WriteHandler, typename Allocator>
  338. struct associated_allocator<
  339. detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  340. ConstBufferIterator, CompletionCondition, WriteHandler>,
  341. Allocator>
  342. {
  343. typedef typename associated_allocator<WriteHandler, Allocator>::type type;
  344. static type get(
  345. const detail::write_at_op<AsyncRandomAccessWriteDevice,
  346. ConstBufferSequence, ConstBufferIterator,
  347. CompletionCondition, WriteHandler>& h,
  348. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  349. {
  350. return associated_allocator<WriteHandler, Allocator>::get(h.handler_, a);
  351. }
  352. };
  353. template <typename AsyncRandomAccessWriteDevice,
  354. typename ConstBufferSequence, typename ConstBufferIterator,
  355. typename CompletionCondition, typename WriteHandler, typename Executor>
  356. struct associated_executor<
  357. detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  358. ConstBufferIterator, CompletionCondition, WriteHandler>,
  359. Executor>
  360. : detail::associated_executor_forwarding_base<WriteHandler, Executor>
  361. {
  362. typedef typename associated_executor<WriteHandler, Executor>::type type;
  363. static type get(
  364. const detail::write_at_op<AsyncRandomAccessWriteDevice,
  365. ConstBufferSequence, ConstBufferIterator,
  366. CompletionCondition, WriteHandler>& h,
  367. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  368. {
  369. return associated_executor<WriteHandler, Executor>::get(h.handler_, ex);
  370. }
  371. };
  372. #endif // !defined(GENERATING_DOCUMENTATION)
  373. template <typename AsyncRandomAccessWriteDevice,
  374. typename ConstBufferSequence, typename CompletionCondition,
  375. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  376. std::size_t)) WriteHandler>
  377. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  378. void (boost::system::error_code, std::size_t))
  379. async_write_at(AsyncRandomAccessWriteDevice& d,
  380. uint64_t offset, const ConstBufferSequence& buffers,
  381. CompletionCondition completion_condition,
  382. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  383. {
  384. return async_initiate<WriteHandler,
  385. void (boost::system::error_code, std::size_t)>(
  386. detail::initiate_async_write_at_buffer_sequence<
  387. AsyncRandomAccessWriteDevice>(d),
  388. handler, offset, buffers,
  389. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
  390. }
  391. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  392. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  393. std::size_t)) WriteHandler>
  394. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  395. void (boost::system::error_code, std::size_t))
  396. async_write_at(AsyncRandomAccessWriteDevice& d,
  397. uint64_t offset, const ConstBufferSequence& buffers,
  398. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  399. {
  400. return async_initiate<WriteHandler,
  401. void (boost::system::error_code, std::size_t)>(
  402. detail::initiate_async_write_at_buffer_sequence<
  403. AsyncRandomAccessWriteDevice>(d),
  404. handler, offset, buffers, transfer_all());
  405. }
  406. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  407. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  408. namespace detail
  409. {
  410. template <typename Allocator, typename WriteHandler>
  411. class write_at_streambuf_op
  412. {
  413. public:
  414. write_at_streambuf_op(
  415. boost::asio::basic_streambuf<Allocator>& streambuf,
  416. WriteHandler& handler)
  417. : streambuf_(streambuf),
  418. handler_(BOOST_ASIO_MOVE_CAST(WriteHandler)(handler))
  419. {
  420. }
  421. #if defined(BOOST_ASIO_HAS_MOVE)
  422. write_at_streambuf_op(const write_at_streambuf_op& other)
  423. : streambuf_(other.streambuf_),
  424. handler_(other.handler_)
  425. {
  426. }
  427. write_at_streambuf_op(write_at_streambuf_op&& other)
  428. : streambuf_(other.streambuf_),
  429. handler_(BOOST_ASIO_MOVE_CAST(WriteHandler)(other.handler_))
  430. {
  431. }
  432. #endif // defined(BOOST_ASIO_HAS_MOVE)
  433. void operator()(const boost::system::error_code& ec,
  434. const std::size_t bytes_transferred)
  435. {
  436. streambuf_.consume(bytes_transferred);
  437. handler_(ec, bytes_transferred);
  438. }
  439. //private:
  440. boost::asio::basic_streambuf<Allocator>& streambuf_;
  441. WriteHandler handler_;
  442. };
  443. template <typename Allocator, typename WriteHandler>
  444. inline asio_handler_allocate_is_deprecated
  445. asio_handler_allocate(std::size_t size,
  446. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  447. {
  448. #if defined(BOOST_ASIO_NO_DEPRECATED)
  449. boost_asio_handler_alloc_helpers::allocate(size, this_handler->handler_);
  450. return asio_handler_allocate_is_no_longer_used();
  451. #else // defined(BOOST_ASIO_NO_DEPRECATED)
  452. return boost_asio_handler_alloc_helpers::allocate(
  453. size, this_handler->handler_);
  454. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  455. }
  456. template <typename Allocator, typename WriteHandler>
  457. inline asio_handler_deallocate_is_deprecated
  458. asio_handler_deallocate(void* pointer, std::size_t size,
  459. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  460. {
  461. boost_asio_handler_alloc_helpers::deallocate(
  462. pointer, size, this_handler->handler_);
  463. #if defined(BOOST_ASIO_NO_DEPRECATED)
  464. return asio_handler_deallocate_is_no_longer_used();
  465. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  466. }
  467. template <typename Allocator, typename WriteHandler>
  468. inline bool asio_handler_is_continuation(
  469. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  470. {
  471. return boost_asio_handler_cont_helpers::is_continuation(
  472. this_handler->handler_);
  473. }
  474. template <typename Function, typename Allocator, typename WriteHandler>
  475. inline asio_handler_invoke_is_deprecated
  476. asio_handler_invoke(Function& function,
  477. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  478. {
  479. boost_asio_handler_invoke_helpers::invoke(
  480. function, this_handler->handler_);
  481. #if defined(BOOST_ASIO_NO_DEPRECATED)
  482. return asio_handler_invoke_is_no_longer_used();
  483. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  484. }
  485. template <typename Function, typename Allocator, typename WriteHandler>
  486. inline asio_handler_invoke_is_deprecated
  487. asio_handler_invoke(const Function& function,
  488. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  489. {
  490. boost_asio_handler_invoke_helpers::invoke(
  491. function, this_handler->handler_);
  492. #if defined(BOOST_ASIO_NO_DEPRECATED)
  493. return asio_handler_invoke_is_no_longer_used();
  494. #endif // defined(BOOST_ASIO_NO_DEPRECATED)
  495. }
  496. template <typename AsyncRandomAccessWriteDevice>
  497. class initiate_async_write_at_streambuf
  498. {
  499. public:
  500. typedef typename AsyncRandomAccessWriteDevice::executor_type executor_type;
  501. explicit initiate_async_write_at_streambuf(
  502. AsyncRandomAccessWriteDevice& device)
  503. : device_(device)
  504. {
  505. }
  506. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  507. {
  508. return device_.get_executor();
  509. }
  510. template <typename WriteHandler,
  511. typename Allocator, typename CompletionCondition>
  512. void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  513. uint64_t offset, basic_streambuf<Allocator>* b,
  514. BOOST_ASIO_MOVE_ARG(CompletionCondition) completion_condition) const
  515. {
  516. // If you get an error on the following line it means that your handler
  517. // does not meet the documented type requirements for a WriteHandler.
  518. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  519. non_const_lvalue<WriteHandler> handler2(handler);
  520. async_write_at(device_, offset, b->data(),
  521. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition),
  522. write_at_streambuf_op<Allocator, typename decay<WriteHandler>::type>(
  523. *b, handler2.value));
  524. }
  525. private:
  526. AsyncRandomAccessWriteDevice& device_;
  527. };
  528. } // namespace detail
  529. #if !defined(GENERATING_DOCUMENTATION)
  530. template <typename Allocator, typename WriteHandler, typename Allocator1>
  531. struct associated_allocator<
  532. detail::write_at_streambuf_op<Allocator, WriteHandler>,
  533. Allocator1>
  534. {
  535. typedef typename associated_allocator<WriteHandler, Allocator1>::type type;
  536. static type get(
  537. const detail::write_at_streambuf_op<Allocator, WriteHandler>& h,
  538. const Allocator1& a = Allocator1()) BOOST_ASIO_NOEXCEPT
  539. {
  540. return associated_allocator<WriteHandler, Allocator1>::get(h.handler_, a);
  541. }
  542. };
  543. template <typename Executor, typename WriteHandler, typename Executor1>
  544. struct associated_executor<
  545. detail::write_at_streambuf_op<Executor, WriteHandler>,
  546. Executor1>
  547. : detail::associated_executor_forwarding_base<WriteHandler, Executor>
  548. {
  549. typedef typename associated_executor<WriteHandler, Executor1>::type type;
  550. static type get(
  551. const detail::write_at_streambuf_op<Executor, WriteHandler>& h,
  552. const Executor1& ex = Executor1()) BOOST_ASIO_NOEXCEPT
  553. {
  554. return associated_executor<WriteHandler, Executor1>::get(h.handler_, ex);
  555. }
  556. };
  557. #endif // !defined(GENERATING_DOCUMENTATION)
  558. template <typename AsyncRandomAccessWriteDevice,
  559. typename Allocator, typename CompletionCondition,
  560. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  561. std::size_t)) WriteHandler>
  562. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  563. void (boost::system::error_code, std::size_t))
  564. async_write_at(AsyncRandomAccessWriteDevice& d,
  565. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  566. CompletionCondition completion_condition,
  567. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  568. {
  569. return async_initiate<WriteHandler,
  570. void (boost::system::error_code, std::size_t)>(
  571. detail::initiate_async_write_at_streambuf<
  572. AsyncRandomAccessWriteDevice>(d),
  573. handler, offset, &b,
  574. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
  575. }
  576. template <typename AsyncRandomAccessWriteDevice, typename Allocator,
  577. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  578. std::size_t)) WriteHandler>
  579. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  580. void (boost::system::error_code, std::size_t))
  581. async_write_at(AsyncRandomAccessWriteDevice& d,
  582. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  583. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  584. {
  585. return async_initiate<WriteHandler,
  586. void (boost::system::error_code, std::size_t)>(
  587. detail::initiate_async_write_at_streambuf<
  588. AsyncRandomAccessWriteDevice>(d),
  589. handler, offset, &b, transfer_all());
  590. }
  591. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  592. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  593. } // namespace asio
  594. } // namespace boost
  595. #include <boost/asio/detail/pop_options.hpp>
  596. #endif // BOOST_ASIO_IMPL_WRITE_AT_HPP