basic_random_access_file.hpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. //
  2. // basic_random_access_file.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_BASIC_RANDOM_ACCESS_FILE_HPP
  11. #define ASIO_BASIC_RANDOM_ACCESS_FILE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #if defined(ASIO_HAS_FILE) \
  17. || defined(GENERATING_DOCUMENTATION)
  18. #include <cstddef>
  19. #include "asio/async_result.hpp"
  20. #include "asio/basic_file.hpp"
  21. #include "asio/detail/handler_type_requirements.hpp"
  22. #include "asio/detail/non_const_lvalue.hpp"
  23. #include "asio/detail/throw_error.hpp"
  24. #include "asio/error.hpp"
  25. #include "asio/detail/push_options.hpp"
  26. namespace asio {
  27. #if !defined(ASIO_BASIC_RANDOM_ACCESS_FILE_FWD_DECL)
  28. #define ASIO_BASIC_RANDOM_ACCESS_FILE_FWD_DECL
  29. // Forward declaration with defaulted arguments.
  30. template <typename Executor = any_io_executor>
  31. class basic_random_access_file;
  32. #endif // !defined(ASIO_BASIC_RANDOM_ACCESS_FILE_FWD_DECL)
  33. /// Provides random-access file functionality.
  34. /**
  35. * The basic_random_access_file class template provides asynchronous and
  36. * blocking random-access file functionality.
  37. *
  38. * @par Thread Safety
  39. * @e Distinct @e objects: Safe.@n
  40. * @e Shared @e objects: Unsafe.
  41. *
  42. * Synchronous @c read_some_at and @c write_some_at operations are thread safe
  43. * with respect to each other, if the underlying operating system calls are
  44. * also thread safe. This means that it is permitted to perform concurrent
  45. * calls to these synchronous operations on a single file object. Other
  46. * synchronous operations, such as @c open or @c close, are not thread safe.
  47. */
  48. template <typename Executor>
  49. class basic_random_access_file
  50. : public basic_file<Executor>
  51. {
  52. private:
  53. class initiate_async_write_some_at;
  54. class initiate_async_read_some_at;
  55. public:
  56. /// The type of the executor associated with the object.
  57. typedef Executor executor_type;
  58. /// Rebinds the file type to another executor.
  59. template <typename Executor1>
  60. struct rebind_executor
  61. {
  62. /// The file type when rebound to the specified executor.
  63. typedef basic_random_access_file<Executor1> other;
  64. };
  65. /// The native representation of a file.
  66. #if defined(GENERATING_DOCUMENTATION)
  67. typedef implementation_defined native_handle_type;
  68. #else
  69. typedef typename basic_file<Executor>::native_handle_type native_handle_type;
  70. #endif
  71. /// Construct a basic_random_access_file without opening it.
  72. /**
  73. * This constructor initialises a file without opening it. The file needs to
  74. * be opened before data can be read from or or written to it.
  75. *
  76. * @param ex The I/O executor that the file will use, by default, to
  77. * dispatch handlers for any asynchronous operations performed on the file.
  78. */
  79. explicit basic_random_access_file(const executor_type& ex)
  80. : basic_file<Executor>(ex)
  81. {
  82. }
  83. /// Construct a basic_random_access_file without opening it.
  84. /**
  85. * This constructor initialises a file without opening it. The file needs to
  86. * be opened before data can be read from or or written to it.
  87. *
  88. * @param context An execution context which provides the I/O executor that
  89. * the file will use, by default, to dispatch handlers for any asynchronous
  90. * operations performed on the file.
  91. */
  92. template <typename ExecutionContext>
  93. explicit basic_random_access_file(ExecutionContext& context,
  94. constraint_t<
  95. is_convertible<ExecutionContext&, execution_context&>::value,
  96. defaulted_constraint
  97. > = defaulted_constraint())
  98. : basic_file<Executor>(context)
  99. {
  100. }
  101. /// Construct and open a basic_random_access_file.
  102. /**
  103. * This constructor initialises and opens a file.
  104. *
  105. * @param ex The I/O executor that the file will use, by default, to
  106. * dispatch handlers for any asynchronous operations performed on the file.
  107. *
  108. * @param path The path name identifying the file to be opened.
  109. *
  110. * @param open_flags A set of flags that determine how the file should be
  111. * opened.
  112. *
  113. * @throws asio::system_error Thrown on failure.
  114. */
  115. basic_random_access_file(const executor_type& ex,
  116. const char* path, file_base::flags open_flags)
  117. : basic_file<Executor>(ex, path, open_flags)
  118. {
  119. }
  120. /// Construct and open a basic_random_access_file.
  121. /**
  122. * This constructor initialises and opens a file.
  123. *
  124. * @param context An execution context which provides the I/O executor that
  125. * the file will use, by default, to dispatch handlers for any asynchronous
  126. * operations performed on the file.
  127. *
  128. * @param path The path name identifying the file to be opened.
  129. *
  130. * @param open_flags A set of flags that determine how the file should be
  131. * opened.
  132. *
  133. * @throws asio::system_error Thrown on failure.
  134. */
  135. template <typename ExecutionContext>
  136. basic_random_access_file(ExecutionContext& context,
  137. const char* path, file_base::flags open_flags,
  138. constraint_t<
  139. is_convertible<ExecutionContext&, execution_context&>::value,
  140. defaulted_constraint
  141. > = defaulted_constraint())
  142. : basic_file<Executor>(context, path, open_flags)
  143. {
  144. }
  145. /// Construct and open a basic_random_access_file.
  146. /**
  147. * This constructor initialises and opens a file.
  148. *
  149. * @param ex The I/O executor that the file will use, by default, to
  150. * dispatch handlers for any asynchronous operations performed on the file.
  151. *
  152. * @param path The path name identifying the file to be opened.
  153. *
  154. * @param open_flags A set of flags that determine how the file should be
  155. * opened.
  156. *
  157. * @throws asio::system_error Thrown on failure.
  158. */
  159. basic_random_access_file(const executor_type& ex,
  160. const std::string& path, file_base::flags open_flags)
  161. : basic_file<Executor>(ex, path, open_flags)
  162. {
  163. }
  164. /// Construct and open a basic_random_access_file.
  165. /**
  166. * This constructor initialises and opens a file.
  167. *
  168. * @param context An execution context which provides the I/O executor that
  169. * the file will use, by default, to dispatch handlers for any asynchronous
  170. * operations performed on the file.
  171. *
  172. * @param path The path name identifying the file to be opened.
  173. *
  174. * @param open_flags A set of flags that determine how the file should be
  175. * opened.
  176. *
  177. * @throws asio::system_error Thrown on failure.
  178. */
  179. template <typename ExecutionContext>
  180. basic_random_access_file(ExecutionContext& context,
  181. const std::string& path, file_base::flags open_flags,
  182. constraint_t<
  183. is_convertible<ExecutionContext&, execution_context&>::value,
  184. defaulted_constraint
  185. > = defaulted_constraint())
  186. : basic_file<Executor>(context, path, open_flags)
  187. {
  188. }
  189. /// Construct a basic_random_access_file on an existing native file.
  190. /**
  191. * This constructor initialises a random-access file object to hold an
  192. * existing native file.
  193. *
  194. * @param ex The I/O executor that the file will use, by default, to
  195. * dispatch handlers for any asynchronous operations performed on the file.
  196. *
  197. * @param native_file The new underlying file implementation.
  198. *
  199. * @throws asio::system_error Thrown on failure.
  200. */
  201. basic_random_access_file(const executor_type& ex,
  202. const native_handle_type& native_file)
  203. : basic_file<Executor>(ex, native_file)
  204. {
  205. }
  206. /// Construct a basic_random_access_file on an existing native file.
  207. /**
  208. * This constructor initialises a random-access file object to hold an
  209. * existing native file.
  210. *
  211. * @param context An execution context which provides the I/O executor that
  212. * the file will use, by default, to dispatch handlers for any asynchronous
  213. * operations performed on the file.
  214. *
  215. * @param native_file The new underlying file implementation.
  216. *
  217. * @throws asio::system_error Thrown on failure.
  218. */
  219. template <typename ExecutionContext>
  220. basic_random_access_file(ExecutionContext& context,
  221. const native_handle_type& native_file,
  222. constraint_t<
  223. is_convertible<ExecutionContext&, execution_context&>::value,
  224. defaulted_constraint
  225. > = defaulted_constraint())
  226. : basic_file<Executor>(context, native_file)
  227. {
  228. }
  229. /// Move-construct a basic_random_access_file from another.
  230. /**
  231. * This constructor moves a random-access file from one object to another.
  232. *
  233. * @param other The other basic_random_access_file object from which the move
  234. * will occur.
  235. *
  236. * @note Following the move, the moved-from object is in the same state as if
  237. * constructed using the @c basic_random_access_file(const executor_type&)
  238. * constructor.
  239. */
  240. basic_random_access_file(basic_random_access_file&& other) noexcept
  241. : basic_file<Executor>(std::move(other))
  242. {
  243. }
  244. /// Move-assign a basic_random_access_file from another.
  245. /**
  246. * This assignment operator moves a random-access file from one object to
  247. * another.
  248. *
  249. * @param other The other basic_random_access_file object from which the move
  250. * will occur.
  251. *
  252. * @note Following the move, the moved-from object is in the same state as if
  253. * constructed using the @c basic_random_access_file(const executor_type&)
  254. * constructor.
  255. */
  256. basic_random_access_file& operator=(basic_random_access_file&& other)
  257. {
  258. basic_file<Executor>::operator=(std::move(other));
  259. return *this;
  260. }
  261. /// Move-construct a basic_random_access_file from a file of another executor
  262. /// type.
  263. /**
  264. * This constructor moves a random-access file from one object to another.
  265. *
  266. * @param other The other basic_random_access_file object from which the move
  267. * will occur.
  268. *
  269. * @note Following the move, the moved-from object is in the same state as if
  270. * constructed using the @c basic_random_access_file(const executor_type&)
  271. * constructor.
  272. */
  273. template <typename Executor1>
  274. basic_random_access_file(basic_random_access_file<Executor1>&& other,
  275. constraint_t<
  276. is_convertible<Executor1, Executor>::value,
  277. defaulted_constraint
  278. > = defaulted_constraint())
  279. : basic_file<Executor>(std::move(other))
  280. {
  281. }
  282. /// Move-assign a basic_random_access_file from a file of another executor
  283. /// type.
  284. /**
  285. * This assignment operator moves a random-access file from one object to
  286. * another.
  287. *
  288. * @param other The other basic_random_access_file object from which the move
  289. * will occur.
  290. *
  291. * @note Following the move, the moved-from object is in the same state as if
  292. * constructed using the @c basic_random_access_file(const executor_type&)
  293. * constructor.
  294. */
  295. template <typename Executor1>
  296. constraint_t<
  297. is_convertible<Executor1, Executor>::value,
  298. basic_random_access_file&
  299. > operator=(basic_random_access_file<Executor1>&& other)
  300. {
  301. basic_file<Executor>::operator=(std::move(other));
  302. return *this;
  303. }
  304. /// Destroys the file.
  305. /**
  306. * This function destroys the file, cancelling any outstanding asynchronous
  307. * operations associated with the file as if by calling @c cancel.
  308. */
  309. ~basic_random_access_file()
  310. {
  311. }
  312. /// Write some data to the handle at the specified offset.
  313. /**
  314. * This function is used to write data to the random-access handle. The
  315. * function call will block until one or more bytes of the data has been
  316. * written successfully, or until an error occurs.
  317. *
  318. * @param offset The offset at which the data will be written.
  319. *
  320. * @param buffers One or more data buffers to be written to the handle.
  321. *
  322. * @returns The number of bytes written.
  323. *
  324. * @throws asio::system_error Thrown on failure. An error code of
  325. * asio::error::eof indicates that the end of the file was reached.
  326. *
  327. * @note The write_some_at operation may not write all of the data. Consider
  328. * using the @ref write_at function if you need to ensure that all data is
  329. * written before the blocking operation completes.
  330. *
  331. * @par Example
  332. * To write a single data buffer use the @ref buffer function as follows:
  333. * @code
  334. * handle.write_some_at(42, asio::buffer(data, size));
  335. * @endcode
  336. * See the @ref buffer documentation for information on writing multiple
  337. * buffers in one go, and how to use it with arrays, boost::array or
  338. * std::vector.
  339. */
  340. template <typename ConstBufferSequence>
  341. std::size_t write_some_at(uint64_t offset,
  342. const ConstBufferSequence& buffers)
  343. {
  344. asio::error_code ec;
  345. std::size_t s = this->impl_.get_service().write_some_at(
  346. this->impl_.get_implementation(), offset, buffers, ec);
  347. asio::detail::throw_error(ec, "write_some_at");
  348. return s;
  349. }
  350. /// Write some data to the handle at the specified offset.
  351. /**
  352. * This function is used to write data to the random-access handle. The
  353. * function call will block until one or more bytes of the data has been
  354. * written successfully, or until an error occurs.
  355. *
  356. * @param offset The offset at which the data will be written.
  357. *
  358. * @param buffers One or more data buffers to be written to the handle.
  359. *
  360. * @param ec Set to indicate what error occurred, if any.
  361. *
  362. * @returns The number of bytes written. Returns 0 if an error occurred.
  363. *
  364. * @note The write_some operation may not write all of the data to the
  365. * file. Consider using the @ref write_at function if you need to ensure that
  366. * all data is written before the blocking operation completes.
  367. */
  368. template <typename ConstBufferSequence>
  369. std::size_t write_some_at(uint64_t offset,
  370. const ConstBufferSequence& buffers, asio::error_code& ec)
  371. {
  372. return this->impl_.get_service().write_some_at(
  373. this->impl_.get_implementation(), offset, buffers, ec);
  374. }
  375. /// Start an asynchronous write at the specified offset.
  376. /**
  377. * This function is used to asynchronously write data to the random-access
  378. * handle. It is an initiating function for an @ref asynchronous_operation,
  379. * and always returns immediately.
  380. *
  381. * @param offset The offset at which the data will be written.
  382. *
  383. * @param buffers One or more data buffers to be written to the handle.
  384. * Although the buffers object may be copied as necessary, ownership of the
  385. * underlying memory blocks is retained by the caller, which must guarantee
  386. * that they remain valid until the completion handler is called.
  387. *
  388. * @param token The @ref completion_token that will be used to produce a
  389. * completion handler, which will be called when the write completes.
  390. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  391. * @ref yield_context, or a function object with the correct completion
  392. * signature. The function signature of the completion handler must be:
  393. * @code void handler(
  394. * const asio::error_code& error, // Result of operation.
  395. * std::size_t bytes_transferred // Number of bytes written.
  396. * ); @endcode
  397. * Regardless of whether the asynchronous operation completes immediately or
  398. * not, the completion handler will not be invoked from within this function.
  399. * On immediate completion, invocation of the handler will be performed in a
  400. * manner equivalent to using asio::post().
  401. *
  402. * @par Completion Signature
  403. * @code void(asio::error_code, std::size_t) @endcode
  404. *
  405. * @note The write operation may not write all of the data to the file.
  406. * Consider using the @ref async_write_at function if you need to ensure that
  407. * all data is written before the asynchronous operation completes.
  408. *
  409. * @par Example
  410. * To write a single data buffer use the @ref buffer function as follows:
  411. * @code
  412. * handle.async_write_some_at(42, asio::buffer(data, size), handler);
  413. * @endcode
  414. * See the @ref buffer documentation for information on writing multiple
  415. * buffers in one go, and how to use it with arrays, boost::array or
  416. * std::vector.
  417. *
  418. * @par Per-Operation Cancellation
  419. * This asynchronous operation supports cancellation for the following
  420. * asio::cancellation_type values:
  421. *
  422. * @li @c cancellation_type::terminal
  423. *
  424. * @li @c cancellation_type::partial
  425. *
  426. * @li @c cancellation_type::total
  427. */
  428. template <typename ConstBufferSequence,
  429. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  430. std::size_t)) WriteToken = default_completion_token_t<executor_type>>
  431. auto async_write_some_at(uint64_t offset, const ConstBufferSequence& buffers,
  432. WriteToken&& token = default_completion_token_t<executor_type>())
  433. -> decltype(
  434. async_initiate<WriteToken,
  435. void (asio::error_code, std::size_t)>(
  436. declval<initiate_async_write_some_at>(), token, offset, buffers))
  437. {
  438. return async_initiate<WriteToken,
  439. void (asio::error_code, std::size_t)>(
  440. initiate_async_write_some_at(this), token, offset, buffers);
  441. }
  442. /// Read some data from the handle at the specified offset.
  443. /**
  444. * This function is used to read data from the random-access handle. The
  445. * function call will block until one or more bytes of data has been read
  446. * successfully, or until an error occurs.
  447. *
  448. * @param offset The offset at which the data will be read.
  449. *
  450. * @param buffers One or more buffers into which the data will be read.
  451. *
  452. * @returns The number of bytes read.
  453. *
  454. * @throws asio::system_error Thrown on failure. An error code of
  455. * asio::error::eof indicates that the end of the file was reached.
  456. *
  457. * @note The read_some operation may not read all of the requested number of
  458. * bytes. Consider using the @ref read_at function if you need to ensure that
  459. * the requested amount of data is read before the blocking operation
  460. * completes.
  461. *
  462. * @par Example
  463. * To read into a single data buffer use the @ref buffer function as follows:
  464. * @code
  465. * handle.read_some_at(42, asio::buffer(data, size));
  466. * @endcode
  467. * See the @ref buffer documentation for information on reading into multiple
  468. * buffers in one go, and how to use it with arrays, boost::array or
  469. * std::vector.
  470. */
  471. template <typename MutableBufferSequence>
  472. std::size_t read_some_at(uint64_t offset,
  473. const MutableBufferSequence& buffers)
  474. {
  475. asio::error_code ec;
  476. std::size_t s = this->impl_.get_service().read_some_at(
  477. this->impl_.get_implementation(), offset, buffers, ec);
  478. asio::detail::throw_error(ec, "read_some_at");
  479. return s;
  480. }
  481. /// Read some data from the handle at the specified offset.
  482. /**
  483. * This function is used to read data from the random-access handle. The
  484. * function call will block until one or more bytes of data has been read
  485. * successfully, or until an error occurs.
  486. *
  487. * @param offset The offset at which the data will be read.
  488. *
  489. * @param buffers One or more buffers into which the data will be read.
  490. *
  491. * @param ec Set to indicate what error occurred, if any.
  492. *
  493. * @returns The number of bytes read. Returns 0 if an error occurred.
  494. *
  495. * @note The read_some operation may not read all of the requested number of
  496. * bytes. Consider using the @ref read_at function if you need to ensure that
  497. * the requested amount of data is read before the blocking operation
  498. * completes.
  499. */
  500. template <typename MutableBufferSequence>
  501. std::size_t read_some_at(uint64_t offset,
  502. const MutableBufferSequence& buffers, asio::error_code& ec)
  503. {
  504. return this->impl_.get_service().read_some_at(
  505. this->impl_.get_implementation(), offset, buffers, ec);
  506. }
  507. /// Start an asynchronous read at the specified offset.
  508. /**
  509. * This function is used to asynchronously read data from the random-access
  510. * handle. It is an initiating function for an @ref asynchronous_operation,
  511. * and always returns immediately.
  512. *
  513. * @param offset The offset at which the data will be read.
  514. *
  515. * @param buffers One or more buffers into which the data will be read.
  516. * Although the buffers object may be copied as necessary, ownership of the
  517. * underlying memory blocks is retained by the caller, which must guarantee
  518. * that they remain valid until the completion handler is called.
  519. *
  520. * @param token The @ref completion_token that will be used to produce a
  521. * completion handler, which will be called when the read completes.
  522. * Potential completion tokens include @ref use_future, @ref use_awaitable,
  523. * @ref yield_context, or a function object with the correct completion
  524. * signature. The function signature of the completion handler must be:
  525. * @code void handler(
  526. * const asio::error_code& error, // Result of operation.
  527. * std::size_t bytes_transferred // Number of bytes read.
  528. * ); @endcode
  529. * Regardless of whether the asynchronous operation completes immediately or
  530. * not, the completion handler will not be invoked from within this function.
  531. * On immediate completion, invocation of the handler will be performed in a
  532. * manner equivalent to using asio::post().
  533. *
  534. * @par Completion Signature
  535. * @code void(asio::error_code, std::size_t) @endcode
  536. *
  537. * @note The read operation may not read all of the requested number of bytes.
  538. * Consider using the @ref async_read_at function if you need to ensure that
  539. * the requested amount of data is read before the asynchronous operation
  540. * completes.
  541. *
  542. * @par Example
  543. * To read into a single data buffer use the @ref buffer function as follows:
  544. * @code
  545. * handle.async_read_some_at(42, asio::buffer(data, size), handler);
  546. * @endcode
  547. * See the @ref buffer documentation for information on reading into multiple
  548. * buffers in one go, and how to use it with arrays, boost::array or
  549. * std::vector.
  550. *
  551. * @par Per-Operation Cancellation
  552. * This asynchronous operation supports cancellation for the following
  553. * asio::cancellation_type values:
  554. *
  555. * @li @c cancellation_type::terminal
  556. *
  557. * @li @c cancellation_type::partial
  558. *
  559. * @li @c cancellation_type::total
  560. */
  561. template <typename MutableBufferSequence,
  562. ASIO_COMPLETION_TOKEN_FOR(void (asio::error_code,
  563. std::size_t)) ReadToken = default_completion_token_t<executor_type>>
  564. auto async_read_some_at(uint64_t offset, const MutableBufferSequence& buffers,
  565. ReadToken&& token = default_completion_token_t<executor_type>())
  566. -> decltype(
  567. async_initiate<ReadToken,
  568. void (asio::error_code, std::size_t)>(
  569. declval<initiate_async_read_some_at>(), token, offset, buffers))
  570. {
  571. return async_initiate<ReadToken,
  572. void (asio::error_code, std::size_t)>(
  573. initiate_async_read_some_at(this), token, offset, buffers);
  574. }
  575. private:
  576. // Disallow copying and assignment.
  577. basic_random_access_file(const basic_random_access_file&) = delete;
  578. basic_random_access_file& operator=(
  579. const basic_random_access_file&) = delete;
  580. class initiate_async_write_some_at
  581. {
  582. public:
  583. typedef Executor executor_type;
  584. explicit initiate_async_write_some_at(basic_random_access_file* self)
  585. : self_(self)
  586. {
  587. }
  588. const executor_type& get_executor() const noexcept
  589. {
  590. return self_->get_executor();
  591. }
  592. template <typename WriteHandler, typename ConstBufferSequence>
  593. void operator()(WriteHandler&& handler,
  594. uint64_t offset, const ConstBufferSequence& buffers) const
  595. {
  596. // If you get an error on the following line it means that your handler
  597. // does not meet the documented type requirements for a WriteHandler.
  598. ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
  599. detail::non_const_lvalue<WriteHandler> handler2(handler);
  600. self_->impl_.get_service().async_write_some_at(
  601. self_->impl_.get_implementation(), offset, buffers,
  602. handler2.value, self_->impl_.get_executor());
  603. }
  604. private:
  605. basic_random_access_file* self_;
  606. };
  607. class initiate_async_read_some_at
  608. {
  609. public:
  610. typedef Executor executor_type;
  611. explicit initiate_async_read_some_at(basic_random_access_file* self)
  612. : self_(self)
  613. {
  614. }
  615. const executor_type& get_executor() const noexcept
  616. {
  617. return self_->get_executor();
  618. }
  619. template <typename ReadHandler, typename MutableBufferSequence>
  620. void operator()(ReadHandler&& handler,
  621. uint64_t offset, const MutableBufferSequence& buffers) const
  622. {
  623. // If you get an error on the following line it means that your handler
  624. // does not meet the documented type requirements for a ReadHandler.
  625. ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
  626. detail::non_const_lvalue<ReadHandler> handler2(handler);
  627. self_->impl_.get_service().async_read_some_at(
  628. self_->impl_.get_implementation(), offset, buffers,
  629. handler2.value, self_->impl_.get_executor());
  630. }
  631. private:
  632. basic_random_access_file* self_;
  633. };
  634. };
  635. } // namespace asio
  636. #include "asio/detail/pop_options.hpp"
  637. #endif // defined(ASIO_HAS_FILE)
  638. // || defined(GENERATING_DOCUMENTATION)
  639. #endif // ASIO_BASIC_RANDOM_ACCESS_FILE_HPP