123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452 |
- #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/config.hpp>
- #if !defined(BOOST_ASIO_NO_IOSTREAM)
- #include <algorithm>
- #include <cstring>
- #include <stdexcept>
- #include <streambuf>
- #include <vector>
- #include <boost/asio/basic_streambuf_fwd.hpp>
- #include <boost/asio/buffer.hpp>
- #include <boost/asio/detail/limits.hpp>
- #include <boost/asio/detail/noncopyable.hpp>
- #include <boost/asio/detail/throw_exception.hpp>
- #include <boost/asio/detail/push_options.hpp>
- namespace boost {
- namespace asio {
- #if defined(GENERATING_DOCUMENTATION)
- template <typename Allocator = std::allocator<char>>
- #else
- template <typename Allocator>
- #endif
- 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_BUFFER const_buffers_type;
- typedef BOOST_ASIO_MUTABLE_BUFFER mutable_buffers_type;
- #endif
-
-
- explicit basic_streambuf(
- std::size_t maximum_size = (std::numeric_limits<std::size_t>::max)(),
- const Allocator& allocator = Allocator())
- : max_size_(maximum_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 noexcept
- {
- return pptr() - gptr();
- }
-
-
- std::size_t max_size() const noexcept
- {
- return max_size_;
- }
-
-
- std::size_t capacity() const noexcept
- {
- return buffer_.capacity();
- }
-
-
- const_buffers_type data() const noexcept
- {
- 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)
- {
- n = std::min<std::size_t>(n, epptr() - pptr());
- pbump(static_cast<int>(n));
- setg(eback(), gptr(), pptr());
- }
-
-
- void consume(std::size_t n)
- {
- if (egptr() < pptr())
- setg(&buffer_[0], gptr(), pptr());
- 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();
- }
- }
- /// Override std::streambuf behaviour.
- /**
- * Behaves according to the specification of @c std::streambuf::overflow(),
- * with the specialisation that @c std::length_error is thrown if appending
- * the character to the input sequence would require the condition
- * <tt>size() > max_size()</tt> to be true.
- */
- 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)
- {
- // Get current stream positions as offsets.
- std::size_t gnext = gptr() - &buffer_[0];
- std::size_t pnext = pptr() - &buffer_[0];
- std::size_t pend = epptr() - &buffer_[0];
- // Check if there is already enough space in the put area.
- if (n <= pend - pnext)
- {
- return;
- }
- // Shift existing contents of get area to start of buffer.
- 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::asio::detail::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_;
-
- friend std::size_t read_size_helper(
- basic_streambuf& sb, std::size_t max_size)
- {
- return std::min<std::size_t>(
- std::max<std::size_t>(512, sb.buffer_.capacity() - sb.size()),
- std::min<std::size_t>(max_size, sb.max_size() - sb.size()));
- }
- };
- #if defined(GENERATING_DOCUMENTATION)
- template <typename Allocator = std::allocator<char>>
- #else
- template <typename Allocator>
- #endif
- class basic_streambuf_ref
- {
- public:
-
- typedef typename basic_streambuf<Allocator>::const_buffers_type
- const_buffers_type;
-
- typedef typename basic_streambuf<Allocator>::mutable_buffers_type
- mutable_buffers_type;
-
- explicit basic_streambuf_ref(basic_streambuf<Allocator>& sb)
- : sb_(sb)
- {
- }
-
- basic_streambuf_ref(const basic_streambuf_ref& other) noexcept
- : sb_(other.sb_)
- {
- }
-
- basic_streambuf_ref(basic_streambuf_ref&& other) noexcept
- : sb_(other.sb_)
- {
- }
-
- std::size_t size() const noexcept
- {
- return sb_.size();
- }
-
- std::size_t max_size() const noexcept
- {
- return sb_.max_size();
- }
-
- std::size_t capacity() const noexcept
- {
- return sb_.capacity();
- }
-
- const_buffers_type data() const noexcept
- {
- return sb_.data();
- }
-
-
- mutable_buffers_type prepare(std::size_t n)
- {
- return sb_.prepare(n);
- }
-
- void commit(std::size_t n)
- {
- return sb_.commit(n);
- }
-
- void consume(std::size_t n)
- {
- return sb_.consume(n);
- }
- private:
- basic_streambuf<Allocator>& sb_;
- };
- }
- }
- #include <boost/asio/detail/pop_options.hpp>
- #endif
- #endif
|