write_at.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. //
  2. // impl/write_at.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2022 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 ASIO_IMPL_WRITE_AT_HPP
  11. #define 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 "asio/associator.hpp"
  16. #include "asio/buffer.hpp"
  17. #include "asio/completion_condition.hpp"
  18. #include "asio/detail/array_fwd.hpp"
  19. #include "asio/detail/base_from_cancellation_state.hpp"
  20. #include "asio/detail/base_from_completion_cond.hpp"
  21. #include "asio/detail/bind_handler.hpp"
  22. #include "asio/detail/consuming_buffers.hpp"
  23. #include "asio/detail/dependent_type.hpp"
  24. #include "asio/detail/handler_alloc_helpers.hpp"
  25. #include "asio/detail/handler_cont_helpers.hpp"
  26. #include "asio/detail/handler_invoke_helpers.hpp"
  27. #include "asio/detail/handler_tracking.hpp"
  28. #include "asio/detail/handler_type_requirements.hpp"
  29. #include "asio/detail/non_const_lvalue.hpp"
  30. #include "asio/detail/throw_error.hpp"
  31. #include "asio/detail/push_options.hpp"
  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. asio::error_code& ec)
  41. {
  42. ec = asio::error_code();
  43. 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, asio::error_code& ec)
  64. {
  65. return detail::write_at_buffer_sequence(d, offset, buffers,
  66. asio::buffer_sequence_begin(buffers),
  67. 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. asio::error_code ec;
  74. std::size_t bytes_transferred = write_at(
  75. d, offset, buffers, transfer_all(), ec);
  76. 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. asio::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. asio::error_code ec;
  93. std::size_t bytes_transferred = write_at(d, offset, buffers,
  94. ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
  95. asio::detail::throw_error(ec, "write_at");
  96. return bytes_transferred;
  97. }
  98. #if !defined(ASIO_NO_EXTENSIONS)
  99. #if !defined(ASIO_NO_IOSTREAM)
  100. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  101. typename CompletionCondition>
  102. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  103. uint64_t offset, asio::basic_streambuf<Allocator>& b,
  104. CompletionCondition completion_condition, asio::error_code& ec)
  105. {
  106. std::size_t bytes_transferred = write_at(d, offset, b.data(),
  107. 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, asio::basic_streambuf<Allocator>& b)
  114. {
  115. asio::error_code ec;
  116. std::size_t bytes_transferred = write_at(d, offset, b, transfer_all(), ec);
  117. 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, asio::basic_streambuf<Allocator>& b,
  123. asio::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, asio::basic_streambuf<Allocator>& b,
  131. CompletionCondition completion_condition)
  132. {
  133. asio::error_code ec;
  134. std::size_t bytes_transferred = write_at(d, offset, b,
  135. ASIO_MOVE_CAST(CompletionCondition)(completion_condition), ec);
  136. asio::detail::throw_error(ec, "write_at");
  137. return bytes_transferred;
  138. }
  139. #endif // !defined(ASIO_NO_IOSTREAM)
  140. #endif // !defined(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_(ASIO_MOVE_CAST(WriteHandler)(handler))
  162. {
  163. }
  164. #if defined(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. ASIO_MOVE_CAST(base_from_cancellation_state<
  178. WriteHandler>)(other)),
  179. base_from_completion_cond<CompletionCondition>(
  180. ASIO_MOVE_CAST(base_from_completion_cond<
  181. CompletionCondition>)(other)),
  182. device_(other.device_),
  183. offset_(other.offset_),
  184. buffers_(ASIO_MOVE_CAST(buffers_type)(other.buffers_)),
  185. start_(other.start_),
  186. handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
  187. {
  188. }
  189. #endif // defined(ASIO_HAS_MOVE)
  190. void operator()(asio::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. ASIO_HANDLER_LOCATION((__FILE__, __LINE__, "async_write_at"));
  202. device_.async_write_some_at(
  203. offset_ + buffers_.total_consumed(), buffers_.prepare(max_size),
  204. 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 = asio::error::operation_aborted;
  216. break;
  217. }
  218. }
  219. ASIO_MOVE_OR_LVALUE(WriteHandler)(handler_)(
  220. static_cast<const asio::error_code&>(ec),
  221. static_cast<const std::size_t&>(buffers_.total_consumed()));
  222. }
  223. }
  224. //private:
  225. typedef 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(ASIO_NO_DEPRECATED)
  242. asio_handler_alloc_helpers::allocate(size, this_handler->handler_);
  243. return asio_handler_allocate_is_no_longer_used();
  244. #else // defined(ASIO_NO_DEPRECATED)
  245. return asio_handler_alloc_helpers::allocate(
  246. size, this_handler->handler_);
  247. #endif // defined(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. asio_handler_alloc_helpers::deallocate(
  258. pointer, size, this_handler->handler_);
  259. #if defined(ASIO_NO_DEPRECATED)
  260. return asio_handler_deallocate_is_no_longer_used();
  261. #endif // defined(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. : 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. asio_handler_invoke_helpers::invoke(
  283. function, this_handler->handler_);
  284. #if defined(ASIO_NO_DEPRECATED)
  285. return asio_handler_invoke_is_no_longer_used();
  286. #endif // defined(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. asio_handler_invoke_helpers::invoke(
  297. function, this_handler->handler_);
  298. #if defined(ASIO_NO_DEPRECATED)
  299. return asio_handler_invoke_is_no_longer_used();
  300. #endif // defined(ASIO_NO_DEPRECATED)
  301. }
  302. template <typename AsyncRandomAccessWriteDevice,
  303. typename ConstBufferSequence, typename ConstBufferIterator,
  304. typename CompletionCondition, typename WriteHandler>
  305. inline void start_write_at_buffer_sequence_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. asio::error_code(), 0, 1);
  314. }
  315. template <typename AsyncRandomAccessWriteDevice>
  316. class initiate_async_write_at_buffer_sequence
  317. {
  318. public:
  319. typedef typename AsyncRandomAccessWriteDevice::executor_type executor_type;
  320. explicit initiate_async_write_at_buffer_sequence(
  321. AsyncRandomAccessWriteDevice& device)
  322. : device_(device)
  323. {
  324. }
  325. executor_type get_executor() const ASIO_NOEXCEPT
  326. {
  327. return device_.get_executor();
  328. }
  329. template <typename WriteHandler, typename ConstBufferSequence,
  330. typename CompletionCondition>
  331. void operator()(ASIO_MOVE_ARG(WriteHandler) handler,
  332. uint64_t offset, const ConstBufferSequence& buffers,
  333. ASIO_MOVE_ARG(CompletionCondition) completion_cond) const
  334. {
  335. // If you get an error on the following line it means that your handler
  336. // does not meet the documented type requirements for a WriteHandler.
  337. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  338. non_const_lvalue<WriteHandler> handler2(handler);
  339. non_const_lvalue<CompletionCondition> completion_cond2(completion_cond);
  340. start_write_at_buffer_sequence_op(device_, offset, buffers,
  341. asio::buffer_sequence_begin(buffers),
  342. completion_cond2.value, handler2.value);
  343. }
  344. private:
  345. AsyncRandomAccessWriteDevice& device_;
  346. };
  347. } // namespace detail
  348. #if !defined(GENERATING_DOCUMENTATION)
  349. template <template <typename, typename> class Associator,
  350. typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  351. typename ConstBufferIterator, typename CompletionCondition,
  352. typename WriteHandler, typename DefaultCandidate>
  353. struct associator<Associator,
  354. detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  355. ConstBufferIterator, CompletionCondition, WriteHandler>,
  356. DefaultCandidate>
  357. : Associator<WriteHandler, DefaultCandidate>
  358. {
  359. static typename Associator<WriteHandler, DefaultCandidate>::type get(
  360. const detail::write_at_op<AsyncRandomAccessWriteDevice,
  361. ConstBufferSequence, ConstBufferIterator,
  362. CompletionCondition, WriteHandler>& h,
  363. const DefaultCandidate& c = DefaultCandidate()) ASIO_NOEXCEPT
  364. {
  365. return Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c);
  366. }
  367. };
  368. #endif // !defined(GENERATING_DOCUMENTATION)
  369. template <typename AsyncRandomAccessWriteDevice,
  370. typename ConstBufferSequence, typename CompletionCondition,
  371. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  372. std::size_t)) WriteToken>
  373. inline ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  374. void (asio::error_code, std::size_t))
  375. async_write_at(AsyncRandomAccessWriteDevice& d,
  376. uint64_t offset, const ConstBufferSequence& buffers,
  377. CompletionCondition completion_condition,
  378. ASIO_MOVE_ARG(WriteToken) token)
  379. {
  380. return async_initiate<WriteToken,
  381. void (asio::error_code, std::size_t)>(
  382. detail::initiate_async_write_at_buffer_sequence<
  383. AsyncRandomAccessWriteDevice>(d),
  384. token, offset, buffers,
  385. ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
  386. }
  387. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  388. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  389. std::size_t)) WriteToken>
  390. inline ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  391. void (asio::error_code, std::size_t))
  392. async_write_at(AsyncRandomAccessWriteDevice& d,
  393. uint64_t offset, const ConstBufferSequence& buffers,
  394. ASIO_MOVE_ARG(WriteToken) token)
  395. {
  396. return async_initiate<WriteToken,
  397. void (asio::error_code, std::size_t)>(
  398. detail::initiate_async_write_at_buffer_sequence<
  399. AsyncRandomAccessWriteDevice>(d),
  400. token, offset, buffers, transfer_all());
  401. }
  402. #if !defined(ASIO_NO_EXTENSIONS)
  403. #if !defined(ASIO_NO_IOSTREAM)
  404. namespace detail
  405. {
  406. template <typename Allocator, typename WriteHandler>
  407. class write_at_streambuf_op
  408. {
  409. public:
  410. write_at_streambuf_op(
  411. asio::basic_streambuf<Allocator>& streambuf,
  412. WriteHandler& handler)
  413. : streambuf_(streambuf),
  414. handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
  415. {
  416. }
  417. #if defined(ASIO_HAS_MOVE)
  418. write_at_streambuf_op(const write_at_streambuf_op& other)
  419. : streambuf_(other.streambuf_),
  420. handler_(other.handler_)
  421. {
  422. }
  423. write_at_streambuf_op(write_at_streambuf_op&& other)
  424. : streambuf_(other.streambuf_),
  425. handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
  426. {
  427. }
  428. #endif // defined(ASIO_HAS_MOVE)
  429. void operator()(const asio::error_code& ec,
  430. const std::size_t bytes_transferred)
  431. {
  432. streambuf_.consume(bytes_transferred);
  433. ASIO_MOVE_OR_LVALUE(WriteHandler)(handler_)(ec, bytes_transferred);
  434. }
  435. //private:
  436. asio::basic_streambuf<Allocator>& streambuf_;
  437. WriteHandler handler_;
  438. };
  439. template <typename Allocator, typename WriteHandler>
  440. inline asio_handler_allocate_is_deprecated
  441. asio_handler_allocate(std::size_t size,
  442. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  443. {
  444. #if defined(ASIO_NO_DEPRECATED)
  445. asio_handler_alloc_helpers::allocate(size, this_handler->handler_);
  446. return asio_handler_allocate_is_no_longer_used();
  447. #else // defined(ASIO_NO_DEPRECATED)
  448. return asio_handler_alloc_helpers::allocate(
  449. size, this_handler->handler_);
  450. #endif // defined(ASIO_NO_DEPRECATED)
  451. }
  452. template <typename Allocator, typename WriteHandler>
  453. inline asio_handler_deallocate_is_deprecated
  454. asio_handler_deallocate(void* pointer, std::size_t size,
  455. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  456. {
  457. asio_handler_alloc_helpers::deallocate(
  458. pointer, size, this_handler->handler_);
  459. #if defined(ASIO_NO_DEPRECATED)
  460. return asio_handler_deallocate_is_no_longer_used();
  461. #endif // defined(ASIO_NO_DEPRECATED)
  462. }
  463. template <typename Allocator, typename WriteHandler>
  464. inline bool asio_handler_is_continuation(
  465. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  466. {
  467. return asio_handler_cont_helpers::is_continuation(
  468. this_handler->handler_);
  469. }
  470. template <typename Function, typename Allocator, typename WriteHandler>
  471. inline asio_handler_invoke_is_deprecated
  472. asio_handler_invoke(Function& function,
  473. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  474. {
  475. asio_handler_invoke_helpers::invoke(
  476. function, this_handler->handler_);
  477. #if defined(ASIO_NO_DEPRECATED)
  478. return asio_handler_invoke_is_no_longer_used();
  479. #endif // defined(ASIO_NO_DEPRECATED)
  480. }
  481. template <typename Function, typename Allocator, typename WriteHandler>
  482. inline asio_handler_invoke_is_deprecated
  483. asio_handler_invoke(const Function& function,
  484. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  485. {
  486. asio_handler_invoke_helpers::invoke(
  487. function, this_handler->handler_);
  488. #if defined(ASIO_NO_DEPRECATED)
  489. return asio_handler_invoke_is_no_longer_used();
  490. #endif // defined(ASIO_NO_DEPRECATED)
  491. }
  492. template <typename AsyncRandomAccessWriteDevice>
  493. class initiate_async_write_at_streambuf
  494. {
  495. public:
  496. typedef typename AsyncRandomAccessWriteDevice::executor_type executor_type;
  497. explicit initiate_async_write_at_streambuf(
  498. AsyncRandomAccessWriteDevice& device)
  499. : device_(device)
  500. {
  501. }
  502. executor_type get_executor() const ASIO_NOEXCEPT
  503. {
  504. return device_.get_executor();
  505. }
  506. template <typename WriteHandler,
  507. typename Allocator, typename CompletionCondition>
  508. void operator()(ASIO_MOVE_ARG(WriteHandler) handler,
  509. uint64_t offset, basic_streambuf<Allocator>* b,
  510. ASIO_MOVE_ARG(CompletionCondition) completion_condition) const
  511. {
  512. // If you get an error on the following line it means that your handler
  513. // does not meet the documented type requirements for a WriteHandler.
  514. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  515. non_const_lvalue<WriteHandler> handler2(handler);
  516. async_write_at(device_, offset, b->data(),
  517. ASIO_MOVE_CAST(CompletionCondition)(completion_condition),
  518. write_at_streambuf_op<Allocator, typename decay<WriteHandler>::type>(
  519. *b, handler2.value));
  520. }
  521. private:
  522. AsyncRandomAccessWriteDevice& device_;
  523. };
  524. } // namespace detail
  525. #if !defined(GENERATING_DOCUMENTATION)
  526. template <template <typename, typename> class Associator,
  527. typename Executor, typename WriteHandler, typename DefaultCandidate>
  528. struct associator<Associator,
  529. detail::write_at_streambuf_op<Executor, WriteHandler>,
  530. DefaultCandidate>
  531. : Associator<WriteHandler, DefaultCandidate>
  532. {
  533. static typename Associator<WriteHandler, DefaultCandidate>::type get(
  534. const detail::write_at_streambuf_op<Executor, WriteHandler>& h,
  535. const DefaultCandidate& c = DefaultCandidate()) ASIO_NOEXCEPT
  536. {
  537. return Associator<WriteHandler, DefaultCandidate>::get(h.handler_, c);
  538. }
  539. };
  540. #endif // !defined(GENERATING_DOCUMENTATION)
  541. template <typename AsyncRandomAccessWriteDevice,
  542. typename Allocator, typename CompletionCondition,
  543. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  544. std::size_t)) WriteToken>
  545. inline ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  546. void (asio::error_code, std::size_t))
  547. async_write_at(AsyncRandomAccessWriteDevice& d,
  548. uint64_t offset, asio::basic_streambuf<Allocator>& b,
  549. CompletionCondition completion_condition,
  550. ASIO_MOVE_ARG(WriteToken) token)
  551. {
  552. return async_initiate<WriteToken,
  553. void (asio::error_code, std::size_t)>(
  554. detail::initiate_async_write_at_streambuf<
  555. AsyncRandomAccessWriteDevice>(d),
  556. token, offset, &b,
  557. ASIO_MOVE_CAST(CompletionCondition)(completion_condition));
  558. }
  559. template <typename AsyncRandomAccessWriteDevice, typename Allocator,
  560. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  561. std::size_t)) WriteToken>
  562. inline ASIO_INITFN_AUTO_RESULT_TYPE(WriteToken,
  563. void (asio::error_code, std::size_t))
  564. async_write_at(AsyncRandomAccessWriteDevice& d,
  565. uint64_t offset, asio::basic_streambuf<Allocator>& b,
  566. ASIO_MOVE_ARG(WriteToken) token)
  567. {
  568. return async_initiate<WriteToken,
  569. void (asio::error_code, std::size_t)>(
  570. detail::initiate_async_write_at_streambuf<
  571. AsyncRandomAccessWriteDevice>(d),
  572. token, offset, &b, transfer_all());
  573. }
  574. #endif // !defined(ASIO_NO_IOSTREAM)
  575. #endif // !defined(ASIO_NO_EXTENSIONS)
  576. } // namespace asio
  577. #include "asio/detail/pop_options.hpp"
  578. #endif // ASIO_IMPL_WRITE_AT_HPP