11 #ifndef ASIO_BASIC_STREAMBUF_HPP 12 #define ASIO_BASIC_STREAMBUF_HPP 14 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 18 #include "asio/detail/config.hpp" 20 #if !defined(ASIO_NO_IOSTREAM) 27 #include "asio/basic_streambuf_fwd.hpp" 28 #include "asio/buffer.hpp" 29 #include "asio/detail/limits.hpp" 30 #include "asio/detail/noncopyable.hpp" 31 #include "asio/detail/throw_exception.hpp" 33 #include "asio/detail/push_options.hpp" 105 #if defined(GENERATING_DOCUMENTATION) 106 template <
typename Allocator = std::allocator<
char> >
108 template <
typename Allocator>
111 :
public std::streambuf,
115 #if defined(GENERATING_DOCUMENTATION) 116 typedef implementation_defined const_buffers_type;
120 typedef implementation_defined mutable_buffers_type;
122 typedef ASIO_CONST_BUFFER const_buffers_type;
123 typedef ASIO_MUTABLE_BUFFER mutable_buffers_type;
132 std::size_t maximum_size = (std::numeric_limits<std::size_t>::max)(),
133 const Allocator&
allocator = Allocator())
134 : max_size_(maximum_size),
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);
158 std::size_t
size() const ASIO_NOEXCEPT
160 return pptr() - gptr();
180 return buffer_.capacity();
192 const_buffers_type
data() const ASIO_NOEXCEPT
195 (pptr() - gptr()) *
sizeof(char_type)));
218 pptr(), n *
sizeof(char_type)));
235 n = std::min<std::size_t>(n, epptr() - pptr());
236 pbump(static_cast<int>(n));
237 setg(eback(), gptr(), pptr());
249 if (egptr() < pptr())
250 setg(&buffer_[0], gptr(), pptr());
251 if (gptr() + n > pptr())
253 gbump(static_cast<int>(n));
257 enum { buffer_delta = 128 };
267 setg(&buffer_[0], gptr(), pptr());
268 return traits_type::to_int_type(*gptr());
272 return traits_type::eof();
285 if (!traits_type::eq_int_type(c, traits_type::eof()))
287 if (pptr() == epptr())
289 std::size_t buffer_size = pptr() - gptr();
290 if (buffer_size < max_size_ && max_size_ - buffer_size < buffer_delta)
292 reserve(max_size_ - buffer_size);
296 reserve(buffer_delta);
300 *pptr() = traits_type::to_char_type(c);
305 return traits_type::not_eof(c);
308 void reserve(std::size_t n)
311 std::size_t gnext = gptr() - &buffer_[0];
312 std::size_t pnext = pptr() - &buffer_[0];
313 std::size_t pend = epptr() - &buffer_[0];
316 if (n <= pend - pnext)
325 std::memmove(&buffer_[0], &buffer_[0] + gnext, pnext);
329 if (n > pend - pnext)
331 if (n <= max_size_ && pnext <= max_size_ - n)
334 buffer_.resize((std::max<std::size_t>)(pend, 1));
338 std::length_error ex(
"asio::streambuf too long");
339 asio::detail::throw_exception(ex);
344 setg(&buffer_[0], &buffer_[0], &buffer_[0] + pnext);
345 setp(&buffer_[0] + pnext, &buffer_[0] + pend);
349 std::size_t max_size_;
350 std::vector<char_type, Allocator> buffer_;
353 friend std::size_t read_size_helper(
356 return std::min<std::size_t>(
357 std::max<std::size_t>(512, sb.buffer_.capacity() - sb.
size()),
358 std::min<std::size_t>(max_size, sb.
max_size() - sb.
size()));
363 #if defined(GENERATING_DOCUMENTATION) 364 template <
typename Allocator = std::allocator<
char> >
366 template <
typename Allocator>
372 typedef typename basic_streambuf<Allocator>::const_buffers_type
376 typedef typename basic_streambuf<Allocator>::mutable_buffers_type
391 #if defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) 397 #endif // defined(ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) 400 std::size_t
size() const ASIO_NOEXCEPT
408 return sb_.max_size();
414 return sb_.capacity();
418 const_buffers_type
data() const ASIO_NOEXCEPT
427 return sb_.prepare(n);
433 return sb_.commit(n);
439 return sb_.consume(n);
448 #include "asio/detail/pop_options.hpp" 450 #endif // !defined(ASIO_NO_IOSTREAM) 452 #endif // ASIO_BASIC_STREAMBUF_HPP std::size_t max_size() const ASIO_NOEXCEPT
Get the maximum size of the basic_streambuf.
Definition: basic_streambuf.hpp:168
basic_streambuf(std::size_t maximum_size=(std::numeric_limits< std::size_t >::max)(), const Allocator &allocator=Allocator())
Construct a basic_streambuf object.
Definition: basic_streambuf.hpp:131
mutable_buffers_type prepare(std::size_t n)
Get a list of buffers that represents the output sequence, with the given size.
Definition: basic_streambuf.hpp:425
basic_streambuf< Allocator >::const_buffers_type const_buffers_type
The type used to represent the input sequence as a list of buffers.
Definition: basic_streambuf.hpp:373
Holds a buffer that cannot be modified.
Definition: buffer.hpp:226
int_type overflow(int_type c)
Override std::streambuf behaviour.
Definition: basic_streambuf.hpp:283
std::size_t capacity() const ASIO_NOEXCEPT
Get the current capacity of the basic_streambuf.
Definition: basic_streambuf.hpp:178
mutable_buffers_type prepare(std::size_t n)
Get a list of buffers that represents the output sequence, with the given size.
Definition: basic_streambuf.hpp:214
Definition: noncopyable.hpp:25
const_buffers_type data() const ASIO_NOEXCEPT
Get a list of buffers that represents the input sequence.
Definition: basic_streambuf.hpp:192
Definition: allocator.hpp:17
std::size_t max_size() const ASIO_NOEXCEPT
Get the maximum size of the dynamic buffer.
Definition: basic_streambuf.hpp:406
int_type underflow()
Override std::streambuf behaviour.
Definition: basic_streambuf.hpp:263
void commit(std::size_t n)
Move characters from the output sequence to the input sequence.
Definition: basic_streambuf.hpp:233
basic_streambuf_ref(basic_streambuf< Allocator > &sb)
Construct a basic_streambuf_ref for the given basic_streambuf object.
Definition: basic_streambuf.hpp:380
std::size_t size() const ASIO_NOEXCEPT
Get the size of the input sequence.
Definition: basic_streambuf.hpp:158
ASIO_MUTABLE_BUFFER buffer(const mutable_buffer &b) ASIO_NOEXCEPT
Create a new modifiable buffer from an existing buffer.
Definition: buffer.hpp:909
basic_streambuf< Allocator >::mutable_buffers_type mutable_buffers_type
The type used to represent the output sequence as a list of buffers.
Definition: basic_streambuf.hpp:377
std::size_t capacity() const ASIO_NOEXCEPT
Get the current capacity of the dynamic buffer.
Definition: basic_streambuf.hpp:412
Holds a buffer that can be modified.
Definition: buffer.hpp:92
std::size_t size() const ASIO_NOEXCEPT
Get the size of the input sequence.
Definition: basic_streambuf.hpp:400
Automatically resizable buffer class based on std::streambuf.
Definition: basic_streambuf.hpp:110
void commit(std::size_t n)
Move bytes from the output sequence to the input sequence.
Definition: basic_streambuf.hpp:431
basic_streambuf_ref(const basic_streambuf_ref &other) ASIO_NOEXCEPT
Copy construct a basic_streambuf_ref.
Definition: basic_streambuf.hpp:386
void consume(std::size_t n)
Remove characters from the input sequence.
Definition: basic_streambuf.hpp:247
Adapts basic_streambuf to the dynamic buffer sequence type requirements.
Definition: basic_streambuf.hpp:368
const_buffers_type data() const ASIO_NOEXCEPT
Get a list of buffers that represents the input sequence.
Definition: basic_streambuf.hpp:418
void consume(std::size_t n)
Remove characters from the input sequence.
Definition: basic_streambuf.hpp:437
Definition: any_io_executor.hpp:28