Realistic 3D camera system
3D camera system components
buffered_write_stream.hpp
Go to the documentation of this file.
1 //
2 // impl/buffered_write_stream.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 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 ASIO_IMPL_BUFFERED_WRITE_STREAM_HPP
12 #define ASIO_IMPL_BUFFERED_WRITE_STREAM_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
22 
24 
25 namespace asio {
26 
27 template <typename Stream>
29 {
30  std::size_t bytes_written = write(next_layer_,
31  buffer(storage_.data(), storage_.size()));
32  storage_.consume(bytes_written);
33  return bytes_written;
34 }
35 
36 template <typename Stream>
38 {
39  std::size_t bytes_written = write(next_layer_,
40  buffer(storage_.data(), storage_.size()),
41  transfer_all(), ec);
42  storage_.consume(bytes_written);
43  return bytes_written;
44 }
45 
46 namespace detail
47 {
48  template <typename WriteHandler>
50  {
51  public:
53  WriteHandler& handler)
54  : storage_(storage),
55  handler_(handler)
56  {
57  }
58 
59 #if defined(ASIO_HAS_MOVE)
61  : storage_(other.storage_),
62  handler_(other.handler_)
63  {
64  }
65 
67  : storage_(other.storage_),
68  handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
69  {
70  }
71 #endif // defined(ASIO_HAS_MOVE)
72 
73  void operator()(const asio::error_code& ec,
74  const std::size_t bytes_written)
75  {
76  storage_.consume(bytes_written);
77  handler_(ec, bytes_written);
78  }
79 
80  //private:
82  WriteHandler handler_;
83  };
84 
85  template <typename WriteHandler>
86  inline void* asio_handler_allocate(std::size_t size,
88  {
90  size, this_handler->handler_);
91  }
92 
93  template <typename WriteHandler>
94  inline void asio_handler_deallocate(void* pointer, std::size_t size,
96  {
98  pointer, size, this_handler->handler_);
99  }
100 
101  template <typename WriteHandler>
104  {
106  this_handler->handler_);
107  }
108 
109  template <typename Function, typename WriteHandler>
110  inline void asio_handler_invoke(Function& function,
112  {
114  function, this_handler->handler_);
115  }
116 
117  template <typename Function, typename WriteHandler>
118  inline void asio_handler_invoke(const Function& function,
120  {
122  function, this_handler->handler_);
123  }
124 }
125 
126 template <typename Stream>
127 template <typename WriteHandler>
129  void (asio::error_code, std::size_t))
130 buffered_write_stream<Stream>::async_flush(
131  ASIO_MOVE_ARG(WriteHandler) handler)
132 {
133  // If you get an error on the following line it means that your handler does
134  // not meet the documented type requirements for a WriteHandler.
135  ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
136 
138  WriteHandler, void (asio::error_code, std::size_t)> init(
139  ASIO_MOVE_CAST(WriteHandler)(handler));
140 
141  async_write(next_layer_, buffer(storage_.data(), storage_.size()),
143  WriteHandler, void (asio::error_code, std::size_t))>(
144  storage_, init.handler));
145 
146  return init.result.get();
147 }
148 
149 template <typename Stream>
150 template <typename ConstBufferSequence>
152  const ConstBufferSequence& buffers)
153 {
154  if (asio::buffer_size(buffers) == 0)
155  return 0;
156 
157  if (storage_.size() == storage_.capacity())
158  this->flush();
159 
160  return this->copy(buffers);
161 }
162 
163 template <typename Stream>
164 template <typename ConstBufferSequence>
166  const ConstBufferSequence& buffers, asio::error_code& ec)
167 {
168  ec = asio::error_code();
169 
170  if (asio::buffer_size(buffers) == 0)
171  return 0;
172 
173  if (storage_.size() == storage_.capacity() && !flush(ec))
174  return 0;
175 
176  return this->copy(buffers);
177 }
178 
179 namespace detail
180 {
181  template <typename ConstBufferSequence, typename WriteHandler>
183  {
184  public:
186  const ConstBufferSequence& buffers, WriteHandler& handler)
187  : storage_(storage),
188  buffers_(buffers),
189  handler_(handler)
190  {
191  }
192 
193 #if defined(ASIO_HAS_MOVE)
195  : storage_(other.storage_),
196  buffers_(other.buffers_),
197  handler_(other.handler_)
198  {
199  }
200 
202  : storage_(other.storage_),
203  buffers_(other.buffers_),
204  handler_(ASIO_MOVE_CAST(WriteHandler)(other.handler_))
205  {
206  }
207 #endif // defined(ASIO_HAS_MOVE)
208 
209  void operator()(const asio::error_code& ec, std::size_t)
210  {
211  if (ec)
212  {
213  const std::size_t length = 0;
214  handler_(ec, length);
215  }
216  else
217  {
218  std::size_t orig_size = storage_.size();
219  std::size_t space_avail = storage_.capacity() - orig_size;
220  std::size_t bytes_avail = asio::buffer_size(buffers_);
221  std::size_t length = bytes_avail < space_avail
222  ? bytes_avail : space_avail;
223  storage_.resize(orig_size + length);
224  const std::size_t bytes_copied = asio::buffer_copy(
225  storage_.data() + orig_size, buffers_, length);
226  handler_(ec, bytes_copied);
227  }
228  }
229 
230  //private:
232  ConstBufferSequence buffers_;
233  WriteHandler handler_;
234  };
235 
236  template <typename ConstBufferSequence, typename WriteHandler>
237  inline void* asio_handler_allocate(std::size_t size,
239  ConstBufferSequence, WriteHandler>* this_handler)
240  {
242  size, this_handler->handler_);
243  }
244 
245  template <typename ConstBufferSequence, typename WriteHandler>
246  inline void asio_handler_deallocate(void* pointer, std::size_t size,
248  ConstBufferSequence, WriteHandler>* this_handler)
249  {
251  pointer, size, this_handler->handler_);
252  }
253 
254  template <typename ConstBufferSequence, typename WriteHandler>
257  ConstBufferSequence, WriteHandler>* this_handler)
258  {
260  this_handler->handler_);
261  }
262 
263  template <typename Function, typename ConstBufferSequence,
264  typename WriteHandler>
265  inline void asio_handler_invoke(Function& function,
267  ConstBufferSequence, WriteHandler>* this_handler)
268  {
270  function, this_handler->handler_);
271  }
272 
273  template <typename Function, typename ConstBufferSequence,
274  typename WriteHandler>
275  inline void asio_handler_invoke(const Function& function,
277  ConstBufferSequence, WriteHandler>* this_handler)
278  {
280  function, this_handler->handler_);
281  }
282 } // namespace detail
283 
284 template <typename Stream>
285 template <typename ConstBufferSequence, typename WriteHandler>
287  void (asio::error_code, std::size_t))
288 buffered_write_stream<Stream>::async_write_some(
289  const ConstBufferSequence& buffers,
290  ASIO_MOVE_ARG(WriteHandler) handler)
291 {
292  // If you get an error on the following line it means that your handler does
293  // not meet the documented type requirements for a WriteHandler.
294  ASIO_WRITE_HANDLER_CHECK(WriteHandler, handler) type_check;
295 
297  WriteHandler, void (asio::error_code, std::size_t)> init(
298  ASIO_MOVE_CAST(WriteHandler)(handler));
299 
300  if (asio::buffer_size(buffers) == 0
301  || storage_.size() < storage_.capacity())
302  {
303  next_layer_.async_write_some(asio::const_buffers_1(0, 0),
305  ConstBufferSequence, ASIO_HANDLER_TYPE(
306  WriteHandler, void (asio::error_code, std::size_t))>(
307  storage_, buffers, init.handler));
308  }
309  else
310  {
311  this->async_flush(detail::buffered_write_some_handler<
312  ConstBufferSequence, ASIO_HANDLER_TYPE(
313  WriteHandler, void (asio::error_code, std::size_t))>(
314  storage_, buffers, init.handler));
315  }
316 
317  return init.result.get();
318 }
319 
320 template <typename Stream>
321 template <typename ConstBufferSequence>
323  const ConstBufferSequence& buffers)
324 {
325  std::size_t orig_size = storage_.size();
326  std::size_t space_avail = storage_.capacity() - orig_size;
327  std::size_t bytes_avail = asio::buffer_size(buffers);
328  std::size_t length = bytes_avail < space_avail ? bytes_avail : space_avail;
329  storage_.resize(orig_size + length);
330  return asio::buffer_copy(
331  storage_.data() + orig_size, buffers, length);
332 }
333 
334 } // namespace asio
335 
337 
338 #endif // ASIO_IMPL_BUFFERED_WRITE_STREAM_HPP
void operator()(const asio::error_code &ec, const std::size_t bytes_written)
void asio_handler_deallocate(void *pointer, std::size_t size, binder1< Handler, Arg1 > *this_handler)
detail::buffered_stream_storage & storage_
void * asio_handler_allocate(std::size_t size, binder1< Handler, Arg1 > *this_handler)
void operator()(const asio::error_code &ec, std::size_t)
asio::basic_streambuf< Allocator > MatchCondition enable_if< is_match_condition< MatchCondition >::value >::type *detail::async_result_init< ReadHandler, void(asio::error_code, std::size_t)> init(ASIO_MOVE_CAST(ReadHandler)(handler))
#define ASIO_HANDLER_TYPE(h, sig)
std::size_t write(SyncWriteStream &s, const ConstBufferSequence &buffers, CompletionCondition completion_condition, asio::error_code &ec)
Write a certain amount of data to a stream before returning.
Definition: write.hpp:37
mutable_buffers_1 buffer(const mutable_buffer &b)
Create a new modifiable buffer from an existing buffer.
Definition: buffer.hpp:706
detail::transfer_all_t transfer_all()
ASIO_INITFN_RESULT_TYPE(ComposedConnectHandler, void(asio::error_code, Iterator)) async_connect(basic_socket< Protocol
std::size_t buffer_size(const mutable_buffer &b)
Get the number of bytes in a modifiable buffer.
Definition: buffer.hpp:357
void invoke(Function &function, Context &context)
const MutableBufferSequence & buffers
Definition: read.hpp:521
void asio_handler_invoke(Function &function, binder1< Handler, Arg1 > *this_handler)
asio::basic_streambuf< Allocator > CompletionCondition ASIO_MOVE_ARG(ReadHandler) handler)
Definition: read.hpp:704
bool is_continuation(Context &context)
detail::buffered_stream_storage & storage_
Class to represent an error code value.
Definition: error_code.hpp:80
void deallocate(void *p, std::size_t s, Handler &h)
void * allocate(std::size_t s, Handler &h)
handler_type< Handler, Signature >::type handler
buffered_write_some_handler(detail::buffered_stream_storage &storage, const ConstBufferSequence &buffers, WriteHandler &handler)
#define ASIO_MOVE_CAST(type)
Definition: config.hpp:138
async_result< typename handler_type< Handler, Signature >::type > result
#define ASIO_WRITE_HANDLER_CHECK(handler_type, handler)
std::size_t write_some(const ConstBufferSequence &buffers)
bool asio_handler_is_continuation(binder1< Handler, Arg1 > *this_handler)
std::size_t buffer_copy(const mutable_buffer &target, const const_buffer &source)
Copies bytes from a source buffer to a target buffer.
Definition: buffer.hpp:1295
Adds buffering to the write-related operations of a stream.
buffered_flush_handler(detail::buffered_stream_storage &storage, WriteHandler &handler)