diff options
Diffstat (limited to 'boost/asio/basic_streambuf.hpp')
-rw-r--r-- | boost/asio/basic_streambuf.hpp | 113 |
1 files changed, 98 insertions, 15 deletions
diff --git a/boost/asio/basic_streambuf.hpp b/boost/asio/basic_streambuf.hpp index 1392596dc5..d8bfecd6b6 100644 --- a/boost/asio/basic_streambuf.hpp +++ b/boost/asio/basic_streambuf.hpp @@ -120,8 +120,8 @@ public: /// The type used to represent the output sequence as a list of buffers. 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; + typedef BOOST_ASIO_CONST_BUFFER const_buffers_type; + typedef BOOST_ASIO_MUTABLE_BUFFER mutable_buffers_type; #endif /// Construct a basic_streambuf object. @@ -152,11 +152,11 @@ public: * while (i != bufs.end()) * { * const_buffer buf(*i++); - * s += buffer_size(buf); + * s += buf.size(); * } * @endcode */ - std::size_t size() const + std::size_t size() const BOOST_ASIO_NOEXCEPT { return pptr() - gptr(); } @@ -166,11 +166,21 @@ public: * @returns The allowed maximum of the sum of the sizes of the input sequence * and output sequence. */ - std::size_t max_size() const + std::size_t max_size() const BOOST_ASIO_NOEXCEPT { return max_size_; } + /// Get the current capacity of the basic_streambuf. + /** + * @returns The current total capacity of the streambuf, i.e. for both the + * input sequence and output sequence. + */ + std::size_t capacity() const BOOST_ASIO_NOEXCEPT + { + return buffer_.capacity(); + } + /// Get a list of buffers that represents the input sequence. /** * @returns An object of type @c const_buffers_type that satisfies @@ -180,7 +190,7 @@ public: * @note The returned object is invalidated by any @c basic_streambuf member * function that modifies the input sequence or output sequence. */ - const_buffers_type data() const + const_buffers_type data() const BOOST_ASIO_NOEXCEPT { return boost::asio::buffer(boost::asio::const_buffer(gptr(), (pptr() - gptr()) * sizeof(char_type))); @@ -223,8 +233,7 @@ public: */ void commit(std::size_t n) { - if (pptr() + n > epptr()) - n = epptr() - pptr(); + n = std::min<std::size_t>(n, epptr() - pptr()); pbump(static_cast<int>(n)); setg(eback(), gptr(), pptr()); } @@ -351,15 +360,89 @@ private: } }; -// Helper function to get the preferred size for reading data. Used for any -// user-provided specialisations of basic_streambuf. +/// Adapts basic_streambuf to the dynamic buffer sequence type requirements. +#if defined(GENERATING_DOCUMENTATION) +template <typename Allocator = std::allocator<char> > +#else template <typename Allocator> -inline std::size_t read_size_helper( - basic_streambuf<Allocator>& sb, std::size_t max_size) +#endif +class basic_streambuf_ref { - return std::min<std::size_t>(512, - std::min<std::size_t>(max_size, sb.max_size() - sb.size())); -} +public: + /// The type used to represent the input sequence as a list of buffers. + typedef typename basic_streambuf<Allocator>::const_buffers_type + const_buffers_type; + + /// The type used to represent the output sequence as a list of buffers. + typedef typename basic_streambuf<Allocator>::mutable_buffers_type + mutable_buffers_type; + + /// Construct a basic_streambuf_ref for the given basic_streambuf object. + explicit basic_streambuf_ref(basic_streambuf<Allocator>& sb) + : sb_(sb) + { + } + + /// Copy construct a basic_streambuf_ref. + basic_streambuf_ref(const basic_streambuf_ref& other) BOOST_ASIO_NOEXCEPT + : sb_(other.sb_) + { + } + +#if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) + /// Move construct a basic_streambuf_ref. + basic_streambuf_ref(basic_streambuf_ref&& other) BOOST_ASIO_NOEXCEPT + : sb_(other.sb_) + { + } +#endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION) + + /// Get the size of the input sequence. + std::size_t size() const BOOST_ASIO_NOEXCEPT + { + return sb_.size(); + } + + /// Get the maximum size of the dynamic buffer. + std::size_t max_size() const BOOST_ASIO_NOEXCEPT + { + return sb_.max_size(); + } + + /// Get the current capacity of the dynamic buffer. + std::size_t capacity() const BOOST_ASIO_NOEXCEPT + { + return sb_.capacity(); + } + + /// Get a list of buffers that represents the input sequence. + const_buffers_type data() const BOOST_ASIO_NOEXCEPT + { + return sb_.data(); + } + + /// Get a list of buffers that represents the output sequence, with the given + /// size. + mutable_buffers_type prepare(std::size_t n) + { + return sb_.prepare(n); + } + + /// Move bytes from the output sequence to the input sequence. + void commit(std::size_t n) + { + return sb_.commit(n); + } + + /// Remove characters from the input sequence. + void consume(std::size_t n) + { + return sb_.consume(n); + } + +private: + basic_streambuf<Allocator>& sb_; +}; } // namespace asio } // namespace boost |