Realistic 3D camera system
3D camera system components
basic_streambuf.hpp
Go to the documentation of this file.
1 //
2 // basic_streambuf.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_BASIC_STREAMBUF_HPP
12 #define ASIO_BASIC_STREAMBUF_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 
20 #if !defined(ASIO_NO_IOSTREAM)
21 
22 #include <algorithm>
23 #include <cstring>
24 #include <stdexcept>
25 #include <streambuf>
26 #include <vector>
28 #include "asio/buffer.hpp"
29 #include "asio/detail/limits.hpp"
32 
34 
35 namespace asio {
36 
38 
105 #if defined(GENERATING_DOCUMENTATION)
106 template <typename Allocator = std::allocator<char> >
107 #else
108 template <typename Allocator>
109 #endif
111  : public std::streambuf,
112  private noncopyable
113 {
114 public:
115 #if defined(GENERATING_DOCUMENTATION)
116  typedef implementation_defined const_buffers_type;
118 
120  typedef implementation_defined mutable_buffers_type;
121 #else
124 #endif
125 
127 
131  explicit basic_streambuf(
132  std::size_t maximum_size = (std::numeric_limits<std::size_t>::max)(),
133  const Allocator& allocator = Allocator())
134  : max_size_(maximum_size),
135  buffer_(allocator)
136  {
137  std::size_t pend = (std::min<std::size_t>)(max_size_, buffer_delta);
138  buffer_.resize((std::max<std::size_t>)(pend, 1));
139  setg(&buffer_[0], &buffer_[0], &buffer_[0]);
140  setp(&buffer_[0], &buffer_[0] + pend);
141  }
142 
144 
158  std::size_t size() const
159  {
160  return pptr() - gptr();
161  }
162 
164 
168  std::size_t max_size() const
169  {
170  return max_size_;
171  }
172 
174 
182  const_buffers_type data() const
183  {
184  return asio::buffer(asio::const_buffer(gptr(),
185  (pptr() - gptr()) * sizeof(char_type)));
186  }
187 
190 
204  mutable_buffers_type prepare(std::size_t n)
205  {
206  reserve(n);
208  pptr(), n * sizeof(char_type)));
209  }
210 
212 
223  void commit(std::size_t n)
224  {
225  if (pptr() + n > epptr())
226  n = epptr() - pptr();
227  pbump(static_cast<int>(n));
228  setg(eback(), gptr(), pptr());
229  }
230 
232 
238  void consume(std::size_t n)
239  {
240  if (egptr() < pptr())
241  setg(&buffer_[0], gptr(), pptr());
242  if (gptr() + n > pptr())
243  n = pptr() - gptr();
244  gbump(static_cast<int>(n));
245  }
246 
247 protected:
248  enum { buffer_delta = 128 };
249 
251 
254  int_type underflow()
255  {
256  if (gptr() < pptr())
257  {
258  setg(&buffer_[0], gptr(), pptr());
259  return traits_type::to_int_type(*gptr());
260  }
261  else
262  {
263  return traits_type::eof();
264  }
265  }
266 
268 
274  int_type overflow(int_type c)
275  {
276  if (!traits_type::eq_int_type(c, traits_type::eof()))
277  {
278  if (pptr() == epptr())
279  {
280  std::size_t buffer_size = pptr() - gptr();
281  if (buffer_size < max_size_ && max_size_ - buffer_size < buffer_delta)
282  {
283  reserve(max_size_ - buffer_size);
284  }
285  else
286  {
288  }
289  }
290 
291  *pptr() = traits_type::to_char_type(c);
292  pbump(1);
293  return c;
294  }
295 
296  return traits_type::not_eof(c);
297  }
298 
299  void reserve(std::size_t n)
300  {
301  // Get current stream positions as offsets.
302  std::size_t gnext = gptr() - &buffer_[0];
303  std::size_t pnext = pptr() - &buffer_[0];
304  std::size_t pend = epptr() - &buffer_[0];
305 
306  // Check if there is already enough space in the put area.
307  if (n <= pend - pnext)
308  {
309  return;
310  }
311 
312  // Shift existing contents of get area to start of buffer.
313  if (gnext > 0)
314  {
315  pnext -= gnext;
316  std::memmove(&buffer_[0], &buffer_[0] + gnext, pnext);
317  }
318 
319  // Ensure buffer is large enough to hold at least the specified size.
320  if (n > pend - pnext)
321  {
322  if (n <= max_size_ && pnext <= max_size_ - n)
323  {
324  pend = pnext + n;
325  buffer_.resize((std::max<std::size_t>)(pend, 1));
326  }
327  else
328  {
329  std::length_error ex("asio::streambuf too long");
331  }
332  }
333 
334  // Update stream positions.
335  setg(&buffer_[0], &buffer_[0], &buffer_[0] + pnext);
336  setp(&buffer_[0] + pnext, &buffer_[0] + pend);
337  }
338 
339 private:
340  std::size_t max_size_;
341  std::vector<char_type, Allocator> buffer_;
342 
343  // Helper function to get the preferred size for reading data.
344  friend std::size_t read_size_helper(
345  basic_streambuf& sb, std::size_t max_size)
346  {
347  return std::min<std::size_t>(
348  std::max<std::size_t>(512, sb.buffer_.capacity() - sb.size()),
349  std::min<std::size_t>(max_size, sb.max_size() - sb.size()));
350  }
351 };
352 
353 // Helper function to get the preferred size for reading data. Used for any
354 // user-provided specialisations of basic_streambuf.
355 template <typename Allocator>
356 inline std::size_t read_size_helper(
357  basic_streambuf<Allocator>& sb, std::size_t max_size)
358 {
359  return std::min<std::size_t>(512,
360  std::min<std::size_t>(max_size, sb.max_size() - sb.size()));
361 }
362 
363 } // namespace asio
364 
366 
367 #endif // !defined(ASIO_NO_IOSTREAM)
368 
369 #endif // ASIO_BASIC_STREAMBUF_HPP
basic_streambuf(std::size_t maximum_size=(std::numeric_limits< std::size_t >::max)(), const Allocator &allocator=Allocator())
Construct a basic_streambuf object.
friend std::size_t read_size_helper(basic_streambuf &sb, std::size_t max_size)
void reserve(std::size_t n)
basic_streambuf streambuf
Typedef for the typical usage of basic_streambuf.
Definition: streambuf.hpp:27
Holds a buffer that cannot be modified.
Definition: buffer.hpp:211
int_type overflow(int_type c)
Override std::streambuf behaviour.
std::size_t max_size() const
Get the maximum size of the basic_streambuf.
mutable_buffers_type prepare(std::size_t n)
mutable_buffers_1 buffer(const mutable_buffer &b)
Create a new modifiable buffer from an existing buffer.
Definition: buffer.hpp:706
asio::mutable_buffers_1 mutable_buffers_type
int_type underflow()
Override std::streambuf behaviour.
End of file or stream.
Definition: error.hpp:217
std::size_t buffer_size(const mutable_buffer &b)
Get the number of bytes in a modifiable buffer.
Definition: buffer.hpp:357
void commit(std::size_t n)
Move characters from the output sequence to the input sequence.
std::size_t size() const
Get the size of the input sequence.
Holds a buffer that can be modified.
Definition: buffer.hpp:91
Automatically resizable buffer class based on std::streambuf.
const_buffers_type data() const
Get a list of buffers that represents the input sequence.
void consume(std::size_t n)
Remove characters from the input sequence.
asio::const_buffers_1 const_buffers_type
void throw_exception(const Exception &e)