5 // Copyright (c) 2003-2011 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_WRITE_AT_HPP
12 #define BOOST_ASIO_WRITE_AT_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #include <boost/cstdint.hpp>
21 #include <boost/asio/basic_streambuf_fwd.hpp>
22 #include <boost/asio/error.hpp>
24 #include <boost/asio/detail/push_options.hpp>
30 * @defgroup write_at boost::asio::write_at
32 * @brief Write a certain amount of data at a specified offset before returning.
36 /// Write all of the supplied data at the specified offset before returning.
38 * This function is used to write a certain number of bytes of data to a random
39 * access device at a specified offset. The call will block until one of the
40 * following conditions is true:
42 * @li All of the data in the supplied buffers has been written. That is, the
43 * bytes transferred is equal to the sum of the buffer sizes.
45 * @li An error occurred.
47 * This operation is implemented in terms of zero or more calls to the device's
48 * write_some_at function.
50 * @param d The device to which the data is to be written. The type must support
51 * the SyncRandomAccessWriteDevice concept.
53 * @param offset The offset at which the data will be written.
55 * @param buffers One or more buffers containing the data to be written. The sum
56 * of the buffer sizes indicates the maximum number of bytes to write to the
59 * @returns The number of bytes transferred.
61 * @throws boost::system::system_error Thrown on failure.
64 * To write a single data buffer use the @ref buffer function as follows:
65 * @code boost::asio::write_at(d, 42, boost::asio::buffer(data, size)); @endcode
66 * See the @ref buffer documentation for information on writing multiple
67 * buffers in one go, and how to use it with arrays, boost::array or
70 * @note This overload is equivalent to calling:
71 * @code boost::asio::write_at(
73 * boost::asio::transfer_all()); @endcode
75 template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
76 std::size_t write_at(SyncRandomAccessWriteDevice& d,
77 boost::uint64_t offset, const ConstBufferSequence& buffers);
79 /// Write all of the supplied data at the specified offset before returning.
81 * This function is used to write a certain number of bytes of data to a random
82 * access device at a specified offset. The call will block until one of the
83 * following conditions is true:
85 * @li All of the data in the supplied buffers has been written. That is, the
86 * bytes transferred is equal to the sum of the buffer sizes.
88 * @li An error occurred.
90 * This operation is implemented in terms of zero or more calls to the device's
91 * write_some_at function.
93 * @param d The device to which the data is to be written. The type must support
94 * the SyncRandomAccessWriteDevice concept.
96 * @param offset The offset at which the data will be written.
98 * @param buffers One or more buffers containing the data to be written. The sum
99 * of the buffer sizes indicates the maximum number of bytes to write to the
102 * @param ec Set to indicate what error occurred, if any.
104 * @returns The number of bytes transferred.
107 * To write a single data buffer use the @ref buffer function as follows:
108 * @code boost::asio::write_at(d, 42,
109 * boost::asio::buffer(data, size), ec); @endcode
110 * See the @ref buffer documentation for information on writing multiple
111 * buffers in one go, and how to use it with arrays, boost::array or
114 * @note This overload is equivalent to calling:
115 * @code boost::asio::write_at(
116 * d, offset, buffers,
117 * boost::asio::transfer_all(), ec); @endcode
119 template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence>
120 std::size_t write_at(SyncRandomAccessWriteDevice& d,
121 boost::uint64_t offset, const ConstBufferSequence& buffers,
122 boost::system::error_code& ec);
124 /// Write a certain amount of data at a specified offset before returning.
126 * This function is used to write a certain number of bytes of data to a random
127 * access device at a specified offset. The call will block until one of the
128 * following conditions is true:
130 * @li All of the data in the supplied buffers has been written. That is, the
131 * bytes transferred is equal to the sum of the buffer sizes.
133 * @li The completion_condition function object returns 0.
135 * This operation is implemented in terms of zero or more calls to the device's
136 * write_some_at function.
138 * @param d The device to which the data is to be written. The type must support
139 * the SyncRandomAccessWriteDevice concept.
141 * @param offset The offset at which the data will be written.
143 * @param buffers One or more buffers containing the data to be written. The sum
144 * of the buffer sizes indicates the maximum number of bytes to write to the
147 * @param completion_condition The function object to be called to determine
148 * whether the write operation is complete. The signature of the function object
150 * @code std::size_t completion_condition(
151 * // Result of latest write_some_at operation.
152 * const boost::system::error_code& error,
154 * // Number of bytes transferred so far.
155 * std::size_t bytes_transferred
157 * A return value of 0 indicates that the write operation is complete. A
158 * non-zero return value indicates the maximum number of bytes to be written on
159 * the next call to the device's write_some_at function.
161 * @returns The number of bytes transferred.
163 * @throws boost::system::system_error Thrown on failure.
166 * To write a single data buffer use the @ref buffer function as follows:
167 * @code boost::asio::write_at(d, 42, boost::asio::buffer(data, size),
168 * boost::asio::transfer_at_least(32)); @endcode
169 * See the @ref buffer documentation for information on writing multiple
170 * buffers in one go, and how to use it with arrays, boost::array or
173 template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
174 typename CompletionCondition>
175 std::size_t write_at(SyncRandomAccessWriteDevice& d,
176 boost::uint64_t offset, const ConstBufferSequence& buffers,
177 CompletionCondition completion_condition);
179 /// Write a certain amount of data at a specified offset before returning.
181 * This function is used to write a certain number of bytes of data to a random
182 * access device at a specified offset. The call will block until one of the
183 * following conditions is true:
185 * @li All of the data in the supplied buffers has been written. That is, the
186 * bytes transferred is equal to the sum of the buffer sizes.
188 * @li The completion_condition function object returns 0.
190 * This operation is implemented in terms of zero or more calls to the device's
191 * write_some_at function.
193 * @param d The device to which the data is to be written. The type must support
194 * the SyncRandomAccessWriteDevice concept.
196 * @param offset The offset at which the data will be written.
198 * @param buffers One or more buffers containing the data to be written. The sum
199 * of the buffer sizes indicates the maximum number of bytes to write to the
202 * @param completion_condition The function object to be called to determine
203 * whether the write operation is complete. The signature of the function object
205 * @code std::size_t completion_condition(
206 * // Result of latest write_some_at operation.
207 * const boost::system::error_code& error,
209 * // Number of bytes transferred so far.
210 * std::size_t bytes_transferred
212 * A return value of 0 indicates that the write operation is complete. A
213 * non-zero return value indicates the maximum number of bytes to be written on
214 * the next call to the device's write_some_at function.
216 * @param ec Set to indicate what error occurred, if any.
218 * @returns The number of bytes written. If an error occurs, returns the total
219 * number of bytes successfully transferred prior to the error.
221 template <typename SyncRandomAccessWriteDevice, typename ConstBufferSequence,
222 typename CompletionCondition>
223 std::size_t write_at(SyncRandomAccessWriteDevice& d,
224 boost::uint64_t offset, const ConstBufferSequence& buffers,
225 CompletionCondition completion_condition, boost::system::error_code& ec);
227 #if !defined(BOOST_NO_IOSTREAM)
229 /// Write all of the supplied data at the specified offset before returning.
231 * This function is used to write a certain number of bytes of data to a random
232 * access device at a specified offset. The call will block until one of the
233 * following conditions is true:
235 * @li All of the data in the supplied basic_streambuf has been written.
237 * @li An error occurred.
239 * This operation is implemented in terms of zero or more calls to the device's
240 * write_some_at function.
242 * @param d The device to which the data is to be written. The type must support
243 * the SyncRandomAccessWriteDevice concept.
245 * @param offset The offset at which the data will be written.
247 * @param b The basic_streambuf object from which data will be written.
249 * @returns The number of bytes transferred.
251 * @throws boost::system::system_error Thrown on failure.
253 * @note This overload is equivalent to calling:
254 * @code boost::asio::write_at(
256 * boost::asio::transfer_all()); @endcode
258 template <typename SyncRandomAccessWriteDevice, typename Allocator>
259 std::size_t write_at(SyncRandomAccessWriteDevice& d,
260 boost::uint64_t offset, basic_streambuf<Allocator>& b);
262 /// Write all of the supplied data at the specified offset before returning.
264 * This function is used to write a certain number of bytes of data to a random
265 * access device at a specified offset. The call will block until one of the
266 * following conditions is true:
268 * @li All of the data in the supplied basic_streambuf has been written.
270 * @li An error occurred.
272 * This operation is implemented in terms of zero or more calls to the device's
273 * write_some_at function.
275 * @param d The device to which the data is to be written. The type must support
276 * the SyncRandomAccessWriteDevice concept.
278 * @param offset The offset at which the data will be written.
280 * @param b The basic_streambuf object from which data will be written.
282 * @param ec Set to indicate what error occurred, if any.
284 * @returns The number of bytes transferred.
286 * @note This overload is equivalent to calling:
287 * @code boost::asio::write_at(
289 * boost::asio::transfer_all(), ec); @endcode
291 template <typename SyncRandomAccessWriteDevice, typename Allocator>
292 std::size_t write_at(SyncRandomAccessWriteDevice& d,
293 boost::uint64_t offset, basic_streambuf<Allocator>& b,
294 boost::system::error_code& ec);
296 /// Write a certain amount of data at a specified offset before returning.
298 * This function is used to write a certain number of bytes of data to a random
299 * access device at a specified offset. The call will block until one of the
300 * following conditions is true:
302 * @li All of the data in the supplied basic_streambuf has been written.
304 * @li The completion_condition function object returns 0.
306 * This operation is implemented in terms of zero or more calls to the device's
307 * write_some_at function.
309 * @param d The device to which the data is to be written. The type must support
310 * the SyncRandomAccessWriteDevice concept.
312 * @param offset The offset at which the data will be written.
314 * @param b The basic_streambuf object from which data will be written.
316 * @param completion_condition The function object to be called to determine
317 * whether the write operation is complete. The signature of the function object
319 * @code std::size_t completion_condition(
320 * // Result of latest write_some_at operation.
321 * const boost::system::error_code& error,
323 * // Number of bytes transferred so far.
324 * std::size_t bytes_transferred
326 * A return value of 0 indicates that the write operation is complete. A
327 * non-zero return value indicates the maximum number of bytes to be written on
328 * the next call to the device's write_some_at function.
330 * @returns The number of bytes transferred.
332 * @throws boost::system::system_error Thrown on failure.
334 template <typename SyncRandomAccessWriteDevice, typename Allocator,
335 typename CompletionCondition>
336 std::size_t write_at(SyncRandomAccessWriteDevice& d, boost::uint64_t offset,
337 basic_streambuf<Allocator>& b, CompletionCondition completion_condition);
339 /// Write a certain amount of data at a specified offset before returning.
341 * This function is used to write a certain number of bytes of data to a random
342 * access device at a specified offset. The call will block until one of the
343 * following conditions is true:
345 * @li All of the data in the supplied basic_streambuf has been written.
347 * @li The completion_condition function object returns 0.
349 * This operation is implemented in terms of zero or more calls to the device's
350 * write_some_at function.
352 * @param d The device to which the data is to be written. The type must support
353 * the SyncRandomAccessWriteDevice concept.
355 * @param offset The offset at which the data will be written.
357 * @param b The basic_streambuf object from which data will be written.
359 * @param completion_condition The function object to be called to determine
360 * whether the write operation is complete. The signature of the function object
362 * @code std::size_t completion_condition(
363 * // Result of latest write_some_at operation.
364 * const boost::system::error_code& error,
366 * // Number of bytes transferred so far.
367 * std::size_t bytes_transferred
369 * A return value of 0 indicates that the write operation is complete. A
370 * non-zero return value indicates the maximum number of bytes to be written on
371 * the next call to the device's write_some_at function.
373 * @param ec Set to indicate what error occurred, if any.
375 * @returns The number of bytes written. If an error occurs, returns the total
376 * number of bytes successfully transferred prior to the error.
378 template <typename SyncRandomAccessWriteDevice, typename Allocator,
379 typename CompletionCondition>
380 std::size_t write_at(SyncRandomAccessWriteDevice& d, boost::uint64_t offset,
381 basic_streambuf<Allocator>& b, CompletionCondition completion_condition,
382 boost::system::error_code& ec);
384 #endif // !defined(BOOST_NO_IOSTREAM)
388 * @defgroup async_write_at boost::asio::async_write_at
390 * @brief Start an asynchronous operation to write a certain amount of data at
391 * the specified offset.
395 /// Start an asynchronous operation to write all of the supplied data at the
396 /// specified offset.
398 * This function is used to asynchronously write a certain number of bytes of
399 * data to a random access device at a specified offset. The function call
400 * always returns immediately. The asynchronous operation will continue until
401 * one of the following conditions is true:
403 * @li All of the data in the supplied buffers has been written. That is, the
404 * bytes transferred is equal to the sum of the buffer sizes.
406 * @li An error occurred.
408 * This operation is implemented in terms of zero or more calls to the device's
409 * async_write_some_at function.
411 * @param d The device to which the data is to be written. The type must support
412 * the AsyncRandomAccessWriteDevice concept.
414 * @param offset The offset at which the data will be written.
416 * @param buffers One or more buffers containing the data to be written.
417 * Although the buffers object may be copied as necessary, ownership of the
418 * underlying memory blocks is retained by the caller, which must guarantee
419 * that they remain valid until the handler is called.
421 * @param handler The handler to be called when the write operation completes.
422 * Copies will be made of the handler as required. The function signature of
423 * the handler must be:
424 * @code void handler(
425 * // Result of operation.
426 * const boost::system::error_code& error,
428 * // Number of bytes written from the buffers. If an error
429 * // occurred, this will be less than the sum of the buffer sizes.
430 * std::size_t bytes_transferred
432 * Regardless of whether the asynchronous operation completes immediately or
433 * not, the handler will not be invoked from within this function. Invocation of
434 * the handler will be performed in a manner equivalent to using
435 * boost::asio::io_service::post().
438 * To write a single data buffer use the @ref buffer function as follows:
440 * boost::asio::async_write_at(d, 42, boost::asio::buffer(data, size), handler);
442 * See the @ref buffer documentation for information on writing multiple
443 * buffers in one go, and how to use it with arrays, boost::array or
446 template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
447 typename WriteHandler>
448 void async_write_at(AsyncRandomAccessWriteDevice& d, boost::uint64_t offset,
449 const ConstBufferSequence& buffers,
450 BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
452 /// Start an asynchronous operation to write a certain amount of data at the
453 /// specified offset.
455 * This function is used to asynchronously write a certain number of bytes of
456 * data to a random access device at a specified offset. The function call
457 * always returns immediately. The asynchronous operation will continue until
458 * one of the following conditions is true:
460 * @li All of the data in the supplied buffers has been written. That is, the
461 * bytes transferred is equal to the sum of the buffer sizes.
463 * @li The completion_condition function object returns 0.
465 * This operation is implemented in terms of zero or more calls to the device's
466 * async_write_some_at function.
468 * @param d The device to which the data is to be written. The type must support
469 * the AsyncRandomAccessWriteDevice concept.
471 * @param offset The offset at which the data will be written.
473 * @param buffers One or more buffers containing the data to be written.
474 * Although the buffers object may be copied as necessary, ownership of the
475 * underlying memory blocks is retained by the caller, which must guarantee
476 * that they remain valid until the handler is called.
478 * @param completion_condition The function object to be called to determine
479 * whether the write operation is complete. The signature of the function object
481 * @code std::size_t completion_condition(
482 * // Result of latest async_write_some_at operation.
483 * const boost::system::error_code& error,
485 * // Number of bytes transferred so far.
486 * std::size_t bytes_transferred
488 * A return value of 0 indicates that the write operation is complete. A
489 * non-zero return value indicates the maximum number of bytes to be written on
490 * the next call to the device's async_write_some_at function.
492 * @param handler The handler to be called when the write operation completes.
493 * Copies will be made of the handler as required. The function signature of the
495 * @code void handler(
496 * // Result of operation.
497 * const boost::system::error_code& error,
499 * // Number of bytes written from the buffers. If an error
500 * // occurred, this will be less than the sum of the buffer sizes.
501 * std::size_t bytes_transferred
503 * Regardless of whether the asynchronous operation completes immediately or
504 * not, the handler will not be invoked from within this function. Invocation of
505 * the handler will be performed in a manner equivalent to using
506 * boost::asio::io_service::post().
509 * To write a single data buffer use the @ref buffer function as follows:
510 * @code boost::asio::async_write_at(d, 42,
511 * boost::asio::buffer(data, size),
512 * boost::asio::transfer_at_least(32),
514 * See the @ref buffer documentation for information on writing multiple
515 * buffers in one go, and how to use it with arrays, boost::array or
518 template <typename AsyncRandomAccessWriteDevice, typename ConstBufferSequence,
519 typename CompletionCondition, typename WriteHandler>
520 void async_write_at(AsyncRandomAccessWriteDevice& d,
521 boost::uint64_t offset, const ConstBufferSequence& buffers,
522 CompletionCondition completion_condition,
523 BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
525 #if !defined(BOOST_NO_IOSTREAM)
527 /// Start an asynchronous operation to write all of the supplied data at the
528 /// specified offset.
530 * This function is used to asynchronously write a certain number of bytes of
531 * data to a random access device at a specified offset. The function call
532 * always returns immediately. The asynchronous operation will continue until
533 * one of the following conditions is true:
535 * @li All of the data in the supplied basic_streambuf has been written.
537 * @li An error occurred.
539 * This operation is implemented in terms of zero or more calls to the device's
540 * async_write_some_at function.
542 * @param d The device to which the data is to be written. The type must support
543 * the AsyncRandomAccessWriteDevice concept.
545 * @param offset The offset at which the data will be written.
547 * @param b A basic_streambuf object from which data will be written. Ownership
548 * of the streambuf is retained by the caller, which must guarantee that it
549 * remains valid until the handler is called.
551 * @param handler The handler to be called when the write operation completes.
552 * Copies will be made of the handler as required. The function signature of the
554 * @code void handler(
555 * // Result of operation.
556 * const boost::system::error_code& error,
558 * // Number of bytes written from the buffers. If an error
559 * // occurred, this will be less than the sum of the buffer sizes.
560 * std::size_t bytes_transferred
562 * Regardless of whether the asynchronous operation completes immediately or
563 * not, the handler will not be invoked from within this function. Invocation of
564 * the handler will be performed in a manner equivalent to using
565 * boost::asio::io_service::post().
567 template <typename AsyncRandomAccessWriteDevice, typename Allocator,
568 typename WriteHandler>
569 void async_write_at(AsyncRandomAccessWriteDevice& d, boost::uint64_t offset,
570 basic_streambuf<Allocator>& b, BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
572 /// Start an asynchronous operation to write a certain amount of data at the
573 /// specified offset.
575 * This function is used to asynchronously write a certain number of bytes of
576 * data to a random access device at a specified offset. The function call
577 * always returns immediately. The asynchronous operation will continue until
578 * one of the following conditions is true:
580 * @li All of the data in the supplied basic_streambuf has been written.
582 * @li The completion_condition function object returns 0.
584 * This operation is implemented in terms of zero or more calls to the device's
585 * async_write_some_at function.
587 * @param d The device to which the data is to be written. The type must support
588 * the AsyncRandomAccessWriteDevice concept.
590 * @param offset The offset at which the data will be written.
592 * @param b A basic_streambuf object from which data will be written. Ownership
593 * of the streambuf is retained by the caller, which must guarantee that it
594 * remains valid until the handler is called.
596 * @param completion_condition The function object to be called to determine
597 * whether the write operation is complete. The signature of the function object
599 * @code std::size_t completion_condition(
600 * // Result of latest async_write_some_at operation.
601 * const boost::system::error_code& error,
603 * // Number of bytes transferred so far.
604 * std::size_t bytes_transferred
606 * A return value of 0 indicates that the write operation is complete. A
607 * non-zero return value indicates the maximum number of bytes to be written on
608 * the next call to the device's async_write_some_at function.
610 * @param handler The handler to be called when the write operation completes.
611 * Copies will be made of the handler as required. The function signature of the
613 * @code void handler(
614 * // Result of operation.
615 * const boost::system::error_code& error,
617 * // Number of bytes written from the buffers. If an error
618 * // occurred, this will be less than the sum of the buffer sizes.
619 * std::size_t bytes_transferred
621 * Regardless of whether the asynchronous operation completes immediately or
622 * not, the handler will not be invoked from within this function. Invocation of
623 * the handler will be performed in a manner equivalent to using
624 * boost::asio::io_service::post().
626 template <typename AsyncRandomAccessWriteDevice, typename Allocator,
627 typename CompletionCondition, typename WriteHandler>
628 void async_write_at(AsyncRandomAccessWriteDevice& d, boost::uint64_t offset,
629 basic_streambuf<Allocator>& b, CompletionCondition completion_condition,
630 BOOST_ASIO_MOVE_ARG(WriteHandler) handler);
632 #endif // !defined(BOOST_NO_IOSTREAM)
639 #include <boost/asio/detail/pop_options.hpp>
641 #include <boost/asio/impl/write_at.hpp>
643 #endif // BOOST_ASIO_WRITE_AT_HPP