read_at.hpp 25 KB

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