write_at.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. //
  2. // impl/write_at.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 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_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. : detail::base_from_completion_cond<CompletionCondition>
  148. {
  149. public:
  150. write_at_op(AsyncRandomAccessWriteDevice& device,
  151. uint64_t offset, const ConstBufferSequence& buffers,
  152. CompletionCondition& completion_condition, WriteHandler& handler)
  153. : detail::base_from_completion_cond<
  154. CompletionCondition>(completion_condition),
  155. device_(device),
  156. offset_(offset),
  157. buffers_(buffers),
  158. start_(0),
  159. handler_(BOOST_ASIO_MOVE_CAST(WriteHandler)(handler))
  160. {
  161. }
  162. #if defined(BOOST_ASIO_HAS_MOVE)
  163. write_at_op(const write_at_op& other)
  164. : detail::base_from_completion_cond<CompletionCondition>(other),
  165. device_(other.device_),
  166. offset_(other.offset_),
  167. buffers_(other.buffers_),
  168. start_(other.start_),
  169. handler_(other.handler_)
  170. {
  171. }
  172. write_at_op(write_at_op&& other)
  173. : detail::base_from_completion_cond<CompletionCondition>(
  174. BOOST_ASIO_MOVE_CAST(detail::base_from_completion_cond<
  175. CompletionCondition>)(other)),
  176. device_(other.device_),
  177. offset_(other.offset_),
  178. buffers_(BOOST_ASIO_MOVE_CAST(buffers_type)(other.buffers_)),
  179. start_(other.start_),
  180. handler_(BOOST_ASIO_MOVE_CAST(WriteHandler)(other.handler_))
  181. {
  182. }
  183. #endif // defined(BOOST_ASIO_HAS_MOVE)
  184. void operator()(const boost::system::error_code& ec,
  185. std::size_t bytes_transferred, int start = 0)
  186. {
  187. std::size_t max_size;
  188. switch (start_ = start)
  189. {
  190. case 1:
  191. max_size = this->check_for_completion(ec, buffers_.total_consumed());
  192. do
  193. {
  194. device_.async_write_some_at(
  195. offset_ + buffers_.total_consumed(), buffers_.prepare(max_size),
  196. BOOST_ASIO_MOVE_CAST(write_at_op)(*this));
  197. return; default:
  198. buffers_.consume(bytes_transferred);
  199. if ((!ec && bytes_transferred == 0) || buffers_.empty())
  200. break;
  201. max_size = this->check_for_completion(ec, buffers_.total_consumed());
  202. } while (max_size > 0);
  203. handler_(ec, buffers_.total_consumed());
  204. }
  205. }
  206. //private:
  207. typedef boost::asio::detail::consuming_buffers<const_buffer,
  208. ConstBufferSequence, ConstBufferIterator> buffers_type;
  209. AsyncRandomAccessWriteDevice& device_;
  210. uint64_t offset_;
  211. buffers_type buffers_;
  212. int start_;
  213. WriteHandler handler_;
  214. };
  215. template <typename AsyncRandomAccessWriteDevice,
  216. typename ConstBufferSequence, typename ConstBufferIterator,
  217. typename CompletionCondition, typename WriteHandler>
  218. inline void* asio_handler_allocate(std::size_t size,
  219. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  220. ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
  221. {
  222. return boost_asio_handler_alloc_helpers::allocate(
  223. size, this_handler->handler_);
  224. }
  225. template <typename AsyncRandomAccessWriteDevice,
  226. typename ConstBufferSequence, typename ConstBufferIterator,
  227. typename CompletionCondition, typename WriteHandler>
  228. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  229. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  230. ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
  231. {
  232. boost_asio_handler_alloc_helpers::deallocate(
  233. pointer, size, this_handler->handler_);
  234. }
  235. template <typename AsyncRandomAccessWriteDevice,
  236. typename ConstBufferSequence, typename ConstBufferIterator,
  237. typename CompletionCondition, typename WriteHandler>
  238. inline bool asio_handler_is_continuation(
  239. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  240. ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
  241. {
  242. return this_handler->start_ == 0 ? true
  243. : boost_asio_handler_cont_helpers::is_continuation(
  244. this_handler->handler_);
  245. }
  246. template <typename Function, typename AsyncRandomAccessWriteDevice,
  247. typename ConstBufferSequence, typename ConstBufferIterator,
  248. typename CompletionCondition, typename WriteHandler>
  249. inline void asio_handler_invoke(Function& function,
  250. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  251. ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
  252. {
  253. boost_asio_handler_invoke_helpers::invoke(
  254. function, this_handler->handler_);
  255. }
  256. template <typename Function, typename AsyncRandomAccessWriteDevice,
  257. typename ConstBufferSequence, typename ConstBufferIterator,
  258. typename CompletionCondition, typename WriteHandler>
  259. inline void asio_handler_invoke(const Function& function,
  260. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  261. ConstBufferIterator, CompletionCondition, WriteHandler>* this_handler)
  262. {
  263. boost_asio_handler_invoke_helpers::invoke(
  264. function, this_handler->handler_);
  265. }
  266. template <typename AsyncRandomAccessWriteDevice,
  267. typename ConstBufferSequence, typename ConstBufferIterator,
  268. typename CompletionCondition, typename WriteHandler>
  269. inline void start_write_at_buffer_sequence_op(AsyncRandomAccessWriteDevice& d,
  270. uint64_t offset, const ConstBufferSequence& buffers,
  271. const ConstBufferIterator&, CompletionCondition& completion_condition,
  272. WriteHandler& handler)
  273. {
  274. detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  275. ConstBufferIterator, CompletionCondition, WriteHandler>(
  276. d, offset, buffers, completion_condition, handler)(
  277. boost::system::error_code(), 0, 1);
  278. }
  279. template <typename AsyncRandomAccessWriteDevice>
  280. class initiate_async_write_at_buffer_sequence
  281. {
  282. public:
  283. typedef typename AsyncRandomAccessWriteDevice::executor_type executor_type;
  284. explicit initiate_async_write_at_buffer_sequence(
  285. AsyncRandomAccessWriteDevice& device)
  286. : device_(device)
  287. {
  288. }
  289. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  290. {
  291. return device_.get_executor();
  292. }
  293. template <typename WriteHandler, typename ConstBufferSequence,
  294. typename CompletionCondition>
  295. void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  296. uint64_t offset, const ConstBufferSequence& buffers,
  297. BOOST_ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
  298. {
  299. // If you get an error on the following line it means that your handler
  300. // does not meet the documented type requirements for a WriteHandler.
  301. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  302. non_const_lvalue<WriteHandler> handler2(handler);
  303. non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
  304. start_write_at_buffer_sequence_op(device_, offset, buffers,
  305. boost::asio::buffer_sequence_begin(buffers),
  306. completion_cond2.value, handler2.value);
  307. }
  308. private:
  309. AsyncRandomAccessWriteDevice& device_;
  310. };
  311. } // namespace detail
  312. #if !defined(GENERATING_DOCUMENTATION)
  313. template <typename AsyncRandomAccessWriteDevice,
  314. typename ConstBufferSequence, typename ConstBufferIterator,
  315. typename CompletionCondition, typename WriteHandler, typename Allocator>
  316. struct associated_allocator<
  317. detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  318. ConstBufferIterator, CompletionCondition, WriteHandler>,
  319. Allocator>
  320. {
  321. typedef typename associated_allocator<WriteHandler, Allocator>::type type;
  322. static type get(
  323. const detail::write_at_op<AsyncRandomAccessWriteDevice,
  324. ConstBufferSequence, ConstBufferIterator,
  325. CompletionCondition, WriteHandler>& h,
  326. const Allocator& a = Allocator()) BOOST_ASIO_NOEXCEPT
  327. {
  328. return associated_allocator<WriteHandler, Allocator>::get(h.handler_, a);
  329. }
  330. };
  331. template <typename AsyncRandomAccessWriteDevice,
  332. typename ConstBufferSequence, typename ConstBufferIterator,
  333. typename CompletionCondition, typename WriteHandler, typename Executor>
  334. struct associated_executor<
  335. detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  336. ConstBufferIterator, CompletionCondition, WriteHandler>,
  337. Executor>
  338. {
  339. typedef typename associated_executor<WriteHandler, Executor>::type type;
  340. static type get(
  341. const detail::write_at_op<AsyncRandomAccessWriteDevice,
  342. ConstBufferSequence, ConstBufferIterator,
  343. CompletionCondition, WriteHandler>& h,
  344. const Executor& ex = Executor()) BOOST_ASIO_NOEXCEPT
  345. {
  346. return associated_executor<WriteHandler, Executor>::get(h.handler_, ex);
  347. }
  348. };
  349. #endif // !defined(GENERATING_DOCUMENTATION)
  350. template <typename AsyncRandomAccessWriteDevice,
  351. typename ConstBufferSequence, typename CompletionCondition,
  352. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  353. std::size_t)) WriteHandler>
  354. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  355. void (boost::system::error_code, std::size_t))
  356. async_write_at(AsyncRandomAccessWriteDevice& d,
  357. uint64_t offset, const ConstBufferSequence& buffers,
  358. CompletionCondition completion_condition,
  359. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  360. {
  361. return async_initiate<WriteHandler,
  362. void (boost::system::error_code, std::size_t)>(
  363. detail::initiate_async_write_at_buffer_sequence<
  364. AsyncRandomAccessWriteDevice>(d),
  365. handler, offset, buffers,
  366. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
  367. }
  368. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  369. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  370. std::size_t)) WriteHandler>
  371. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  372. void (boost::system::error_code, std::size_t))
  373. async_write_at(AsyncRandomAccessWriteDevice& d,
  374. uint64_t offset, const ConstBufferSequence& buffers,
  375. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  376. {
  377. return async_initiate<WriteHandler,
  378. void (boost::system::error_code, std::size_t)>(
  379. detail::initiate_async_write_at_buffer_sequence<
  380. AsyncRandomAccessWriteDevice>(d),
  381. handler, offset, buffers, transfer_all());
  382. }
  383. #if !defined(BOOST_ASIO_NO_EXTENSIONS)
  384. #if !defined(BOOST_ASIO_NO_IOSTREAM)
  385. namespace detail
  386. {
  387. template <typename Allocator, typename WriteHandler>
  388. class write_at_streambuf_op
  389. {
  390. public:
  391. write_at_streambuf_op(
  392. boost::asio::basic_streambuf<Allocator>& streambuf,
  393. WriteHandler& handler)
  394. : streambuf_(streambuf),
  395. handler_(BOOST_ASIO_MOVE_CAST(WriteHandler)(handler))
  396. {
  397. }
  398. #if defined(BOOST_ASIO_HAS_MOVE)
  399. write_at_streambuf_op(const write_at_streambuf_op& other)
  400. : streambuf_(other.streambuf_),
  401. handler_(other.handler_)
  402. {
  403. }
  404. write_at_streambuf_op(write_at_streambuf_op&& other)
  405. : streambuf_(other.streambuf_),
  406. handler_(BOOST_ASIO_MOVE_CAST(WriteHandler)(other.handler_))
  407. {
  408. }
  409. #endif // defined(BOOST_ASIO_HAS_MOVE)
  410. void operator()(const boost::system::error_code& ec,
  411. const std::size_t bytes_transferred)
  412. {
  413. streambuf_.consume(bytes_transferred);
  414. handler_(ec, bytes_transferred);
  415. }
  416. //private:
  417. boost::asio::basic_streambuf<Allocator>& streambuf_;
  418. WriteHandler handler_;
  419. };
  420. template <typename Allocator, typename WriteHandler>
  421. inline void* asio_handler_allocate(std::size_t size,
  422. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  423. {
  424. return boost_asio_handler_alloc_helpers::allocate(
  425. size, this_handler->handler_);
  426. }
  427. template <typename Allocator, typename WriteHandler>
  428. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  429. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  430. {
  431. boost_asio_handler_alloc_helpers::deallocate(
  432. pointer, size, this_handler->handler_);
  433. }
  434. template <typename Allocator, typename WriteHandler>
  435. inline bool asio_handler_is_continuation(
  436. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  437. {
  438. return boost_asio_handler_cont_helpers::is_continuation(
  439. this_handler->handler_);
  440. }
  441. template <typename Function, typename Allocator, typename WriteHandler>
  442. inline void asio_handler_invoke(Function& function,
  443. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  444. {
  445. boost_asio_handler_invoke_helpers::invoke(
  446. function, this_handler->handler_);
  447. }
  448. template <typename Function, typename Allocator, typename WriteHandler>
  449. inline void asio_handler_invoke(const Function& function,
  450. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  451. {
  452. boost_asio_handler_invoke_helpers::invoke(
  453. function, this_handler->handler_);
  454. }
  455. template <typename AsyncRandomAccessWriteDevice>
  456. class initiate_async_write_at_streambuf
  457. {
  458. public:
  459. typedef typename AsyncRandomAccessWriteDevice::executor_type executor_type;
  460. explicit initiate_async_write_at_streambuf(
  461. AsyncRandomAccessWriteDevice& device)
  462. : device_(device)
  463. {
  464. }
  465. executor_type get_executor() const BOOST_ASIO_NOEXCEPT
  466. {
  467. return device_.get_executor();
  468. }
  469. template <typename WriteHandler,
  470. typename Allocator, typename CompletionCondition>
  471. void operator()(BOOST_ASIO_MOVE_ARG(WriteHandler) handler,
  472. uint64_t offset, basic_streambuf<Allocator>* b,
  473. BOOST_ASIO_MOVE_ARG(CompletionCondition) completion_condition) const
  474. {
  475. // If you get an error on the following line it means that your handler
  476. // does not meet the documented type requirements for a WriteHandler.
  477. BOOST_ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  478. non_const_lvalue<WriteHandler> handler2(handler);
  479. async_write_at(device_, offset, b->data(),
  480. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition),
  481. write_at_streambuf_op<Allocator, typename decay<WriteHandler>::type>(
  482. *b, handler2.value));
  483. }
  484. private:
  485. AsyncRandomAccessWriteDevice& device_;
  486. };
  487. } // namespace detail
  488. #if !defined(GENERATING_DOCUMENTATION)
  489. template <typename Allocator, typename WriteHandler, typename Allocator1>
  490. struct associated_allocator<
  491. detail::write_at_streambuf_op<Allocator, WriteHandler>,
  492. Allocator1>
  493. {
  494. typedef typename associated_allocator<WriteHandler, Allocator1>::type type;
  495. static type get(
  496. const detail::write_at_streambuf_op<Allocator, WriteHandler>& h,
  497. const Allocator1& a = Allocator1()) BOOST_ASIO_NOEXCEPT
  498. {
  499. return associated_allocator<WriteHandler, Allocator1>::get(h.handler_, a);
  500. }
  501. };
  502. template <typename Executor, typename WriteHandler, typename Executor1>
  503. struct associated_executor<
  504. detail::write_at_streambuf_op<Executor, WriteHandler>,
  505. Executor1>
  506. {
  507. typedef typename associated_executor<WriteHandler, Executor1>::type type;
  508. static type get(
  509. const detail::write_at_streambuf_op<Executor, WriteHandler>& h,
  510. const Executor1& ex = Executor1()) BOOST_ASIO_NOEXCEPT
  511. {
  512. return associated_executor<WriteHandler, Executor1>::get(h.handler_, ex);
  513. }
  514. };
  515. #endif // !defined(GENERATING_DOCUMENTATION)
  516. template <typename AsyncRandomAccessWriteDevice,
  517. typename Allocator, typename CompletionCondition,
  518. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  519. std::size_t)) WriteHandler>
  520. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  521. void (boost::system::error_code, std::size_t))
  522. async_write_at(AsyncRandomAccessWriteDevice& d,
  523. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  524. CompletionCondition completion_condition,
  525. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  526. {
  527. return async_initiate<WriteHandler,
  528. void (boost::system::error_code, std::size_t)>(
  529. detail::initiate_async_write_at_streambuf<
  530. AsyncRandomAccessWriteDevice>(d),
  531. handler, offset, &b,
  532. BOOST_ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
  533. }
  534. template <typename AsyncRandomAccessWriteDevice, typename Allocator,
  535. BOOST_ASIO_COMPLETION_TOKEN_FOR(void (boost::system::error_code,
  536. std::size_t)) WriteHandler>
  537. inline BOOST_ASIO_INITFN_AUTO_RESULT_TYPE(WriteHandler,
  538. void (boost::system::error_code, std::size_t))
  539. async_write_at(AsyncRandomAccessWriteDevice& d,
  540. uint64_t offset, boost::asio::basic_streambuf<Allocator>& b,
  541. BOOST_ASIO_MOVE_ARG(WriteHandler) handler)
  542. {
  543. return async_initiate<WriteHandler,
  544. void (boost::system::error_code, std::size_t)>(
  545. detail::initiate_async_write_at_streambuf<
  546. AsyncRandomAccessWriteDevice>(d),
  547. handler, offset, &b, transfer_all());
  548. }
  549. #endif // !defined(BOOST_ASIO_NO_IOSTREAM)
  550. #endif // !defined(BOOST_ASIO_NO_EXTENSIONS)
  551. } // namespace asio
  552. } // namespace boost
  553. #include <boost/asio/detail/pop_options.hpp>
  554. #endif // BOOST_ASIO_IMPL_WRITE_AT_HPP