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