123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- #ifndef BOOST_ASIO_BASIC_STREAMBUF_HPP
- #define BOOST_ASIO_BASIC_STREAMBUF_HPP
- #if defined(_MSC_VER) && (_MSC_VER >= 1200)
- # pragma once
- #endif
- #include <boost/asio/detail/push_options.hpp>
- #include <boost/asio/detail/push_options.hpp>
- #include <algorithm>
- #include <cstring>
- #include <limits>
- #include <memory>
- #include <stdexcept>
- #include <streambuf>
- #include <vector>
- #include <boost/throw_exception.hpp>
- #include <boost/asio/detail/pop_options.hpp>
- #include <boost/asio/buffer.hpp>
- #include <boost/asio/detail/noncopyable.hpp>
- namespace boost {
- namespace asio {
- template <typename Allocator = std::allocator<char> >
- class basic_streambuf
- : public std::streambuf,
- private noncopyable
- {
- public:
- #if defined(GENERATING_DOCUMENTATION)
-
- typedef implementation_defined const_buffers_type;
-
- typedef implementation_defined mutable_buffers_type;
- #else
- typedef boost::asio::const_buffers_1 const_buffers_type;
- typedef boost::asio::mutable_buffers_1 mutable_buffers_type;
- #endif
-
-
- explicit basic_streambuf(
- std::size_t max_size = (std::numeric_limits<std::size_t>::max)(),
- const Allocator& allocator = Allocator())
- : max_size_(max_size),
- buffer_(allocator)
- {
- std::size_t pend = (std::min<std::size_t>)(max_size_, buffer_delta);
- buffer_.resize((std::max<std::size_t>)(pend, 1));
- setg(&buffer_[0], &buffer_[0], &buffer_[0]);
- setp(&buffer_[0], &buffer_[0] + pend);
- }
-
-
- std::size_t size() const
- {
- return pptr() - gptr();
- }
-
-
- std::size_t max_size() const
- {
- return max_size_;
- }
-
-
- const_buffers_type data() const
- {
- return boost::asio::buffer(boost::asio::const_buffer(gptr(),
- (pptr() - gptr()) * sizeof(char_type)));
- }
-
-
-
- mutable_buffers_type prepare(std::size_t n)
- {
- reserve(n);
- return boost::asio::buffer(boost::asio::mutable_buffer(
- pptr(), n * sizeof(char_type)));
- }
-
-
- void commit(std::size_t n)
- {
- if (pptr() + n > epptr())
- n = epptr() - pptr();
- pbump(static_cast<int>(n));
- setg(eback(), gptr(), pptr());
- }
-
-
- void consume(std::size_t n)
- {
- if (gptr() + n > pptr())
- n = pptr() - gptr();
- gbump(static_cast<int>(n));
- }
- protected:
- enum { buffer_delta = 128 };
-
-
- int_type underflow()
- {
- if (gptr() < pptr())
- {
- setg(&buffer_[0], gptr(), pptr());
- return traits_type::to_int_type(*gptr());
- }
- else
- {
- return traits_type::eof();
- }
- }
-
-
- int_type overflow(int_type c)
- {
- if (!traits_type::eq_int_type(c, traits_type::eof()))
- {
- if (pptr() == epptr())
- {
- std::size_t buffer_size = pptr() - gptr();
- if (buffer_size < max_size_ && max_size_ - buffer_size < buffer_delta)
- {
- reserve(max_size_ - buffer_size);
- }
- else
- {
- reserve(buffer_delta);
- }
- }
- *pptr() = traits_type::to_char_type(c);
- pbump(1);
- return c;
- }
- return traits_type::not_eof(c);
- }
- void reserve(std::size_t n)
- {
-
- std::size_t gnext = gptr() - &buffer_[0];
- std::size_t pnext = pptr() - &buffer_[0];
- std::size_t pend = epptr() - &buffer_[0];
-
- if (n <= pend - pnext)
- {
- return;
- }
-
- if (gnext > 0)
- {
- pnext -= gnext;
- std::memmove(&buffer_[0], &buffer_[0] + gnext, pnext);
- }
-
- if (n > pend - pnext)
- {
- if (n <= max_size_ && pnext <= max_size_ - n)
- {
- pend = pnext + n;
- buffer_.resize((std::max<std::size_t>)(pend, 1));
- }
- else
- {
- std::length_error ex("boost::asio::streambuf too long");
- boost::throw_exception(ex);
- }
- }
-
- setg(&buffer_[0], &buffer_[0], &buffer_[0] + pnext);
- setp(&buffer_[0] + pnext, &buffer_[0] + pend);
- }
- private:
- std::size_t max_size_;
- std::vector<char_type, Allocator> buffer_;
- };
- }
- }
- #include <boost/asio/detail/pop_options.hpp>
- #endif
|