Realistic 3D camera system
3D camera system components
buffered_read_stream.hpp
Go to the documentation of this file.
1 //
2 // impl/buffered_read_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_READ_STREAM_HPP
12 #define ASIO_IMPL_BUFFERED_READ_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 {
31  resize_guard(storage_);
32  std::size_t previous_size = storage_.size();
33  storage_.resize(storage_.capacity());
34  storage_.resize(previous_size + next_layer_.read_some(buffer(
35  storage_.data() + previous_size,
36  storage_.size() - previous_size)));
37  resize_guard.commit();
38  return storage_.size() - previous_size;
39 }
40 
41 template <typename Stream>
43 {
45  resize_guard(storage_);
46  std::size_t previous_size = storage_.size();
47  storage_.resize(storage_.capacity());
48  storage_.resize(previous_size + next_layer_.read_some(buffer(
49  storage_.data() + previous_size,
50  storage_.size() - previous_size),
51  ec));
52  resize_guard.commit();
53  return storage_.size() - previous_size;
54 }
55 
56 namespace detail
57 {
58  template <typename ReadHandler>
60  {
61  public:
63  std::size_t previous_size, ReadHandler& handler)
64  : storage_(storage),
65  previous_size_(previous_size),
66  handler_(ASIO_MOVE_CAST(ReadHandler)(handler))
67  {
68  }
69 
70 #if defined(ASIO_HAS_MOVE)
72  : storage_(other.storage_),
74  handler_(other.handler_)
75  {
76  }
77 
79  : storage_(other.storage_),
81  handler_(ASIO_MOVE_CAST(ReadHandler)(other.handler_))
82  {
83  }
84 #endif // defined(ASIO_HAS_MOVE)
85 
86  void operator()(const asio::error_code& ec,
87  const std::size_t bytes_transferred)
88  {
89  storage_.resize(previous_size_ + bytes_transferred);
90  handler_(ec, bytes_transferred);
91  }
92 
93  //private:
95  std::size_t previous_size_;
96  ReadHandler handler_;
97  };
98 
99  template <typename ReadHandler>
100  inline void* asio_handler_allocate(std::size_t size,
102  {
104  size, this_handler->handler_);
105  }
106 
107  template <typename ReadHandler>
108  inline void asio_handler_deallocate(void* pointer, std::size_t size,
110  {
112  pointer, size, this_handler->handler_);
113  }
114 
115  template <typename ReadHandler>
118  {
120  this_handler->handler_);
121  }
122 
123  template <typename Function, typename ReadHandler>
124  inline void asio_handler_invoke(Function& function,
126  {
128  function, this_handler->handler_);
129  }
130 
131  template <typename Function, typename ReadHandler>
132  inline void asio_handler_invoke(const Function& function,
134  {
136  function, this_handler->handler_);
137  }
138 } // namespace detail
139 
140 template <typename Stream>
141 template <typename ReadHandler>
143  void (asio::error_code, std::size_t))
144 buffered_read_stream<Stream>::async_fill(
145  ASIO_MOVE_ARG(ReadHandler) handler)
146 {
147  // If you get an error on the following line it means that your handler does
148  // not meet the documented type requirements for a ReadHandler.
149  ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
150 
152  ReadHandler, void (asio::error_code, std::size_t)> init(
153  ASIO_MOVE_CAST(ReadHandler)(handler));
154 
155  std::size_t previous_size = storage_.size();
157  next_layer_.async_read_some(
158  buffer(
159  storage_.data() + previous_size,
160  storage_.size() - previous_size),
162  ReadHandler, void (asio::error_code, std::size_t))>(
163  storage_, previous_size, init.handler));
164 
165  return init.result.get();
166 }
167 
168 template <typename Stream>
169 template <typename MutableBufferSequence>
171  const MutableBufferSequence& buffers)
172 {
173  if (asio::buffer_size(buffers) == 0)
174  return 0;
175 
176  if (storage_.empty())
177  this->fill();
178 
179  return this->copy(buffers);
180 }
181 
182 template <typename Stream>
183 template <typename MutableBufferSequence>
185  const MutableBufferSequence& buffers, asio::error_code& ec)
186 {
187  ec = asio::error_code();
188 
189  if (asio::buffer_size(buffers) == 0)
190  return 0;
191 
192  if (storage_.empty() && !this->fill(ec))
193  return 0;
194 
195  return this->copy(buffers);
196 }
197 
198 namespace detail
199 {
200  template <typename MutableBufferSequence, typename ReadHandler>
202  {
203  public:
205  const MutableBufferSequence& buffers, ReadHandler& handler)
206  : storage_(storage),
207  buffers_(buffers),
208  handler_(handler)
209  {
210  }
211 
212 #if defined(ASIO_HAS_MOVE)
214  : storage_(other.storage_),
215  buffers_(other.buffers_),
216  handler_(other.handler_)
217  {
218  }
219 
221  : storage_(other.storage_),
222  buffers_(other.buffers_),
223  handler_(ASIO_MOVE_CAST(ReadHandler)(other.handler_))
224  {
225  }
226 #endif // defined(ASIO_HAS_MOVE)
227 
228  void operator()(const asio::error_code& ec, std::size_t)
229  {
230  if (ec || storage_.empty())
231  {
232  const std::size_t length = 0;
233  handler_(ec, length);
234  }
235  else
236  {
237  const std::size_t bytes_copied = asio::buffer_copy(
238  buffers_, storage_.data(), storage_.size());
239  storage_.consume(bytes_copied);
240  handler_(ec, bytes_copied);
241  }
242  }
243 
244  //private:
246  MutableBufferSequence buffers_;
247  ReadHandler handler_;
248  };
249 
250  template <typename MutableBufferSequence, typename ReadHandler>
251  inline void* asio_handler_allocate(std::size_t size,
253  MutableBufferSequence, ReadHandler>* this_handler)
254  {
256  size, this_handler->handler_);
257  }
258 
259  template <typename MutableBufferSequence, typename ReadHandler>
260  inline void asio_handler_deallocate(void* pointer, std::size_t size,
262  MutableBufferSequence, ReadHandler>* this_handler)
263  {
265  pointer, size, this_handler->handler_);
266  }
267 
268  template <typename MutableBufferSequence, typename ReadHandler>
271  MutableBufferSequence, ReadHandler>* this_handler)
272  {
274  this_handler->handler_);
275  }
276 
277  template <typename Function, typename MutableBufferSequence,
278  typename ReadHandler>
279  inline void asio_handler_invoke(Function& function,
281  MutableBufferSequence, ReadHandler>* this_handler)
282  {
284  function, this_handler->handler_);
285  }
286 
287  template <typename Function, typename MutableBufferSequence,
288  typename ReadHandler>
289  inline void asio_handler_invoke(const Function& function,
291  MutableBufferSequence, ReadHandler>* this_handler)
292  {
294  function, this_handler->handler_);
295  }
296 } // namespace detail
297 
298 template <typename Stream>
299 template <typename MutableBufferSequence, typename ReadHandler>
301  void (asio::error_code, std::size_t))
302 buffered_read_stream<Stream>::async_read_some(
303  const MutableBufferSequence& buffers,
304  ASIO_MOVE_ARG(ReadHandler) handler)
305 {
306  // If you get an error on the following line it means that your handler does
307  // not meet the documented type requirements for a ReadHandler.
308  ASIO_READ_HANDLER_CHECK(ReadHandler, handler) type_check;
309 
311  ReadHandler, void (asio::error_code, std::size_t)> init(
312  ASIO_MOVE_CAST(ReadHandler)(handler));
313 
314  if (asio::buffer_size(buffers) == 0 || !storage_.empty())
315  {
316  next_layer_.async_read_some(asio::mutable_buffers_1(0, 0),
318  MutableBufferSequence, ASIO_HANDLER_TYPE(
319  ReadHandler, void (asio::error_code, std::size_t))>(
320  storage_, buffers, init.handler));
321  }
322  else
323  {
324  this->async_fill(detail::buffered_read_some_handler<
325  MutableBufferSequence, ASIO_HANDLER_TYPE(
326  ReadHandler, void (asio::error_code, std::size_t))>(
327  storage_, buffers, init.handler));
328  }
329 
330  return init.result.get();
331 }
332 
333 template <typename Stream>
334 template <typename MutableBufferSequence>
336  const MutableBufferSequence& buffers)
337 {
338  if (storage_.empty())
339  this->fill();
340  return this->peek_copy(buffers);
341 }
342 
343 template <typename Stream>
344 template <typename MutableBufferSequence>
346  const MutableBufferSequence& buffers, asio::error_code& ec)
347 {
348  ec = asio::error_code();
349  if (storage_.empty() && !this->fill(ec))
350  return 0;
351  return this->peek_copy(buffers);
352 }
353 
354 } // namespace asio
355 
357 
358 #endif // ASIO_IMPL_BUFFERED_READ_STREAM_HPP
void asio_handler_deallocate(void *pointer, std::size_t size, binder1< Handler, Arg1 > *this_handler)
void * asio_handler_allocate(std::size_t size, binder1< Handler, Arg1 > *this_handler)
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)
mutable_buffers_1 buffer(const mutable_buffer &b)
Create a new modifiable buffer from an existing buffer.
Definition: buffer.hpp:706
detail::buffered_stream_storage & storage_
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)
void operator()(const asio::error_code &ec, std::size_t)
const MutableBufferSequence & buffers
Definition: read.hpp:521
#define ASIO_READ_HANDLER_CHECK(handler_type, handler)
void operator()(const asio::error_code &ec, const std::size_t bytes_transferred)
buffered_fill_handler(detail::buffered_stream_storage &storage, std::size_t previous_size, ReadHandler &handler)
buffered_read_some_handler(detail::buffered_stream_storage &storage, const MutableBufferSequence &buffers, ReadHandler &handler)
void asio_handler_invoke(Function &function, binder1< Handler, Arg1 > *this_handler)
std::size_t peek(const MutableBufferSequence &buffers)
Adds buffering to the read-related operations of a stream.
asio::basic_streambuf< Allocator > CompletionCondition ASIO_MOVE_ARG(ReadHandler) handler)
Definition: read.hpp:704
bool is_continuation(Context &context)
Class to represent an error code value.
Definition: error_code.hpp:80
void deallocate(void *p, std::size_t s, Handler &h)
detail::buffered_stream_storage & storage_
void * allocate(std::size_t s, Handler &h)
handler_type< Handler, Signature >::type handler
#define ASIO_MOVE_CAST(type)
Definition: config.hpp:138
std::size_t read_some(const MutableBufferSequence &buffers)
async_result< typename handler_type< Handler, Signature >::type > result
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