write_at.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  1. //
  2. // impl/write_at.hpp
  3. // ~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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/buffer.hpp"
  16. #include "asio/completion_condition.hpp"
  17. #include "asio/detail/array_fwd.hpp"
  18. #include "asio/detail/base_from_completion_cond.hpp"
  19. #include "asio/detail/bind_handler.hpp"
  20. #include "asio/detail/consuming_buffers.hpp"
  21. #include "asio/detail/dependent_type.hpp"
  22. #include "asio/detail/handler_alloc_helpers.hpp"
  23. #include "asio/detail/handler_cont_helpers.hpp"
  24. #include "asio/detail/handler_invoke_helpers.hpp"
  25. #include "asio/detail/handler_type_requirements.hpp"
  26. #include "asio/detail/throw_error.hpp"
  27. #include "asio/detail/push_options.hpp"
  28. namespace asio {
  29. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  30. typename CompletionCondition>
  31. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  32. uint64_t offset, const ConstBufferSequence& buffers,
  33. CompletionCondition completion_condition, asio::error_code& ec)
  34. {
  35. ec = asio::error_code();
  36. asio::detail::consuming_buffers<
  37. const_buffer, ConstBufferSequence> tmp(buffers);
  38. std::size_t total_transferred = 0;
  39. tmp.prepare(detail::adapt_completion_condition_result(
  40. completion_condition(ec, total_transferred)));
  41. while (tmp.begin() != tmp.end())
  42. {
  43. std::size_t bytes_transferred = d.write_some_at(
  44. offset + total_transferred, tmp, ec);
  45. tmp.consume(bytes_transferred);
  46. total_transferred += bytes_transferred;
  47. tmp.prepare(detail::adapt_completion_condition_result(
  48. completion_condition(ec, total_transferred)));
  49. }
  50. return total_transferred;
  51. }
  52. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
  53. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  54. uint64_t offset, const ConstBufferSequence& buffers)
  55. {
  56. asio::error_code ec;
  57. std::size_t bytes_transferred = write_at(
  58. d, offset, buffers, transfer_all(), ec);
  59. asio::detail::throw_error(ec, "write_at");
  60. return bytes_transferred;
  61. }
  62. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
  63. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  64. uint64_t offset, const ConstBufferSequence& buffers,
  65. asio::error_code& ec)
  66. {
  67. return write_at(d, offset, buffers, transfer_all(), ec);
  68. }
  69. template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
  70. typename CompletionCondition>
  71. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  72. uint64_t offset, const ConstBufferSequence& buffers,
  73. CompletionCondition completion_condition)
  74. {
  75. asio::error_code ec;
  76. std::size_t bytes_transferred = write_at(
  77. d, offset, buffers, completion_condition, ec);
  78. asio::detail::throw_error(ec, "write_at");
  79. return bytes_transferred;
  80. }
  81. #if !defined(ASIO_NO_IOSTREAM)
  82. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  83. typename CompletionCondition>
  84. std::size_t write_at(SyncRandomAccessWriteDevice& d,
  85. uint64_t offset, asio::basic_streambuf<Allocator>& b,
  86. CompletionCondition completion_condition, asio::error_code& ec)
  87. {
  88. std::size_t bytes_transferred = write_at(
  89. d, offset, b.data(), completion_condition, ec);
  90. b.consume(bytes_transferred);
  91. return bytes_transferred;
  92. }
  93. template <typename SyncRandomAccessWriteDevice, typename Allocator>
  94. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  95. uint64_t offset, asio::basic_streambuf<Allocator>& b)
  96. {
  97. asio::error_code ec;
  98. std::size_t bytes_transferred = write_at(d, offset, b, transfer_all(), ec);
  99. asio::detail::throw_error(ec, "write_at");
  100. return bytes_transferred;
  101. }
  102. template <typename SyncRandomAccessWriteDevice, typename Allocator>
  103. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  104. uint64_t offset, asio::basic_streambuf<Allocator>& b,
  105. asio::error_code& ec)
  106. {
  107. return write_at(d, offset, b, transfer_all(), ec);
  108. }
  109. template <typename SyncRandomAccessWriteDevice, typename Allocator,
  110. typename CompletionCondition>
  111. inline std::size_t write_at(SyncRandomAccessWriteDevice& d,
  112. uint64_t offset, asio::basic_streambuf<Allocator>& b,
  113. CompletionCondition completion_condition)
  114. {
  115. asio::error_code ec;
  116. std::size_t bytes_transferred = write_at(
  117. d, offset, b, completion_condition, ec);
  118. asio::detail::throw_error(ec, "write_at");
  119. return bytes_transferred;
  120. }
  121. #endif // !defined(ASIO_NO_IOSTREAM)
  122. namespace detail
  123. {
  124. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  125. typename CompletionCondition, typename WriteHandler>
  126. class write_at_op
  127. : detail::base_from_completion_cond<CompletionCondition>
  128. {
  129. public:
  130. write_at_op(AsyncRandomAccessWriteDevice& device,
  131. uint64_t offset, const ConstBufferSequence& buffers,
  132. CompletionCondition completion_condition, WriteHandler& handler)
  133. : detail::base_from_completion_cond<
  134. CompletionCondition>(completion_condition),
  135. device_(device),
  136. offset_(offset),
  137. buffers_(buffers),
  138. start_(0),
  139. total_transferred_(0),
  140. handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
  141. {
  142. }
  143. #if defined(ASIO_HAS_MOVE)
  144. write_at_op(const write_at_op& other)
  145. : detail::base_from_completion_cond<CompletionCondition>(other),
  146. device_(other.device_),
  147. offset_(other.offset_),
  148. buffers_(other.buffers_),
  149. start_(other.start_),
  150. total_transferred_(other.total_transferred_),
  151. handler_(other.handler_)
  152. {
  153. }
  154. write_at_op(write_at_op&& other)
  155. : detail::base_from_completion_cond<CompletionCondition>(other),
  156. device_(other.device_),
  157. offset_(other.offset_),
  158. buffers_(other.buffers_),
  159. start_(other.start_),
  160. total_transferred_(other.total_transferred_),
  161. handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
  162. {
  163. }
  164. #endif // defined(ASIO_HAS_MOVE)
  165. void operator()(const asio::error_code& ec,
  166. std::size_t bytes_transferred, int start = 0)
  167. {
  168. switch (start_ = start)
  169. {
  170. case 1:
  171. buffers_.prepare(this->check_for_completion(ec, total_transferred_));
  172. for (;;)
  173. {
  174. device_.async_write_some_at(
  175. offset_ + total_transferred_, buffers_,
  176. ASIO_MOVE_CAST(write_at_op)(*this));
  177. return; default:
  178. total_transferred_ += bytes_transferred;
  179. buffers_.consume(bytes_transferred);
  180. buffers_.prepare(this->check_for_completion(ec, total_transferred_));
  181. if ((!ec && bytes_transferred == 0)
  182. || buffers_.begin() == buffers_.end())
  183. break;
  184. }
  185. handler_(ec, static_cast<const std::size_t&>(total_transferred_));
  186. }
  187. }
  188. //private:
  189. AsyncRandomAccessWriteDevice& device_;
  190. uint64_t offset_;
  191. asio::detail::consuming_buffers<
  192. const_buffer, ConstBufferSequence> buffers_;
  193. int start_;
  194. std::size_t total_transferred_;
  195. WriteHandler handler_;
  196. };
  197. template <typename AsyncRandomAccessWriteDevice,
  198. typename CompletionCondition, typename WriteHandler>
  199. class write_at_op<AsyncRandomAccessWriteDevice,
  200. asio::mutable_buffers_1, CompletionCondition, WriteHandler>
  201. : detail::base_from_completion_cond<CompletionCondition>
  202. {
  203. public:
  204. write_at_op(AsyncRandomAccessWriteDevice& device,
  205. uint64_t offset, const asio::mutable_buffers_1& buffers,
  206. CompletionCondition completion_condition,
  207. WriteHandler& handler)
  208. : detail::base_from_completion_cond<
  209. CompletionCondition>(completion_condition),
  210. device_(device),
  211. offset_(offset),
  212. buffer_(buffers),
  213. start_(0),
  214. total_transferred_(0),
  215. handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
  216. {
  217. }
  218. #if defined(ASIO_HAS_MOVE)
  219. write_at_op(const write_at_op& other)
  220. : detail::base_from_completion_cond<CompletionCondition>(other),
  221. device_(other.device_),
  222. offset_(other.offset_),
  223. buffer_(other.buffer_),
  224. start_(other.start_),
  225. total_transferred_(other.total_transferred_),
  226. handler_(other.handler_)
  227. {
  228. }
  229. write_at_op(write_at_op&& other)
  230. : detail::base_from_completion_cond<CompletionCondition>(other),
  231. device_(other.device_),
  232. offset_(other.offset_),
  233. buffer_(other.buffer_),
  234. start_(other.start_),
  235. total_transferred_(other.total_transferred_),
  236. handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
  237. {
  238. }
  239. #endif // defined(ASIO_HAS_MOVE)
  240. void operator()(const asio::error_code& ec,
  241. std::size_t bytes_transferred, int start = 0)
  242. {
  243. std::size_t n = 0;
  244. switch (start_ = start)
  245. {
  246. case 1:
  247. n = this->check_for_completion(ec, total_transferred_);
  248. for (;;)
  249. {
  250. device_.async_write_some_at(offset_ + total_transferred_,
  251. asio::buffer(buffer_ + total_transferred_, n),
  252. ASIO_MOVE_CAST(write_at_op)(*this));
  253. return; default:
  254. total_transferred_ += bytes_transferred;
  255. if ((!ec && bytes_transferred == 0)
  256. || (n = this->check_for_completion(ec, total_transferred_)) == 0
  257. || total_transferred_ == asio::buffer_size(buffer_))
  258. break;
  259. }
  260. handler_(ec, static_cast<const std::size_t&>(total_transferred_));
  261. }
  262. }
  263. //private:
  264. AsyncRandomAccessWriteDevice& device_;
  265. uint64_t offset_;
  266. asio::mutable_buffer buffer_;
  267. int start_;
  268. std::size_t total_transferred_;
  269. WriteHandler handler_;
  270. };
  271. template <typename AsyncRandomAccessWriteDevice,
  272. typename CompletionCondition, typename WriteHandler>
  273. class write_at_op<AsyncRandomAccessWriteDevice, asio::const_buffers_1,
  274. CompletionCondition, WriteHandler>
  275. : detail::base_from_completion_cond<CompletionCondition>
  276. {
  277. public:
  278. write_at_op(AsyncRandomAccessWriteDevice& device,
  279. uint64_t offset, const asio::const_buffers_1& buffers,
  280. CompletionCondition completion_condition,
  281. WriteHandler& handler)
  282. : detail::base_from_completion_cond<
  283. CompletionCondition>(completion_condition),
  284. device_(device),
  285. offset_(offset),
  286. buffer_(buffers),
  287. start_(0),
  288. total_transferred_(0),
  289. handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
  290. {
  291. }
  292. #if defined(ASIO_HAS_MOVE)
  293. write_at_op(const write_at_op& other)
  294. : detail::base_from_completion_cond<CompletionCondition>(other),
  295. device_(other.device_),
  296. offset_(other.offset_),
  297. buffer_(other.buffer_),
  298. start_(other.start_),
  299. total_transferred_(other.total_transferred_),
  300. handler_(other.handler_)
  301. {
  302. }
  303. write_at_op(write_at_op&& other)
  304. : detail::base_from_completion_cond<CompletionCondition>(other),
  305. device_(other.device_),
  306. offset_(other.offset_),
  307. buffer_(other.buffer_),
  308. start_(other.start_),
  309. total_transferred_(other.total_transferred_),
  310. handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
  311. {
  312. }
  313. #endif // defined(ASIO_HAS_MOVE)
  314. void operator()(const asio::error_code& ec,
  315. std::size_t bytes_transferred, int start = 0)
  316. {
  317. std::size_t n = 0;
  318. switch (start_ = start)
  319. {
  320. case 1:
  321. n = this->check_for_completion(ec, total_transferred_);
  322. for (;;)
  323. {
  324. device_.async_write_some_at(offset_ + total_transferred_,
  325. asio::buffer(buffer_ + total_transferred_, n),
  326. ASIO_MOVE_CAST(write_at_op)(*this));
  327. return; default:
  328. total_transferred_ += bytes_transferred;
  329. if ((!ec && bytes_transferred == 0)
  330. || (n = this->check_for_completion(ec, total_transferred_)) == 0
  331. || total_transferred_ == asio::buffer_size(buffer_))
  332. break;
  333. }
  334. handler_(ec, static_cast<const std::size_t&>(total_transferred_));
  335. }
  336. }
  337. //private:
  338. AsyncRandomAccessWriteDevice& device_;
  339. uint64_t offset_;
  340. asio::const_buffer buffer_;
  341. int start_;
  342. std::size_t total_transferred_;
  343. WriteHandler handler_;
  344. };
  345. template <typename AsyncRandomAccessWriteDevice, typename Elem,
  346. typename CompletionCondition, typename WriteHandler>
  347. class write_at_op<AsyncRandomAccessWriteDevice, boost::array<Elem, 2>,
  348. CompletionCondition, WriteHandler>
  349. : detail::base_from_completion_cond<CompletionCondition>
  350. {
  351. public:
  352. write_at_op(AsyncRandomAccessWriteDevice& device,
  353. uint64_t offset, const boost::array<Elem, 2>& buffers,
  354. CompletionCondition completion_condition, WriteHandler& handler)
  355. : detail::base_from_completion_cond<
  356. CompletionCondition>(completion_condition),
  357. device_(device),
  358. offset_(offset),
  359. buffers_(buffers),
  360. start_(0),
  361. total_transferred_(0),
  362. handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
  363. {
  364. }
  365. #if defined(ASIO_HAS_MOVE)
  366. write_at_op(const write_at_op& other)
  367. : detail::base_from_completion_cond<CompletionCondition>(other),
  368. device_(other.device_),
  369. offset_(other.offset_),
  370. buffers_(other.buffers_),
  371. start_(other.start_),
  372. total_transferred_(other.total_transferred_),
  373. handler_(other.handler_)
  374. {
  375. }
  376. write_at_op(write_at_op&& other)
  377. : detail::base_from_completion_cond<CompletionCondition>(other),
  378. device_(other.device_),
  379. offset_(other.offset_),
  380. buffers_(other.buffers_),
  381. start_(other.start_),
  382. total_transferred_(other.total_transferred_),
  383. handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
  384. {
  385. }
  386. #endif // defined(ASIO_HAS_MOVE)
  387. void operator()(const asio::error_code& ec,
  388. std::size_t bytes_transferred, int start = 0)
  389. {
  390. typename asio::detail::dependent_type<Elem,
  391. boost::array<asio::const_buffer, 2> >::type bufs = {{
  392. asio::const_buffer(buffers_[0]),
  393. asio::const_buffer(buffers_[1]) }};
  394. std::size_t buffer_size0 = asio::buffer_size(bufs[0]);
  395. std::size_t buffer_size1 = asio::buffer_size(bufs[1]);
  396. std::size_t n = 0;
  397. switch (start_ = start)
  398. {
  399. case 1:
  400. n = this->check_for_completion(ec, total_transferred_);
  401. for (;;)
  402. {
  403. bufs[0] = asio::buffer(bufs[0] + total_transferred_, n);
  404. bufs[1] = asio::buffer(
  405. bufs[1] + (total_transferred_ < buffer_size0
  406. ? 0 : total_transferred_ - buffer_size0),
  407. n - asio::buffer_size(bufs[0]));
  408. device_.async_write_some_at(offset_ + total_transferred_,
  409. bufs, ASIO_MOVE_CAST(write_at_op)(*this));
  410. return; default:
  411. total_transferred_ += bytes_transferred;
  412. if ((!ec && bytes_transferred == 0)
  413. || (n = this->check_for_completion(ec, total_transferred_)) == 0
  414. || total_transferred_ == buffer_size0 + buffer_size1)
  415. break;
  416. }
  417. handler_(ec, static_cast<const std::size_t&>(total_transferred_));
  418. }
  419. }
  420. //private:
  421. AsyncRandomAccessWriteDevice& device_;
  422. uint64_t offset_;
  423. boost::array<Elem, 2> buffers_;
  424. int start_;
  425. std::size_t total_transferred_;
  426. WriteHandler handler_;
  427. };
  428. #if defined(ASIO_HAS_STD_ARRAY)
  429. template <typename AsyncRandomAccessWriteDevice, typename Elem,
  430. typename CompletionCondition, typename WriteHandler>
  431. class write_at_op<AsyncRandomAccessWriteDevice, std::array<Elem, 2>,
  432. CompletionCondition, WriteHandler>
  433. : detail::base_from_completion_cond<CompletionCondition>
  434. {
  435. public:
  436. write_at_op(AsyncRandomAccessWriteDevice& device,
  437. uint64_t offset, const std::array<Elem, 2>& buffers,
  438. CompletionCondition completion_condition, WriteHandler& handler)
  439. : detail::base_from_completion_cond<
  440. CompletionCondition>(completion_condition),
  441. device_(device),
  442. offset_(offset),
  443. buffers_(buffers),
  444. start_(0),
  445. total_transferred_(0),
  446. handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
  447. {
  448. }
  449. #if defined(ASIO_HAS_MOVE)
  450. write_at_op(const write_at_op& other)
  451. : detail::base_from_completion_cond<CompletionCondition>(other),
  452. device_(other.device_),
  453. offset_(other.offset_),
  454. buffers_(other.buffers_),
  455. start_(other.start_),
  456. total_transferred_(other.total_transferred_),
  457. handler_(other.handler_)
  458. {
  459. }
  460. write_at_op(write_at_op&& other)
  461. : detail::base_from_completion_cond<CompletionCondition>(other),
  462. device_(other.device_),
  463. offset_(other.offset_),
  464. buffers_(other.buffers_),
  465. start_(other.start_),
  466. total_transferred_(other.total_transferred_),
  467. handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
  468. {
  469. }
  470. #endif // defined(ASIO_HAS_MOVE)
  471. void operator()(const asio::error_code& ec,
  472. std::size_t bytes_transferred, int start = 0)
  473. {
  474. typename asio::detail::dependent_type<Elem,
  475. std::array<asio::const_buffer, 2> >::type bufs = {{
  476. asio::const_buffer(buffers_[0]),
  477. asio::const_buffer(buffers_[1]) }};
  478. std::size_t buffer_size0 = asio::buffer_size(bufs[0]);
  479. std::size_t buffer_size1 = asio::buffer_size(bufs[1]);
  480. std::size_t n = 0;
  481. switch (start_ = start)
  482. {
  483. case 1:
  484. n = this->check_for_completion(ec, total_transferred_);
  485. for (;;)
  486. {
  487. bufs[0] = asio::buffer(bufs[0] + total_transferred_, n);
  488. bufs[1] = asio::buffer(
  489. bufs[1] + (total_transferred_ < buffer_size0
  490. ? 0 : total_transferred_ - buffer_size0),
  491. n - asio::buffer_size(bufs[0]));
  492. device_.async_write_some_at(offset_ + total_transferred_,
  493. bufs, ASIO_MOVE_CAST(write_at_op)(*this));
  494. return; default:
  495. total_transferred_ += bytes_transferred;
  496. if ((!ec && bytes_transferred == 0)
  497. || (n = this->check_for_completion(ec, total_transferred_)) == 0
  498. || total_transferred_ == buffer_size0 + buffer_size1)
  499. break;
  500. }
  501. handler_(ec, static_cast<const std::size_t&>(total_transferred_));
  502. }
  503. }
  504. //private:
  505. AsyncRandomAccessWriteDevice& device_;
  506. uint64_t offset_;
  507. std::array<Elem, 2> buffers_;
  508. int start_;
  509. std::size_t total_transferred_;
  510. WriteHandler handler_;
  511. };
  512. #endif // defined(ASIO_HAS_STD_ARRAY)
  513. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  514. typename CompletionCondition, typename WriteHandler>
  515. inline void* asio_handler_allocate(std::size_t size,
  516. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  517. CompletionCondition, WriteHandler>* this_handler)
  518. {
  519. return asio_handler_alloc_helpers::allocate(
  520. size, this_handler->handler_);
  521. }
  522. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  523. typename CompletionCondition, typename WriteHandler>
  524. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  525. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  526. CompletionCondition, WriteHandler>* this_handler)
  527. {
  528. asio_handler_alloc_helpers::deallocate(
  529. pointer, size, this_handler->handler_);
  530. }
  531. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  532. typename CompletionCondition, typename WriteHandler>
  533. inline bool asio_handler_is_continuation(
  534. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  535. CompletionCondition, WriteHandler>* this_handler)
  536. {
  537. return this_handler->start_ == 0 ? true
  538. : asio_handler_cont_helpers::is_continuation(
  539. this_handler->handler_);
  540. }
  541. template <typename Function, typename AsyncRandomAccessWriteDevice,
  542. typename ConstBufferSequence, typename CompletionCondition,
  543. typename WriteHandler>
  544. inline void asio_handler_invoke(Function& function,
  545. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  546. CompletionCondition, WriteHandler>* this_handler)
  547. {
  548. asio_handler_invoke_helpers::invoke(
  549. function, this_handler->handler_);
  550. }
  551. template <typename Function, typename AsyncRandomAccessWriteDevice,
  552. typename ConstBufferSequence, typename CompletionCondition,
  553. typename WriteHandler>
  554. inline void asio_handler_invoke(const Function& function,
  555. write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  556. CompletionCondition, WriteHandler>* this_handler)
  557. {
  558. asio_handler_invoke_helpers::invoke(
  559. function, this_handler->handler_);
  560. }
  561. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  562. typename CompletionCondition, typename WriteHandler>
  563. inline write_at_op<AsyncRandomAccessWriteDevice,
  564. ConstBufferSequence, CompletionCondition, WriteHandler>
  565. make_write_at_op(AsyncRandomAccessWriteDevice& d,
  566. uint64_t offset, const ConstBufferSequence& buffers,
  567. CompletionCondition completion_condition, WriteHandler handler)
  568. {
  569. return write_at_op<AsyncRandomAccessWriteDevice,
  570. ConstBufferSequence, CompletionCondition, WriteHandler>(
  571. d, offset, buffers, completion_condition, handler);
  572. }
  573. } // namespace detail
  574. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  575. typename CompletionCondition, typename WriteHandler>
  576. inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
  577. void (asio::error_code, std::size_t))
  578. async_write_at(AsyncRandomAccessWriteDevice& d,
  579. uint64_t offset, const ConstBufferSequence& buffers,
  580. CompletionCondition completion_condition,
  581. ASIO_MOVE_ARG(WriteHandler) handler)
  582. {
  583. // If you get an error on the following line it means that your handler does
  584. // not meet the documented type requirements for a WriteHandler.
  585. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  586. detail::async_result_init<
  587. WriteHandler, void (asio::error_code, std::size_t)> init(
  588. ASIO_MOVE_CAST(WriteHandler)(handler));
  589. detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  590. CompletionCondition, ASIO_HANDLER_TYPE(
  591. WriteHandler, void (asio::error_code, std::size_t))>(
  592. d, offset, buffers, completion_condition, init.handler)(
  593. asio::error_code(), 0, 1);
  594. return init.result.get();
  595. }
  596. template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
  597. typename WriteHandler>
  598. inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
  599. void (asio::error_code, std::size_t))
  600. async_write_at(AsyncRandomAccessWriteDevice& d,
  601. uint64_t offset, const ConstBufferSequence& buffers,
  602. ASIO_MOVE_ARG(WriteHandler) handler)
  603. {
  604. // If you get an error on the following line it means that your handler does
  605. // not meet the documented type requirements for a WriteHandler.
  606. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  607. detail::async_result_init<
  608. WriteHandler, void (asio::error_code, std::size_t)> init(
  609. ASIO_MOVE_CAST(WriteHandler)(handler));
  610. detail::write_at_op<AsyncRandomAccessWriteDevice, ConstBufferSequence,
  611. detail::transfer_all_t, ASIO_HANDLER_TYPE(
  612. WriteHandler, void (asio::error_code, std::size_t))>(
  613. d, offset, buffers, transfer_all(), init.handler)(
  614. asio::error_code(), 0, 1);
  615. return init.result.get();
  616. }
  617. #if !defined(ASIO_NO_IOSTREAM)
  618. namespace detail
  619. {
  620. template <typename Allocator, typename WriteHandler>
  621. class write_at_streambuf_op
  622. {
  623. public:
  624. write_at_streambuf_op(
  625. asio::basic_streambuf<Allocator>& streambuf,
  626. WriteHandler& handler)
  627. : streambuf_(streambuf),
  628. handler_(ASIO_MOVE_CAST(WriteHandler)(handler))
  629. {
  630. }
  631. #if defined(ASIO_HAS_MOVE)
  632. write_at_streambuf_op(const write_at_streambuf_op& other)
  633. : streambuf_(other.streambuf_),
  634. handler_(other.handler_)
  635. {
  636. }
  637. write_at_streambuf_op(write_at_streambuf_op&& other)
  638. : streambuf_(other.streambuf_),
  639. handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
  640. {
  641. }
  642. #endif // defined(ASIO_HAS_MOVE)
  643. void operator()(const asio::error_code& ec,
  644. const std::size_t bytes_transferred)
  645. {
  646. streambuf_.consume(bytes_transferred);
  647. handler_(ec, bytes_transferred);
  648. }
  649. //private:
  650. asio::basic_streambuf<Allocator>& streambuf_;
  651. WriteHandler handler_;
  652. };
  653. template <typename Allocator, typename WriteHandler>
  654. inline void* asio_handler_allocate(std::size_t size,
  655. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  656. {
  657. return asio_handler_alloc_helpers::allocate(
  658. size, this_handler->handler_);
  659. }
  660. template <typename Allocator, typename WriteHandler>
  661. inline void asio_handler_deallocate(void* pointer, std::size_t size,
  662. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  663. {
  664. asio_handler_alloc_helpers::deallocate(
  665. pointer, size, this_handler->handler_);
  666. }
  667. template <typename Allocator, typename WriteHandler>
  668. inline bool asio_handler_is_continuation(
  669. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  670. {
  671. return asio_handler_cont_helpers::is_continuation(
  672. this_handler->handler_);
  673. }
  674. template <typename Function, typename Allocator, typename WriteHandler>
  675. inline void asio_handler_invoke(Function& function,
  676. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  677. {
  678. asio_handler_invoke_helpers::invoke(
  679. function, this_handler->handler_);
  680. }
  681. template <typename Function, typename Allocator, typename WriteHandler>
  682. inline void asio_handler_invoke(const Function& function,
  683. write_at_streambuf_op<Allocator, WriteHandler>* this_handler)
  684. {
  685. asio_handler_invoke_helpers::invoke(
  686. function, this_handler->handler_);
  687. }
  688. template <typename Allocator, typename WriteHandler>
  689. inline write_at_streambuf_op<Allocator, WriteHandler>
  690. make_write_at_streambuf_op(
  691. asio::basic_streambuf<Allocator>& b, WriteHandler handler)
  692. {
  693. return write_at_streambuf_op<Allocator, WriteHandler>(b, handler);
  694. }
  695. } // namespace detail
  696. template <typename AsyncRandomAccessWriteDevice, typename Allocator,
  697. typename CompletionCondition, typename WriteHandler>
  698. inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
  699. void (asio::error_code, std::size_t))
  700. async_write_at(AsyncRandomAccessWriteDevice& d,
  701. uint64_t offset, asio::basic_streambuf<Allocator>& b,
  702. CompletionCondition completion_condition,
  703. ASIO_MOVE_ARG(WriteHandler) handler)
  704. {
  705. // If you get an error on the following line it means that your handler does
  706. // not meet the documented type requirements for a WriteHandler.
  707. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  708. detail::async_result_init<
  709. WriteHandler, void (asio::error_code, std::size_t)> init(
  710. ASIO_MOVE_CAST(WriteHandler)(handler));
  711. async_write_at(d, offset, b.data(), completion_condition,
  712. detail::write_at_streambuf_op<Allocator, ASIO_HANDLER_TYPE(
  713. WriteHandler, void (asio::error_code, std::size_t))>(
  714. b, init.handler));
  715. return init.result.get();
  716. }
  717. template <typename AsyncRandomAccessWriteDevice, typename Allocator,
  718. typename WriteHandler>
  719. inline ASIO_INITFN_RESULT_TYPE(WriteHandler,
  720. void (asio::error_code, std::size_t))
  721. async_write_at(AsyncRandomAccessWriteDevice& d,
  722. uint64_t offset, asio::basic_streambuf<Allocator>& b,
  723. ASIO_MOVE_ARG(WriteHandler) handler)
  724. {
  725. // If you get an error on the following line it means that your handler does
  726. // not meet the documented type requirements for a WriteHandler.
  727. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  728. detail::async_result_init<
  729. WriteHandler, void (asio::error_code, std::size_t)> init(
  730. ASIO_MOVE_CAST(WriteHandler)(handler));
  731. async_write_at(d, offset, b.data(), transfer_all(),
  732. detail::write_at_streambuf_op<Allocator, ASIO_HANDLER_TYPE(
  733. WriteHandler, void (asio::error_code, std::size_t))>(
  734. b, init.handler));
  735. return init.result.get();
  736. }
  737. #endif // !defined(ASIO_NO_IOSTREAM)
  738. } // namespace asio
  739. #include "asio/detail/pop_options.hpp"
  740. #endif // ASIO_IMPL_WRITE_AT_HPP