annotate DEPENDENCIES/generic/include/boost/asio/impl/write_at.hpp @ 133:4acb5d8d80b6 tip

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