write_at.hpp 25 KB

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